Skip to content

Commit

Permalink
add additional test case
Browse files Browse the repository at this point in the history
  • Loading branch information
lbalmaceda committed Oct 21, 2020
1 parent 37b170e commit 7b2d7d9
Showing 1 changed file with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,56 @@ public void shouldGetNonExpiredCredentialsFromStorageWhenOnlyAccessTokenIsAvaila
assertThat(retrievedCredentials.getScope(), is("scope"));
}

@Test
public void shouldRenewExpiredCredentialsWhenScopeHasChanged() {
when(storage.retrieveString("com.auth0.id_token")).thenReturn("idToken");
when(storage.retrieveString("com.auth0.access_token")).thenReturn("accessToken");
when(storage.retrieveString("com.auth0.refresh_token")).thenReturn("refreshToken");
when(storage.retrieveString("com.auth0.token_type")).thenReturn("type");
long expirationTime = CredentialsMock.CURRENT_TIME_MS; // expired credentials
when(storage.retrieveLong("com.auth0.expires_at")).thenReturn(expirationTime);
when(storage.retrieveLong("com.auth0.cache_expires_at")).thenReturn(expirationTime);
when(storage.retrieveString("com.auth0.scope")).thenReturn("scope");
when(client.renewAuth("refreshToken")).thenReturn(request);

Date newDate = new Date(CredentialsMock.ONE_HOUR_AHEAD_MS + ONE_HOUR_SECONDS * 1000);
JWT jwtMock = mock(JWT.class);
when(jwtMock.getExpiresAt()).thenReturn(newDate);
when(jwtDecoder.decode("newId")).thenReturn(jwtMock);

manager.getCredentials("some scope", 0, callback);
verify(request).start(requestCallbackCaptor.capture());
verify(request).addParameter(eq("scope"), eq("some scope"));

// Trigger success
String newRefresh = null;
Credentials renewedCredentials = new Credentials("newId", "newAccess", "newType", newRefresh, newDate, "some scope");
requestCallbackCaptor.getValue().onSuccess(renewedCredentials);
verify(callback).onSuccess(credentialsCaptor.capture());

// Verify the credentials are property stored
verify(storage).store("com.auth0.id_token", renewedCredentials.getIdToken());
verify(storage).store("com.auth0.access_token", renewedCredentials.getAccessToken());
// RefreshToken should not be replaced
verify(storage, never()).store("com.auth0.refresh_token", newRefresh);
verify(storage).store("com.auth0.refresh_token", "refreshToken");
verify(storage).store("com.auth0.token_type", renewedCredentials.getType());
verify(storage).store("com.auth0.expires_at", renewedCredentials.getExpiresAt().getTime());
verify(storage).store("com.auth0.scope", renewedCredentials.getScope());
verify(storage).store("com.auth0.cache_expires_at", renewedCredentials.getExpiresAt().getTime());
verify(storage, never()).remove(anyString());

// Verify the returned credentials are the latest
Credentials retrievedCredentials = credentialsCaptor.getValue();
assertThat(retrievedCredentials, is(notNullValue()));
assertThat(retrievedCredentials.getIdToken(), is("newId"));
assertThat(retrievedCredentials.getAccessToken(), is("newAccess"));
assertThat(retrievedCredentials.getType(), is("newType"));
assertThat(retrievedCredentials.getRefreshToken(), is("refreshToken"));
assertThat(retrievedCredentials.getExpiresAt(), is(newDate));
assertThat(retrievedCredentials.getScope(), is("some scope"));
}

@Test
public void shouldRenewCredentialsWhenScopeHasChanged() {
when(storage.retrieveString("com.auth0.id_token")).thenReturn("idToken");
Expand Down Expand Up @@ -554,6 +604,7 @@ public void shouldGetAndSuccessfullyRenewExpiredCredentialsWithRefreshTokenRotat
when(jwtDecoder.decode("newId")).thenReturn(jwtMock);

manager.getCredentials(callback);
verify(request, never()).addParameter(eq("scope"), anyString());
verify(request).start(requestCallbackCaptor.capture());

//Trigger success
Expand Down

0 comments on commit 7b2d7d9

Please sign in to comment.