fix(gateway): let the public QR registration reach its backend calls
A visitor has no user token, so the gateway refused every call the public page makes. Open only the exact method and path pairs it needs, and replace the full user lookup with a yes/no activeAdminOf endpoint so no user record is exposed. 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
1a7c542f5e
commit
eff4899c0d
8 files changed
+329
-10
No files matched your search
+121
@@ -0,0 +1,121 @@
|
||||
package com.solvway.mobile.api.service;
|
||||
|
||||
import com.solvway.core.enums.GeneralEnum;
|
||||
import com.solvway.core.utils.HttpReqRespUtils;
|
||||
import com.solvway.mobile.api.dto.PublicRegistrationRequest;
|
||||
import com.solvway.mobile.api.dto.admin.CompanyEvent;
|
||||
import com.solvway.mobile.api.useraccess.AdminCompanyScope;
|
||||
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.mockito.MockedStatic;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.ValueOperations;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** The visitor has no token, so the salesperson check must use the open activeAdminOf call, never the closed findById. */
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
class PublicRegistrationSalespersonTest {
|
||||
|
||||
private static final String ACTIVE_ADMIN_OF = "/user/{id}/activeAdminOf/{companyId}";
|
||||
private static final String FIND_BY_ID = "/user/findById/{id}";
|
||||
private static final long EV = 3312726793093644288L;
|
||||
private static final long COMPANY = 5L;
|
||||
private static final String TOKEN = "abc123";
|
||||
|
||||
private RestTemplateCustomUtil rest;
|
||||
private MockedStatic<HttpReqRespUtils> http;
|
||||
private PublicRegistrationService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
rest = mock(RestTemplateCustomUtil.class);
|
||||
RedisTemplate<String, Object> redis = mock(RedisTemplate.class);
|
||||
ValueOperations<String, Object> ops = mock(ValueOperations.class);
|
||||
when(redis.opsForValue()).thenReturn(ops);
|
||||
when(ops.increment(any())).thenReturn(1L);
|
||||
when(rest.getURLforApiService(any())).thenAnswer(i -> i.getArgument(0));
|
||||
http = mockStatic(HttpReqRespUtils.class);
|
||||
http.when(HttpReqRespUtils::getClientIpAddressIfServletRequestExist).thenReturn("1.2.3.4");
|
||||
AdminEventLeadService leads = new AdminEventLeadService(rest, mock(AdminAuthenticationService.class), new AdminCompanyScope());
|
||||
service = new PublicRegistrationService(rest, leads, redis, Clock.fixed(Instant.parse("2027-01-16T04:00:00Z"), ZoneOffset.UTC));
|
||||
|
||||
Map<String, Object> link = new HashMap<>(Map.of("token", TOKEN, "enabled", true, "eventUuid", EV, "companyId", COMPANY, "salespersonUserId", 21L));
|
||||
stub("/counter/registrationLink/findByToken/{token}", TOKEN, link);
|
||||
CompanyEvent e = new CompanyEvent();
|
||||
e.setCompanyId(COMPANY);
|
||||
e.setUuid(EV);
|
||||
e.setName("KL Food Fair 2027");
|
||||
e.setDateEnd(LocalDateTime.of(2027, 1, 17, 23, 59, 59));
|
||||
e.setEnabled(GeneralEnum.FlagYesNo.Y);
|
||||
stub("/counter/event/findByUuid/{uuid}", EV, e);
|
||||
stub("/company/findById/{id}", COMPANY, new HashMap<>(Map.of("id", COMPANY, "name", "INS", "status", "A")));
|
||||
stub("/company/{id}/privacyNotice", COMPANY, Map.of("id", 9L, "textEn", "en", "textMs", "ms"));
|
||||
stub("/counter/eventSalesperson/findByEventUuid/{eventUuid}", EV, List.of(Map.of("userId", 21L)));
|
||||
stub("/counter/interest/findByCompany/{companyId}", COMPANY, List.of(Map.of("id", 3L, "companyId", COMPANY, "name", "Cookware", "active", true, "sortOrder", 0)));
|
||||
when(rest.postForEntity(eq("/counter/registration/register"), any(ParameterizedTypeReference.class), any())).thenReturn(ResponseEntity.ok(Map.of()));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void close() { http.close(); }
|
||||
|
||||
private void stub(String url, Object var, Object body) {
|
||||
when(rest.getForEntity(eq(url), any(ParameterizedTypeReference.class), eq(var))).thenReturn(ResponseEntity.ok(body));
|
||||
}
|
||||
|
||||
private Object registeredSalesperson() {
|
||||
service.register(TOKEN, new PublicRegistrationRequest("Aisyah", "012-345 6789", "a@x.com", "Acme", "Chef", List.of(3L), true, null));
|
||||
ArgumentCaptor<Object> body = ArgumentCaptor.forClass(Object.class);
|
||||
verify(rest).postForEntity(eq("/counter/registration/register"), any(ParameterizedTypeReference.class), body.capture());
|
||||
verify(rest, never()).getForEntity(eq(FIND_BY_ID), any(ParameterizedTypeReference.class), any());
|
||||
return ((Map<String, Object>) body.getValue()).get("salespersonUserId");
|
||||
}
|
||||
|
||||
@Test
|
||||
void activeAdminOf_true_keepsTheSalesperson() {
|
||||
when(rest.getForEntity(eq(ACTIVE_ADMIN_OF), any(ParameterizedTypeReference.class), eq(21L), eq(COMPANY))).thenReturn(ResponseEntity.ok(true));
|
||||
assertThat(registeredSalesperson()).isEqualTo(21L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void activeAdminOf_false_isNull() {
|
||||
when(rest.getForEntity(eq(ACTIVE_ADMIN_OF), any(ParameterizedTypeReference.class), eq(21L), eq(COMPANY))).thenReturn(ResponseEntity.ok(false));
|
||||
assertThat(registeredSalesperson()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void activeAdminOf_404_isNull_not500() {
|
||||
when(rest.getForEntity(eq(ACTIVE_ADMIN_OF), any(ParameterizedTypeReference.class), eq(21L), eq(COMPANY)))
|
||||
.thenThrow(HttpClientErrorException.create(HttpStatus.NOT_FOUND, "nf", null, null, null));
|
||||
assertThat(registeredSalesperson()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void activeAdminOf_otherFailure_isNull_not500() {
|
||||
when(rest.getForEntity(eq(ACTIVE_ADMIN_OF), any(ParameterizedTypeReference.class), eq(21L), eq(COMPANY)))
|
||||
.thenThrow(HttpClientErrorException.create(HttpStatus.UNAUTHORIZED, "no", null, null, null));
|
||||
assertThat(registeredSalesperson()).isNull();
|
||||
}
|
||||
}
|
||||
+3
-9
@@ -46,7 +46,7 @@ class PublicRegistrationServiceTest {
|
||||
private static final String COMPANY_URL = "/company/findById/{id}";
|
||||
private static final String NOTICE = "/company/{id}/privacyNotice";
|
||||
private static final String TEAM = "/counter/eventSalesperson/findByEventUuid/{eventUuid}";
|
||||
private static final String USER = "/user/findById/{id}";
|
||||
private static final String USER = "/user/{id}/activeAdminOf/{companyId}";
|
||||
private static final String INTERESTS = "/counter/interest/findByCompany/{companyId}";
|
||||
private static final String REGISTER = "/counter/registration/register";
|
||||
private static final long EV = 3312726793093644288L;
|
||||
@@ -116,13 +116,7 @@ class PublicRegistrationServiceTest {
|
||||
}
|
||||
|
||||
private void stubUser(long id, boolean enabled) {
|
||||
Map<String, Object> u = new HashMap<>();
|
||||
u.put("id", id);
|
||||
u.put("name", "Sales " + id);
|
||||
u.put("type", "A");
|
||||
u.put("companyId", COMPANY);
|
||||
u.put("enabled", enabled);
|
||||
stub(USER, id, u);
|
||||
when(rest.getForEntity(eq(USER), any(ParameterizedTypeReference.class), eq(id), eq(COMPANY))).thenReturn(ResponseEntity.ok(enabled));
|
||||
}
|
||||
|
||||
private static PublicRegistrationRequest req(String website, Boolean consent, List<Long> interests) {
|
||||
@@ -308,7 +302,7 @@ class PublicRegistrationServiceTest {
|
||||
|
||||
@Test
|
||||
void register_deletedSalesperson_isNull_not500() {
|
||||
when(rest.getForEntity(eq(USER), any(ParameterizedTypeReference.class), eq(21L)))
|
||||
when(rest.getForEntity(eq(USER), any(ParameterizedTypeReference.class), eq(21L), eq(COMPANY)))
|
||||
.thenThrow(HttpClientErrorException.create(HttpStatus.NOT_FOUND, "nf", null, null, null));
|
||||
service.register(TOKEN, ok());
|
||||
assertThat(registered().get("salespersonUserId")).isNull();
|
||||
|
||||
Reference in new issue
Block a user