Skip to content

Commit

Permalink
AYS-501 | Fixed the time and user were not being updated in the User …
Browse files Browse the repository at this point in the history
…update (#388)
  • Loading branch information
MenekseYuncu authored Oct 16, 2024
1 parent 5b2bc1a commit a8f09a2
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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<String> roleIds) {
private void validateRolesAndSet(final AysUser user, final Set<String> roleIds, final String institutionId) {

boolean isRoleNotChanged = user.getRoles().stream()
.allMatch(role -> roleIds.contains(role.getId()));
Expand All @@ -217,7 +228,7 @@ private void validateRolesAndSet(final AysUser user, final Set<String> roleIds)

final List<AysRole> 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()) {
Expand Down
74 changes: 74 additions & 0 deletions src/test/java/org/ays/auth/controller/AysUserEndToEndTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<AysRole> roles = roleReadPort.findAllActivesByInstitutionId(institution.getId());

AysUser user = userSavePort.save(
new AysUserBuilder()
.withValidValues()
.withFirstName("Test")
.withLastName("Dene")
.withEmailAddress("[email protected]")
.withCity("İzmir")
.withPhoneNumber(new AysPhoneNumberBuilder().withValidValues().build())
.withoutId()
.withRoles(roles)
.withInstitution(institution)
.build()
);

// Given
String id = user.getId();
AysRole newRole = roles.get(0);
List<AysRole> newRoles = List.of(newRole);

Set<String> newRoleIds = newRoles.stream()
.map(AysRole::getId)
.limit(1)
.collect(Collectors.toSet());

AysUserUpdateRequest updateRequest = new AysUserUpdateRequestBuilder()
.withValidValues()
.withFirstName("Test")
.withLastName("Dene")
.withEmailAddress("[email protected]")
.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<Void> mockResponse = AysResponseBuilder.SUCCESS;

aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse)
.andExpect(AysMockResultMatchersBuilders.status()
.isOk())
.andExpect(AysMockResultMatchersBuilders.response()
.doesNotExist());

// Verify
Optional<AysUser> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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<String> 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<AysRole> 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() {

Expand Down Expand Up @@ -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";
Expand All @@ -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";
Expand Down Expand Up @@ -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";
Expand All @@ -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";
Expand Down

0 comments on commit a8f09a2

Please sign in to comment.