Search in sources :

Example 1 with AccountRecord

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

the class CacheKeyValueDelegateTest method accountExtraValueDeserialization.

@Test
public void accountExtraValueDeserialization() throws JSONException {
    final AccountRecord account = new AccountRecord();
    account.setHomeAccountId(HOME_ACCOUNT_ID);
    account.setEnvironment(ENVIRONMENT);
    account.setRealm(REALM);
    account.setLocalAccountId(LOCAL_ACCOUNT_ID);
    account.setAuthorityType(AUTHORITY_TYPE);
    account.setAlternativeAccountId(ALTERNATIVE_ACCOUNT_ID);
    account.setFirstName(FIRST_NAME);
    account.setFamilyName(FAMILY_NAME);
    account.setAvatarUrl(AVATAR_URL);
    String serializedValue = mDelegate.generateCacheValue(account);
    // Turn the serialized value into a JSONObject and start testing field equality.
    final JSONObject jsonObject = new JSONObject(serializedValue);
    // Add more non-standard data to this object...
    final JSONArray numbers = new JSONArray("[1, 2, 3]");
    final JSONArray objects = new JSONArray("[{\"hello\" : \"hallo\"}, {\"goodbye\" : \"auf wiedersehen\"}]");
    jsonObject.put("foo", "bar");
    jsonObject.put("numbers", numbers);
    jsonObject.put("objects", objects);
    serializedValue = jsonObject.toString();
    final AccountRecord deserializedValue = mDelegate.fromCacheValue(serializedValue, AccountRecord.class);
    assertNotNull(deserializedValue);
    assertNull(deserializedValue.getAdditionalFields().get(AccountRecord.SerializedNames.ENVIRONMENT));
    assertEquals(HOME_ACCOUNT_ID, deserializedValue.getHomeAccountId());
    assertEquals(ENVIRONMENT, deserializedValue.getEnvironment());
    assertEquals(REALM, deserializedValue.getRealm());
    assertEquals(LOCAL_ACCOUNT_ID, deserializedValue.getLocalAccountId());
    assertEquals(AUTHORITY_TYPE, deserializedValue.getAuthorityType());
    assertEquals(ALTERNATIVE_ACCOUNT_ID, deserializedValue.getAlternativeAccountId());
    assertEquals(FIRST_NAME, deserializedValue.getFirstName());
    assertEquals(FAMILY_NAME, deserializedValue.getFamilyName());
    assertEquals(AVATAR_URL, deserializedValue.getAvatarUrl());
    assertEquals(3, deserializedValue.getAdditionalFields().size());
    assertEquals("bar", deserializedValue.getAdditionalFields().get("foo").getAsString());
    assertEquals(numbers.toString(), deserializedValue.getAdditionalFields().get("numbers").toString());
}
Also used : JSONObject(org.json.JSONObject) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) JSONArray(org.json.JSONArray) Test(org.junit.Test)

Example 2 with AccountRecord

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

the class CacheKeyValueDelegateTest method accountCreateCacheKeyCompleteNoRealm.

@Test
public void accountCreateCacheKeyCompleteNoRealm() {
    final AccountRecord account = new AccountRecord();
    account.setHomeAccountId(HOME_ACCOUNT_ID);
    account.setEnvironment(ENVIRONMENT);
    final String expectedKey = "" + HOME_ACCOUNT_ID + CACHE_VALUE_SEPARATOR + ENVIRONMENT + CACHE_VALUE_SEPARATOR;
    assertEquals(expectedKey, mDelegate.generateCacheKey(account));
}
Also used : AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) Test(org.junit.Test)

Example 3 with AccountRecord

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

the class CacheKeyValueDelegateTest method accountCreateCacheValue.

@Test
public void accountCreateCacheValue() throws JSONException {
    final AccountRecord account = new AccountRecord();
    account.setHomeAccountId(HOME_ACCOUNT_ID);
    account.setEnvironment(ENVIRONMENT);
    account.setRealm(REALM);
    account.setLocalAccountId(LOCAL_ACCOUNT_ID);
    account.setAuthorityType(AUTHORITY_TYPE);
    account.setAlternativeAccountId(ALTERNATIVE_ACCOUNT_ID);
    account.setFirstName(FIRST_NAME);
    account.setFamilyName(FAMILY_NAME);
    account.setAvatarUrl(AVATAR_URL);
    final String serializedValue = mDelegate.generateCacheValue(account);
    final JSONObject jsonObject = new JSONObject(serializedValue);
    assertEquals(HOME_ACCOUNT_ID, jsonObject.getString(AccountRecord.SerializedNames.HOME_ACCOUNT_ID));
    assertEquals(ENVIRONMENT, jsonObject.getString(AccountRecord.SerializedNames.ENVIRONMENT));
    assertEquals(REALM, jsonObject.getString(AccountRecord.SerializedNames.REALM));
    assertEquals(LOCAL_ACCOUNT_ID, jsonObject.getString("local_account_id"));
    assertEquals(AUTHORITY_TYPE, jsonObject.getString("authority_type"));
    assertEquals(ALTERNATIVE_ACCOUNT_ID, jsonObject.getString("alternative_account_id"));
    assertEquals(FIRST_NAME, jsonObject.getString("first_name"));
    assertEquals(FAMILY_NAME, jsonObject.getString("family_name"));
    assertEquals(AVATAR_URL, jsonObject.getString("avatar_url"));
}
Also used : JSONObject(org.json.JSONObject) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) Test(org.junit.Test)

Example 4 with AccountRecord

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

the class BrokerOAuth2TokenCacheTest method testCanSaveIntoAppUidCache.

@Test
@SuppressWarnings("unchecked")
public void testCanSaveIntoAppUidCache() throws ClientException {
    configureMocksForAppUid();
    mBrokerOAuth2TokenCache.save(mockStrategy, mockRequest, mockResponse);
    final List<AccountRecord> accounts = mAppUidCredentialCache.getAccounts();
    assertEquals(1, accounts.size());
    assertEquals(mDefaultAppUidTestBundle.mGeneratedAccount, accounts.get(0));
    final List<Credential> credentials = mAppUidCredentialCache.getCredentials();
    assertEquals(3, credentials.size());
    final List<Credential> rts = new ArrayList<>();
    final List<Credential> ats = new ArrayList<>();
    final List<Credential> ids = new ArrayList<>();
    for (final Credential credential : credentials) {
        if (credential.getCredentialType().equalsIgnoreCase(CredentialType.AccessToken.name())) {
            ats.add(credential);
        } else if (credential.getCredentialType().equalsIgnoreCase(CredentialType.RefreshToken.name())) {
            rts.add(credential);
        } else if (credential.getCredentialType().equalsIgnoreCase(CredentialType.IdToken.name())) {
            ids.add(credential);
        } else {
            fail();
        }
    }
    assertEquals(mDefaultAppUidTestBundle.mGeneratedAccessToken, ats.get(0));
    assertEquals(mDefaultAppUidTestBundle.mGeneratedRefreshToken, rts.get(0));
    assertEquals(mDefaultAppUidTestBundle.mGeneratedIdToken, ids.get(0));
}
Also used : Credential(com.microsoft.identity.common.internal.dto.Credential) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 5 with AccountRecord

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

the class BrokerOAuth2TokenCacheTest method testGetAccountsAdal.

@Test
public void testGetAccountsAdal() throws ClientException {
    // Load up the 'other caches' which a bunch of test credentials, see if we can get them out...
    int ii = 0;
    for (final OAuth2TokenCache cache : mOtherAppTokenCaches) {
        configureMocks(mOtherCacheTestBundles.get(ii));
        final ICacheRecord cacheRecord = cache.save(mockStrategy, mockRequest, mockResponse);
        final BrokerApplicationMetadata applicationMetadata = new BrokerApplicationMetadata();
        applicationMetadata.setClientId(cacheRecord.getIdToken().getClientId());
        applicationMetadata.setEnvironment(cacheRecord.getIdToken().getEnvironment());
        applicationMetadata.setFoci(cacheRecord.getRefreshToken().getFamilyId());
        applicationMetadata.setUid(testAppUids[ii++]);
        mApplicationMetadataCache.insert(applicationMetadata);
    }
    final List<String> clientIds = new ArrayList<>();
    for (final MsalOAuth2TokenCacheTest.AccountCredentialTestBundle testBundle : mOtherCacheTestBundles) {
        clientIds.add(testBundle.mGeneratedRefreshToken.getClientId());
    }
    final List<AccountRecord> xAppAccounts = new ArrayList<>();
    for (final int testUid : testAppUids) {
        // Create the cache to query...
        mBrokerOAuth2TokenCache = new BrokerOAuth2TokenCache(InstrumentationRegistry.getContext(), testUid, mApplicationMetadataCache, new BrokerOAuth2TokenCache.ProcessUidCacheFactory() {

            @Override
            public MsalOAuth2TokenCache getTokenCache(Context context, int bindingProcessUid) {
                return initAppUidCache(context, bindingProcessUid);
            }
        }, mFociCache);
        for (final String clientId : clientIds) {
            final List<AccountRecord> accountsInCache = mBrokerOAuth2TokenCache.getAccounts(ENVIRONMENT, clientId);
            xAppAccounts.addAll(accountsInCache);
        }
    }
    assertEquals(clientIds.size(), xAppAccounts.size());
    final List<AccountRecord> xAppAccountsNoParam = new ArrayList<>(mBrokerOAuth2TokenCache.getAccounts());
    assertEquals(xAppAccounts.size(), xAppAccountsNoParam.size());
}
Also used : Context(android.content.Context) ICacheRecord(com.microsoft.identity.common.internal.cache.ICacheRecord) BrokerApplicationMetadata(com.microsoft.identity.common.internal.cache.BrokerApplicationMetadata) ArrayList(java.util.ArrayList) OAuth2TokenCache(com.microsoft.identity.common.internal.providers.oauth2.OAuth2TokenCache) MicrosoftFamilyOAuth2TokenCache(com.microsoft.identity.common.internal.cache.MicrosoftFamilyOAuth2TokenCache) MsalOAuth2TokenCache(com.microsoft.identity.common.internal.cache.MsalOAuth2TokenCache) BrokerOAuth2TokenCache(com.microsoft.identity.common.internal.cache.BrokerOAuth2TokenCache) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) BrokerOAuth2TokenCache(com.microsoft.identity.common.internal.cache.BrokerOAuth2TokenCache) 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