diff --git a/src/main/java/org/ays/auth/service/impl/AysUserUpdateServiceImpl.java b/src/main/java/org/ays/auth/service/impl/AysUserUpdateServiceImpl.java index 1b6d26529..1761c16d3 100644 --- a/src/main/java/org/ays/auth/service/impl/AysUserUpdateServiceImpl.java +++ b/src/main/java/org/ays/auth/service/impl/AysUserUpdateServiceImpl.java @@ -54,9 +54,13 @@ public void update(final String id, final AysUserUpdateRequest updateRequest) { final AysUser user = userReadPort.findById(id) - .filter(userFromDatabase -> identity.getInstitutionId().equals(userFromDatabase.getInstitution().getId())) .orElseThrow(() -> new AysUserNotExistByIdException(id)); + String institutionId = identity.getInstitutionId(); + if (!institutionId.equals(user.getInstitution().getId())) { + throw new AysUserNotExistByIdException(id); + } + if (!(user.isActive() || user.isPassive())) { throw new AysUserIsNotActiveOrPassiveException(id); } @@ -66,15 +70,22 @@ public void update(final String id, .lineNumber(updateRequest.getPhoneNumber().getLineNumber()) .build(); - this.validatePhoneNumber(user, phoneNumber); - this.validateEmailAddress(user, updateRequest.getEmailAddress()); - this.validateRolesAndSet(user, updateRequest.getRoleIds()); + if (!user.getPhoneNumber().equals(phoneNumber)) { + this.validatePhoneNumber(user, phoneNumber); + user.setPhoneNumber(phoneNumber); + } + + if (!user.getEmailAddress().equals(updateRequest.getEmailAddress())) { + this.validateEmailAddress(user, updateRequest.getEmailAddress()); + user.setEmailAddress(updateRequest.getEmailAddress()); + } + + this.validateRolesAndSet(user, updateRequest.getRoleIds(), institutionId); user.setFirstName(updateRequest.getFirstName()); user.setLastName(updateRequest.getLastName()); - user.setEmailAddress(updateRequest.getEmailAddress()); user.setCity(updateRequest.getCity()); - user.setPhoneNumber(phoneNumber); + user.setUpdatedUser(identity.getUserId()); userSavePort.save(user); } @@ -207,7 +218,7 @@ private void validateEmailAddress(AysUser user, String emailAddress) { * @param roleIds The set of role IDs to be checked and retrieved. * @throws AysRolesNotExistException if any of the provided role IDs do not exist. */ - private void validateRolesAndSet(final AysUser user, final Set roleIds) { + private void validateRolesAndSet(final AysUser user, final Set roleIds, final String institutionId) { boolean isRoleNotChanged = user.getRoles().stream() .allMatch(role -> roleIds.contains(role.getId())); @@ -217,7 +228,7 @@ private void validateRolesAndSet(final AysUser user, final Set roleIds) final List roles = roleReadPort.findAllByIds(roleIds).stream() .filter(AysRole::isActive) - .filter(role -> identity.getInstitutionId().equals(role.getInstitution().getId())) + .filter(role -> institutionId.equals(role.getInstitution().getId())) .toList(); if (roles.size() == roleIds.size()) { diff --git a/src/test/java/org/ays/auth/controller/AysUserEndToEndTest.java b/src/test/java/org/ays/auth/controller/AysUserEndToEndTest.java index 002865a71..8bee11b29 100644 --- a/src/test/java/org/ays/auth/controller/AysUserEndToEndTest.java +++ b/src/test/java/org/ays/auth/controller/AysUserEndToEndTest.java @@ -17,6 +17,8 @@ import org.ays.auth.port.AysRoleReadPort; import org.ays.auth.port.AysUserReadPort; import org.ays.auth.port.AysUserSavePort; +import org.ays.common.model.AysPhoneNumberBuilder; +import org.ays.common.model.request.AysPhoneNumberRequestBuilder; import org.ays.common.model.response.AysPageResponse; import org.ays.common.model.response.AysResponse; import org.ays.common.model.response.AysResponseBuilder; @@ -373,6 +375,78 @@ void givenValidIdAndUserUpdateRequest_whenUserUpdated_thenReturnSuccess() throws Assertions.assertNotNull(userFromDatabase.get().getUpdatedAt()); } + @Test + void givenValidIdAndUserUpdateRequest_whenRolesUpdatedOnly_thenReturnSuccess() throws Exception { + + // Initialize + Institution institution = new InstitutionBuilder() + .withId(AysValidTestData.Admin.INSTITUTION_ID) + .build(); + + List roles = roleReadPort.findAllActivesByInstitutionId(institution.getId()); + + AysUser user = userSavePort.save( + new AysUserBuilder() + .withValidValues() + .withFirstName("Test") + .withLastName("Dene") + .withEmailAddress("Test.deneme@afetyonetimsistemi.org") + .withCity("İzmir") + .withPhoneNumber(new AysPhoneNumberBuilder().withValidValues().build()) + .withoutId() + .withRoles(roles) + .withInstitution(institution) + .build() + ); + + // Given + String id = user.getId(); + AysRole newRole = roles.get(0); + List newRoles = List.of(newRole); + + Set newRoleIds = newRoles.stream() + .map(AysRole::getId) + .limit(1) + .collect(Collectors.toSet()); + + AysUserUpdateRequest updateRequest = new AysUserUpdateRequestBuilder() + .withValidValues() + .withFirstName("Test") + .withLastName("Dene") + .withEmailAddress("Test.deneme@afetyonetimsistemi.org") + .withCity("İzmir") + .withPhoneNumber(new AysPhoneNumberRequestBuilder().withValidValues().build()) + .withRoleIds(newRoleIds) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(id); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .put(endpoint, adminToken.getAccessToken(), updateRequest); + + AysResponse mockResponse = AysResponseBuilder.SUCCESS; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Optional userFromDatabase = userReadPort.findById(id); + + Assertions.assertTrue(userFromDatabase.isPresent()); + Assertions.assertNotNull(userFromDatabase.get().getId()); + Assertions.assertNotNull(userFromDatabase.get().getInstitution()); + newRoleIds.forEach(roleId -> Assertions.assertTrue( + userFromDatabase.get().getRoles().stream() + .anyMatch(role -> role.getId().equals(roleId)) + ) + ); + Assertions.assertNotNull(userFromDatabase.get().getUpdatedUser()); + Assertions.assertNotNull(userFromDatabase.get().getUpdatedAt()); + } + @Test void givenValidId_whenActivateUser_thenReturnSuccess() throws Exception { diff --git a/src/test/java/org/ays/auth/service/impl/AysUserUpdateServiceImplTest.java b/src/test/java/org/ays/auth/service/impl/AysUserUpdateServiceImplTest.java index f4a795c5f..895e2ccbf 100644 --- a/src/test/java/org/ays/auth/service/impl/AysUserUpdateServiceImplTest.java +++ b/src/test/java/org/ays/auth/service/impl/AysUserUpdateServiceImplTest.java @@ -112,7 +112,7 @@ void givenValidIdAndValidUpdateRequest_whenAllFieldsChanged_thenUpdateUser() { Mockito.verify(userReadPort, Mockito.times(1)) .findById(Mockito.anyString()); - Mockito.verify(identity, Mockito.times(mockRoles.size() + 1)) + Mockito.verify(identity, Mockito.times(1)) .getInstitutionId(); Mockito.verify(userReadPort, Mockito.times(1)) @@ -128,6 +128,78 @@ void givenValidIdAndValidUpdateRequest_whenAllFieldsChanged_thenUpdateUser() { .save(Mockito.any(AysUser.class)); } + @Test + void givenValidIdAndValidUpdateRequest_whenRolesUpdatedOnly_thenUpdateUser() { + + // Given + String mockId = "3c57d56b-4a97-4f70-86a9-b4c9235cbe13"; + + Set mockRoleIds = Set.of( + "00a07704-8d7c-4048-b001-9fb69b22bfe8", + "7913e093-ae70-4029-ad8d-efbb21d79f26" + ); + AysUserUpdateRequest mockUpdateRequest = new AysUserUpdateRequestBuilder() + .withValidValues() + .withRoleIds(mockRoleIds) + .build(); + + // When + Institution mockInstitution = new InstitutionBuilder() + .withValidValues() + .build(); + Mockito.when(identity.getInstitutionId()) + .thenReturn(mockInstitution.getId()); + + AysUser mockUser = new AysUserBuilder() + .withValidValues() + .withId(mockId) + .withFirstName(mockUpdateRequest.getFirstName()) + .withLastName(mockUpdateRequest.getLastName()) + .withEmailAddress(mockUpdateRequest.getEmailAddress()) + .withPhoneNumber(AysPhoneNumber.builder().build()) + .withCity(mockUpdateRequest.getCity()) + .withRoles(List.of( + new AysRoleBuilder() + .withValidValues() + .build() + )) + .withInstitution(mockInstitution) + .build(); + Mockito.when(userReadPort.findById(Mockito.anyString())) + .thenReturn(Optional.of(mockUser)); + + List mockRoles = new ArrayList<>(); + mockUpdateRequest.getRoleIds().forEach(roleId -> { + AysRole mockRole = new AysRoleBuilder() + .withValidValues() + .withId(roleId) + .withInstitution(mockInstitution) + .build(); + mockRoles.add(mockRole); + }); + Mockito.when(roleReadPort.findAllByIds(Mockito.anySet())) + .thenReturn(mockRoles); + + Mockito.when(userSavePort.save(Mockito.any(AysUser.class))) + .thenReturn(Mockito.mock(AysUser.class)); + + // Then + userUpdateService.update(mockId, mockUpdateRequest); + + // Verify + Mockito.verify(userReadPort, Mockito.times(1)) + .findById(Mockito.anyString()); + + Mockito.verify(identity, Mockito.times(1)) + .getInstitutionId(); + + Mockito.verify(roleReadPort, Mockito.times(1)) + .findAllByIds(Mockito.anySet()); + + Mockito.verify(userSavePort, Mockito.times(1)) + .save(Mockito.any(AysUser.class)); + } + @Test void givenValidIdAndValidUpdateRequest_whenPhoneNumberChanged_thenUpdateUser() { @@ -566,7 +638,7 @@ void givenValidId_whenUserIsPassive_thenActivateUser() { } @Test - void givenValidId_whenUserNotFound_thenThrowAysUserNotExistByIdException(){ + void givenValidId_whenUserNotFound_thenThrowAysUserNotExistByIdException() { // Given String mockId = "9f1eb072-7830-4c43-9a32-d77b62ccddd3"; @@ -593,7 +665,7 @@ void givenValidId_whenUserNotFound_thenThrowAysUserNotExistByIdException(){ } @Test - void givenValidId_whenUserIsNotPassive_thenThrowAysUserIsNotPassiveException(){ + void givenValidId_whenUserIsNotPassive_thenThrowAysUserIsNotPassiveException() { // Given String mockId = "bf7cc8d4-eab7-487d-8564-19be0f439b4a"; @@ -863,7 +935,7 @@ void givenValidId_whenUserIsActive_thenPassivateUser() { } @Test - void givenValidUserId_whenUserNotFound_thenThrowAysUserNotExistByIdException(){ + void givenValidUserId_whenUserNotFound_thenThrowAysUserNotExistByIdException() { // Given String mockId = "9f1eb072-7830-4c43-9a32-d77b62ccddd3"; @@ -890,7 +962,7 @@ void givenValidUserId_whenUserNotFound_thenThrowAysUserNotExistByIdException(){ } @Test - void givenValidId_whenUserIsNotActive_thenThrowAysUserNotActiveException(){ + void givenValidId_whenUserIsNotActive_thenThrowAysUserNotActiveException() { // Given String mockId = "bf7cc8d4-eab7-487d-8564-19be0f439b4a";