chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session, api-gateway, mobile-api, services-auth, services-system, services-counter, services-member, services-log, services-email. solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal from tastefully 8bd93d15. Copied as tracked files only. Literal secrets replaced with environment placeholders: - services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY, MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default) - services-system: JUMIO_TOKEN, JUMIO_SECRET (optional) - services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed - core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default removed, ipgeolocation key read from IPGEOLOCATION_API_KEY - services-system EkycAppConfig: commented Jumio credentials removed 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
e396568cc3
commit
5ee51d1913
2918 files changed
+302373
-3
No files matched your search
+158
@@ -0,0 +1,158 @@
|
||||
package com.solvway.mobile.api.useraccess;
|
||||
|
||||
import com.google.cloud.spring.pubsub.core.PubSubTemplate;
|
||||
import com.solvway.core.annotation.AppAuth;
|
||||
import com.solvway.core.utils.HttpReqRespUtils;
|
||||
import com.solvway.core.utils.JwtTokenUtil;
|
||||
import com.solvway.core.utils.PubSubUtil;
|
||||
import com.solvway.mobile.api.config.AuthAppKeyConfig;
|
||||
import com.solvway.mobile.api.config.PubSubConfig;
|
||||
import com.solvway.mobile.api.service.AuthenticationService;
|
||||
import com.solvway.mobile.api.service.MemberService;
|
||||
import com.solvway.mobile.api.service.UserService;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.mockito.junit.jupiter.MockitoSettings;
|
||||
import org.mockito.quality.Strictness;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@MockitoSettings(strictness = Strictness.LENIENT)
|
||||
class AppAccessAspectAuthTest {
|
||||
|
||||
private static final String VALID_API_KEY = "valid-api-key";
|
||||
|
||||
@Mock private MemberService memberService;
|
||||
@Mock private UserService userService;
|
||||
@Mock private PubSubConfig pubSubConfig;
|
||||
@Mock private PubSubTemplate pubSubTemplate;
|
||||
@Mock private PubSubUtil pubSubUtil;
|
||||
@Mock private AuthenticationService authenticationService;
|
||||
@Mock private JwtTokenUtil jwtTokenUtil;
|
||||
@Mock private ProceedingJoinPoint joinPoint;
|
||||
@Mock private MethodSignature methodSignature;
|
||||
|
||||
private AppAccessAspect aspect;
|
||||
|
||||
static class Target {
|
||||
@AppAuth
|
||||
public void secured() {
|
||||
}
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
AuthAppKeyConfig authAppKeyConfig = new AuthAppKeyConfig();
|
||||
authAppKeyConfig.setList(List.of(VALID_API_KEY));
|
||||
aspect = new AppAccessAspect(memberService, userService, pubSubConfig, pubSubTemplate,
|
||||
pubSubUtil, authAppKeyConfig, authenticationService, jwtTokenUtil);
|
||||
|
||||
Method secured = Target.class.getMethod("secured");
|
||||
when(joinPoint.getSignature()).thenReturn(methodSignature);
|
||||
when(methodSignature.getMethod()).thenReturn(secured);
|
||||
when(methodSignature.toShortString()).thenReturn("Target.secured()");
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingApiKeyIsUnauthorized() {
|
||||
try (MockedStatic<HttpReqRespUtils> http = mockStatic(HttpReqRespUtils.class)) {
|
||||
http.when(() -> HttpReqRespUtils.getHeaderValues("api-key")).thenReturn("");
|
||||
http.when(HttpReqRespUtils::getJwtToken).thenReturn("");
|
||||
http.when(HttpReqRespUtils::getAllHeaders).thenReturn("{}");
|
||||
|
||||
assertThatThrownBy(() -> aspect.appAccessAuthCheck(joinPoint))
|
||||
.isInstanceOf(ResponseStatusException.class)
|
||||
.satisfies(ex -> assertThat(((ResponseStatusException) ex).getStatusCode())
|
||||
.isEqualTo(HttpStatus.UNAUTHORIZED));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidApiKeyIsUnauthorized() {
|
||||
try (MockedStatic<HttpReqRespUtils> http = mockStatic(HttpReqRespUtils.class)) {
|
||||
http.when(() -> HttpReqRespUtils.getHeaderValues("api-key")).thenReturn("wrong-key");
|
||||
http.when(HttpReqRespUtils::getJwtToken).thenReturn("");
|
||||
http.when(HttpReqRespUtils::getAllHeaders).thenReturn("{}");
|
||||
|
||||
assertThatThrownBy(() -> aspect.appAccessAuthCheck(joinPoint))
|
||||
.isInstanceOf(ResponseStatusException.class)
|
||||
.satisfies(ex -> assertThat(((ResponseStatusException) ex).getStatusCode())
|
||||
.isEqualTo(HttpStatus.UNAUTHORIZED));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingAuthorizationHeaderIsUnauthorizedNotBadRequest() {
|
||||
try (MockedStatic<HttpReqRespUtils> http = mockStatic(HttpReqRespUtils.class)) {
|
||||
http.when(() -> HttpReqRespUtils.getHeaderValues("api-key")).thenReturn(VALID_API_KEY);
|
||||
http.when(HttpReqRespUtils::getJwtToken).thenReturn("");
|
||||
http.when(HttpReqRespUtils::getAllHeaders).thenReturn("{}");
|
||||
|
||||
assertThatThrownBy(() -> aspect.appAccessAuthCheck(joinPoint))
|
||||
.isInstanceOf(ResponseStatusException.class)
|
||||
.satisfies(ex -> assertThat(((ResponseStatusException) ex).getStatusCode())
|
||||
.isEqualTo(HttpStatus.UNAUTHORIZED));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void expiredTokenIsUnauthorizedNotServerError() {
|
||||
try (MockedStatic<HttpReqRespUtils> http = mockStatic(HttpReqRespUtils.class)) {
|
||||
http.when(() -> HttpReqRespUtils.getHeaderValues("api-key")).thenReturn(VALID_API_KEY);
|
||||
http.when(HttpReqRespUtils::getJwtToken).thenReturn("expired-token");
|
||||
http.when(HttpReqRespUtils::getAllHeaders).thenReturn("{}");
|
||||
when(jwtTokenUtil.isTokenExpired("expired-token")).thenReturn(true);
|
||||
|
||||
assertThatThrownBy(() -> aspect.appAccessAuthCheck(joinPoint))
|
||||
.isInstanceOf(ResponseStatusException.class)
|
||||
.satisfies(ex -> assertThat(((ResponseStatusException) ex).getStatusCode())
|
||||
.isEqualTo(HttpStatus.UNAUTHORIZED));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void malformedTokenIsUnauthorizedNotServerError() {
|
||||
try (MockedStatic<HttpReqRespUtils> http = mockStatic(HttpReqRespUtils.class)) {
|
||||
http.when(() -> HttpReqRespUtils.getHeaderValues("api-key")).thenReturn(VALID_API_KEY);
|
||||
http.when(HttpReqRespUtils::getJwtToken).thenReturn("garbage");
|
||||
http.when(HttpReqRespUtils::getAllHeaders).thenReturn("{}");
|
||||
when(jwtTokenUtil.isTokenExpired("garbage")).thenThrow(new RuntimeException("bad signature"));
|
||||
|
||||
assertThatThrownBy(() -> aspect.appAccessAuthCheck(joinPoint))
|
||||
.isInstanceOf(ResponseStatusException.class)
|
||||
.satisfies(ex -> assertThat(((ResponseStatusException) ex).getStatusCode())
|
||||
.isEqualTo(HttpStatus.UNAUTHORIZED));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void validTokenProceedsToController() throws Throwable {
|
||||
try (MockedStatic<HttpReqRespUtils> http = mockStatic(HttpReqRespUtils.class)) {
|
||||
http.when(() -> HttpReqRespUtils.getHeaderValues("api-key")).thenReturn(VALID_API_KEY);
|
||||
http.when(HttpReqRespUtils::getJwtToken).thenReturn("good-token");
|
||||
http.when(HttpReqRespUtils::getAllHeaders).thenReturn("{}");
|
||||
when(jwtTokenUtil.isTokenExpired("good-token")).thenReturn(false);
|
||||
when(authenticationService.getUsernameFromToken("good-token")).thenReturn("alice");
|
||||
when(joinPoint.proceed()).thenReturn("controller-result");
|
||||
|
||||
Object result = aspect.appAccessAuthCheck(joinPoint);
|
||||
|
||||
assertThat(result).isEqualTo("controller-result");
|
||||
}
|
||||
}
|
||||
}
|
||||
+191
@@ -0,0 +1,191 @@
|
||||
package com.solvway.mobile.api.useraccess;
|
||||
|
||||
import com.google.cloud.spring.pubsub.core.PubSubTemplate;
|
||||
import com.solvway.core.annotation.AppAuth;
|
||||
import com.solvway.core.enums.GeneralEnum.FlagYesNo;
|
||||
import com.solvway.core.model.User;
|
||||
import com.solvway.core.utils.HttpReqRespUtils;
|
||||
import com.solvway.core.utils.JwtTokenUtil;
|
||||
import com.solvway.core.utils.PubSubUtil;
|
||||
import com.solvway.mobile.api.config.AuthAppKeyConfig;
|
||||
import com.solvway.mobile.api.config.PubSubConfig;
|
||||
import com.solvway.mobile.api.controller.MemberController;
|
||||
import com.solvway.mobile.api.service.AuthenticationService;
|
||||
import com.solvway.mobile.api.service.MemberService;
|
||||
import com.solvway.mobile.api.service.UserService;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.mockito.junit.jupiter.MockitoSettings;
|
||||
import org.mockito.quality.Strictness;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
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;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@MockitoSettings(strictness = Strictness.LENIENT)
|
||||
class AppAccessAspectPhoneVerificationTest {
|
||||
|
||||
private static final String CP_KEY = "cp-key";
|
||||
private static final String APP_KEY = "app-key";
|
||||
private static final String JWT = "good-token";
|
||||
|
||||
@Mock private MemberService memberService;
|
||||
@Mock private UserService userService;
|
||||
@Mock private PubSubConfig pubSubConfig;
|
||||
@Mock private PubSubTemplate pubSubTemplate;
|
||||
@Mock private PubSubUtil pubSubUtil;
|
||||
@Mock private AuthenticationService authenticationService;
|
||||
@Mock private JwtTokenUtil jwtTokenUtil;
|
||||
@Mock private ProceedingJoinPoint joinPoint;
|
||||
@Mock private MethodSignature methodSignature;
|
||||
|
||||
private AppAccessAspect aspect;
|
||||
|
||||
static class Target {
|
||||
@AppAuth
|
||||
public void secured() {
|
||||
}
|
||||
|
||||
@AppAuth
|
||||
@PhoneVerificationExempt
|
||||
public void exempt() {
|
||||
}
|
||||
|
||||
@AppAuth(skipValidate = true)
|
||||
public void open() {
|
||||
}
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Throwable {
|
||||
AuthAppKeyConfig cfg = new AuthAppKeyConfig();
|
||||
cfg.setList(List.of(CP_KEY + ":CP", APP_KEY + ":APP"));
|
||||
aspect = new AppAccessAspect(memberService, userService, pubSubConfig, pubSubTemplate,
|
||||
pubSubUtil, cfg, authenticationService, jwtTokenUtil);
|
||||
when(joinPoint.getSignature()).thenReturn(methodSignature);
|
||||
when(methodSignature.toShortString()).thenReturn("Target.m()");
|
||||
when(jwtTokenUtil.isTokenExpired(JWT)).thenReturn(false);
|
||||
when(authenticationService.getUsernameFromToken(JWT)).thenReturn("alice");
|
||||
when(joinPoint.proceed()).thenReturn("result");
|
||||
}
|
||||
|
||||
private void useMethod(String name) throws Exception {
|
||||
when(methodSignature.getMethod()).thenReturn(Target.class.getMethod(name));
|
||||
}
|
||||
|
||||
private static User user(FlagYesNo phone, FlagYesNo email) {
|
||||
User u = new User();
|
||||
u.setPhoneVerified(phone);
|
||||
u.setEmailVerified(email);
|
||||
return u;
|
||||
}
|
||||
|
||||
private Object call(String apiKey) throws Throwable {
|
||||
try (MockedStatic<HttpReqRespUtils> http = mockStatic(HttpReqRespUtils.class)) {
|
||||
http.when(() -> HttpReqRespUtils.getHeaderValues("api-key")).thenReturn(apiKey);
|
||||
http.when(HttpReqRespUtils::getJwtToken).thenReturn(JWT);
|
||||
http.when(HttpReqRespUtils::getAllHeaders).thenReturn("{}");
|
||||
return aspect.appAccessAuthCheck(joinPoint);
|
||||
}
|
||||
}
|
||||
|
||||
private void assertForbidden(String apiKey) throws Throwable {
|
||||
assertThatThrownBy(() -> call(apiKey))
|
||||
.isInstanceOf(ResponseStatusException.class)
|
||||
.satisfies(ex -> {
|
||||
ResponseStatusException r = (ResponseStatusException) ex;
|
||||
assertThat(r.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
|
||||
assertThat(r.getReason()).isEqualTo("Phone verification required.");
|
||||
assertThat(r.getHeaders().getFirst("X-Phone-Verification-Required")).isEqualTo("true");
|
||||
});
|
||||
verify(joinPoint, never()).proceed();
|
||||
}
|
||||
|
||||
@Test
|
||||
void cpUnverifiedIsForbiddenAndControllerNotRun() throws Throwable {
|
||||
useMethod("secured");
|
||||
when(userService.findByUsername("alice")).thenReturn(user(FlagYesNo.N, FlagYesNo.N));
|
||||
assertForbidden(CP_KEY);
|
||||
}
|
||||
|
||||
@Test
|
||||
void cpPhoneVerifiedProceeds() throws Throwable {
|
||||
useMethod("secured");
|
||||
when(userService.findByUsername("alice")).thenReturn(user(FlagYesNo.Y, FlagYesNo.N));
|
||||
assertThat(call(CP_KEY)).isEqualTo("result");
|
||||
}
|
||||
|
||||
@Test
|
||||
void cpEmailVerifiedProceeds() throws Throwable {
|
||||
useMethod("secured");
|
||||
when(userService.findByUsername("alice")).thenReturn(user(FlagYesNo.N, FlagYesNo.Y));
|
||||
assertThat(call(CP_KEY)).isEqualTo("result");
|
||||
}
|
||||
|
||||
@Test
|
||||
void cpExemptMethodProceedsWhenUnverified() throws Throwable {
|
||||
useMethod("exempt");
|
||||
when(userService.findByUsername("alice")).thenReturn(user(FlagYesNo.N, FlagYesNo.N));
|
||||
assertThat(call(CP_KEY)).isEqualTo("result");
|
||||
}
|
||||
|
||||
@Test
|
||||
void appPlatformUnverifiedProceedsWithoutLookup() throws Throwable {
|
||||
useMethod("secured");
|
||||
when(userService.findByUsername("alice")).thenReturn(user(FlagYesNo.N, FlagYesNo.N));
|
||||
assertThat(call(APP_KEY)).isEqualTo("result");
|
||||
verify(userService, never()).findByUsername(anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void skipValidateMethodDoesNoUserLookup() throws Throwable {
|
||||
useMethod("open");
|
||||
assertThat(call(CP_KEY)).isEqualTo("result");
|
||||
verify(userService, never()).findByUsername(anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void cpNullUserIsForbidden() throws Throwable {
|
||||
useMethod("secured");
|
||||
when(userService.findByUsername("alice")).thenReturn(null);
|
||||
assertForbidden(CP_KEY);
|
||||
}
|
||||
|
||||
@Test
|
||||
void cpLookupThrowingIsForbidden() throws Throwable {
|
||||
useMethod("secured");
|
||||
when(userService.findByUsername("alice")).thenThrow(new RuntimeException("system-service down"));
|
||||
assertForbidden(CP_KEY);
|
||||
}
|
||||
|
||||
@Test
|
||||
void otpEndpointsCarryPhoneVerificationExempt() throws Exception {
|
||||
Method required = MemberController.class.getMethod("getOtpRequired");
|
||||
Method verify = MemberController.class.getMethod("postOtpVerification", Map.class);
|
||||
assertThat(required.isAnnotationPresent(PhoneVerificationExempt.class)).isTrue();
|
||||
assertThat(verify.isAnnotationPresent(PhoneVerificationExempt.class)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void firstTimePasswordCarriesPhoneVerificationExempt() throws Exception {
|
||||
Method m = MemberController.class.getMethod("firstTimePassword", Map.class);
|
||||
assertThat(m.isAnnotationPresent(PhoneVerificationExempt.class)).isTrue();
|
||||
}
|
||||
}
|
||||
+156
@@ -0,0 +1,156 @@
|
||||
package com.solvway.mobile.api.useraccess;
|
||||
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.read.ListAppender;
|
||||
import com.google.cloud.spring.pubsub.core.PubSubTemplate;
|
||||
import com.solvway.core.annotation.AppAuth;
|
||||
import com.solvway.core.enums.GeneralEnum.CmsPlatform;
|
||||
import com.solvway.core.model.ResponseTime;
|
||||
import com.solvway.core.utils.JwtTokenUtil;
|
||||
import com.solvway.core.utils.PubSubUtil;
|
||||
import com.solvway.mobile.api.config.AuthAppKeyConfig;
|
||||
import com.solvway.mobile.api.config.PubSubConfig;
|
||||
import com.solvway.mobile.api.service.AuthenticationService;
|
||||
import com.solvway.mobile.api.service.MemberService;
|
||||
import com.solvway.mobile.api.service.UserService;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
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.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.mockito.junit.jupiter.MockitoSettings;
|
||||
import org.mockito.quality.Strictness;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** add-response-time-platform-attribution: rows carry the platform of the api-key that authorised the call. */
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@MockitoSettings(strictness = Strictness.LENIENT)
|
||||
class AppAccessAspectResponseTimeTest {
|
||||
|
||||
private static final String TOPIC = "response-time-log-topic";
|
||||
|
||||
@Mock private ProceedingJoinPoint joinPoint;
|
||||
@Mock private MethodSignature methodSignature;
|
||||
@Mock private PubSubTemplate pubSubTemplate;
|
||||
@Mock private PubSubUtil pubSubUtil;
|
||||
@Mock private JwtTokenUtil jwtTokenUtil;
|
||||
@Mock private AuthenticationService authenticationService;
|
||||
|
||||
private AppAccessAspect aspect;
|
||||
|
||||
static class Target {
|
||||
@AppAuth(skipValidate = true) public void publicEndpoint() {}
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Throwable {
|
||||
AuthAppKeyConfig keys = new AuthAppKeyConfig();
|
||||
keys.setList(List.of("cpKey:CP", "legacyKey", "abcd1234:CP"));
|
||||
PubSubConfig pubSubConfig = mock(PubSubConfig.class);
|
||||
pubSubConfig.responseTimeLogTopic = TOPIC;
|
||||
aspect = new AppAccessAspect(mock(MemberService.class), mock(UserService.class), pubSubConfig,
|
||||
pubSubTemplate, pubSubUtil, keys, authenticationService, jwtTokenUtil);
|
||||
|
||||
when(joinPoint.getSignature()).thenReturn(methodSignature);
|
||||
when(methodSignature.getMethod()).thenReturn(Target.class.getMethod("publicEndpoint"));
|
||||
when(methodSignature.toShortString()).thenReturn("Target.publicEndpoint()");
|
||||
when(joinPoint.proceed()).thenReturn("ok");
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
RequestContextHolder.resetRequestAttributes();
|
||||
}
|
||||
|
||||
private void requestWithApiKey(String apiKey) {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/api/public/banner");
|
||||
request.addHeader("api-key", apiKey);
|
||||
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
|
||||
}
|
||||
|
||||
private ResponseTime published() {
|
||||
ArgumentCaptor<ResponseTime> captor = ArgumentCaptor.forClass(ResponseTime.class);
|
||||
verify(pubSubUtil).publishMessage(eq(pubSubTemplate), eq(TOPIC), captor.capture());
|
||||
return captor.getValue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void customerPortalKeyPassesGateAndPublishesCp() throws Throwable {
|
||||
requestWithApiKey("cpKey");
|
||||
|
||||
assertThat(aspect.appAccessAuthCheck(joinPoint)).isEqualTo("ok");
|
||||
|
||||
ResponseTime rt = published();
|
||||
assertThat(rt.getSourcePlatform()).isEqualTo(CmsPlatform.CP);
|
||||
assertThat(rt.getUrl()).isEqualTo("/api/public/banner");
|
||||
assertThat(rt.getMethod()).isEqualTo("GET");
|
||||
}
|
||||
|
||||
@Test
|
||||
void bareKeyPublishesApp() throws Throwable {
|
||||
requestWithApiKey("legacyKey");
|
||||
|
||||
aspect.appAccessAuthCheck(joinPoint);
|
||||
|
||||
assertThat(published().getSourcePlatform()).isEqualTo(CmsPlatform.APP);
|
||||
}
|
||||
|
||||
@Test
|
||||
void rawSuffixedTokenIsRejectedAndNotPublished() {
|
||||
requestWithApiKey("cpKey:CP");
|
||||
|
||||
assertThatThrownBy(() -> aspect.appAccessAuthCheck(joinPoint))
|
||||
.isInstanceOfSatisfying(ResponseStatusException.class,
|
||||
ex -> assertThat(ex.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED));
|
||||
verify(pubSubUtil, never()).publishMessage(any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void publisherFailureDoesNotBreakSuccessfulResponse() throws Throwable {
|
||||
requestWithApiKey("cpKey");
|
||||
doThrow(new RuntimeException("pubsub down")).when(pubSubUtil).publishMessage(any(), any(), any());
|
||||
|
||||
assertThat(aspect.appAccessAuthCheck(joinPoint)).isEqualTo("ok");
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectLogNeverPrintsAConfiguredKey() {
|
||||
requestWithApiKey("wrong-key");
|
||||
Logger logger = (Logger) LoggerFactory.getLogger(AppAccessAspect.class);
|
||||
ListAppender<ILoggingEvent> appender = new ListAppender<>();
|
||||
appender.start();
|
||||
logger.addAppender(appender);
|
||||
try {
|
||||
assertThatThrownBy(() -> aspect.appAccessAuthCheck(joinPoint)).isInstanceOf(ResponseStatusException.class);
|
||||
} finally {
|
||||
logger.detachAppender(appender);
|
||||
}
|
||||
|
||||
// An 8-character key with a platform suffix used to be printed in full by the masked allow-list.
|
||||
assertThat(appender.list).isNotEmpty()
|
||||
.allSatisfy(event -> assertThat(event.getFormattedMessage())
|
||||
.doesNotContain("abcd1234").doesNotContain("cpKey").doesNotContain("legacyKey"));
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.solvway.mobile.api.useraccess;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
class PhoneVerificationHeaderMvcTest {
|
||||
|
||||
@RestController
|
||||
static class ThrowingController {
|
||||
@GetMapping("/gated")
|
||||
public String gated() {
|
||||
throw AppAccessAspect.phoneVerificationRequired();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void gateExceptionBecomes403WithHeaderOnTheHttpResponse() throws Exception {
|
||||
MockMvc mvc = MockMvcBuilders.standaloneSetup(new ThrowingController()).build();
|
||||
mvc.perform(get("/gated"))
|
||||
.andExpect(status().isForbidden())
|
||||
.andExpect(header().string("X-Phone-Verification-Required", "true"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user