diff --git a/backend/src/main/java/gov/cdc/usds/simplereport/service/LoggedInAuthorizationService.java b/backend/src/main/java/gov/cdc/usds/simplereport/service/LoggedInAuthorizationService.java index 7d6fec0593..ab8446840f 100644 --- a/backend/src/main/java/gov/cdc/usds/simplereport/service/LoggedInAuthorizationService.java +++ b/backend/src/main/java/gov/cdc/usds/simplereport/service/LoggedInAuthorizationService.java @@ -5,6 +5,7 @@ import gov.cdc.usds.simplereport.config.authorization.OrganizationExtractor; import gov.cdc.usds.simplereport.config.authorization.OrganizationRoleClaims; import gov.cdc.usds.simplereport.config.authorization.TenantDataAuthenticationProvider; +import gov.cdc.usds.simplereport.service.errors.NobodyAuthenticatedException; import java.util.List; import java.util.Set; import org.springframework.context.annotation.Bean; @@ -33,7 +34,7 @@ public LoggedInAuthorizationService( private Authentication getCurrentAuth() { Authentication currentAuth = SecurityContextHolder.getContext().getAuthentication(); if (currentAuth == null) { - throw new RuntimeException("Nobody is currently authenticated"); + throw new NobodyAuthenticatedException(); } return currentAuth; } diff --git a/backend/src/main/java/gov/cdc/usds/simplereport/service/OrganizationInitializingService.java b/backend/src/main/java/gov/cdc/usds/simplereport/service/OrganizationInitializingService.java index 135d96519d..896d810adf 100644 --- a/backend/src/main/java/gov/cdc/usds/simplereport/service/OrganizationInitializingService.java +++ b/backend/src/main/java/gov/cdc/usds/simplereport/service/OrganizationInitializingService.java @@ -26,6 +26,7 @@ import gov.cdc.usds.simplereport.db.repository.ProviderRepository; import gov.cdc.usds.simplereport.db.repository.SpecimenTypeRepository; import gov.cdc.usds.simplereport.idp.repository.OktaRepository; +import gov.cdc.usds.simplereport.service.errors.UserFacilityNotInitializedException; import gov.cdc.usds.simplereport.service.model.IdentityAttributes; import java.util.ArrayList; import java.util.List; @@ -323,11 +324,8 @@ private void configureDemoUsers(List users, Map faci f -> { Facility facility = facilitiesByName.get(f); if (facility == null) { - throw new RuntimeException( - "User's facility=" - + f - + " was not initialized. Valid facilities=" - + facilitiesByName.keySet().toString()); + throw new UserFacilityNotInitializedException( + f, facilitiesByName.keySet().toString()); } return facility; }) diff --git a/backend/src/main/java/gov/cdc/usds/simplereport/service/errors/NobodyAuthenticatedException.java b/backend/src/main/java/gov/cdc/usds/simplereport/service/errors/NobodyAuthenticatedException.java new file mode 100644 index 0000000000..29e5b4be83 --- /dev/null +++ b/backend/src/main/java/gov/cdc/usds/simplereport/service/errors/NobodyAuthenticatedException.java @@ -0,0 +1,13 @@ +package gov.cdc.usds.simplereport.service.errors; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) +public class NobodyAuthenticatedException extends RuntimeException { + private static final long serialVersionUID = 1L; + + public NobodyAuthenticatedException() { + super("Nobody is currently authenticated"); + } +} diff --git a/backend/src/main/java/gov/cdc/usds/simplereport/service/errors/UserFacilityNotInitializedException.java b/backend/src/main/java/gov/cdc/usds/simplereport/service/errors/UserFacilityNotInitializedException.java new file mode 100644 index 0000000000..9fe308a265 --- /dev/null +++ b/backend/src/main/java/gov/cdc/usds/simplereport/service/errors/UserFacilityNotInitializedException.java @@ -0,0 +1,17 @@ +package gov.cdc.usds.simplereport.service.errors; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(HttpStatus.BAD_REQUEST) +public class UserFacilityNotInitializedException extends RuntimeException { + private static final long serialVersionUID = 1L; + + public UserFacilityNotInitializedException(String invalidFacility, String validFacilitiesList) { + super( + "User's facility=" + + invalidFacility + + " was not initialized. Valid facilities=" + + validFacilitiesList); + } +} diff --git a/backend/src/test/java/gov/cdc/usds/simplereport/service/LoggedInAuthorizationServiceTest.java b/backend/src/test/java/gov/cdc/usds/simplereport/service/LoggedInAuthorizationServiceTest.java new file mode 100644 index 0000000000..196d425037 --- /dev/null +++ b/backend/src/test/java/gov/cdc/usds/simplereport/service/LoggedInAuthorizationServiceTest.java @@ -0,0 +1,20 @@ +package gov.cdc.usds.simplereport.service; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +import gov.cdc.usds.simplereport.service.errors.NobodyAuthenticatedException; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.context.SecurityContextHolder; + +class LoggedInAuthorizationServiceTest extends BaseServiceTest { + @Autowired LoggedInAuthorizationService loggedInAuthorizationService; + + @Test + void findAllOrganizationRoles_NobodyAuthenticatedException() { + SecurityContextHolder.getContext().setAuthentication(null); + assertThrows( + NobodyAuthenticatedException.class, + () -> loggedInAuthorizationService.findAllOrganizationRoles()); + } +}