feat: companies API, company on user lookups, company filters for users and login security
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
840b5a3bae
commit
c9c0745955
23 files changed
+1181
-13
No files matched your search
+103
@@ -0,0 +1,103 @@
|
||||
package com.solvway.services.system.controller;
|
||||
|
||||
import com.solvway.services.system.model.Company;
|
||||
import com.solvway.services.system.service.CompanyService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
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 org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
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 CompanyControllerTest {
|
||||
|
||||
@Mock private CompanyService service;
|
||||
@InjectMocks private CompanyController controller;
|
||||
private MockMvc mvc;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
mvc = MockMvcBuilders.standaloneSetup(controller).build();
|
||||
}
|
||||
|
||||
private static Company company(Long id, String name, String code) {
|
||||
Company c = new Company();
|
||||
c.setId(id);
|
||||
c.setName(name);
|
||||
c.setCode(code);
|
||||
c.setStatus("A");
|
||||
return c;
|
||||
}
|
||||
|
||||
@Test
|
||||
void findAllReturnsCompanies() throws Exception {
|
||||
when(service.findAll()).thenReturn(List.of(company(2L, "CV", "cv"), company(1L, "INS", "ins")));
|
||||
|
||||
mvc.perform(get("/company/findAll")).andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$[0].code").value("cv")).andExpect(jsonPath("$[1].status").value("A"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void findByIdAndCode() throws Exception {
|
||||
when(service.findById(1L)).thenReturn(company(1L, "INS", "ins"));
|
||||
when(service.findByCode("INS")).thenReturn(company(1L, "INS", "ins"));
|
||||
|
||||
mvc.perform(get("/company/findById/1")).andExpect(status().isOk()).andExpect(jsonPath("$.name").value("INS"));
|
||||
mvc.perform(get("/company/findByCode/INS")).andExpect(status().isOk()).andExpect(jsonPath("$.id").value(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveReturnsSavedCompany() throws Exception {
|
||||
when(service.save(any(Company.class))).thenReturn(company(3L, "New", "new"));
|
||||
|
||||
mvc.perform(post("/company/save").contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"name\":\"New\",\"code\":\"NEW\",\"status\":\"A\"}"))
|
||||
.andExpect(status().isOk()).andExpect(jsonPath("$.id").value(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidCodeIs400WithMessage() throws Exception {
|
||||
when(service.save(any(Company.class))).thenThrow(new IllegalArgumentException("Code must be 2 to 20 characters"));
|
||||
|
||||
mvc.perform(post("/company/save").contentType(MediaType.APPLICATION_JSON).content("{\"name\":\"X\",\"code\":\"a\"}"))
|
||||
.andExpect(status().isBadRequest()).andExpect(jsonPath("$.message").value("Code must be 2 to 20 characters"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void duplicateCodeIs409() throws Exception {
|
||||
when(service.save(any(Company.class))).thenThrow(new ResponseStatusException(HttpStatus.CONFLICT, "Code is already taken"));
|
||||
|
||||
mvc.perform(post("/company/save").contentType(MediaType.APPLICATION_JSON).content("{\"name\":\"X\",\"code\":\"ins\"}"))
|
||||
.andExpect(status().isConflict());
|
||||
}
|
||||
|
||||
@Test
|
||||
void renameCodePostsToService() throws Exception {
|
||||
when(service.renameCode(2L, "cvx")).thenReturn(company(2L, "CV", "cvx"));
|
||||
|
||||
mvc.perform(post("/company/2/code").contentType(MediaType.APPLICATION_JSON).content("{\"code\":\"cvx\"}"))
|
||||
.andExpect(status().isOk()).andExpect(jsonPath("$.code").value("cvx"));
|
||||
verify(service).renameCode(2L, "cvx");
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingBodyIs400() throws Exception {
|
||||
mvc.perform(post("/company/save").contentType(MediaType.APPLICATION_JSON).content("")).andExpect(status().isBadRequest());
|
||||
}
|
||||
}
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
package com.solvway.services.system.controller;
|
||||
|
||||
import com.solvway.services.system.service.LoginSecurityService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
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 org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
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.status;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class LoginSecurityControllerCompanyTest {
|
||||
|
||||
@Mock private LoginSecurityService service;
|
||||
@InjectMocks private LoginSecurityController controller;
|
||||
private MockMvc mvc;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
mvc = MockMvcBuilders.standaloneSetup(controller).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
void auditPassesCompanyId() throws Exception {
|
||||
when(service.audit(any(), any(), any(), any(), any(), any(), any())).thenReturn(Map.of());
|
||||
|
||||
mvc.perform(post("/loginSecurity/audit").contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"username\":\"bo\",\"from\":\"2025-06-01\",\"page\":1,\"size\":50,\"companyId\":2}"))
|
||||
.andExpect(status().isOk());
|
||||
|
||||
verify(service).audit("bo", null, LocalDate.of(2025, 6, 1), null, 1, 50, 2L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void lockedPassesCompanyIdQueryParam() throws Exception {
|
||||
when(service.lockedUsers(2L)).thenReturn(List.of());
|
||||
|
||||
mvc.perform(get("/loginSecurity/locked").param("companyId", "2")).andExpect(status().isOk());
|
||||
|
||||
verify(service).lockedUsers(2L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void unlockPassesCompanyId() throws Exception {
|
||||
mvc.perform(post("/loginSecurity/unlock").contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"username\":\"a@cv\",\"by\":\"boss\",\"companyId\":2}")).andExpect(status().isOk());
|
||||
|
||||
verify(service).unlock("a@cv", "boss", 2L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void unlockOfAnotherCompanysUserIs404() throws Exception {
|
||||
doThrow(new ResponseStatusException(HttpStatus.NOT_FOUND, "user not found")).when(service).unlock("a@ins", "boss", 2L);
|
||||
|
||||
mvc.perform(post("/loginSecurity/unlock").contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"username\":\"a@ins\",\"by\":\"boss\",\"companyId\":2}")).andExpect(status().isNotFound());
|
||||
}
|
||||
}
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
package com.solvway.services.system.controller;
|
||||
|
||||
import com.solvway.core.enums.UserEnum.UserType;
|
||||
import com.solvway.services.system.model.Role;
|
||||
import com.solvway.services.system.model.User;
|
||||
import com.solvway.services.system.service.UserService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.mockito.junit.jupiter.MockitoSettings;
|
||||
import org.mockito.quality.Strictness;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
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.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@MockitoSettings(strictness = Strictness.LENIENT)
|
||||
class UserControllerCompanyGuardTest {
|
||||
|
||||
@Mock private UserService userService;
|
||||
@InjectMocks private UserController controller;
|
||||
|
||||
private static User user(UserType type, Long companyId, Long... roleIds) {
|
||||
User u = new User();
|
||||
u.setType(type);
|
||||
u.setCompanyId(companyId);
|
||||
List<Role> roles = new ArrayList<>();
|
||||
for (Long id : roleIds) {
|
||||
Role r = new Role();
|
||||
r.setId(id);
|
||||
roles.add(r);
|
||||
}
|
||||
u.setRoles(roles);
|
||||
return u;
|
||||
}
|
||||
|
||||
private void refused(User u, String message) {
|
||||
assertThatThrownBy(() -> controller.save(u)).isInstanceOfSatisfying(ResponseStatusException.class, e -> {
|
||||
assertThat(e.getStatusCode().value()).isEqualTo(400);
|
||||
assertThat(e.getReason()).contains(message);
|
||||
});
|
||||
verify(userService, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void adminWithCompanyCannotHoldSuperAdminRole() {
|
||||
refused(user(UserType.A, 2L, 1L, 5L), "super admin");
|
||||
}
|
||||
|
||||
@Test
|
||||
void companylessAdminMustHoldSuperAdminRole() {
|
||||
refused(user(UserType.A, null, 5L), "company");
|
||||
}
|
||||
|
||||
@Test
|
||||
void companylessAdminWithRoleOneIsSaved() {
|
||||
when(userService.save(any(User.class))).thenAnswer(i -> i.getArgument(0));
|
||||
|
||||
assertThat(controller.save(user(UserType.A, null, 1L)).getCompanyId()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void adminWithCompanyAndOtherRoleIsSaved() {
|
||||
when(userService.save(any(User.class))).thenAnswer(i -> i.getArgument(0));
|
||||
|
||||
assertThat(controller.save(user(UserType.A, 2L, 5L)).getCompanyId()).isEqualTo(2L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void membersAreUnaffected() {
|
||||
when(userService.save(any(User.class))).thenAnswer(i -> i.getArgument(0));
|
||||
|
||||
assertThat(controller.save(user(UserType.M, null)).getType()).isEqualTo(UserType.M);
|
||||
assertThat(controller.save(user(UserType.M, 2L, 1L)).getCompanyId()).isEqualTo(2L);
|
||||
}
|
||||
|
||||
private void stored(Long id, UserType type, Long companyId, Long... roleIds) {
|
||||
User s = user(type, companyId, roleIds);
|
||||
s.setId(id);
|
||||
when(userService.findById(id)).thenReturn(java.util.Optional.of(s));
|
||||
}
|
||||
|
||||
private static User edit(Long id, UserType type, Long companyId, Long... roleIds) {
|
||||
User u = user(type, companyId, roleIds);
|
||||
u.setId(id);
|
||||
return u;
|
||||
}
|
||||
|
||||
@Test
|
||||
void editOmittingCompanyKeepsStoredCompanyAndRefusesSuperAdminRole() {
|
||||
stored(7L, UserType.A, 2L, 5L);
|
||||
refused(edit(7L, null, null, 1L), "super admin");
|
||||
}
|
||||
|
||||
@Test
|
||||
void editOmittingCompanyOfStoredCompanyAdminWithOtherRoleIsSaved() {
|
||||
stored(7L, UserType.A, 2L, 5L);
|
||||
when(userService.save(any(User.class))).thenAnswer(i -> i.getArgument(0));
|
||||
|
||||
assertThat(controller.save(edit(7L, null, null, 5L))).isNotNull();
|
||||
verify(userService).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void editOmittingTypeOfStoredAdminStillAppliesGuard() {
|
||||
stored(7L, UserType.A, null, 1L);
|
||||
refused(edit(7L, null, null, 5L), "company");
|
||||
}
|
||||
|
||||
@Test
|
||||
void editOmittingRolesKeepsStoredRoles() {
|
||||
stored(7L, UserType.A, null, 1L);
|
||||
when(userService.save(any(User.class))).thenAnswer(i -> i.getArgument(0));
|
||||
User u = edit(7L, UserType.A, null);
|
||||
u.setRoles(null);
|
||||
|
||||
assertThat(controller.save(u)).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void emailFindersPassTheCompany() {
|
||||
User other = new User();
|
||||
when(userService.findByEmailAndCompanyId("a@x.com", 2L)).thenReturn(other);
|
||||
when(userService.findByEmailAndCompanyIdAndIdNot("a@x.com", null, 9L)).thenReturn(other);
|
||||
|
||||
assertThat(controller.findByEmailAndCompanyId("a@x.com", 2L)).isSameAs(other);
|
||||
assertThat(controller.findByEmailAndCompanyIdAndIdNot("a@x.com", 9L, null)).isSameAs(other);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user