public void GetAllAdalEntriesForMsal_MultipleRTsPerEnv()
        {
            // Arrange
            LegacyTokenCacheHelper.PopulateLegacyWithRtAndId(
                _logger,
                _legacyCachePersistence,
                TestConstants.ClientId,
                TestConstants.ProductionPrefNetworkEnvironment,
                "uid",
                "tenantId1",
                "user1");

            LegacyTokenCacheHelper.PopulateLegacyWithRtAndId(
                _logger,
                _legacyCachePersistence,
                TestConstants.ClientId,
                TestConstants.ProductionPrefNetworkEnvironment,
                "uid",
                "tenantId2",
                "user2");

            // Act
            var rt = CacheFallbackOperations.GetRefreshToken(
                _logger,
                _legacyCachePersistence,
                new[] { TestConstants.ProductionPrefNetworkEnvironment },
                TestConstants.ClientId,
                new Account("uid.b", null, null));

            // Assert
            Assert.IsNotNull(rt, "One of the tokens should be returned");
        }
        public void RemoveAdalUser_RemovesUserWithSameId()
        {
            // Arrange
            LegacyTokenCacheHelper.PopulateLegacyCache(_logger, _legacyCachePersistence);

            LegacyTokenCacheHelper.PopulateLegacyWithRtAndId( // different clientId -> should not be deleted
                _logger,
                _legacyCachePersistence,
                "other_client_id",
                TestConstants.ProductionPrefNetworkEnvironment,
                "uid1",
                "tenantId1",
                "user1_other_client_id");

            LegacyTokenCacheHelper.PopulateLegacyWithRtAndId( // different env -> should be deleted
                _logger,
                _legacyCachePersistence,
                TestConstants.ClientId,
                "other_env",
                "uid1",
                "tenantId1",
                "user1_other_env");

            // Act - delete with id and displayname
            CacheFallbackOperations.RemoveAdalUser(
                _logger,
                _legacyCachePersistence,
                TestConstants.ClientId,
                "username_does_not_matter",
                "uid1.tenantId1");

            // Assert
            var adalUsers =
                CacheFallbackOperations.GetAllAdalUsersForMsal(
                    _logger,
                    _legacyCachePersistence,
                    TestConstants.ClientId);

            AssertByUsername(
                adalUsers,
                new[] { TestConstants.ProductionPrefNetworkEnvironment },
                new[]
            {
                "user2",
            },
                new[]
            {
                "no_client_info_user3",
                "no_client_info_user4"
            });
        }
        public void RemoveAdalUser_RemovesAdalEntitiesWithClientInfoAndWithout()
        {
            // in case of adalv3 -> adalv4 -> msal2 migration
            // adal cache can have different cache entities for the
            // same user/account with client info and wihout
            // CacheFallbackOperations.RemoveAdalUser should remove both
            LegacyTokenCacheHelper.PopulateLegacyWithRtAndId(
                _logger,
                _legacyCachePersistence,
                TestConstants.ClientId,
                TestConstants.ProductionPrefNetworkEnvironment,
                TestConstants.Uid,
                TestConstants.Utid,
                TestConstants.DisplayableId,
                TestConstants.ScopeStr);

            AssertCacheEntryCount(1);

            LegacyTokenCacheHelper.PopulateLegacyWithRtAndId(
                _logger,
                _legacyCachePersistence,
                TestConstants.ClientId,
                TestConstants.ProductionPrefNetworkEnvironment,
                null,
                null,
                TestConstants.DisplayableId,
                TestConstants.ScopeForAnotherResourceStr);

            AssertCacheEntryCount(2);

            CacheFallbackOperations.RemoveAdalUser(
                _logger,
                _legacyCachePersistence,
                TestConstants.ClientId,
                TestConstants.DisplayableId,
                TestConstants.Uid + "." + TestConstants.Utid);

            AssertCacheEntryCount(0);
        }
        public void RemoveAdalUser_RemovesUserNoClientInfo()
        {
            // Arrange
            LegacyTokenCacheHelper.PopulateLegacyCache(_logger, _legacyCachePersistence);

            LegacyTokenCacheHelper.PopulateLegacyWithRtAndId(
                _logger,
                _legacyCachePersistence,
                "other_client_id",
                TestConstants.ProductionPrefNetworkEnvironment,
                null,
                null,
                "no_client_info_user3"); // no client info, different client id -> won't be deleted

            LegacyTokenCacheHelper.PopulateLegacyWithRtAndId(
                _logger,
                _legacyCachePersistence,
                TestConstants.ClientId,
                "other_env",
                null,
                null,
                "no_client_info_user3"); // no client info, different env -> won't be deleted

            AssertCacheEntryCount(8);

            var adalUsers =
                CacheFallbackOperations.GetAllAdalUsersForMsal(
                    _logger,
                    _legacyCachePersistence,
                    TestConstants.ClientId);

            AssertByUsername(
                adalUsers,
                new[] { TestConstants.ProductionPrefNetworkEnvironment },
                new[]
            {
                "user2",
                "user1",
            },
                new[]
            {
                "no_client_info_user3",
                "no_client_info_user4"
            });

            // Act - delete with no client info -> displayable id is used
            CacheFallbackOperations.RemoveAdalUser(
                _logger,
                _legacyCachePersistence,
                TestConstants.ClientId,
                "no_client_info_user3",
                "");

            AssertCacheEntryCount(6);

            // Assert
            adalUsers = CacheFallbackOperations.GetAllAdalUsersForMsal(
                _logger,
                _legacyCachePersistence,
                TestConstants.ClientId);

            AssertByUsername(
                adalUsers,
                new[] { TestConstants.ProductionPrefNetworkEnvironment },
                new[]
            {
                "user2",
                "user1",
            },
                new[]
            {
                "no_client_info_user4"
            });
        }