AuthCodeAuthorization implements ClientActivity { @Override public Response execute(Client client, Request request, DateTime instant) { String authCode = request.param("code"); Optional<Authorization> possibleAuthorization = clientAuthorizationRepository.findAuthorization(client, authCode, instant); if (!possibleAuthorization.isPresent()) { return OAuthError.invalidGrant("Authorization was not found."); } Authorization authorization = possibleAuthorization.get(); return clientActivity.execute(authorization, client, request, instant); } AuthCodeAuthorization(ClientAuthorizationRepository clientAuthorizationRepository, AuthorizedClientActivity clientActivity); @Override Response execute(Client client, Request request, DateTime instant);  }
@Test public void happyPath() throws Exception { final Client anyClient = aNewClient().build(); final DateTime anyInstantTime = new DateTime(); final Authorization anyAuth = newAuthorization().withId("::user_id::").build(); final Request request = aNewRequest().param("code", "::any code::").build(); context.checking(new Expectations() {{ oneOf(repository).findAuthorization(anyClient, "::any code::", anyInstantTime); will(returnValue(Optional.of(anyAuth))); oneOf(authorizedClientActivity).execute(anyAuth, anyClient, request, anyInstantTime); will(returnValue(new RsText("::response::"))); }}); Response response = authorization.execute(anyClient, request, anyInstantTime); assertThat(new RsPrint(response).printBody(), is(equalTo("::response::"))); }
@Test public void authorizationNotFound() throws Exception { final Client anyClient = aNewClient().build(); final DateTime anyInstantTime = new DateTime(); final Request request = aNewRequest().param("code", "::nonExistentAuth::").param("code_verifier", "::codeVerifier::").build(); context.checking(new Expectations() {{ oneOf(repository).findAuthorization(anyClient, "::nonExistentAuth::", anyInstantTime); will(returnValue(Optional.absent())); }}); Response response = authorization.execute(anyClient, request, anyInstantTime); assertThat(new RsPrint(response).printBody(), containsString("invalid_grant")); }