Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AYS-500 | Fixed the time and user were not being updated in the role update #386

Merged
merged 5 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
54 changes: 54 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,60 @@ void givenValidRoleUpdateRequest_whenRoleUpdated_thenReturnSuccess() throws Exce
));
}

@Test
void givenValidRoleUpdateRequest_whenPermissionsUpdatedAndNameUnchanged_thenReturnSuccess() throws Exception {

// Initialize
List<AysPermission> permissions = permissionReadPort.findAllByIsSuperFalse();
Set<String> permissionIds = permissions.stream()
.map(AysPermission::getId)
.collect(Collectors.toSet());


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();
AysRoleUpdateRequest updateRequest = new AysRoleUpdateRequestBuilder()
.withName("Admin Role")
.withPermissionIds(permissionIds)
.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());
updateRequest.getPermissionIds().forEach(permissionId -> Assertions.assertTrue(
roleFromDatabase.get().getPermissions().stream()
.anyMatch(permission -> permission.getId().equals(permissionId))
));
}
agitrubard marked this conversation as resolved.
Show resolved Hide resolved

@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
Loading