AccessTokenManager implements AccessTokenStorage { @Override @Nullable public AccessToken getAccessToken() { long expiresIn; String token; Set<String> scopesString; String refreshToken; String tokenType; try { expiresIn = sharedPreferences.getLong(accessTokenKey + DATE_KEY_SUFFIX, -1); token = sharedPreferences.getString(accessTokenKey + TOKEN_KEY_SUFFIX, null); scopesString = sharedPreferences.getStringSet(accessTokenKey + SCOPES_KEY_SUFFIX, null); refreshToken = sharedPreferences.getString(accessTokenKey + REFRESH_TOKEN_KEY_SUFFIX, null); tokenType = sharedPreferences.getString(accessTokenKey + TOKEN_TYPE_KEY_SUFFIX, null); } catch (ClassCastException ignored) { return null; } if (expiresIn == -1 || token == null || scopesString == null) { return null; } Collection<Scope> scopes; try { scopes = AuthUtils.stringCollectionToScopeCollection(scopesString); } catch (IllegalArgumentException ignored) { return null; } return new AccessToken(expiresIn, scopes, token, refreshToken, tokenType); } AccessTokenManager(@NonNull Context context);  AccessTokenManager(@NonNull Context context, @NonNull String accessTokenKey); @VisibleForTesting  AccessTokenManager(@NonNull Context context,
                       @NonNull CookieUtils cookieManagerUtil);   AccessTokenManager(@NonNull Context context,
                               @NonNull CookieUtils cookieManagerUtil,
                               @NonNull String accessTokenKey); @Override @Nullable AccessToken getAccessToken(); @Override void removeAccessToken(); @Override void setAccessToken(@NonNull AccessToken accessToken); static final String ACCESS_TOKEN_DEFAULT_KEY; }
@Test public void getAccessToken_whentNoDefaultTokenStored_shouldReturnNull() { assertNull(accessTokenManager.getAccessToken()); }
@Test public void getAccessToken_whenNoCustomTokenStored_shouldReturnNull() { assertNull(customAccessTokenManager.getAccessToken()); }
@Test public void getAccessToken_whenNoDateStored_shouldReturnNull() { Set<String> scopes = ImmutableSet.of("profile"); tokenPreferences.setAccessTokensScopesOnly(scopes, DEFAULT_ACCESS_TOKEN_KEY); tokenPreferences.setAccessTokensTokenOnly("accessToken", DEFAULT_ACCESS_TOKEN_KEY); assertNull(accessTokenManager.getAccessToken()); }
@Test public void getAccessToken_whenNoScopesStringStored_shouldReturnNull() { tokenPreferences.setAccessTokensDateOnly(2592000, DEFAULT_ACCESS_TOKEN_KEY); tokenPreferences.setAccessTokensTokenOnly("accessToken", DEFAULT_ACCESS_TOKEN_KEY); assertNull(accessTokenManager.getAccessToken()); }
@Test public void getAccessToken_whenNoTokenStringStored_shouldReturnNull() { tokenPreferences.setAccessTokensDateOnly(2592000, DEFAULT_ACCESS_TOKEN_KEY); Set<String> scopes = ImmutableSet.of("profile"); tokenPreferences.setAccessTokensScopesOnly(scopes, DEFAULT_ACCESS_TOKEN_KEY); assertNull(accessTokenManager.getAccessToken()); }
@Test public void getAccessToken_whenInvalidScopesStringStored_shouldReturnNull() { tokenPreferences.setAccessTokensDateOnly(2592000, DEFAULT_ACCESS_TOKEN_KEY); tokenPreferences.setAccessTokensTokenOnly("accessToken", DEFAULT_ACCESS_TOKEN_KEY); Set<String> scopes = ImmutableSet.of("thisIsNotAScope"); tokenPreferences.setAccessTokensScopesOnly(scopes, DEFAULT_ACCESS_TOKEN_KEY); assertNull(accessTokenManager.getAccessToken()); }
@Test public void getAccessToken_whenWrongClassDateStored_shouldReturnNull() { tokenPreferences.setAccessTokensDateBad(DEFAULT_ACCESS_TOKEN_KEY); tokenPreferences.setAccessTokensTokenOnly("accessToken", DEFAULT_ACCESS_TOKEN_KEY); Set<String> scopes = ImmutableSet.of("profile"); tokenPreferences.setAccessTokensScopesOnly(scopes, DEFAULT_ACCESS_TOKEN_KEY); assertNull(accessTokenManager.getAccessToken()); }
@Test public void getAccessToken_whenWrongClassTokenStored_shouldReturnNull() { tokenPreferences.setAccessTokensDateOnly(2592000, DEFAULT_ACCESS_TOKEN_KEY); tokenPreferences.setAccessTokensTokenBad(DEFAULT_ACCESS_TOKEN_KEY); Set<String> scopes = ImmutableSet.of("profile"); tokenPreferences.setAccessTokensScopesOnly(scopes, DEFAULT_ACCESS_TOKEN_KEY); assertNull(accessTokenManager.getAccessToken()); }
@Test public void getAccessToken_whenWrongClassScopesStored_shouldReturnNull() { tokenPreferences.setAccessTokensDateOnly(2592000, DEFAULT_ACCESS_TOKEN_KEY); tokenPreferences.setAccessTokensTokenOnly("accessToken", DEFAULT_ACCESS_TOKEN_KEY); tokenPreferences.setAccessTokensScopesBad(DEFAULT_ACCESS_TOKEN_KEY); assertNull(accessTokenManager.getAccessToken()); }