### Question:
UmaResourceServerFilter extends GenericHeapObject implements Filter { @Override public Promise<Response, NeverThrowsException> filter(final Context context, final Request request, final Handler next) { try { final Share share = umaService.findShare(request); String rpt = tokenExtractor.getAccessToken(request.getHeaders().getFirst("Authorization")); if (rpt != null) { return introspectToken(context, rpt, share.getPAT()) .thenAsync(new VerifyScopesAsyncFunction(share, context, request, next)); } return ticket(context, share, request); } catch (UmaException e) { return newResponsePromise(e.getResponse().setStatus(Status.NOT_FOUND)); } } UmaResourceServerFilter(final UmaSharingService umaService,
                                   final Handler protectionApiHandler,
                                   final String realm); @Override Promise<Response, NeverThrowsException> filter(final Context context,
                                                          final Request request,
                                                          final Handler next);  }### Answer:
@Test public void shouldReturnTicketBecauseNoRptProvided() throws Exception { mockTicketCreation(); UmaResourceServerFilter filter = new UmaResourceServerFilter(service, handler, null); Response response = filter.filter(null, request, terminal).get(); assertThatTicketIsReturnedWithStatusForbidden(response); verifyZeroInteractions(terminal); }
@Test public void shouldAuthorizeIncomingRequestWithValidRpt() throws Exception { request.getHeaders().put("Authorization", format("Bearer %s", RPT)); mockTokenIntrospection(new Response(Status.OK).setEntity(validToken())); UmaResourceServerFilter filter = new UmaResourceServerFilter(service, handler, null); filter.filter(null, request, terminal); verify(terminal).handle(null, request); }
@Test public void shouldReturnTicketWhenInactiveRpt() throws Exception { request.getHeaders().put("Authorization", format("Bearer %s", RPT)); mockTokenIntrospection(new Response(Status.OK).setEntity(inactiveToken())); mockTicketCreation(); UmaResourceServerFilter filter = new UmaResourceServerFilter(service, handler, null); Response response = filter.filter(null, request, terminal).get(); assertThatTicketIsReturnedWithStatusForbidden(response); verifyZeroInteractions(terminal); }
@Test public void shouldReturnForbiddenWithWarningOnTicketError() throws Exception { mockTicketCreation(new Response(Status.BAD_REQUEST)); UmaResourceServerFilter filter = new UmaResourceServerFilter(service, handler, null); Response response = filter.filter(null, request, terminal).get(); assertThat(response.getStatus()).isEqualTo(Status.FORBIDDEN); assertThat(response.getHeaders().getFirst("Warning")) .isEqualTo("199 - \"UMA Authorization Server Unreachable\""); verifyZeroInteractions(terminal); }
@Test public void shouldReturnTicketWhenRptDoesNotContainsEnoughScopes() throws Exception { request.getHeaders().put("Authorization", format("Bearer %s", RPT)); mockTokenIntrospection(new Response(Status.OK).setEntity(insufficientScopesToken())); mockTicketCreation(); UmaResourceServerFilter filter = new UmaResourceServerFilter(service, handler, null); Response response = filter.filter(null, request, terminal).get(); assertThatTicketIsReturnedWithStatusForbidden(response); assertThat(response.getHeaders().getFirst("WWW-Authenticate")).contains("insufficient_scope"); verifyZeroInteractions(terminal); }