### Question:
AuthorizationFlowHandler { void submitUserConsent(Authorization authorization) throws UserConsentException { if(authorization.requiresUserApproval()) { if(config.isDefaultApproveMissingScopes()) { authorization.setUserApprovedScopes(((ScopeApprovalConsent) authorization.getConsent()).getMissingScopes()); approveScopes(authorization); } else { throw new UserConsentException("There are missing scopes for a successful user consent handling but missing scopes should not " + "be approved by default according to the provided config."); } } String code = authorizationApiHandler.submitConsent( ((PlainConsent) authorization.getConsent()).getAction(), authorization.getSession().getId(), authorization.getSession().getData()); if(Strings.isNullOrEmpty(code)) { throw new UserConsentException("Retrieved auth code after consent submission was null or empty."); } authorization.setAuthCode(code); } AuthorizationFlowHandler(OAuthConfig config, TokenRepository tokenRepository, WebFormHandler webFormHandler, AuthorizationApiHandler authorizationApiHandler); static AuthorizationFlowHandler setup(OAuthConfig config, TokenRepository tokenRepository); static AuthorizationFlowHandler setup(OAuthConfig config); static TokenProvider fromRepository(OAuthConfig config, TokenRepository tokenRepository); TokenProvider authorize(String username, String password);  }### Answer:
@Test public void testSubmitUserConsent_whenConsentInformationHasMissingScopesAndDefaultApproveIsEnabled_shouldMakeApprovalCallFirst() throws Exception { ScopeApprovalConsent approvalConsent = createScopeApprovalConsent(); Authorization authorization = createAuthorizationWithoutConsent(); authorization.setConsent(approvalConsent); PlainConsent plainConsent = new PlainConsent(); plainConsent.setAction("Grant"); plainConsent.setSessionID(authorization.getSession().getId()); plainConsent.setSessionData(authorization.getSession().getData()); Mockito.when(webFormHandler.consent( eq("app-id"), eq("session-id"), eq("session-data"), eq(approvalConsent.getMissingScopes()), any(BaseClient.CookieStorage.class))).thenReturn(plainConsent); Mockito.when(authorizationApiHandler.submitConsent(anyString(), anyString(), anyString())).thenReturn("authCode"); authorizationFlowHandler.submitUserConsent(authorization); assertThat(authorization.getAuthCode()).isEqualTo("authCode"); }
@Test(expected = UserConsentException.class) public void testSubmitUserConsent_whenMissingScopesAndConfigDoesNotApproveByDefault_shouldThrowException() throws Exception { authorizationFlowHandler = new AuthorizationFlowHandler( getDefaultBuilder().defaultApproveMissingScopes(false).build(), null, webFormHandler, authorizationApiHandler); ScopeApprovalConsent approvalConsent = createScopeApprovalConsent(); Authorization authorization = createAuthorizationWithoutConsent(); authorization.setConsent(approvalConsent); authorizationFlowHandler.submitUserConsent(authorization); }
@Test(expected = UserConsentException.class) public void testSubmitUserConsent_whenReturnedAuthCodeIsEmpty_shouldThrowException() throws Exception { ScopeApprovalConsent approvalConsent = createScopeApprovalConsent(); Authorization authorization = createAuthorizationWithoutConsent(); authorization.setConsent(approvalConsent); PlainConsent plainConsent = new PlainConsent(); plainConsent.setAction("Grant"); plainConsent.setSessionID(authorization.getSession().getId()); plainConsent.setSessionData(authorization.getSession().getData()); Mockito.when(webFormHandler.consent( eq("app-id"), eq("session-id"), eq("session-data"), eq(approvalConsent.getMissingScopes()), any(BaseClient.CookieStorage.class))).thenReturn(plainConsent); Mockito.when(authorizationApiHandler.submitConsent(anyString(), anyString(), anyString())).thenReturn(""); authorizationFlowHandler.submitUserConsent(authorization); }