Skip to content

Commit

Permalink
Bob/fix mutation error states (#6437)
Browse files Browse the repository at this point in the history
* make errors more specific

* lint
  • Loading branch information
fzhao99 authored Aug 29, 2023
1 parent e96603d commit e03891c
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package gov.cdc.usds.simplereport.api.apiuser;

import static gov.cdc.usds.simplereport.service.ApiUserService.MOVE_USER_ARGUMENT_ERROR;

import gov.cdc.usds.simplereport.api.Translators;
import gov.cdc.usds.simplereport.api.model.Role;
import gov.cdc.usds.simplereport.api.model.User;
import gov.cdc.usds.simplereport.api.model.UserInput;
import gov.cdc.usds.simplereport.api.model.errors.IllegalGraphqlArgumentException;
import gov.cdc.usds.simplereport.config.AuthorizationConfiguration;
import gov.cdc.usds.simplereport.db.model.ApiUser;
import gov.cdc.usds.simplereport.db.model.auxiliary.PersonName;
Expand Down Expand Up @@ -172,19 +169,13 @@ public User updateUserPrivilegesAndGroupAccess(
@Argument boolean accessAllFacilities,
@Argument List<UUID> facilities,
@Argument Role role) {
try {
List<UUID> facilityIdsToAssign = facilities == null ? List.of() : facilities;
_us.updateUserPrivilegesAndGroupAccess(
username,
orgExternalId,
accessAllFacilities,
facilityIdsToAssign,
role.toOrganizationRole());
return new User(_us.getUserByLoginEmail(username));

} catch (IllegalArgumentException e) {
throw new IllegalGraphqlArgumentException(
"Error updating user privileges and / or group access: " + MOVE_USER_ARGUMENT_ERROR);
}
List<UUID> facilityIdsToAssign = facilities == null ? List.of() : facilities;
_us.updateUserPrivilegesAndGroupAccess(
username,
orgExternalId,
accessAllFacilities,
facilityIdsToAssign,
role.toOrganizationRole());
return new User(_us.getUserByLoginEmail(username));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package gov.cdc.usds.simplereport.api.model.errors;

import graphql.ErrorClassification;
import graphql.ErrorType;
import graphql.GraphQLError;
import graphql.language.SourceLocation;
import java.util.Collections;
import java.util.List;

/** Exception to throw when a facility ID can't be found in an organization query */
public class PrivilegeUpdateFacilityAccessException extends RuntimeException
implements GraphQLError {

private static final long serialVersionUID = 1L;

public static final String PRIVILEGE_UPDATE_FACILITY_ACCESS_ERROR =
"Operation must specify a list of facilities for the user to access or allow them access to all facilities";

public PrivilegeUpdateFacilityAccessException() {
super(PRIVILEGE_UPDATE_FACILITY_ACCESS_ERROR);
}

@Override // should-be-defaulted unused interface method
public List<SourceLocation> getLocations() {
return Collections.emptyList();
}

@Override
public ErrorClassification getErrorType() {
return ErrorType.ExecutionAborted;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import gov.cdc.usds.simplereport.api.model.errors.IllegalGraphqlFieldAccessException;
import gov.cdc.usds.simplereport.api.model.errors.NonexistentUserException;
import gov.cdc.usds.simplereport.api.model.errors.OktaAccountUserException;
import gov.cdc.usds.simplereport.api.model.errors.PrivilegeUpdateFacilityAccessException;
import gov.cdc.usds.simplereport.api.model.errors.RestrictedAccessUserException;
import gov.cdc.usds.simplereport.api.model.errors.TestEventSerializationFailureException;
import gov.cdc.usds.simplereport.api.model.errors.UnidentifiedFacilityException;
Expand Down Expand Up @@ -105,6 +106,13 @@ public DataFetcherExceptionResolver dataFetcherExceptionResolver() {
return Mono.just(singletonList(new GenericGraphqlException(errorMessage, errorPath)));
}

if (exception instanceof PrivilegeUpdateFacilityAccessException) {
String errorMessage =
"header: Error updating user privileges and / or group access; body: "
+ exception.getMessage();
return Mono.just(singletonList(new GenericGraphqlException(errorMessage, errorPath)));
}

return Mono.just(singletonList(new GenericGraphqlException((errorPath))));
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import gov.cdc.usds.simplereport.api.model.errors.MisconfiguredUserException;
import gov.cdc.usds.simplereport.api.model.errors.NonexistentUserException;
import gov.cdc.usds.simplereport.api.model.errors.OktaAccountUserException;
import gov.cdc.usds.simplereport.api.model.errors.PrivilegeUpdateFacilityAccessException;
import gov.cdc.usds.simplereport.api.model.errors.RestrictedAccessUserException;
import gov.cdc.usds.simplereport.api.model.errors.UnidentifiedFacilityException;
import gov.cdc.usds.simplereport.api.model.errors.UnidentifiedUserException;
Expand Down Expand Up @@ -72,9 +73,6 @@ public class ApiUserService {

@Autowired private ApiUserContextHolder _apiUserContextHolder;

public static final String MOVE_USER_ARGUMENT_ERROR =
"Operation must specify a list of facilities for the user to access or allow them access to all facilities";

private void createUserUpdatedAuditLog(Object authorId, Object updatedUserId) {
log.info("User with id={} updated by user with id={}", authorId, updatedUserId);
}
Expand Down Expand Up @@ -724,7 +722,7 @@ public void updateUserPrivilegesAndGroupAccess(
throws IllegalGraphqlArgumentException {

if (!allFacilitiesAccess && facilities.isEmpty()) {
throw new IllegalArgumentException(MOVE_USER_ARGUMENT_ERROR);
throw new PrivilegeUpdateFacilityAccessException();
}

Organization newOrg = _orgService.getOrganization(orgExternalId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package gov.cdc.usds.simplereport.service;

import static gov.cdc.usds.simplereport.service.ApiUserService.MOVE_USER_ARGUMENT_ERROR;
import static gov.cdc.usds.simplereport.api.model.errors.PrivilegeUpdateFacilityAccessException.PRIVILEGE_UPDATE_FACILITY_ACCESS_ERROR;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand All @@ -17,6 +17,7 @@
import gov.cdc.usds.simplereport.api.model.errors.IllegalGraphqlArgumentException;
import gov.cdc.usds.simplereport.api.model.errors.NonexistentUserException;
import gov.cdc.usds.simplereport.api.model.errors.OktaAccountUserException;
import gov.cdc.usds.simplereport.api.model.errors.PrivilegeUpdateFacilityAccessException;
import gov.cdc.usds.simplereport.api.model.errors.RestrictedAccessUserException;
import gov.cdc.usds.simplereport.api.model.errors.UnidentifiedFacilityException;
import gov.cdc.usds.simplereport.config.authorization.OrganizationRole;
Expand Down Expand Up @@ -508,27 +509,27 @@ void updateUserPrivilegesAndGroupAccess_assignToAllFacilities_success() {
@Test
@WithSimpleReportSiteAdminUser
void
updateUserPrivilegesAndGroupAccess_assignToAllFalseWithoutFacilities_throwsIllegalArgException() {
updateUserPrivilegesAndGroupAccess_assignToAllFalseWithoutFacilities_throwsPrivilegeUpdateFacilityAccessException() {
initSampleData();
final String email = "[email protected]";
Organization orgToTestMovementTo = _dataFactory.saveValidOrganization();
String moveOrgExternalId = orgToTestMovementTo.getExternalId();
List<UUID> emptyList = List.of();
IllegalArgumentException caught =
PrivilegeUpdateFacilityAccessException caught =
assertThrows(
IllegalArgumentException.class,
PrivilegeUpdateFacilityAccessException.class,
() ->
_service.updateUserPrivilegesAndGroupAccess(
email, moveOrgExternalId, false, emptyList, OrganizationRole.USER));
assertEquals(MOVE_USER_ARGUMENT_ERROR, caught.getMessage());
assertEquals(PRIVILEGE_UPDATE_FACILITY_ACCESS_ERROR, caught.getMessage());

IllegalArgumentException caught2 =
PrivilegeUpdateFacilityAccessException caught2 =
assertThrows(
IllegalArgumentException.class,
PrivilegeUpdateFacilityAccessException.class,
() ->
_service.updateUserPrivilegesAndGroupAccess(
email, moveOrgExternalId, false, OrganizationRole.USER));
assertEquals(MOVE_USER_ARGUMENT_ERROR, caught2.getMessage());
assertEquals(PRIVILEGE_UPDATE_FACILITY_ACCESS_ERROR, caught2.getMessage());
}

@Test
Expand Down

0 comments on commit e03891c

Please sign in to comment.