### Question:
TokenHandler extends HttpResource { protected FullHttpResponse doHandleAuthorizationCode(String code, String redirect, String appId) throws Exception { Application app = appRegistry.getApplication(appId); if(!OAuthUtil.validateRedirect(redirect, app.getRedirect())) { OAuthMetrics.incAccessTokenInvalidRedirect(); return OAuthUtil.badRequest(); } Pair<UUID, Integer> personWithTtl = oauthDao.getPersonWithCode(appId, code); if(personWithTtl == null) { OAuthMetrics.incAccessTokenExpiredCode(); return OAuthUtil.badRequest(); } oauthDao.removeCode(appId, code); try { FullHttpResponse response = updateTokensAndRespond(personWithTtl.getKey(), 0, appId, null); triggerPlaceAuthorizedIfNecessary(appId, personWithTtl.getKey()); OAuthMetrics.incAccessTokenSuccess(); return response; } catch(Exception e) { logger.warn("Failed to update tokens", e); OAuthMetrics.incAccessTokenException(); throw e; } } @Inject  TokenHandler(
         ApplicationAuth authorizer,
         BridgeMetrics metrics,
         AppRegistry appRegistry,
         OAuthDAO oauthDao,
         OAuthConfig config,
         PlaceSelectionHandler placeSelectionHandler
   ); @Override FullHttpResponse respond(FullHttpRequest req, ChannelHandlerContext ctx);  }### Answer:
@Test public void testAuthNonMatchingRedirectBadRequest() throws Exception { FullHttpResponse response = tokenHandler.doHandleAuthorizationCode(token, "http: assertEquals(HttpResponseStatus.BAD_REQUEST, response.getStatus()); }
@Test public void testAuthNoPersonBadRequest() throws Exception { EasyMock.expect(mockDao.getPersonWithCode("app1", token)).andReturn(null); replay(); FullHttpResponse response = tokenHandler.doHandleAuthorizationCode(token, "http: assertEquals(HttpResponseStatus.BAD_REQUEST, response.getStatus()); verify(); }
@Test public void testAuthSuccess() throws Exception { EasyMock.expect(mockDao.getPersonWithCode("app1", token)).andReturn(new ImmutablePair<>(person, (int) TimeUnit.MINUTES.toSeconds(5))); mockDao.updateTokens(EasyMock.eq("app1"), EasyMock.anyString(), EasyMock.anyString(), EasyMock.eq(person)); EasyMock.expect(mockDao.getAttrs("app1", person)).andReturn(ImmutableMap.of()).anyTimes(); EasyMock.expectLastCall(); mockDao.removeCode("app1", token); EasyMock.expectLastCall(); replay(); FullHttpResponse response = tokenHandler.doHandleAuthorizationCode(token, "http: String body = new String(response.content().array(), StandardCharsets.UTF_8).trim(); TokenResponse tokRes = gson.fromJson(body, TokenResponse.class); assertNotNull(tokRes.getAccessToken()); assertNotNull(tokRes.getRefreshToken()); assertEquals(config.getAccessTtlMinutes(), TimeUnit.MINUTES.convert(tokRes.getExpiresIn(), TimeUnit.SECONDS)); assertEquals(HttpResponseStatus.OK, response.getStatus()); verify(); }