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:
ngqileandClaude Opus 5.5 committed 2026-11-30 13:43:42 +08:00
1 parent f6fe246ad0
commit 39708224ad
7 files changed
+349 -3

No files matched your search

@@ -0,0 +1,106 @@
package com.solvway.mobile.api.service;
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@SuppressWarnings({"unchecked", "rawtypes"})
class AdminAuthenticationServiceLoginSecurityTest {
private RestTemplateCustomUtil rest;
private AdminAuthenticationService service;
@BeforeEach
void setUp() {
rest = mock(RestTemplateCustomUtil.class);
service = new AdminAuthenticationService(rest);
when(rest.getURLforApiService(any())).thenReturn("http://gw/loginSecurity/x");
}
@AfterEach
void clear() { RequestContextHolder.resetRequestAttributes(); }
@Test
void isLocked_true() {
when(rest.getForEntity(any(String.class), eq(Map.class), eq("sales1")))
.thenReturn(ResponseEntity.ok(Map.of("locked", true)));
assertThat(service.isLocked("sales1")).isTrue();
}
@Test
void isLocked_false() {
when(rest.getForEntity(any(String.class), eq(Map.class), eq("sales1")))
.thenReturn(ResponseEntity.ok(Map.of("locked", false)));
assertThat(service.isLocked("sales1")).isFalse();
}
@Test
void isLocked_exception_failsOpen() {
when(rest.getForEntity(any(String.class), eq(Map.class), eq("sales1")))
.thenThrow(new IllegalStateException("down"));
assertThat(service.isLocked("sales1")).isFalse();
}
@Test
void isLocked_non200_failsOpen() {
when(rest.getForEntity(any(String.class), eq(Map.class), eq("sales1")))
.thenReturn(new ResponseEntity<>(HttpStatus.NO_CONTENT));
assertThat(service.isLocked("sales1")).isFalse();
}
@Test
void record_swallowsExceptions() {
when(rest.postForEntity(any(String.class), eq(String.class), any()))
.thenThrow(new IllegalStateException("down"));
assertThatCode(() -> service.record("sales1", "LOGIN_FAIL", null)).doesNotThrowAnyException();
}
@Test
void record_sendsClientIpAndUserAgentFromRequest() {
MockHttpServletRequest req = new MockHttpServletRequest();
req.addHeader("X-Client-IP", "203.0.113.7");
req.addHeader("User-Agent", "JUnit");
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(req));
when(rest.postForEntity(any(String.class), eq(String.class), any())).thenReturn(ResponseEntity.ok(""));
service.record("sales1", "LOGIN_FAIL", "why");
ArgumentCaptor<Object> body = ArgumentCaptor.forClass(Object.class);
verify(rest).postForEntity(any(String.class), eq(String.class), body.capture());
assertThat((Map<String, Object>) body.getValue()).containsOnly(
Map.entry("username", "sales1"), Map.entry("event", "LOGIN_FAIL"), Map.entry("ip", "203.0.113.7"),
Map.entry("userAgent", "JUnit"), Map.entry("message", "why"));
}
@Test
void record_noHeaders_sendsNullIpAndUserAgent() {
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(new MockHttpServletRequest()));
when(rest.postForEntity(any(String.class), eq(String.class), any())).thenReturn(ResponseEntity.ok(""));
service.record("sales1", "LOGOUT", null);
ArgumentCaptor<Object> body = ArgumentCaptor.forClass(Object.class);
verify(rest).postForEntity(any(String.class), eq(String.class), body.capture());
Map<String, Object> m = (Map<String, Object>) body.getValue();
assertThat(m.get("ip")).isNull();
assertThat(m.get("userAgent")).isNull();
assertThat(m).containsEntry("event", "LOGOUT");
}
}