feat: lock check and login history on admin sign-in and sign-out
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
1 parent
f6fe246ad0
commit
39708224ad
7 files changed
+349
-3
No files matched your search
+179
@@ -0,0 +1,179 @@
|
||||
package com.solvway.mobile.api.controller.admin;
|
||||
|
||||
import com.solvway.core.enums.GeneralEnum;
|
||||
import com.solvway.core.model.Role;
|
||||
import com.solvway.core.model.User;
|
||||
import com.solvway.core.utils.HttpReqRespUtils;
|
||||
import com.solvway.mobile.api.service.AdminAuthenticationService;
|
||||
import com.solvway.mobile.api.service.AuthenticationService;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.ResultActions;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class AdminAuthControllerLoginSecurityTest {
|
||||
|
||||
@Mock private AdminAuthenticationService auth;
|
||||
@Mock private AuthenticationService authenticationService;
|
||||
@InjectMocks private AdminAuthController controller;
|
||||
|
||||
private MockMvc mvc;
|
||||
private MockedStatic<HttpReqRespUtils> http;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
mvc = MockMvcBuilders.standaloneSetup(controller).build();
|
||||
http = mockStatic(HttpReqRespUtils.class);
|
||||
http.when(HttpReqRespUtils::getJwtToken).thenReturn("tok");
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void close() { http.close(); }
|
||||
|
||||
private User user(boolean enabled) {
|
||||
User u = new User();
|
||||
u.setId(9L);
|
||||
u.setName("Admin User");
|
||||
u.setUsername("admin");
|
||||
u.setEnabled(enabled);
|
||||
u.setLogin(GeneralEnum.LoginStatus.Y);
|
||||
u.setRoles(List.of(new Role("ROLE_ADMIN")));
|
||||
return u;
|
||||
}
|
||||
|
||||
private ResultActions login(String username, String password) throws Exception {
|
||||
return mvc.perform(post("/api/admin/auth/login").contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"username\":\"" + username + "\",\"password\":\"" + password + "\"}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void blankFields_recordNothing_andSkipLockCheck() throws Exception {
|
||||
login("admin", "").andExpect(status().isUnauthorized());
|
||||
login(" ", "pw").andExpect(status().isUnauthorized());
|
||||
verify(auth, never()).isLocked(anyString());
|
||||
verify(auth, never()).record(any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void locked_is401AccountLocked_recordsLocked_andSkipsServicesAuth() throws Exception {
|
||||
when(auth.isLocked("admin")).thenReturn(true);
|
||||
|
||||
login("admin", "pw").andExpect(status().isUnauthorized())
|
||||
.andExpect(content().json("{\"result\":\"false\",\"code\":\"ACCOUNT_LOCKED\",\"message\":"
|
||||
+ "\"Too many failed sign-ins. Try again later or ask an administrator to unlock your account.\"}"));
|
||||
verify(auth).record("admin", "LOGIN_LOCKED", null);
|
||||
verify(auth, never()).getToken(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void wrongPassword_recordsFail() throws Exception {
|
||||
when(auth.getToken(any())).thenReturn("");
|
||||
|
||||
login("admin", "pw").andExpect(status().isUnauthorized())
|
||||
.andExpect(jsonPath("$.message").value("Invalid username or password"));
|
||||
verify(auth).record("admin", "LOGIN_FAIL", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownUser_noToken_recordsFail() throws Exception {
|
||||
when(auth.getToken(any())).thenReturn(null);
|
||||
|
||||
login("ghost", "pw").andExpect(status().isUnauthorized());
|
||||
verify(auth).record("ghost", "LOGIN_FAIL", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void userNull_recordsFail() throws Exception {
|
||||
when(auth.getToken(any())).thenReturn("t");
|
||||
when(auth.loadUserByToken("t")).thenReturn(null);
|
||||
|
||||
login("admin", "pw").andExpect(status().isUnauthorized());
|
||||
verify(auth).record("admin", "LOGIN_FAIL", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void disabledUser_recordsFail() throws Exception {
|
||||
when(auth.getToken(any())).thenReturn("t");
|
||||
when(auth.loadUserByToken("t")).thenReturn(user(false));
|
||||
|
||||
login("admin", "pw").andExpect(status().isUnauthorized());
|
||||
verify(auth).record("admin", "LOGIN_FAIL", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void restClientException_recordsFail() throws Exception {
|
||||
when(auth.getToken(any())).thenThrow(new org.springframework.web.client.ResourceAccessException("down"));
|
||||
|
||||
login("admin", "pw").andExpect(status().isUnauthorized());
|
||||
verify(auth).record("admin", "LOGIN_FAIL", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void success_recordsSuccess() throws Exception {
|
||||
when(auth.getToken(any())).thenReturn("t");
|
||||
when(auth.loadUserByToken("t")).thenReturn(user(true));
|
||||
|
||||
login("admin", "pw").andExpect(status().isOk()).andExpect(jsonPath("$.token").value("t"));
|
||||
verify(auth).record("admin", "LOGIN_SUCCESS", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void username_isTrimmedForLockCheckAndRecord() throws Exception {
|
||||
when(auth.getToken(any())).thenReturn("t");
|
||||
when(auth.loadUserByToken("t")).thenReturn(user(true));
|
||||
|
||||
login(" admin ", "pw").andExpect(status().isOk());
|
||||
verify(auth).isLocked("admin");
|
||||
verify(auth).record("admin", "LOGIN_SUCCESS", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void logout_recordsLogoutForTokenUser_beforeRevoking() throws Exception {
|
||||
when(auth.loadUserByToken("tok")).thenReturn(user(true));
|
||||
|
||||
mvc.perform(post("/api/admin/auth/logout")).andExpect(status().isOk())
|
||||
.andExpect(content().json("{\"result\":\"true\"}"));
|
||||
verify(auth).record("admin", "LOGOUT", null);
|
||||
verify(authenticationService).logoutToken("tok");
|
||||
}
|
||||
|
||||
@Test
|
||||
void logout_nullUser_recordsNothing() throws Exception {
|
||||
when(auth.loadUserByToken("tok")).thenReturn(null);
|
||||
|
||||
mvc.perform(post("/api/admin/auth/logout")).andExpect(status().isOk());
|
||||
verify(auth, never()).record(any(), any(), any());
|
||||
verify(authenticationService).logoutToken("tok");
|
||||
}
|
||||
|
||||
@Test
|
||||
void logout_loadUserThrows_stillReturns200() throws Exception {
|
||||
when(auth.loadUserByToken("tok")).thenThrow(new IllegalStateException("down"));
|
||||
|
||||
mvc.perform(post("/api/admin/auth/logout")).andExpect(status().isOk())
|
||||
.andExpect(content().json("{\"result\":\"true\"}"));
|
||||
verify(authenticationService).logoutToken("tok");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user