예제 #1
0
        public async Task CanGetUsersWithClaims()
        {
            var claimType11 = new Claim("claimType1", "valueType1");
            var claimType12 = new Claim("claimType2", "valueType1");
            var claimType21 = new Claim("claimType1", "valueType2");

            // user type 11
            ElasticIdentityUser noClaimsUser11 = new ElasticIdentityUser(nameof(noClaimsUser11));

            noClaimsUser11.Claims.Add(new ElasticIdentityUserClaim(claimType11));
            await _store.CreateAsync(noClaimsUser11, CancellationToken.None);

            // user type 12
            ElasticIdentityUser noClaimsUser12 = new ElasticIdentityUser(nameof(noClaimsUser12));

            noClaimsUser12.Claims.Add(new ElasticIdentityUserClaim(claimType12));
            noClaimsUser12.Claims.Add(new ElasticIdentityUserClaim(claimType21));
            await _store.CreateAsync(noClaimsUser12, CancellationToken.None);

            // user type 21
            ElasticIdentityUser noClaimsUser21 = new ElasticIdentityUser(nameof(noClaimsUser21));

            noClaimsUser21.Claims.Add(new ElasticIdentityUserClaim(claimType21));
            noClaimsUser21.Claims.Add(new ElasticIdentityUserClaim(claimType12));
            await _store.CreateAsync(noClaimsUser21, CancellationToken.None);

            _elasticClient.Indices.Refresh(_indexName);

            var users = await _store.GetUsersForClaimAsync(claimType11, CancellationToken.None);

            Assert.Equal(nameof(noClaimsUser11), users.FirstOrDefault().UserName);
        }
        public async Task GetUsersForClaim_Test()
        {
            var store = new ElasticUserStore <ElasticUser, ElasticRole>(_nestClient, new ElasticOptions {
                UsersIndex = _userIndex
            });

            var user1 = new ElasticUser
            {
                UserName     = "******",
                PasswordHash = "phash",
                Logins       = new List <ElasticUserLogin> {
                    new ElasticUserLogin(new UserLoginInfo("prov1", "key1", "test1"))
                },
                Claims = new List <ElasticClaim> {
                    new ElasticClaim("type", "value1")
                },
                Email = new ElasticConfirmation("*****@*****.**")
            };
            var user2 = new ElasticUser
            {
                UserName     = "******",
                PasswordHash = "phash",
                Logins       = new List <ElasticUserLogin> {
                    new ElasticUserLogin(new UserLoginInfo("prov2", "key2", "test2"))
                },
                Claims = new List <ElasticClaim> {
                    new ElasticClaim("type", "value2")
                },
                Email = new ElasticConfirmation("*****@*****.**")
            };

            var createResult1 = await store.CreateAsync(user1, NoCancellationToken);

            var createResult2 = await store.CreateAsync(user2, NoCancellationToken);

            var elasticUser = await store.FindByLoginAsync("prov2", "key2", NoCancellationToken);

            var foundByClaim = await store.GetUsersForClaimAsync(new Claim("type", "value2"), NoCancellationToken);

            Assert.Equal(createResult1, IdentityResult.Success);
            Assert.Equal(createResult2, IdentityResult.Success);
            Assert.Equal(foundByClaim.Count, 1);
        }