Search in sources :

Example 66 with AccountRecord

use of com.microsoft.identity.common.internal.dto.AccountRecord in project microsoft-authentication-library-common-for-android by AzureAD.

the class SharedPreferencesAccountCredentialCacheTest method getAccountsWithMatchingHomeAccountIdEnvironment.

@Test
public void getAccountsWithMatchingHomeAccountIdEnvironment() {
    final AccountRecord account1 = new AccountRecord();
    account1.setLocalAccountId(LOCAL_ACCOUNT_ID);
    account1.setUsername(USERNAME);
    account1.setAuthorityType(AUTHORITY_TYPE);
    account1.setHomeAccountId(HOME_ACCOUNT_ID);
    account1.setEnvironment(ENVIRONMENT);
    account1.setRealm(REALM);
    final AccountRecord account2 = new AccountRecord();
    account2.setLocalAccountId(LOCAL_ACCOUNT_ID);
    account2.setUsername(USERNAME);
    account2.setAuthorityType(AUTHORITY_TYPE);
    account2.setHomeAccountId(HOME_ACCOUNT_ID);
    account2.setEnvironment(ENVIRONMENT);
    account2.setRealm(REALM2);
    final AccountRecord account3 = new AccountRecord();
    account3.setLocalAccountId(LOCAL_ACCOUNT_ID);
    account3.setUsername(USERNAME);
    account3.setAuthorityType(AUTHORITY_TYPE);
    account3.setHomeAccountId(HOME_ACCOUNT_ID);
    account3.setEnvironment(ENVIRONMENT);
    account3.setRealm(REALM3);
    final AccountRecord account4 = new AccountRecord();
    account4.setLocalAccountId(LOCAL_ACCOUNT_ID);
    account4.setUsername(USERNAME);
    account4.setAuthorityType(AUTHORITY_TYPE);
    account4.setHomeAccountId(HOME_ACCOUNT_ID);
    account4.setEnvironment("Foo");
    account4.setRealm(REALM);
    // Save the Accounts
    mSharedPreferencesAccountCredentialCache.saveAccount(account1);
    mSharedPreferencesAccountCredentialCache.saveAccount(account2);
    mSharedPreferencesAccountCredentialCache.saveAccount(account3);
    mSharedPreferencesAccountCredentialCache.saveAccount(account4);
    final List<AccountRecord> accounts = mSharedPreferencesAccountCredentialCache.getAccountsFilteredBy(HOME_ACCOUNT_ID, ENVIRONMENT, null);
    assertEquals(3, accounts.size());
}
Also used : AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) Test(org.junit.Test)

Example 67 with AccountRecord

use of com.microsoft.identity.common.internal.dto.AccountRecord in project microsoft-authentication-library-common-for-android by AzureAD.

the class SharedPreferencesAccountCredentialCacheTest method clearAccounts.

@Test
public void clearAccounts() {
    // Save an Account into the cache
    final AccountRecord account = new AccountRecord();
    account.setHomeAccountId(HOME_ACCOUNT_ID);
    account.setEnvironment(ENVIRONMENT);
    account.setRealm(REALM);
    account.setLocalAccountId(LOCAL_ACCOUNT_ID);
    account.setUsername(USERNAME);
    account.setAuthorityType(AUTHORITY_TYPE);
    mSharedPreferencesAccountCredentialCache.saveAccount(account);
    // Save an AccessToken into the cache
    final AccessTokenRecord accessToken = new AccessTokenRecord();
    accessToken.setCredentialType(CredentialType.AccessToken.name());
    accessToken.setHomeAccountId(HOME_ACCOUNT_ID);
    accessToken.setRealm(REALM);
    accessToken.setEnvironment(ENVIRONMENT);
    accessToken.setClientId(CLIENT_ID);
    accessToken.setTarget(TARGET);
    accessToken.setCachedAt(CACHED_AT);
    accessToken.setExpiresOn(EXPIRES_ON);
    accessToken.setSecret(SECRET);
    mSharedPreferencesAccountCredentialCache.saveCredential(accessToken);
    // Save a RefreshToken into the cache
    final RefreshTokenRecord refreshToken = new RefreshTokenRecord();
    refreshToken.setCredentialType(CredentialType.RefreshToken.name());
    refreshToken.setEnvironment(ENVIRONMENT);
    refreshToken.setHomeAccountId(HOME_ACCOUNT_ID);
    refreshToken.setClientId(CLIENT_ID);
    refreshToken.setSecret(SECRET);
    refreshToken.setTarget(TARGET);
    mSharedPreferencesAccountCredentialCache.saveCredential(refreshToken);
    // Call clearAccounts()
    mSharedPreferencesAccountCredentialCache.removeAccount(account);
    // Verify getAccounts() returns zero items
    assertTrue(mSharedPreferencesAccountCredentialCache.getAccounts().isEmpty());
    // Verify getCredentials() returns two items
    final List<Credential> credentials = mSharedPreferencesAccountCredentialCache.getCredentials();
    assertTrue(credentials.size() == 2);
}
Also used : Credential(com.microsoft.identity.common.internal.dto.Credential) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) PrimaryRefreshTokenRecord(com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord) RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord) AccessTokenRecord(com.microsoft.identity.common.internal.dto.AccessTokenRecord) Test(org.junit.Test)

Example 68 with AccountRecord

use of com.microsoft.identity.common.internal.dto.AccountRecord in project microsoft-authentication-library-common-for-android by AzureAD.

the class SharedPreferencesAccountCredentialCacheTest method getAccountsComplete.

@Test
public void getAccountsComplete() {
    final AccountRecord account = new AccountRecord();
    account.setHomeAccountId(HOME_ACCOUNT_ID);
    account.setEnvironment(ENVIRONMENT);
    account.setRealm(REALM);
    account.setLocalAccountId(LOCAL_ACCOUNT_ID);
    account.setUsername(USERNAME);
    account.setAuthorityType(AUTHORITY_TYPE);
    // Save the Account
    mSharedPreferencesAccountCredentialCache.saveAccount(account);
    // Test retrieval
    final List<AccountRecord> accounts = mSharedPreferencesAccountCredentialCache.getAccountsFilteredBy(HOME_ACCOUNT_ID, ENVIRONMENT, REALM);
    assertEquals(1, accounts.size());
    final AccountRecord retrievedAccount = accounts.get(0);
    assertEquals(HOME_ACCOUNT_ID, retrievedAccount.getHomeAccountId());
    assertEquals(ENVIRONMENT, retrievedAccount.getEnvironment());
    assertEquals(REALM, retrievedAccount.getRealm());
}
Also used : AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) Test(org.junit.Test)

Example 69 with AccountRecord

use of com.microsoft.identity.common.internal.dto.AccountRecord in project microsoft-authentication-library-common-for-android by AzureAD.

the class SharedPreferencesAccountCredentialCacheTest method malformedJsonCacheValueForAccount.

@Test
public void malformedJsonCacheValueForAccount() {
    final AccountRecord account = new AccountRecord();
    account.setHomeAccountId(HOME_ACCOUNT_ID);
    account.setEnvironment(ENVIRONMENT);
    account.setRealm(REALM);
    // Generate a cache key
    final String cacheKey = mDelegate.generateCacheKey(account);
    mSharedPreferencesFileManager.putString(cacheKey, "{\"thing\" \"not an account\"}");
    final AccountRecord malformedAccount = mSharedPreferencesAccountCredentialCache.getAccount(cacheKey);
    assertNull(malformedAccount);
    assertNotNull(mSharedPreferencesFileManager.getString(cacheKey));
}
Also used : AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) Test(org.junit.Test)

Example 70 with AccountRecord

use of com.microsoft.identity.common.internal.dto.AccountRecord in project microsoft-authentication-library-common-for-android by AzureAD.

the class SharedPreferencesAccountCredentialCacheTest method getAccounts.

@Test
public void getAccounts() {
    // Save an Account into the cache
    final AccountRecord account = new AccountRecord();
    account.setHomeAccountId(HOME_ACCOUNT_ID);
    account.setEnvironment(ENVIRONMENT);
    account.setRealm(REALM);
    account.setLocalAccountId(LOCAL_ACCOUNT_ID);
    account.setUsername(USERNAME);
    account.setAuthorityType(AUTHORITY_TYPE);
    mSharedPreferencesAccountCredentialCache.saveAccount(account);
    // Save an AccessToken into the cache
    final AccessTokenRecord accessToken = new AccessTokenRecord();
    accessToken.setCredentialType(CredentialType.AccessToken.name());
    accessToken.setHomeAccountId(HOME_ACCOUNT_ID);
    accessToken.setRealm("Foo");
    accessToken.setEnvironment(ENVIRONMENT);
    accessToken.setClientId(CLIENT_ID);
    accessToken.setTarget(TARGET);
    accessToken.setCachedAt(CACHED_AT);
    accessToken.setExpiresOn(EXPIRES_ON);
    accessToken.setSecret(SECRET);
    mSharedPreferencesAccountCredentialCache.saveCredential(accessToken);
    // Save a RefreshToken into the cache
    final RefreshTokenRecord refreshToken = new RefreshTokenRecord();
    refreshToken.setCredentialType(CredentialType.RefreshToken.name());
    refreshToken.setEnvironment(ENVIRONMENT);
    refreshToken.setHomeAccountId(HOME_ACCOUNT_ID);
    refreshToken.setClientId(CLIENT_ID);
    refreshToken.setSecret(SECRET);
    refreshToken.setTarget(TARGET);
    mSharedPreferencesAccountCredentialCache.saveCredential(refreshToken);
    // Verify getAccountsFilteredBy() returns one matching element
    final List<AccountRecord> accounts = mSharedPreferencesAccountCredentialCache.getAccounts();
    assertTrue(accounts.size() == 1);
    assertEquals(account, accounts.get(0));
}
Also used : AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) PrimaryRefreshTokenRecord(com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord) RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord) AccessTokenRecord(com.microsoft.identity.common.internal.dto.AccessTokenRecord) Test(org.junit.Test)

Aggregations

AccountRecord (com.microsoft.identity.common.internal.dto.AccountRecord)92 Test (org.junit.Test)61 ArrayList (java.util.ArrayList)20 ICacheRecord (com.microsoft.identity.common.internal.cache.ICacheRecord)11 AccessTokenRecord (com.microsoft.identity.common.internal.dto.AccessTokenRecord)11 OAuth2TokenCache (com.microsoft.identity.common.internal.providers.oauth2.OAuth2TokenCache)11 Credential (com.microsoft.identity.common.internal.dto.Credential)10 IdTokenRecord (com.microsoft.identity.common.internal.dto.IdTokenRecord)10 RefreshTokenRecord (com.microsoft.identity.common.internal.dto.RefreshTokenRecord)10 AccountDeletionRecord (com.microsoft.identity.common.internal.cache.AccountDeletionRecord)9 PrimaryRefreshTokenRecord (com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord)7 Nullable (androidx.annotation.Nullable)6 MsalOAuth2TokenCache (com.microsoft.identity.common.internal.cache.MsalOAuth2TokenCache)5 HashMap (java.util.HashMap)5 IAccountRecord (com.microsoft.identity.common.internal.dto.IAccountRecord)4 Context (android.content.Context)3 NonNull (androidx.annotation.NonNull)3 JsonElement (com.google.gson.JsonElement)3 JsonPrimitive (com.google.gson.JsonPrimitive)3 BearerAuthenticationSchemeInternal (com.microsoft.identity.common.internal.authscheme.BearerAuthenticationSchemeInternal)3