feat: admin change-password endpoint with lockout on wrong current password
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
39708224ad
commit
6c6734baa3
7 files changed
+277
-1
No files matched your search
+169
@@ -0,0 +1,169 @@
|
||||
package com.solvway.mobile.api.controller.admin;
|
||||
|
||||
import com.solvway.core.model.User;
|
||||
import com.solvway.core.utils.HttpReqRespUtils;
|
||||
import com.solvway.mobile.api.dto.admin.AdminChangePasswordRequest;
|
||||
import com.solvway.mobile.api.errorhandler.CodedResponseStatusException;
|
||||
import com.solvway.mobile.api.service.AdminAuthenticationService;
|
||||
import com.solvway.mobile.api.service.AuthenticationService;
|
||||
import com.solvway.mobile.api.useraccess.AdminModule;
|
||||
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.HttpStatus;
|
||||
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 static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
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.status;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class AdminAuthControllerChangePasswordTest {
|
||||
|
||||
@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 void caller() {
|
||||
User u = new User();
|
||||
u.setId(9L);
|
||||
u.setUsername("admin.user");
|
||||
when(auth.loadUserByToken("tok")).thenReturn(u);
|
||||
}
|
||||
|
||||
private ResultActions change(String current, String next, String confirm) throws Exception {
|
||||
String body = "{\"currentPassword\":" + q(current) + ",\"newPassword\":" + q(next)
|
||||
+ ",\"confirmPassword\":" + q(confirm) + "}";
|
||||
return mvc.perform(post("/api/admin/auth/change-password").contentType(MediaType.APPLICATION_JSON).content(body));
|
||||
}
|
||||
|
||||
private static String q(String s) { return s == null ? "null" : "\"" + s + "\""; }
|
||||
|
||||
private void assertCoded(ResultActions r, HttpStatus status, String code, String message) {
|
||||
Exception ex = r.andReturn().getResolvedException();
|
||||
assertThat(ex).isInstanceOf(CodedResponseStatusException.class);
|
||||
CodedResponseStatusException c = (CodedResponseStatusException) ex;
|
||||
assertThat(c.getStatusCode()).isEqualTo(status);
|
||||
assertThat(c.getCode()).isEqualTo(code);
|
||||
assertThat(c.getReason()).isEqualTo(message);
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullCaller_is401() throws Exception {
|
||||
when(auth.loadUserByToken("tok")).thenReturn(null);
|
||||
change("old", "new", "new").andExpect(status().isUnauthorized());
|
||||
verify(auth, never()).changeAdminPassword(any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void blankField_is400RequiredField() throws Exception {
|
||||
caller();
|
||||
assertCoded(change("old", " ", "new"), HttpStatus.BAD_REQUEST, "REQUIRED_FIELD",
|
||||
"Current password, new password and confirmation are required");
|
||||
assertCoded(change(null, "new", "new"), HttpStatus.BAD_REQUEST, "REQUIRED_FIELD",
|
||||
"Current password, new password and confirmation are required");
|
||||
verify(auth, never()).changeAdminPassword(any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void mismatch_is400PasswordMismatch() throws Exception {
|
||||
caller();
|
||||
assertCoded(change("old", "new", "other"), HttpStatus.BAD_REQUEST, "PASSWORD_MISMATCH", "Passwords do not match");
|
||||
verify(auth, never()).changeAdminPassword(any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void locked_is400AndServicesSystemNotCalled() throws Exception {
|
||||
caller();
|
||||
when(auth.isLocked("admin.user")).thenReturn(true);
|
||||
assertCoded(change("old", "new", "new"), HttpStatus.BAD_REQUEST, "ACCOUNT_LOCKED",
|
||||
"Too many failed sign-ins. Try again later or ask an administrator to unlock your account.");
|
||||
verify(auth, never()).changeAdminPassword(any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void currentPasswordWrong_recordsLoginFailAnd400() throws Exception {
|
||||
caller();
|
||||
when(auth.changeAdminPassword(9L, "old", "new")).thenReturn("CURRENT_PASSWORD_WRONG");
|
||||
assertCoded(change("old", "new", "new"), HttpStatus.BAD_REQUEST, "CURRENT_PASSWORD_WRONG",
|
||||
"Current password is incorrect");
|
||||
verify(auth).record("admin.user", "LOGIN_FAIL", "change-password");
|
||||
}
|
||||
|
||||
@Test
|
||||
void passwordSame_is400() throws Exception {
|
||||
caller();
|
||||
when(auth.changeAdminPassword(9L, "old", "old")).thenReturn("PASSWORD_SAME");
|
||||
assertCoded(change("old", "old", "old"), HttpStatus.BAD_REQUEST, "PASSWORD_SAME",
|
||||
"The new password must differ from the current one");
|
||||
}
|
||||
|
||||
@Test
|
||||
void passwordPolicy_is400() throws Exception {
|
||||
caller();
|
||||
when(auth.changeAdminPassword(9L, "old", "weak")).thenReturn("PASSWORD_POLICY");
|
||||
assertCoded(change("old", "weak", "weak"), HttpStatus.BAD_REQUEST, "PASSWORD_POLICY",
|
||||
"Password does not meet the password policy");
|
||||
}
|
||||
|
||||
@Test
|
||||
void notFoundOrUnknown_is500SaveFailed() throws Exception {
|
||||
caller();
|
||||
when(auth.changeAdminPassword(9L, "old", "new")).thenReturn("NOT_FOUND");
|
||||
assertCoded(change("old", "new", "new"), HttpStatus.INTERNAL_SERVER_ERROR, "SAVE_FAILED",
|
||||
"The password was not changed");
|
||||
when(auth.changeAdminPassword(9L, "old", "new2")).thenReturn("???");
|
||||
assertCoded(change("old", "new2", "new2"), HttpStatus.INTERNAL_SERVER_ERROR, "SAVE_FAILED",
|
||||
"The password was not changed");
|
||||
}
|
||||
|
||||
@Test
|
||||
void ok_recordsPasswordChanged_204_andKeepsSession() throws Exception {
|
||||
caller();
|
||||
when(auth.changeAdminPassword(9L, "old", "new")).thenReturn("OK");
|
||||
change("old", "new", "new").andExpect(status().isNoContent());
|
||||
verify(auth).record("admin.user", "PASSWORD_CHANGED", null);
|
||||
verifyNoInteractions(authenticationService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void requestToString_hidesPasswords() {
|
||||
String s = new AdminChangePasswordRequest("curSecret1", "newSecret2", "confSecret3").toString();
|
||||
assertThat(s).doesNotContain("curSecret1").doesNotContain("newSecret2").doesNotContain("confSecret3");
|
||||
}
|
||||
|
||||
@Test
|
||||
void handler_refusesPendingFirstLogin() throws Exception {
|
||||
AdminModule m = AdminAuthController.class
|
||||
.getMethod("changePassword", AdminChangePasswordRequest.class).getAnnotation(AdminModule.class);
|
||||
assertThat(m.value()).containsExactly(AdminModule.ANY_ADMIN);
|
||||
assertThat(m.allowPendingReset()).isFalse();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user