Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Johanna/5600 replace generic exceptions #6688

Merged
merged 3 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -323,11 +324,8 @@ private void configureDemoUsers(List<DemoUser> users, Map<String, Facility> 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;
})
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<DiseaseService> {
@Autowired LoggedInAuthorizationService loggedInAuthorizationService;

@Test
void findAllOrganizationRoles_NobodyAuthenticatedException() {
SecurityContextHolder.getContext().setAuthentication(null);
assertThrows(
NobodyAuthenticatedException.class,
() -> loggedInAuthorizationService.findAllOrganizationRoles());
}
}
Loading