/** * Gets the OAuth2 header. * * @throws OAuthException if the OAuth2 token could not be refreshed. */ private String getOAuth2Header(OAuth2Compatible oAuth2Compatible) throws OAuthException { if (adsLibConfiguration.isAutoRefreshOAuth2TokenEnabled()) { try { oAuth2Helper.refreshCredential(oAuth2Compatible.getOAuth2Credential()); } catch (IOException e) { throw new OAuthException("OAuth2 token could not be refreshed.", e); } } return oAuth2AuthorizationHeaderProvider.getOAuth2AuthorizationHeader(oAuth2Compatible); } }
/** * Gets the OAuth2 header. * * @throws OAuthException if the OAuth2 token could not be refreshed. */ private String getOAuth2Header(OAuth2Compatible oAuth2Compatible) throws OAuthException { if (adsLibConfiguration.isAutoRefreshOAuth2TokenEnabled()) { try { oAuth2Helper.refreshCredential(oAuth2Compatible.getOAuth2Credential()); } catch (IOException e) { throw new OAuthException("OAuth2 token could not be refreshed.", e); } } return oAuth2AuthorizationHeaderProvider.getOAuth2AuthorizationHeader(oAuth2Compatible); } }
@Test public void testGetAuthorizationHeader_oAuth2NoRefresh() throws Exception { final Credential credential = new Credential(BearerToken.authorizationHeaderAccessMethod()); OAuth2Session adsSession = () -> credential; when(oAuth2AuthorizationHeaderProvider.getOAuth2AuthorizationHeader( (OAuth2Compatible) adsSession)).thenReturn("OAuth2 Header"); when(adsLibConfiguration.isAutoRefreshOAuth2TokenEnabled()).thenReturn(false); assertEquals("OAuth2 Header", authorizationHeaderProvider.getAuthorizationHeader(adsSession, ENDPOINT_URL.toString())); verify(oAuth2Helper, times(0)).refreshCredential(credential); }
@Test public void testGetAuthorizationHeader_oAuth2Refresh() throws Exception { final Credential credential = new Credential(BearerToken.authorizationHeaderAccessMethod()); OAuth2Session adsSession = () -> credential; when(oAuth2AuthorizationHeaderProvider.getOAuth2AuthorizationHeader( (OAuth2Compatible) adsSession)).thenReturn("OAuth2 Header"); when(adsLibConfiguration.isAutoRefreshOAuth2TokenEnabled()).thenReturn(true); assertEquals("OAuth2 Header", authorizationHeaderProvider.getAuthorizationHeader(adsSession, ENDPOINT_URL.toString())); verify(oAuth2Helper).refreshCredential(credential); }
@Test public void testRefreshCredential_refresh() throws Exception { credential.setAccessToken(ACCESS_TOKEN); credential.setRefreshToken(REFRESH_TOKEN); credential.setExpiresInSeconds(REFRESH_WINDOW_SECS - 10L); Mockito.doReturn(true).when(oAuth2Helper).callRefreshToken(credential); oAuth2Helper.refreshCredential(credential); verify(oAuth2Helper).callRefreshToken(credential); }
@Test public void testRefreshCredential_refreshFailedNoException() throws Exception { credential.setAccessToken(ACCESS_TOKEN); credential.setRefreshToken(REFRESH_TOKEN); credential.setExpiresInSeconds(REFRESH_WINDOW_SECS - 10L); Mockito.doReturn(false).when(oAuth2Helper).callRefreshToken(credential); oAuth2Helper.refreshCredential(credential); }
@Test public void testRefreshCredential_refreshFailedException() throws Exception { credential.setAccessToken(ACCESS_TOKEN); credential.setRefreshToken(REFRESH_TOKEN); credential.setExpiresInSeconds(REFRESH_WINDOW_SECS - 10L); Mockito.doThrow(new IOException("Failed")).when(oAuth2Helper).callRefreshToken(credential); thrown.expect(IOException.class); oAuth2Helper.refreshCredential(credential); } }
@Test public void testRefreshCredential_noRefreshToken() throws Exception { credential.setAccessToken(ACCESS_TOKEN); credential.setRefreshToken(null); credential.setExpiresInSeconds(null); assertSame(credential, oAuth2Helper.refreshCredential(credential)); }
@Test public void testRefreshCredential_notSoonEnough() throws Exception { credential.setAccessToken(ACCESS_TOKEN); credential.setRefreshToken(REFRESH_TOKEN); credential.setExpiresInSeconds(REFRESH_WINDOW_SECS * 2L); assertSame(credential, oAuth2Helper.refreshCredential(credential)); }