Skip to content

Commit c6dac35

Browse files
authored
AYS-343 | Institution Control for Role Activation Has Been Implemented (#359)
1 parent 067720b commit c6dac35

File tree

4 files changed

+63
-18
lines changed

4 files changed

+63
-18
lines changed

src/main/java/org/ays/auth/service/impl/AysRoleUpdateServiceImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public void update(final String id,
8888
@Override
8989
public void activate(String id) {
9090
final AysRole role = roleReadPort.findById(id)
91+
.filter(roleFromDatabase -> identity.getInstitutionId().equals(roleFromDatabase.getInstitution().getId()))
9192
.orElseThrow(() -> new AysRoleNotExistByIdException(id));
9293

9394
if (!role.isPassive()) {

src/test/java/org/ays/auth/controller/AysRoleControllerTest.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -683,12 +683,12 @@ void givenValidIdAndInvalidRoleUpdateRequest_whenPermissionIdIsNotValid_thenRetu
683683
void givenValidId_whenRoleActivated_thenReturnSuccess() throws Exception {
684684

685685
// Given
686-
String mockId = AysRandomUtil.generateUUID();
686+
String mockId = "3ce7dace-e006-459c-b3c0-693bf2a36e26";
687687

688688
// When
689689
Mockito.doNothing()
690690
.when(roleUpdateService)
691-
.activate(Mockito.any());
691+
.activate(Mockito.anyString());
692692

693693
// Then
694694
String endpoint = BASE_PATH.concat("/role/".concat(mockId).concat("/activate"));
@@ -708,6 +708,30 @@ void givenValidId_whenRoleActivated_thenReturnSuccess() throws Exception {
708708
.activate(Mockito.anyString());
709709
}
710710

711+
@Test
712+
void givenValidId_whenUserUnauthorizedForActivation_thenReturnAccessDeniedException() throws Exception {
713+
714+
// Given
715+
String mockId = "8c19bb92-5f11-4752-a93f-11c440d1725b";
716+
717+
// Then
718+
String endpoint = BASE_PATH.concat("/role/").concat(mockId).concat("/activate");
719+
MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders
720+
.patch(endpoint, mockUserToken.getAccessToken());
721+
722+
AysErrorResponse mockErrorResponse = AysErrorBuilder.FORBIDDEN;
723+
724+
aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse)
725+
.andExpect(AysMockResultMatchersBuilders.status()
726+
.isForbidden())
727+
.andExpect(AysMockResultMatchersBuilders.subErrors()
728+
.doesNotExist());
729+
730+
// Verify
731+
Mockito.verify(roleUpdateService, Mockito.never())
732+
.activate(Mockito.anyString());
733+
}
734+
711735
@ParameterizedTest
712736
@ValueSource(strings = {
713737
"A",

src/test/java/org/ays/auth/controller/AysRoleEndToEndTest.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.ays.common.model.response.AysResponseBuilder;
2424
import org.ays.institution.model.Institution;
2525
import org.ays.institution.model.InstitutionBuilder;
26-
import org.ays.institution.port.InstitutionSavePort;
2726
import org.ays.util.AysMockMvcRequestBuilders;
2827
import org.ays.util.AysMockResultMatchersBuilders;
2928
import org.ays.util.AysValidTestData;
@@ -42,9 +41,6 @@
4241

4342
class AysRoleEndToEndTest extends AysEndToEndTest {
4443

45-
@Autowired
46-
private InstitutionSavePort institutionSavePort;
47-
4844
@Autowired
4945
private AysRoleSavePort roleSavePort;
5046

@@ -484,17 +480,16 @@ void givenValidRoleUpdateRequest_whenRoleUpdated_thenReturnSuccess() throws Exce
484480
void givenId_whenRoleActivated_thenReturnSuccess() throws Exception {
485481

486482
// Initialize
487-
Institution institution = institutionSavePort.save(
488-
new InstitutionBuilder()
489-
.withValidValues()
490-
.withoutId()
491-
.build()
492-
);
493-
List<AysPermission> permissions = permissionReadPort.findAll();
483+
Institution institution = new InstitutionBuilder()
484+
.withId(AysValidTestData.Admin.INSTITUTION_ID)
485+
.build();
486+
487+
List<AysPermission> permissions = permissionReadPort.findAllByIsSuperFalse();
494488
AysRole role = roleSavePort.save(
495489
new AysRoleBuilder()
496490
.withValidValues()
497491
.withoutId()
492+
.withName("buBirRol")
498493
.withStatus(AysRoleStatus.PASSIVE)
499494
.withInstitution(institution)
500495
.withPermissions(permissions)
@@ -507,7 +502,7 @@ void givenId_whenRoleActivated_thenReturnSuccess() throws Exception {
507502
// Then
508503
String endpoint = BASE_PATH.concat("/role/".concat(id).concat("/activate"));
509504
MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders
510-
.patch(endpoint, superAdminToken.getAccessToken());
505+
.patch(endpoint, adminToken.getAccessToken());
511506

512507
AysResponse<Void> mockResponse = AysResponseBuilder.SUCCESS;
513508

src/test/java/org/ays/auth/service/impl/AysRoleUpdateServiceImplTest.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,13 +452,21 @@ void givenValidIdAndRoleUpdateRequest_whenPermissionsNotExists_thenThrowAysPermi
452452

453453
@Test
454454
void givenValidId_whenRoleIsPassive_thenActivateRole() {
455+
455456
// Given
456-
String mockId = AysRandomUtil.generateUUID();
457+
String mockId = "c1614e48-725f-4c5f-88e8-4442332d0f57";
457458

458459
// When
460+
Institution mockInstitution = new InstitutionBuilder()
461+
.withValidValues()
462+
.build();
463+
Mockito.when(identity.getInstitutionId())
464+
.thenReturn(mockInstitution.getId());
465+
459466
AysRole mockRole = new AysRoleBuilder()
460467
.withValidValues()
461468
.withId(mockId)
469+
.withInstitution(mockInstitution)
462470
.withStatus(AysRoleStatus.PASSIVE)
463471
.build();
464472

@@ -472,6 +480,9 @@ void givenValidId_whenRoleIsPassive_thenActivateRole() {
472480
roleUpdateService.activate(mockId);
473481

474482
// Verify
483+
Mockito.verify(identity, Mockito.times(1))
484+
.getInstitutionId();
485+
475486
Mockito.verify(roleReadPort, Mockito.times(1))
476487
.findById(Mockito.anyString());
477488

@@ -490,12 +501,19 @@ void givenValidId_whenRoleIsNotPassive_thenThrowAysInvalidRoleStatusException(St
490501
AysRoleStatus status = AysRoleStatus.valueOf(roleStatus);
491502

492503
// Given
493-
String mockId = AysRandomUtil.generateUUID();
504+
String mockId = "f0229414-0179-4a56-bead-1ce03e168539";
494505

495506
// When
507+
Institution mockInstitution = new InstitutionBuilder()
508+
.withValidValues()
509+
.build();
510+
Mockito.when(identity.getInstitutionId())
511+
.thenReturn(mockInstitution.getId());
512+
496513
AysRole mockRole = new AysRoleBuilder()
497514
.withValidValues()
498515
.withId(mockId)
516+
.withInstitution(mockInstitution)
499517
.withStatus(status)
500518
.build();
501519

@@ -509,6 +527,9 @@ void givenValidId_whenRoleIsNotPassive_thenThrowAysInvalidRoleStatusException(St
509527
);
510528

511529
// Verify
530+
Mockito.verify(identity, Mockito.times(1))
531+
.getInstitutionId();
532+
512533
Mockito.verify(roleReadPort, Mockito.times(1))
513534
.findById(Mockito.anyString());
514535

@@ -518,9 +539,10 @@ void givenValidId_whenRoleIsNotPassive_thenThrowAysInvalidRoleStatusException(St
518539
}
519540

520541
@Test
521-
void givenValidId_whenRoleNotFound_thenThrowAysRoleNotExistByIdException() {
542+
void givenValidId_whenRoleNotFoundForActivation_thenThrowAysRoleNotExistByIdException() {
543+
522544
// Given
523-
String mockId = AysRandomUtil.generateUUID();
545+
String mockId = "f6ecfa12-17e0-4294-a8fb-6598224fcd93";
524546

525547
// When
526548
Mockito.when(roleReadPort.findById(Mockito.anyString()))
@@ -538,6 +560,9 @@ void givenValidId_whenRoleNotFound_thenThrowAysRoleNotExistByIdException() {
538560

539561
Mockito.verify(roleSavePort, Mockito.never())
540562
.save(Mockito.any(AysRole.class));
563+
564+
Mockito.verify(identity, Mockito.never())
565+
.getInstitutionId();
541566
}
542567

543568

0 commit comments

Comments
 (0)