Skip to content

Commit

Permalink
AYS-500 | Fixed the time and user were not being updated in the role …
Browse files Browse the repository at this point in the history
…update (#386)
  • Loading branch information
MenekseYuncu authored Oct 14, 2024
1 parent a1d182b commit 0445c7e
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,15 @@ public void update(final String id,
.filter(roleFromDatabase -> identity.getInstitutionId().equals(roleFromDatabase.getInstitution().getId()))
.orElseThrow(() -> new AysRoleNotExistByIdException(id));

this.checkExistingRoleNameByWithoutId(id, updateRequest.getName());
if (!role.getName().equals(updateRequest.getName())) {
this.checkExistingRoleNameByWithoutId(id, updateRequest.getName());
}

final List<AysPermission> permissions = this.checkExistingPermissionsAndGet(updateRequest.getPermissionIds());

role.setName(updateRequest.getName());
role.setPermissions(permissions);
role.setUpdatedUser(identity.getUserId());

roleSavePort.save(role);
}
Expand Down
56 changes: 56 additions & 0 deletions src/test/java/org/ays/auth/controller/AysRoleEndToEndTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,62 @@ void givenValidRoleUpdateRequest_whenRoleUpdated_thenReturnSuccess() throws Exce
));
}

@Test
void givenValidRoleUpdateRequest_whenPermissionsUpdatedAndNameUnchanged_thenReturnSuccess() throws Exception {

// Initialize
List<AysPermission> permissions = permissionReadPort.findAll();
AysRole role = roleSavePort.save(
new AysRoleBuilder()
.withValidValues()
.withoutId()
.withName("Admin Role")
.withPermissions(permissions)
.withInstitution(new InstitutionBuilder().withId(AysValidTestData.Admin.INSTITUTION_ID).build())
.build()
);

// Given
String id = role.getId();
AysPermission newPermission = permissions.get(0);
List<AysPermission> newPermissions = List.of(newPermission);

Set<String> newPermissionIds = newPermissions.stream()
.map(AysPermission::getId)
.collect(Collectors.toSet());

AysRoleUpdateRequest updateRequest = new AysRoleUpdateRequestBuilder()
.withName("Admin Role")
.withPermissionIds(newPermissionIds)
.build();

// Then
String endpoint = BASE_PATH.concat("/role/").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<AysRole> roleFromDatabase = roleReadPort.findById(id);

Assertions.assertTrue(roleFromDatabase.isPresent());
Assertions.assertNotNull(roleFromDatabase.get().getId());
Assertions.assertNotNull(roleFromDatabase.get().getInstitution());
Assertions.assertEquals(updateRequest.getName(), roleFromDatabase.get().getName());
Assertions.assertEquals(AysRoleStatus.ACTIVE, roleFromDatabase.get().getStatus());
newPermissionIds.forEach(permissionId -> Assertions.assertTrue(
roleFromDatabase.get().getPermissions().stream()
.anyMatch(permission -> permission.getId().equals(permissionId))
));
}

@Test
void givenValidIdAndRoleUpdateRequest_whenRequestHasSuperPermissionsAndUserIsNotSuperAdmin_thenReturnForbiddenError() throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.ays.auth.model.AysPermissionBuilder;
import org.ays.auth.model.AysRole;
import org.ays.auth.model.AysRoleBuilder;
import org.ays.auth.model.enums.AysPermissionCategory;
import org.ays.auth.model.enums.AysRoleStatus;
import org.ays.auth.model.request.AysRoleUpdateRequest;
import org.ays.auth.model.request.AysRoleUpdateRequestBuilder;
Expand Down Expand Up @@ -34,6 +35,7 @@
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.Set;

class AysRoleUpdateServiceImplTest extends AysUnitTest {

Expand Down Expand Up @@ -112,7 +114,7 @@ void givenIdAndRoleUpdateRequest_whenValuesValid_thenUpdateRoleWithSuperPermissi
Mockito.verify(identity, Mockito.times(1))
.isSuperAdmin();

Mockito.verify(identity, Mockito.never())
Mockito.verify(identity, Mockito.times(1))
.getUserId();

Mockito.verify(roleSavePort, Mockito.times(1))
Expand Down Expand Up @@ -156,6 +158,9 @@ void givenIdAndRoleUpdateRequest_whenValuesValid_thenUpdateRole() {
Mockito.when(identity.isSuperAdmin())
.thenReturn(false);

Mockito.when(identity.getUserId())
.thenReturn("mockUpdatedUser");

Mockito.when(roleSavePort.save(Mockito.any(AysRole.class)))
.thenReturn(Mockito.mock(AysRole.class));

Expand All @@ -178,7 +183,87 @@ void givenIdAndRoleUpdateRequest_whenValuesValid_thenUpdateRole() {
Mockito.verify(identity, Mockito.times(1))
.isSuperAdmin();

Mockito.verify(identity, Mockito.never())
Mockito.verify(identity, Mockito.times(1))
.getUserId();

Mockito.verify(roleSavePort, Mockito.times(1))
.save(Mockito.any(AysRole.class));
}

@Test
void givenIdAndRoleUpdateRequest_whenPermissionsUpdatedAndNameUnchanged_thenUpdateRole() {
// Given
String mockId = AysRandomUtil.generateUUID();
AysRoleUpdateRequest mockUpdateRequest = new AysRoleUpdateRequestBuilder()
.withName("ExistingName")
.withPermissionIds(Set.of(UUID.randomUUID().toString()))
.build();

AysRole mockRole = new AysRoleBuilder()
.withName("ExistingName")
.withPermissions(List.of(
new AysPermissionBuilder().withValidValues()
.withName("institution:page")
.withCategory(AysPermissionCategory.PAGE)
.build()
))
.withId(mockId)
.build();

// When
Mockito.when(roleReadPort.findById(Mockito.anyString()))
.thenReturn(Optional.of(mockRole));

Mockito.when(identity.getInstitutionId())
.thenReturn(mockRole.getInstitution().getId());

Mockito.when(roleReadPort.findByName(Mockito.anyString())).
thenReturn(Optional.of(mockRole));


List<AysPermission> mockPermissions = new ArrayList<>();
mockUpdateRequest.getPermissionIds().forEach(permissionId -> {
AysPermission mockPermission = new AysPermissionBuilder()
.withValidValues()
.withId(permissionId)
.withIsSuper(false)
.build();
mockPermissions.add(mockPermission);
});
Mockito.when(permissionReadPort.findAllByIds(Mockito.anySet()))
.thenReturn(mockPermissions);

Mockito.when(identity.isSuperAdmin())
.thenReturn(false);

Mockito.when(identity.getUserId())
.thenReturn("mockUserId");

Mockito.when(roleSavePort.save(Mockito.any(AysRole.class)))
.thenReturn(Mockito.mock(AysRole.class));

// Then
roleUpdateService.update(mockId, mockUpdateRequest);

// Verify
Mockito.verify(roleReadPort, Mockito.times(1))
.findById(Mockito.anyString());

Mockito.verify(identity, Mockito.times(1))
.getInstitutionId();

Mockito.verify(roleReadPort, Mockito.never())
.findByName(Mockito.anyString());


Mockito.verify(permissionReadPort, Mockito.times(1))
.findAllByIds(Mockito.anySet());

Mockito.verify(identity, Mockito.times(1))
.isSuperAdmin();


Mockito.verify(identity, Mockito.times(1))
.getUserId();

Mockito.verify(roleSavePort, Mockito.times(1))
Expand Down

0 comments on commit 0445c7e

Please sign in to comment.