-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mutation to clear user roles and facilities
- Loading branch information
Showing
8 changed files
with
133 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,18 @@ | ||
package gov.cdc.usds.simplereport.api.apiuser; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import gov.cdc.usds.simplereport.api.model.Role; | ||
import gov.cdc.usds.simplereport.api.model.User; | ||
import gov.cdc.usds.simplereport.api.model.errors.IllegalGraphqlArgumentException; | ||
import gov.cdc.usds.simplereport.api.model.errors.NonexistentUserException; | ||
import gov.cdc.usds.simplereport.db.model.ApiUser; | ||
import gov.cdc.usds.simplereport.db.model.Organization; | ||
import gov.cdc.usds.simplereport.db.repository.ApiUserRepository; | ||
import gov.cdc.usds.simplereport.service.ApiUserService; | ||
import gov.cdc.usds.simplereport.service.BaseServiceTest; | ||
import gov.cdc.usds.simplereport.service.model.UserInfo; | ||
|
@@ -21,6 +27,8 @@ | |
|
||
@WithSimpleReportOrgAdminUser | ||
class UserMutationResolverTest extends BaseServiceTest<ApiUserService> { | ||
@Autowired | ||
ApiUserRepository apiUserRepository; | ||
@Mock ApiUserService mockedApiUserService; | ||
|
||
@Autowired private TestDataFactory _dataFactory; | ||
|
@@ -32,11 +40,11 @@ class UserMutationResolverTest extends BaseServiceTest<ApiUserService> { | |
@BeforeEach | ||
void setup() { | ||
Organization org = _dataFactory.saveValidOrganization(); | ||
orgUserInfo = _dataFactory.createValidApiUser("[email protected]", org); | ||
orgUserInfo = _dataFactory.createValidApiUser("[email protected]", org, Role.USER); | ||
} | ||
|
||
@Test | ||
void reactivateUserAndResetPassword_orgAdmin_success() { | ||
void reactivateUserAndResetPassword_success() { | ||
UUID userInfoInternalId = orgUserInfo.getInternalId(); | ||
|
||
// GIVEN | ||
|
@@ -50,4 +58,35 @@ void reactivateUserAndResetPassword_orgAdmin_success() { | |
assertThat(resetUser.getInternalId()).isEqualTo(userInfoInternalId); | ||
verify(mockedApiUserService, times(1)).reactivateUserAndResetPassword(userInfoInternalId); | ||
} | ||
|
||
@Test | ||
void markUserRolesAndFacilitiesAsDeleted_nonExistentUser_throwException() { | ||
String username = orgUserInfo.getEmail(); | ||
|
||
// GIVEN | ||
ApiUser foundUser = apiUserRepository.findByLoginEmail(username).get(); | ||
when(mockedApiUserService.markUserRolesAndFacilitiesAsDeleted(username)) | ||
.thenReturn(foundUser); | ||
|
||
// WHEN | ||
ApiUser resetUser = userMutationResolver.markUserRolesAndFacilitiesAsDeleted(username); | ||
|
||
// THEN | ||
assertThat(resetUser.getLoginEmail()).isEqualTo(orgUserInfo.getEmail()); | ||
verify(mockedApiUserService, times(1)).markUserRolesAndFacilitiesAsDeleted(username); | ||
} | ||
|
||
@Test | ||
void markUserRolesAndFacilitiesAsDeleted_failure() { | ||
String username = "[email protected]"; | ||
|
||
when(mockedApiUserService.markUserRolesAndFacilitiesAsDeleted(username)) | ||
.thenThrow(new NonexistentUserException()); | ||
|
||
assertThrows( | ||
NonexistentUserException.class, | ||
() -> { | ||
userMutationResolver.markUserRolesAndFacilitiesAsDeleted(username); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.doReturn; | ||
import static org.mockito.Mockito.reset; | ||
|
@@ -42,12 +43,16 @@ | |
import java.util.Set; | ||
import java.util.UUID; | ||
import java.util.stream.Collectors; | ||
|
||
import gov.cdc.usds.simplereport.test_util.TestUserIdentities; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.mock.mockito.SpyBean; | ||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.test.context.TestPropertySource; | ||
|
||
@TestPropertySource(properties = {"spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true"}) | ||
class ApiUserServiceTest extends BaseServiceTest<ApiUserService> { | ||
|
||
@Autowired @SpyBean ApiUserRepository _apiUserRepo; | ||
|
@@ -564,6 +569,50 @@ void updateUserPrivilegesAndGroupAccess_facilityToMoveNotFoundInOrg_throwsExcept | |
assertEquals(expectedError, caught.getMessage()); | ||
} | ||
|
||
@Test | ||
@WithSimpleReportOrgAdminUser | ||
void orgAdminUser_markUserRolesAndFacilitiesAsDeleted_error() { | ||
String username = "[email protected]"; | ||
assertThrows( | ||
AccessDeniedException.class, | ||
() -> | ||
_service.markUserRolesAndFacilitiesAsDeleted(username)); | ||
} | ||
|
||
@Test | ||
@WithSimpleReportSiteAdminUser | ||
void siteAdminUser_markUserRolesAndFacilitiesAsDeleted_nonExistentUser_throws() { | ||
final String email = "[email protected]"; | ||
|
||
assertThrows( | ||
NonexistentUserException.class, | ||
() -> { | ||
_service.markUserRolesAndFacilitiesAsDeleted(email); | ||
}); | ||
} | ||
|
||
@Test | ||
@WithSimpleReportSiteAdminUser | ||
void siteAdminUser_markUserRolesAndFacilitiesAsDeleted_success() { | ||
initSampleData(); | ||
final String email = TestUserIdentities.STANDARD_USER; | ||
ApiUser foundUser = _apiUserRepo.findByLoginEmail(email).get(); | ||
|
||
// check initial facilities and roles | ||
int initialFacilitiesCount = foundUser.getFacilities().size(); | ||
Set<OrganizationRole> initialOrgRoles = foundUser.getRoles(); | ||
assertEquals(1, initialFacilitiesCount); | ||
assertEquals(1, initialOrgRoles.size()); | ||
assertEquals("USER", initialOrgRoles.stream().findFirst().get().getName()); | ||
|
||
ApiUser updatedUser = _service.markUserRolesAndFacilitiesAsDeleted(email); | ||
// check facilities and roles after deletion | ||
int updatedFacilitiesCount = updatedUser.getFacilities().size(); | ||
int updatedOrgRolesCount = updatedUser.getRoles().size(); | ||
assertEquals(0, updatedFacilitiesCount); | ||
assertEquals(0, updatedOrgRolesCount); | ||
} | ||
|
||
private void roleCheck(final UserInfo userInfo, final Set<OrganizationRole> expected) { | ||
EnumSet<OrganizationRole> actual = EnumSet.copyOf(userInfo.getRoles()); | ||
assertEquals(expected, actual); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters