feat: company sign-in, company scope and super-admin Companies API
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
91a0f339a5
commit
69d3ebc58c
16 files changed
+702
-12
No files matched your search
+163
@@ -0,0 +1,163 @@
|
||||
package com.solvway.mobile.api.controller.admin;
|
||||
|
||||
import com.solvway.core.enums.GeneralEnum;
|
||||
import com.solvway.core.model.Role;
|
||||
import com.solvway.core.model.User;
|
||||
import com.solvway.core.utils.HttpReqRespUtils;
|
||||
import com.solvway.mobile.api.model.AuthRequest;
|
||||
import com.solvway.mobile.api.service.AdminAuthenticationService;
|
||||
import com.solvway.mobile.api.service.AuthenticationService;
|
||||
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.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
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 java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
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;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class AdminAuthControllerCompanyTest {
|
||||
|
||||
@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 static User user(Long companyId, String code, String status, long roleId) {
|
||||
User u = new User();
|
||||
u.setId(9L);
|
||||
u.setUsername(code == null ? "root" : "john@" + code);
|
||||
u.setName("John");
|
||||
u.setEnabled(true);
|
||||
u.setLogin(GeneralEnum.LoginStatus.Y);
|
||||
u.setCompanyId(companyId);
|
||||
u.setCompanyCode(code);
|
||||
u.setCompanyStatus(status);
|
||||
Role r = new Role("R");
|
||||
r.setId(roleId);
|
||||
u.setRoles(List.of(r));
|
||||
return u;
|
||||
}
|
||||
|
||||
private ResultActions login(String body) throws Exception {
|
||||
return mvc.perform(post("/api/admin/auth/login").contentType(MediaType.APPLICATION_JSON).content(body));
|
||||
}
|
||||
|
||||
@Test
|
||||
void login_buildsLoginNameFromCompanyAndUsername() throws Exception {
|
||||
when(auth.getToken(any())).thenReturn("tok");
|
||||
when(auth.loadUserByToken("tok")).thenReturn(user(5L, "ins", "A", 4));
|
||||
|
||||
login("{\"company\":\" INS \",\"username\":\"john\",\"password\":\"pw\"}").andExpect(status().isOk());
|
||||
|
||||
ArgumentCaptor<AuthRequest> sent = ArgumentCaptor.forClass(AuthRequest.class);
|
||||
verify(auth).getToken(sent.capture());
|
||||
assertThat(sent.getValue().getUsername()).isEqualTo("john@ins");
|
||||
verify(auth).isLocked("john@ins");
|
||||
verify(auth).record("john@ins", "LOGIN_SUCCESS", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void login_blankOrAbsentCompany_isSuperAdminPlainUsername() throws Exception {
|
||||
when(auth.getToken(any())).thenReturn("tok");
|
||||
when(auth.loadUserByToken("tok")).thenReturn(user(null, null, null, 1));
|
||||
|
||||
login("{\"company\":\"\",\"username\":\"root\",\"password\":\"pw\"}").andExpect(status().isOk());
|
||||
login("{\"username\":\"root\",\"password\":\"pw\"}").andExpect(status().isOk());
|
||||
|
||||
ArgumentCaptor<AuthRequest> sent = ArgumentCaptor.forClass(AuthRequest.class);
|
||||
verify(auth, org.mockito.Mockito.times(2)).getToken(sent.capture());
|
||||
assertThat(sent.getAllValues()).allMatch(r -> r.getUsername().equals("root"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void login_atSignInUsername_isBadLoginWithoutDownstreamCall() throws Exception {
|
||||
login("{\"company\":\"ins\",\"username\":\"john@cv\",\"password\":\"pw\"}")
|
||||
.andExpect(status().isUnauthorized()).andExpect(jsonPath("$.message").value("Invalid username or password"));
|
||||
verify(auth, never()).getToken(any());
|
||||
verify(auth, never()).isLocked(anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void login_wrongCompanyOrUnknownUser_isBadLogin() throws Exception {
|
||||
when(auth.getToken(any())).thenReturn(null);
|
||||
login("{\"company\":\"nope\",\"username\":\"john\",\"password\":\"pw\"}").andExpect(status().isUnauthorized());
|
||||
verify(auth).record("john@nope", "LOGIN_FAIL", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void login_inactiveCompany_isBadLoginAndRecordedAndTokenRevoked() throws Exception {
|
||||
when(auth.getToken(any())).thenReturn("tok");
|
||||
when(auth.loadUserByToken("tok")).thenReturn(user(5L, "ins", "I", 4));
|
||||
|
||||
login("{\"company\":\"ins\",\"username\":\"john\",\"password\":\"pw\"}")
|
||||
.andExpect(status().isUnauthorized()).andExpect(jsonPath("$.message").value("Invalid username or password"));
|
||||
verify(auth).record("john@ins", "LOGIN_FAIL", null);
|
||||
verify(authenticationService).logoutToken("tok");
|
||||
}
|
||||
|
||||
@Test
|
||||
void login_response_carriesCompanyFieldsAndIsSuperKey() throws Exception {
|
||||
when(auth.getToken(any())).thenReturn("tok");
|
||||
when(auth.loadUserByToken("tok")).thenReturn(user(5L, "ins", "A", 4));
|
||||
login("{\"company\":\"ins\",\"username\":\"john\",\"password\":\"pw\"}")
|
||||
.andExpect(jsonPath("$.companyId").value(5)).andExpect(jsonPath("$.companyCode").value("ins"))
|
||||
.andExpect(jsonPath("$.isSuper").value(false));
|
||||
|
||||
when(auth.loadUserByToken("tok")).thenReturn(user(null, null, null, 1));
|
||||
login("{\"username\":\"root\",\"password\":\"pw\"}")
|
||||
.andExpect(jsonPath("$.companyId").doesNotExist()).andExpect(jsonPath("$.isSuper").value(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void me_carriesCompanyFieldsAndIsSuperKey() throws Exception {
|
||||
when(auth.loadUserByToken("tok")).thenReturn(user(5L, "ins", "A", 4));
|
||||
mvc.perform(get("/api/admin/me")).andExpect(jsonPath("$.companyId").value(5))
|
||||
.andExpect(jsonPath("$.companyCode").value("ins")).andExpect(jsonPath("$.isSuper").value(false))
|
||||
.andExpect(jsonPath("$.username").value("john@ins"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void forgotPassword_buildsLoginName_andIgnoresAtSign() throws Exception {
|
||||
mvc.perform(post("/api/admin/auth/forgot-password").contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"company\":\"INS\",\"username\":\"john\",\"email\":\"j@x.com\"}"))
|
||||
.andExpect(status().isOk()).andExpect(jsonPath("$.result").value("true"));
|
||||
verify(auth).resetAdminPassword("john@ins", "j@x.com");
|
||||
|
||||
mvc.perform(post("/api/admin/auth/forgot-password").contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"company\":\"ins\",\"username\":\"a@b\",\"email\":\"j@x.com\"}")).andExpect(status().isOk());
|
||||
verify(auth, never()).resetAdminPassword(org.mockito.ArgumentMatchers.eq("a@b@ins"), anyString());
|
||||
}
|
||||
}
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
package com.solvway.mobile.api.controller.admin;
|
||||
|
||||
import com.solvway.core.annotation.AppAuth;
|
||||
import com.solvway.core.model.Role;
|
||||
import com.solvway.core.model.User;
|
||||
import com.solvway.core.utils.HttpReqRespUtils;
|
||||
import com.solvway.mobile.api.errorhandler.CodedResponseStatusException;
|
||||
import com.solvway.mobile.api.service.AdminAuthenticationService;
|
||||
import com.solvway.mobile.api.service.AdminCompanyService;
|
||||
import com.solvway.mobile.api.useraccess.AdminCompanyScope;
|
||||
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.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.setup.MockMvcBuilders;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
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.Mockito.mockStatic;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class AdminCompanyControllerTest {
|
||||
|
||||
@Mock private AdminCompanyService companies;
|
||||
@Mock private AdminAuthenticationService auth;
|
||||
private MockMvc mvc;
|
||||
private MockedStatic<HttpReqRespUtils> http;
|
||||
private static final String BODY = "{\"name\":\"Ins\",\"code\":\"ins\",\"status\":\"A\"}";
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
mvc = MockMvcBuilders.standaloneSetup(new AdminCompanyController(companies, auth, new AdminCompanyScope())).build();
|
||||
http = mockStatic(HttpReqRespUtils.class);
|
||||
http.when(HttpReqRespUtils::getJwtToken).thenReturn("tok");
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void close() { http.close(); }
|
||||
|
||||
private void caller(Long companyId, long roleId) {
|
||||
User u = new User();
|
||||
u.setCompanyId(companyId);
|
||||
Role r = new Role("R");
|
||||
r.setId(roleId);
|
||||
u.setRoles(List.of(r));
|
||||
when(auth.loadUserByToken("tok")).thenReturn(u);
|
||||
}
|
||||
|
||||
private static CodedResponseStatusException coded(HttpStatus s, String reason) {
|
||||
return new CodedResponseStatusException(s, reason, "X");
|
||||
}
|
||||
|
||||
@Test
|
||||
void everyHandlerIsAuthenticated() {
|
||||
for (Method m : AdminCompanyController.class.getDeclaredMethods()) {
|
||||
if (m.isSynthetic() || !java.lang.reflect.Modifier.isPublic(m.getModifiers())) continue;
|
||||
if (m.getName().equals("upstream")) continue;
|
||||
assertThat(m.isAnnotationPresent(AppAuth.class)).as(m.getName()).isTrue();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void companyStaff_get403_onEveryHandler_andNothingIsCalled() throws Exception {
|
||||
caller(5L, 1);
|
||||
mvc.perform(get("/api/admin/companies")).andExpect(status().isForbidden());
|
||||
mvc.perform(get("/api/admin/companies/1")).andExpect(status().isForbidden());
|
||||
mvc.perform(post("/api/admin/companies").contentType(MediaType.APPLICATION_JSON).content(BODY)).andExpect(status().isForbidden());
|
||||
mvc.perform(post("/api/admin/companies/1/code").contentType(MediaType.APPLICATION_JSON).content("{\"code\":\"x1\"}"))
|
||||
.andExpect(status().isForbidden());
|
||||
verifyNoInteractions(companies);
|
||||
}
|
||||
|
||||
@Test
|
||||
void superAdmin_listsGetsAndSaves() throws Exception {
|
||||
caller(null, 1);
|
||||
when(companies.list()).thenReturn(List.of(Map.of("id", 1, "name", "Ins", "code", "ins", "status", "A")));
|
||||
when(companies.get(1L)).thenReturn(Map.of("id", 1, "code", "ins"));
|
||||
when(companies.save(any())).thenReturn(Map.of("id", 2, "code", "cv"));
|
||||
when(companies.renameCode(1L, "new1")).thenReturn(Map.of("id", 1, "code", "new1"));
|
||||
mvc.perform(get("/api/admin/companies")).andExpect(status().isOk()).andExpect(jsonPath("$[0].code").value("ins"));
|
||||
mvc.perform(get("/api/admin/companies/1")).andExpect(jsonPath("$.code").value("ins"));
|
||||
mvc.perform(post("/api/admin/companies").contentType(MediaType.APPLICATION_JSON).content(BODY))
|
||||
.andExpect(status().isOk()).andExpect(jsonPath("$.code").value("cv"));
|
||||
mvc.perform(post("/api/admin/companies/1/code").contentType(MediaType.APPLICATION_JSON).content("{\"code\":\"new1\"}"))
|
||||
.andExpect(jsonPath("$.code").value("new1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void upstream400_409_404_areMappedWithMessage() throws Exception {
|
||||
caller(null, 1);
|
||||
when(companies.save(any())).thenThrow(coded(HttpStatus.BAD_REQUEST, "Name is required"));
|
||||
mvc.perform(post("/api/admin/companies").contentType(MediaType.APPLICATION_JSON).content(BODY))
|
||||
.andExpect(status().isBadRequest()).andExpect(jsonPath("$.message").value("Name is required"));
|
||||
|
||||
when(companies.renameCode(1L, "taken")).thenThrow(coded(HttpStatus.CONFLICT, "Code is already taken"));
|
||||
mvc.perform(post("/api/admin/companies/1/code").contentType(MediaType.APPLICATION_JSON).content("{\"code\":\"taken\"}"))
|
||||
.andExpect(status().isConflict()).andExpect(jsonPath("$.message").value("Code is already taken"));
|
||||
|
||||
when(companies.get(9L)).thenThrow(coded(HttpStatus.NOT_FOUND, "Company not found"));
|
||||
mvc.perform(get("/api/admin/companies/9")).andExpect(status().isNotFound())
|
||||
.andExpect(jsonPath("$.message").value("Company not found"));
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package com.solvway.mobile.api.service;
|
||||
|
||||
import com.solvway.mobile.api.errorhandler.CodedResponseStatusException;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
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.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class AdminCompanyServiceTest {
|
||||
|
||||
private final RestTemplateCustomUtil rest = mock(RestTemplateCustomUtil.class);
|
||||
private final AdminCompanyService service = new AdminCompanyService(rest);
|
||||
|
||||
private static HttpClientErrorException upstream(HttpStatus s, String body) {
|
||||
return HttpClientErrorException.create(s, s.name(), null, body.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private void assertCoded(Runnable call, HttpStatus status, String message) {
|
||||
assertThatThrownBy(call::run).isInstanceOf(CodedResponseStatusException.class).satisfies(ex -> {
|
||||
CodedResponseStatusException c = (CodedResponseStatusException) ex;
|
||||
assertThat(c.getStatusCode()).isEqualTo(status);
|
||||
assertThat(c.getReason()).isEqualTo(message);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void upstream400_409_404_becomeCodedExceptions() {
|
||||
when(rest.getURLforApiService(any())).thenAnswer(i -> i.getArgument(0));
|
||||
when(rest.postForEntity(any(String.class), any(ParameterizedTypeReference.class), any()))
|
||||
.thenThrow(upstream(HttpStatus.BAD_REQUEST, "{\"message\":\"Name is required\"}"));
|
||||
assertCoded(() -> service.save(Map.of("name", "")), HttpStatus.BAD_REQUEST, "Name is required");
|
||||
|
||||
when(rest.postForEntity(any(String.class), any(ParameterizedTypeReference.class), any()))
|
||||
.thenThrow(upstream(HttpStatus.CONFLICT, ""));
|
||||
assertCoded(() -> service.renameCode(1L, "taken"), HttpStatus.CONFLICT, "Code is already taken");
|
||||
|
||||
when(rest.getForEntity(any(String.class), any(ParameterizedTypeReference.class))).thenThrow(upstream(HttpStatus.NOT_FOUND, ""));
|
||||
assertCoded(() -> service.get(9L), HttpStatus.NOT_FOUND, "Company not found");
|
||||
}
|
||||
|
||||
@Test
|
||||
void blankOrMissingCode_is400_beforeAnyDownstreamCall() {
|
||||
assertCoded(() -> service.renameCode(1L, null), HttpStatus.BAD_REQUEST, "Code is required");
|
||||
assertCoded(() -> service.renameCode(1L, " "), HttpStatus.BAD_REQUEST, "Code is required");
|
||||
verifyNoMoreInteractions(rest);
|
||||
}
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
package com.solvway.mobile.api.useraccess;
|
||||
|
||||
import com.solvway.core.model.Role;
|
||||
import com.solvway.core.model.User;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.http.HttpStatus;
|
||||
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;
|
||||
|
||||
class AdminCompanyScopeTest {
|
||||
|
||||
private final AdminCompanyScope scope = new AdminCompanyScope();
|
||||
|
||||
private static User user(Long companyId, long... roleIds) {
|
||||
User u = new User();
|
||||
u.setCompanyId(companyId);
|
||||
u.setRoles(java.util.Arrays.stream(roleIds).mapToObj(id -> {
|
||||
Role r = new Role("R" + id);
|
||||
r.setId(id);
|
||||
return r;
|
||||
}).toList());
|
||||
return u;
|
||||
}
|
||||
|
||||
private static void assertStatus(Runnable call, HttpStatus expected) {
|
||||
assertThatThrownBy(call::run).isInstanceOf(ResponseStatusException.class)
|
||||
.satisfies(ex -> assertThat(((ResponseStatusException) ex).getStatusCode()).isEqualTo(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isSuper_needsNoCompanyAndRoleOne() {
|
||||
assertThat(AdminCompanyScope.isSuper(user(null, 1))).isTrue();
|
||||
assertThat(AdminCompanyScope.isSuper(user(5L, 1))).isFalse();
|
||||
assertThat(AdminCompanyScope.isSuper(user(null, 2, 4))).isFalse();
|
||||
assertThat(AdminCompanyScope.isSuper(user(null))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void scope_staffIgnoresRequest_superGetsRequestedOrNull() {
|
||||
assertThat(scope.scope(user(5L, 4), 9L)).isEqualTo(5L);
|
||||
assertThat(scope.scope(user(5L, 4), null)).isEqualTo(5L);
|
||||
assertThat(scope.scope(user(null, 1), 9L)).isEqualTo(9L);
|
||||
assertThat(scope.scope(user(null, 1), null)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void requireCompanyForCreate_staffOwn_superRequestedOr400() {
|
||||
assertThat(scope.requireCompanyForCreate(user(5L, 4), 9L)).isEqualTo(5L);
|
||||
assertThat(scope.requireCompanyForCreate(user(null, 1), 9L)).isEqualTo(9L);
|
||||
assertThatThrownBy(() -> scope.requireCompanyForCreate(user(null, 1), null))
|
||||
.isInstanceOf(ResponseStatusException.class)
|
||||
.satisfies(ex -> {
|
||||
ResponseStatusException e = (ResponseStatusException) ex;
|
||||
assertThat(e.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
|
||||
assertThat(e.getBody().getProperties()).containsEntry("message", "Choose a company.");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void requireSuper_forbidsStaff() {
|
||||
scope.requireSuper(user(null, 1));
|
||||
assertStatus(() -> scope.requireSuper(user(5L, 1)), HttpStatus.FORBIDDEN);
|
||||
assertStatus(() -> scope.requireSuper(user(null, 4)), HttpStatus.FORBIDDEN);
|
||||
}
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package com.solvway.mobile.api.useraccess;
|
||||
|
||||
import com.solvway.core.enums.GeneralEnum.LoginStatus;
|
||||
import com.solvway.core.model.Role;
|
||||
import com.solvway.core.model.User;
|
||||
import com.solvway.mobile.api.service.AdminAuthenticationService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class AdminSessionGuardCompanyTest {
|
||||
|
||||
private final AdminAuthenticationService auth = mock(AdminAuthenticationService.class);
|
||||
private final AdminSessionGuard guard = new AdminSessionGuard(auth);
|
||||
|
||||
private static User user(Long companyId, String status, Long roleId) {
|
||||
User u = new User();
|
||||
u.setEnabled(true);
|
||||
u.setLogin(LoginStatus.Y);
|
||||
u.setCompanyId(companyId);
|
||||
u.setCompanyStatus(status);
|
||||
Role r = new Role("R");
|
||||
r.setId(roleId);
|
||||
u.setRoles(java.util.List.of(r));
|
||||
return u;
|
||||
}
|
||||
|
||||
@Test
|
||||
void inactiveCompany_endsStaffSession() {
|
||||
when(auth.loadUserByToken("tok")).thenReturn(user(5L, "I", 4L));
|
||||
assertThatThrownBy(() -> guard.check("tok", false)).isInstanceOf(ResponseStatusException.class)
|
||||
.satisfies(ex -> {
|
||||
assertThat(((ResponseStatusException) ex).getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
|
||||
assertThat(((ResponseStatusException) ex).getReason()).isEqualTo("Invalid Token"); // same as a dead session
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void activeCompany_andSuperAdmin_pass() {
|
||||
when(auth.loadUserByToken("tok")).thenReturn(user(5L, "A", 4L));
|
||||
assertThat(guard.check("tok", false)).isNotNull();
|
||||
when(auth.loadUserByToken("sup")).thenReturn(user(null, null, 1L));
|
||||
assertThat(guard.check("sup", false)).isNotNull();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user