AutomationClientAuthFactory extends ClientAuthFactory { public AutomationClient provide(ContainerRequest containerRequest, HttpServletRequest httpServletRequest) { Client client = super.provide(containerRequest, httpServletRequest); return Optional.ofNullable(AutomationClient.of(client)) .orElseThrow(() -> new ForbiddenException( format("Client %s not authorized as a AutomationClient", client.getName()))); } @Inject  AutomationClientAuthFactory(ClientDAOFactory clientDAOFactory,
      KeywhizConfig keywhizConfig); @VisibleForTesting  AutomationClientAuthFactory(ClientDAO clientDAO,
      ClientAuthConfig clientAuthConfig); AutomationClient provide(ContainerRequest containerRequest,
      HttpServletRequest httpServletRequest);  }
@Test public void automationClientWhenClientPresent() { when(securityContext.getUserPrincipal()).thenReturn(principal); when(clientDAO.getClientByName("principal")).thenReturn(Optional.of(client)); assertThat(factory.provide(request, httpServletRequest)).isEqualTo(automationClient); }
@Test(expected = ForbiddenException.class) public void automationClientRejectsClientsWithoutAutomation() { Client clientWithoutAutomation = new Client(3423, "clientWithoutAutomation", null, null, null, null, null, null, null, null, true, false ); when(securityContext.getUserPrincipal()).thenReturn( SimplePrincipal.of("CN=clientWithoutAutomation")); when(clientDAO.getClientByName("clientWithoutAutomation")) .thenReturn(Optional.of(clientWithoutAutomation)); factory.provide(request, httpServletRequest); }
@Test(expected = NotAuthorizedException.class) public void automationClientRejectsClientsWithoutDBEntries() { when(securityContext.getUserPrincipal()).thenReturn( SimplePrincipal.of("CN=clientWithoutDBEntry")); when(clientDAO.getClientByName("clientWithoutDBEntry")).thenReturn(Optional.empty()); factory.provide(request, httpServletRequest); }