public async Task SetTwoFactorEnabled_Test()
        {
            var store = new ElasticUserStore <ElasticUser, ElasticRole>(_nestClient, new ElasticOptions {
                UsersIndex = _userIndex
            });

            var user = 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 createResult = await store.CreateAsync(user, NoCancellationToken);

            await store.SetTwoFactorEnabledAsync(user, true, NoCancellationToken);

            await store.UpdateAsync(user, NoCancellationToken);

            var elasticUser = await store.FindByIdAsync(user.Id, NoCancellationToken);

            Assert.Equal(createResult, IdentityResult.Success);
            Assert.True(elasticUser.IsTwoFactorEnabled);
        }
        public async Task FindByLogin_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"))
                },
                Email = new ElasticConfirmation("*****@*****.**")
            };
            var user2 = new ElasticUser
            {
                UserName     = "******",
                PasswordHash = "phash",
                Logins       = new List <ElasticUserLogin> {
                    new ElasticUserLogin(new UserLoginInfo("prov2", "key2", "test2"))
                },
                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);

            Assert.Equal(createResult1, IdentityResult.Success);
            Assert.Equal(createResult2, IdentityResult.Success);
            Assert.Equal(user2.Id, elasticUser.Id);
        }
        public async Task RemoveClaim_Test()
        {
            var store = new ElasticUserStore <ElasticUser, ElasticRole>(_nestClient, new ElasticOptions {
                UsersIndex = _userIndex
            });

            var user = new ElasticUser
            {
                UserName     = "******",
                PasswordHash = "phash",
                Logins       = new List <ElasticUserLogin> {
                    new ElasticUserLogin(new UserLoginInfo("prov1", "key1", "test1"))
                },
                Claims = new List <ElasticClaim> {
                    new ElasticClaim("type1", "value1"),
                    new ElasticClaim("type2", "value2")
                },
                Email = new ElasticConfirmation("*****@*****.**")
            };

            var createResult = await store.CreateAsync(user, NoCancellationToken);

            var claimsToBeRemoved = new List <Claim>
            {
                new Claim("type2", "value2")
            };

            await store.RemoveClaimsAsync(user, claimsToBeRemoved, NoCancellationToken);

            var elasticUser = await store.FindByIdAsync(user.Id, NoCancellationToken);

            Assert.Equal(createResult, IdentityResult.Success);
            Assert.Equal(elasticUser.Claims.Count, 2);
        }
Пример #4
0
        public async Task CustomTypeName()
        {
            const string indexName  = "some-index";
            const string entityName = "world";

            try {
                var userStore = new ElasticUserStore <ElasticUser>(
                    _connectionString,
                    indexName: indexName,
                    entityName: entityName,
                    forceRecreate: true
                    );

                var user = new ElasticUser {
                    UserName = "******"
                };

                user.Roles.UnionWith(new[] { "hello" });

                var userManager = new UserManager <ElasticUser>(userStore);
                AssertIdentityResult(await userManager.CreateAsync(user, "some password"));


                var response = Client.Get <ElasticUser>(user.Id, indexName, entityName);
                Assert.That(response.Source, Is.Not.Null);
                Assert.That(response.Source.UserName, Is.EqualTo(user.UserName));
            }
            finally {
                Client.DeleteIndex(i => i.Index(indexName));
            }
        }
        public async Task AddLogin_ShouldNotAdd_Test()
        {
            var store = new ElasticUserStore <ElasticUser, ElasticRole>(_nestClient, new ElasticOptions {
                UsersIndex = _userIndex
            });

            var user = new ElasticUser
            {
                UserName     = "******",
                PasswordHash = "phash",
                Logins       = new List <ElasticUserLogin> {
                    new ElasticUserLogin(new UserLoginInfo("prov1", "key1", "test1"))
                },
                Email = new ElasticConfirmation("*****@*****.**")
            };

            var createResult = await store.CreateAsync(user, NoCancellationToken);

            await store.AddLoginAsync(user, new UserLoginInfo("prov1", "key1", "test1"), NoCancellationToken);

            var elasticUser = await store.FindByIdAsync(user.Id, NoCancellationToken);

            Assert.Equal(createResult, IdentityResult.Success);
            Assert.Equal(user.Logins.Count, 1);
        }
Пример #6
0
        public async Task CanFindByName()
        {
            var store = new ElasticUserStore <string, ElasticIdentityUser>(_elasticClient);

            var user = await store.FindByNameAsync(_user1.NormalizedUserName, CancellationToken.None);

            Assert.Equal(_user1.UserName, user.UserName);
        }
        public async Task GetUsersInRole_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);

            await store.AddToRoleAsync(user1, "role1", NoCancellationToken);

            await store.AddToRoleAsync(user1, "role2", NoCancellationToken);

            await store.AddToRoleAsync(user2, "role1", NoCancellationToken);

            var usersForRole1 = await store.GetUsersInRoleAsync("role1", NoCancellationToken);

            var usersForRole2 = await store.GetUsersInRoleAsync("role2", NoCancellationToken);

            Assert.Equal(createResult1, IdentityResult.Success);
            Assert.Equal(createResult2, IdentityResult.Success);
            Assert.Equal(usersForRole1.Count, 2);
            Assert.Equal(usersForRole2.Count, 1);
        }
Пример #8
0
        public ElasticUserStoreTest()
        {
            _indexName = $"{nameof(ElasticUserStoreTest)}_{Guid.NewGuid()}".ToLowerInvariant();
            var node = new Uri("http://localhost:9200");

            _elasticClient = ElasticClientFactory.Create(node, _indexName);

            _store = new ElasticUserStore(_elasticClient);

            // the generic store refreshes immediately
            var store = new ElasticUserStore <string, ElasticIdentityUser>(_elasticClient);

            store.CreateAsync(_user1, CancellationToken.None).GetAwaiter().GetResult();

            _elasticClient.Indices.Refresh(_indexName);
        }
        public async Task Create_Test()
        {
            var store = new ElasticUserStore <ElasticUser, ElasticRole>(_nestClient, new ElasticOptions {
                UsersIndex = _userIndex
            });

            var user = new ElasticUser
            {
                UserName     = "******",
                PasswordHash = "phash",
                Email        = new ElasticConfirmation("*****@*****.**")
            };

            var result = await store.CreateAsync(user, NoCancellationToken);

            Assert.Equal(result, IdentityResult.Success);
        }
Пример #10
0
        public async Task CustomIndexName()
        {
            const string indexName = "hello";

            Assert.False(Client.IndexExists(i => i.Index(indexName)).Exists);

            var store = new ElasticUserStore <ElasticUser>(
                _connectionString,
                indexName: indexName,
                entityName: "world",
                forceRecreate: true);

            await store.ConnectionSetup();

            Assert.True(Client.IndexExists(i => i.Index(indexName)).Exists);
            Client.DeleteIndex(i => i.Index(indexName));
        }
Пример #11
0
        public async Task GetNormalizedUserName_Test()
        {
            var store = new ElasticUserStore <ElasticUser, ElasticRole>(_nestClient, new ElasticOptions {
                UsersIndex = _userIndex
            });

            const string expected = "test1";
            var          user     = new ElasticUser
            {
                UserName     = "******",
                PasswordHash = "phash",
                Logins       = new List <ElasticUserLogin> {
                    new ElasticUserLogin(new UserLoginInfo("prov1", "key", "test1")),
                    new ElasticUserLogin(new UserLoginInfo("prov2", "key2", "test1"))
                },
                Email = new ElasticConfirmation("*****@*****.**")
            };
            var actual = await store.GetNormalizedUserNameAsync(user, NoCancellationToken);

            Assert.Equal(expected, actual);
        }
Пример #12
0
        public async Task GetLogins_Test()
        {
            var store = new ElasticUserStore <ElasticUser, ElasticRole>(_nestClient, new ElasticOptions {
                UsersIndex = _userIndex
            });

            var user = new ElasticUser
            {
                UserName     = "******",
                PasswordHash = "phash",
                Logins       = new List <ElasticUserLogin> {
                    new ElasticUserLogin(new UserLoginInfo("prov1", "key", "test1")),
                    new ElasticUserLogin(new UserLoginInfo("prov2", "key2", "test1"))
                },
                Email = new ElasticConfirmation("*****@*****.**")
            };

            var logins = await store.GetLoginsAsync(user, NoCancellationToken);

            Assert.Equal(2, logins.Count);
        }
Пример #13
0
        public async Task TestExtendedProperties()
        {
            const string indexName = "hello-users";

            try {
                var store = new ElasticUserStore <HelloUser>(_connectionString,
                                                             indexName,
                                                             forceRecreate: true);

                await store.CreateAsync(new HelloUser {
                    UserName = "******",
                    Car      = new Tesla {
                        LicensePlate = "ABC123",
                        Model        = TeslaModel.ModelS
                    }
                });

                await store.CreateAsync(new HelloUser {
                    UserName = "******",
                    Car      = new Koenigsegg {
                        LicensePlate = "ABC123",
                        Model        = KoenigseggModel.One
                    }
                });

                var users = await store.GetAllAsync();

                var teslaUser      = users.FirstOrDefault(x => x.UserName == "abc123");
                var koenigseggUser = users.FirstOrDefault(x => x.UserName == "def456");

                Assert.That(teslaUser, Is.Not.Null, "No Telsa user found");
                Assert.That(koenigseggUser, Is.Not.Null, "No Koenigsegg user found");

                Assert.That(teslaUser.Car, Is.AssignableTo <Tesla>(), "Tesla Car is not Tesla");
                Assert.That(koenigseggUser.Car, Is.AssignableTo <Koenigsegg>(), "Koenigsegg Car is not Koenigsegg");
            }
            finally {
                Client.DeleteIndex(i => i.Index(indexName));
            }
        }