feat(mobile-api): admin login, first-login, forgot password, logout, refresh and me

Wrong, blank or failed logins all answer the same 401. A deactivated admin is
refused and the token services-auth issued is revoked. resetAdminPassword now
refuses deactivated users and users without an email.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
ngqileandClaude Opus 5.5 committed 2026-11-26 19:55:58 +08:00
1 parent 1bfc1c73c6
commit 9f22837a00
14 files changed
+567 -2

No files matched your search

@@ -0,0 +1,67 @@
package com.solvway.mobile.api.service;
import com.solvway.core.model.User;
import com.solvway.mobile.api.model.AuthRequest;
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.RestTemplate;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.http.HttpMethod.POST;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.content;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
class AdminAuthenticationServiceWireTest {
private MockRestServiceServer server;
private AdminAuthenticationService service;
@BeforeEach
void setUp() {
RestTemplate restTemplate = new RestTemplate();
server = MockRestServiceServer.bindTo(restTemplate).build();
RestTemplateCustomUtil util = new RestTemplateCustomUtil(restTemplate);
ReflectionTestUtils.setField(util, "apiUrl", "http://gw");
ReflectionTestUtils.setField(util, "apiKey", "k");
service = new AdminAuthenticationService(util);
}
@Test
void getToken_postsToAdminAuthenticate_emptyBodyIsNull() {
server.expect(requestTo("http://gw/auth/authenticate")).andExpect(method(POST))
.andExpect(content().json("{\"username\":\"admin\",\"password\":\"pw\"}"))
.andRespond(withSuccess("", MediaType.TEXT_PLAIN));
AuthRequest r = new AuthRequest();
r.setUsername("admin");
r.setPassword("pw");
assertThat(service.getToken(r)).isNullOrEmpty();
server.verify();
}
@Test
void firstTimeLogin_postsUserIdAndBothPasswordsToAdminEndpoint() {
server.expect(requestTo("http://gw/user/firstTimeLogin")).andExpect(method(POST))
.andExpect(content().json("{\"userId\":9,\"newPassword\":\"New#Pass1\",\"repeatPassword\":\"New#Pass1\"}"))
.andRespond(withSuccess("true", MediaType.APPLICATION_JSON));
assertThat(service.firstTimeLogin(9L, "New#Pass1")).isTrue();
server.verify();
}
@Test
void resetAdminPassword_postsUsernameAndEmail() {
server.expect(requestTo("http://gw/user/resetAdminPassword")).andExpect(method(POST))
.andExpect(content().json("{\"username\":\"sales1\",\"email\":\"a@b.c\"}"))
.andRespond(withSuccess("1", MediaType.APPLICATION_JSON));
assertThat(service.resetAdminPassword("sales1", "a@b.c")).isEqualTo(1);
server.verify();
}
}