Search in sources :

Example 1 with BrokerApplicationMetadata

use of com.microsoft.identity.common.internal.cache.BrokerApplicationMetadata 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)

Example 2 with BrokerApplicationMetadata

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

the class BrokerOAuth2TokenCacheTest method testRemoveAccountFromDevice.

@Test
public void testRemoveAccountFromDevice() 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 = mBrokerOAuth2TokenCache.getAccounts();
    // Deleting one of these AccountRecords should remove all of them...
    final AccountDeletionRecord deletionRecord = mBrokerOAuth2TokenCache.removeAccountFromDevice(xAppAccounts.get(0));
    assertEquals(xAppAccounts.size(), deletionRecord.size());
    assertEquals(0, mBrokerOAuth2TokenCache.getAccounts().size());
}
Also used : 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) ICacheRecord(com.microsoft.identity.common.internal.cache.ICacheRecord) AccountDeletionRecord(com.microsoft.identity.common.internal.cache.AccountDeletionRecord) BrokerApplicationMetadata(com.microsoft.identity.common.internal.cache.BrokerApplicationMetadata) ArrayList(java.util.ArrayList) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) Test(org.junit.Test)

Example 3 with BrokerApplicationMetadata

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

the class SharedPreferencesBrokerApplicationMetadataCacheTest method testInsert.

@Test
public void testInsert() {
    final BrokerApplicationMetadata randomMetadata = generateRandomMetadata();
    mMetadataCache.insert(randomMetadata);
    assertEquals(1, mMetadataCache.getAll().size());
    assertEquals(randomMetadata, mMetadataCache.getAll().get(0));
}
Also used : BrokerApplicationMetadata(com.microsoft.identity.common.internal.cache.BrokerApplicationMetadata) Test(org.junit.Test)

Example 4 with BrokerApplicationMetadata

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

the class SharedPreferencesBrokerApplicationMetadataCacheTest method testDuplicateDataCannotExist.

@Test
public void testDuplicateDataCannotExist() {
    // Create a random app
    final BrokerApplicationMetadata metadata = generateRandomMetadata();
    // Mark that app as non-foci
    metadata.setFoci(null);
    // Insert that record into the cache...
    final boolean inserted = mMetadataCache.insert(metadata);
    // Assert it was correctly inserted
    assertTrue(inserted);
    // Take the original app, and make it now foci
    metadata.setFoci("1");
    // Insert that record into the cache...
    final boolean insertedAgain = mMetadataCache.insert(metadata);
    // Assert it was correctly inserted
    assertTrue(insertedAgain);
    // Because those were the 'same app', assert that our cache only contains 1 entry
    // AND that that entry contains the updated foci state...
    assertEquals(1, mMetadataCache.getAll().size());
    assertTrue(mMetadataCache.getAllNonFociClientIds().isEmpty());
    assertEquals(1, mMetadataCache.getAllFociClientIds().size());
}
Also used : BrokerApplicationMetadata(com.microsoft.identity.common.internal.cache.BrokerApplicationMetadata) Test(org.junit.Test)

Example 5 with BrokerApplicationMetadata

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

the class SharedPreferencesBrokerApplicationMetadataCacheTest method testRemoveMultiple.

@Test
public void testRemoveMultiple() {
    final int expected = 10;
    final List<BrokerApplicationMetadata> metadataList = new ArrayList<>();
    for (int ii = 0; ii < expected; ii++) {
        metadataList.add(generateRandomMetadata());
    }
    for (final BrokerApplicationMetadata metadata : metadataList) {
        mMetadataCache.insert(metadata);
    }
    final List<BrokerApplicationMetadata> cacheContents = mMetadataCache.getAll();
    for (final BrokerApplicationMetadata metadata : cacheContents) {
        assertTrue(mMetadataCache.remove(metadata));
    }
    assertTrue(mMetadataCache.getAll().isEmpty());
}
Also used : BrokerApplicationMetadata(com.microsoft.identity.common.internal.cache.BrokerApplicationMetadata) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

BrokerApplicationMetadata (com.microsoft.identity.common.internal.cache.BrokerApplicationMetadata)9 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)5 BrokerOAuth2TokenCache (com.microsoft.identity.common.internal.cache.BrokerOAuth2TokenCache)3 ICacheRecord (com.microsoft.identity.common.internal.cache.ICacheRecord)3 MicrosoftFamilyOAuth2TokenCache (com.microsoft.identity.common.internal.cache.MicrosoftFamilyOAuth2TokenCache)3 MsalOAuth2TokenCache (com.microsoft.identity.common.internal.cache.MsalOAuth2TokenCache)3 AccountRecord (com.microsoft.identity.common.internal.dto.AccountRecord)3 OAuth2TokenCache (com.microsoft.identity.common.internal.providers.oauth2.OAuth2TokenCache)3 Context (android.content.Context)2 AccountDeletionRecord (com.microsoft.identity.common.internal.cache.AccountDeletionRecord)1 SharedPreferencesBrokerApplicationMetadataCache (com.microsoft.identity.common.internal.cache.SharedPreferencesBrokerApplicationMetadataCache)1 Random (java.util.Random)1