File tree Expand file tree Collapse file tree 3 files changed +78
-3
lines changed
main/java/org/ays/auth/service/impl Expand file tree Collapse file tree 3 files changed +78
-3
lines changed Original file line number Diff line number Diff line change @@ -103,13 +103,14 @@ public void activate(String id) {
103
103
/**
104
104
* Passivates an existing role.
105
105
* <p>
106
- * This method sets the status of the role identified by its ID to passivate. If the role does not exist,
107
- * an exception is thrown. Additionally, if the role's status is not {@link AysRoleStatus#ACTIVE},
108
- * an exception is thrown .
106
+ * This method sets the status of the role identified by its ID to passivate.
107
+ * It also verifies that the role belongs to the same institution as the current user's institution
108
+ * and no user is assigned to the role .
109
109
* </p>
110
110
*
111
111
* @param id The ID of the role to passivate.
112
112
* @throws AysRoleNotExistByIdException if a role with the given ID does not exist.
113
+ * @throws AysRoleAssignedToUserException if any user is assigned to the role.
113
114
* @throws AysInvalidRoleStatusException if the role's current status is not {@link AysRoleStatus#ACTIVE}.
114
115
*/
115
116
@ Override
@@ -118,6 +119,10 @@ public void passivate(String id) {
118
119
.filter (roleFromDatabase -> identity .getInstitutionId ().equals (roleFromDatabase .getInstitution ().getId ()))
119
120
.orElseThrow (() -> new AysRoleNotExistByIdException (id ));
120
121
122
+ if (roleReadPort .isRoleUsing (id )) {
123
+ throw new AysRoleAssignedToUserException (id );
124
+ }
125
+
121
126
if (!role .isActive ()) {
122
127
throw new AysInvalidRoleStatusException (AysRoleStatus .ACTIVE );
123
128
}
Original file line number Diff line number Diff line change @@ -836,6 +836,35 @@ void givenId_whenIdIsNotValidForPassivation_thenReturnValidationError(String inv
836
836
.passivate (Mockito .anyString ());
837
837
}
838
838
839
+ @ Test
840
+ void givenValidIdForPassivation_whenRoleUsing_thenReturnConflict () throws Exception {
841
+
842
+ // Given
843
+ String mockId = "13e8ff0e-8d85-4f4f-8e45-efb04d1d8bf8" ;
844
+
845
+ // When
846
+ Mockito .doThrow (new AysRoleAssignedToUserException (mockId ))
847
+ .when (roleUpdateService )
848
+ .passivate (mockId );
849
+
850
+ // Then
851
+ String endpoint = BASE_PATH .concat ("/role/" .concat (mockId ).concat ("/passivate" ));
852
+ MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders
853
+ .patch (endpoint , mockSuperAdminToken .getAccessToken ());
854
+
855
+ AysErrorResponse mockErrorResponse = AysErrorBuilder .ALREADY_EXIST ;
856
+
857
+ aysMockMvc .perform (mockHttpServletRequestBuilder , mockErrorResponse )
858
+ .andExpect (AysMockResultMatchersBuilders .status ()
859
+ .isConflict ())
860
+ .andExpect (AysMockResultMatchersBuilders .subErrors ()
861
+ .doesNotExist ());
862
+
863
+ // Verify
864
+ Mockito .verify (roleUpdateService , Mockito .times (1 ))
865
+ .passivate (mockId );
866
+ }
867
+
839
868
840
869
@ Test
841
870
void givenValidId_whenRoleDeleted_thenReturnSuccess () throws Exception {
Original file line number Diff line number Diff line change @@ -727,6 +727,47 @@ void givenValidId_whenInstitutionIdDoesNotMatch_thenThrowAysRoleNotExistByIdExce
727
727
.save (mockRole );
728
728
}
729
729
730
+ @ Test
731
+ void givenValidIdForPassivation_whenRoleUsing_thenThrowRoleAssignedToUserException () {
732
+
733
+ // Given
734
+ String mockId = "731f4ba4-c34b-41c3-b488-d9a0c69904a3" ;
735
+
736
+ // When
737
+ AysRole mockRole = new AysRoleBuilder ()
738
+ .withValidValues ()
739
+ .withId (mockId )
740
+ .build ();
741
+
742
+ Mockito .when (roleReadPort .findById (Mockito .anyString ()))
743
+ .thenReturn (Optional .of (mockRole ));
744
+
745
+ Mockito .when (identity .getInstitutionId ())
746
+ .thenReturn (mockRole .getInstitution ().getId ());
747
+
748
+ Mockito .when (roleReadPort .isRoleUsing (Mockito .anyString ()))
749
+ .thenReturn (true );
750
+
751
+ // Then
752
+ Assertions .assertThrows (
753
+ AysRoleAssignedToUserException .class ,
754
+ () -> roleUpdateService .passivate (mockId )
755
+ );
756
+
757
+ // Verify
758
+ Mockito .verify (roleReadPort , Mockito .times (1 ))
759
+ .findById (Mockito .anyString ());
760
+
761
+ Mockito .verify (roleReadPort , Mockito .times (1 ))
762
+ .isRoleUsing (Mockito .anyString ());
763
+
764
+ Mockito .verify (identity , Mockito .times (1 ))
765
+ .getInstitutionId ();
766
+
767
+ Mockito .verify (roleSavePort , Mockito .never ())
768
+ .save (Mockito .any (AysRole .class ));
769
+ }
770
+
730
771
731
772
@ Test
732
773
void givenValidId_whenRoleFound_thenDeleteRole () {
You can’t perform that action at this time.
0 commit comments