OfframpAuthorisation { public void checkAuthorisation(Authentication authentication, String roadName, Set<Sensitivity> grants) throws UnknownRoadException { AuthenticationType type = AuthenticationType.of(authentication); AuthorisationOutcome outcome = ERROR; try { checkAuthorisation(authentication, roadName, grants, type); outcome = AUTHORISED; } catch (AccessDeniedException e) { outcome = UNAUTHORISED; throw e; } finally { metrics.increment(roadName, type, outcome); } } @Autowired  OfframpAuthorisation(@Value("#{store}") Map<String, Road> store, MeterRegistry registry); void checkAuthorisation(Authentication authentication, String roadName, Set<Sensitivity> grants);  }
@Test(expected = UnknownRoadException.class) public void roadDoesNotExist() throws Exception { store.clear(); try { underTest.checkAuthorisation(null, ROAD_NAME, emptySet()); } finally { verify(metrics).increment("road1", ANONYMOUS, ERROR); } }
@Test public void anonymousRequestNoGrants() throws Exception { Authentication authentication = mock(AnonymousAuthenticationToken.class); underTest.checkAuthorisation(authentication, ROAD_NAME, emptySet()); verify(metrics).increment("road1", ANONYMOUS, AUTHORISED); }
@Test(expected = AccessDeniedException.class) public void anonymousRequestGrants() throws Exception { Authentication authentication = mock(AnonymousAuthenticationToken.class); try { underTest.checkAuthorisation(authentication, ROAD_NAME, singleton(PII)); } finally { verify(metrics).increment("road1", ANONYMOUS, UNAUTHORISED); } }
@Test public void authenticatedRequestNoGrants() throws Exception { offramp.setAuthorities(singletonMap("AUTHORITY", singleton(PUBLIC))); doReturn(true).when(authentication).isAuthenticated(); doReturn(singletonList(new SimpleGrantedAuthority("AUTHORITY"))).when(authentication).getAuthorities(); underTest.checkAuthorisation(authentication, ROAD_NAME, emptySet()); verify(metrics).increment("road1", AUTHENTICATED, AUTHORISED); }
@Test(expected = AccessDeniedException.class) public void authenticatedRequestNoGrantsNoAuthority() throws Exception { offramp.setAuthorities(emptyMap()); doReturn(true).when(authentication).isAuthenticated(); doReturn(singletonList(new SimpleGrantedAuthority("AUTHORITY"))).when(authentication).getAuthorities(); try { underTest.checkAuthorisation(authentication, ROAD_NAME, emptySet()); } finally { verify(metrics).increment("road1", AUTHENTICATED, UNAUTHORISED); } }
@Test public void authenticatedRequestGrants() throws Exception { offramp.setAuthorities(singletonMap("AUTHORITY", ImmutableSet.of(PUBLIC, PII))); doReturn(true).when(authentication).isAuthenticated(); doReturn(singletonList(new SimpleGrantedAuthority("AUTHORITY"))).when(authentication).getAuthorities(); underTest.checkAuthorisation(authentication, ROAD_NAME, singleton(PII)); verify(metrics).increment("road1", AUTHENTICATED, AUTHORISED); }
@Test public void authenticatedRequestGrantsWithWildcard() throws Exception { offramp.setAuthorities(singletonMap("*", ImmutableSet.of(PUBLIC, PII))); doReturn(true).when(authentication).isAuthenticated(); doReturn(singletonList(new SimpleGrantedAuthority("AUTHORITY"))).when(authentication).getAuthorities(); underTest.checkAuthorisation(authentication, ROAD_NAME, singleton(PII)); verify(metrics).increment("road1", AUTHENTICATED, AUTHORISED); }
@Test(expected = AccessDeniedException.class) public void authenticatedRequestGrantsDenied() throws Exception { offramp.setAuthorities(singletonMap("AUTHORITY", emptySet())); doReturn(true).when(authentication).isAuthenticated(); doReturn(singletonList(new SimpleGrantedAuthority("AUTHORITY"))).when(authentication).getAuthorities(); try { underTest.checkAuthorisation(authentication, ROAD_NAME, singleton(PII)); } finally { verify(metrics).increment("road1", AUTHENTICATED, UNAUTHORISED); } }