Ejemplo n.º 1
0
        public async Task CreateAsync_ShouldCreateUser()
        {
            // ARRANGE
            using (var dbProvider = DynamoDbServerTestUtils.CreateDatabase())
            {
                var roleStore = new DynamoRoleUsersStore <DynamoIdentityRole, DynamoIdentityUser>();
                await roleStore.EnsureInitializedAsync(dbProvider.Client, dbProvider.Context);

                var userStore = new DynamoUserStore <DynamoIdentityUser, DynamoIdentityRole>(roleStore);
                await userStore.EnsureInitializedAsync(dbProvider.Client, dbProvider.Context);

                var generalStore = userStore as IUserStore <DynamoIdentityUser>;

                var user = new DynamoIdentityUser(TestUtils.RandomString(10));

                // ACT
                await generalStore.CreateAsync(user, CancellationToken.None);

                // ASSERT
                var retrievedUser = await dbProvider.Context.LoadAsync(user);

                Assert.NotNull(retrievedUser);
                Assert.Equal(user.UserName, retrievedUser.UserName);
                Assert.Equal(user.NormalizedUserName, retrievedUser.NormalizedUserName);
            }
        }
        public async Task DynamoRoleUsersStore_RemoveFromRole_RemovesUserFromRole()
        {
            var user     = new DynamoIdentityUser(TestUtils.RandomString(10));
            var roleName = TestUtils.RandomString(10);

            using (var dbProvider = DynamoDbServerTestUtils.CreateDatabase())
            {
                var roleStore = new DynamoRoleUsersStore <DynamoIdentityRole, DynamoIdentityUser>();
                await roleStore.EnsureInitializedAsync(dbProvider.Client, dbProvider.Context);

                await roleStore.AddToRoleAsync(user, roleName, CancellationToken.None);

                var result = await roleStore.IsInRoleAsync(user, roleName, CancellationToken.None);

                Assert.True(result);

                // ACT
                await roleStore.RemoveFromRoleAsync(user, roleName, CancellationToken.None);

                // ASSERT
                var result2 = await roleStore.IsInRoleAsync(user, roleName, CancellationToken.None);

                Assert.False(result2);
            }
        }
        public async Task DynamoRoleUsersStore_AddToRole_HandlesDuplicateRoleUserEntry()
        {
            var user     = new DynamoIdentityUser(TestUtils.RandomString(10));
            var roleName = TestUtils.RandomString(10);

            using (var dbProvider = DynamoDbServerTestUtils.CreateDatabase())
            {
                var roleStore = new DynamoRoleUsersStore <DynamoIdentityRole, DynamoIdentityUser>();
                await roleStore.EnsureInitializedAsync(dbProvider.Client, dbProvider.Context);

                await roleStore.AddToRoleAsync(user, roleName, CancellationToken.None);

                var result = await roleStore.IsInRoleAsync(user, roleName, CancellationToken.None);

                Assert.True(result);

                // ACT
                await roleStore.AddToRoleAsync(user, roleName, CancellationToken.None);

                // ASSERT
                var roles = await roleStore.GetRolesAsync(user, CancellationToken.None);

                Assert.Equal(1, roles.Count);
            }
        }
        public async Task DynamoRoleUsersStore_GetUserIdsInRole_GetsUsers()
        {
            var user1    = new DynamoIdentityUser(TestUtils.RandomString(10));
            var user2    = new DynamoIdentityUser(TestUtils.RandomString(10));
            var roleName = TestUtils.RandomString(10);

            using (var dbProvider = DynamoDbServerTestUtils.CreateDatabase())
            {
                var roleStore = new DynamoRoleUsersStore <DynamoIdentityRole, DynamoIdentityUser>();
                await roleStore.EnsureInitializedAsync(dbProvider.Client, dbProvider.Context);

                await roleStore.AddToRoleAsync(user1, roleName, CancellationToken.None);

                Assert.True(await roleStore.IsInRoleAsync(user1, roleName, CancellationToken.None));

                await roleStore.AddToRoleAsync(user2, roleName, CancellationToken.None);

                Assert.True(await roleStore.IsInRoleAsync(user2, roleName, CancellationToken.None));

                // ACT
                var result = await roleStore.GetUserIdsInRoleAsync(roleName, CancellationToken.None);

                // ASSERT
                Assert.Contains(user1.Id, result);
                Assert.Contains(user2.Id, result);
                Assert.Equal(2, result.Count);
            }
        }
Ejemplo n.º 5
0
        public async Task DynamoRoleStore_Update_UpdatesRole()
        {
            var roleName = TestUtils.RandomString(10);
            var role     = new DynamoIdentityRole(roleName);

            using (var dbProvider = DynamoDbServerTestUtils.CreateDatabase())
            {
                var roleStore = new DynamoRoleStore <DynamoIdentityRole>();
                await roleStore.EnsureInitializedAsync(dbProvider.Client, dbProvider.Context);

                await roleStore.CreateAsync(role, CancellationToken.None);

                role.AddClaim(new Claim("test", "test"));

                // ACT
                await roleStore.UpdateAsync(role, CancellationToken.None);

                // ASSERT
                var result = await roleStore.FindByIdAsync(role.Id, CancellationToken.None);

                Assert.Equal(role.Id, result.Id);
                Assert.Contains("test", result.ClaimTypes);
                Assert.Contains("test", result.ClaimValues);
            }
        }
Ejemplo n.º 6
0
        public async Task DynamoRoleStore_Delete_DeletesRole()
        {
            var roleName = TestUtils.RandomString(10);
            var role     = new DynamoIdentityRole(roleName);

            using (var dbProvider = DynamoDbServerTestUtils.CreateDatabase())
            {
                var roleStore = new DynamoRoleStore <DynamoIdentityRole>();
                await roleStore.EnsureInitializedAsync(dbProvider.Client, dbProvider.Context);

                await roleStore.CreateAsync(role, CancellationToken.None);

                await Task.Delay(2000);

                role = await roleStore.FindByIdAsync(role.Id, CancellationToken.None);

                // ACT
                await roleStore.DeleteAsync(role, CancellationToken.None);

                // ASSERT
                var result = await roleStore.FindByIdAsync(role.Id, CancellationToken.None);

                Assert.Null(result);
            }
        }
Ejemplo n.º 7
0
        public async Task DynamoIdentityUser_ShouldSaveAndRetrieveTheFutureOccuranceCorrectly()
        {
            var lockoutEndDate = new DateTime(2018, 2, 1, 0, 0, 0, DateTimeKind.Utc).AddTicks(8996910);
            var user           = new DynamoIdentityUser(TestUtils.RandomString(10));

            user.LockUntil(lockoutEndDate);

            using (var dbProvider = DynamoDbServerTestUtils.CreateDatabase())
            {
                var roleUsers = new DynamoRoleUsersStore <DynamoIdentityRole, DynamoIdentityUser>();
                await roleUsers.EnsureInitializedAsync(dbProvider.Client, dbProvider.Context, TestUtils.NewTableName());

                var store = new DynamoUserStore <DynamoIdentityUser, DynamoIdentityRole>(roleUsers);
                await store.EnsureInitializedAsync(dbProvider.Client, dbProvider.Context, TestUtils.NewTableName());

                // ACT
                var result = await store.CreateAsync(user, CancellationToken.None);

                // ASSERT
                Assert.True(result.Succeeded);
                var retrievedUser = await dbProvider.Context.LoadAsync(user);

                Assert.Equal(user.LockoutEndDate, retrievedUser.LockoutEndDate);
            }
        }
Ejemplo n.º 8
0
        public async Task DynamoUserStore_ShouldPutThingsIntoUsersTableByDefault()
        {
            var user = new DynamoIdentityUser(TestUtils.RandomString(10));

            using (var dbProvider = DynamoDbServerTestUtils.CreateDatabase())
            {
                var roleStore = new DynamoRoleUsersStore <DynamoIdentityRole, DynamoIdentityUser>();
                await roleStore.EnsureInitializedAsync(dbProvider.Client, dbProvider.Context, TestUtils.NewTableName());

                var store = new DynamoUserStore <DynamoIdentityUser, DynamoIdentityRole>(roleStore);
                await store.EnsureInitializedAsync(dbProvider.Client, dbProvider.Context);

                // ACT
                var result = await store.CreateAsync(user, CancellationToken.None);

                // ASSERT
                Assert.True(result.Succeeded);
                var tableNames  = (await dbProvider.Client.ListTablesAsync()).TableNames;
                var tableExists = tableNames.Any(x => x.Equals("users", StringComparison.Ordinal));
                Assert.True(tableExists);
            }
        }
Ejemplo n.º 9
0
        public async Task DynamoRoleStore_FindByName_FindsRole()
        {
            var roleName = TestUtils.RandomString(10);
            var role     = new DynamoIdentityRole(roleName);

            Assert.Equal(roleName.ToUpper(), role.NormalizedName);

            using (var dbProvider = DynamoDbServerTestUtils.CreateDatabase())
            {
                var roleStore = new DynamoRoleStore <DynamoIdentityRole>();
                await roleStore.EnsureInitializedAsync(dbProvider.Client, dbProvider.Context);

                await roleStore.CreateAsync(role, CancellationToken.None);

                // ACT
                var result = await roleStore.FindByNameAsync(roleName.ToUpper(), CancellationToken.None);

                // ASSERT
                Assert.NotNull(result);
                Assert.Equal(roleName, result.Name);
                Assert.Equal(role.Id, result.Id);
            }
        }
Ejemplo n.º 10
0
        public async Task DynamoIdentityUser_CanBeSavedAndRetrieved_WhenItBecomesTheSubclass()
        {
            var username      = TestUtils.RandomString(10);
            var email         = TestUtils.RandomString(10);
            var countryName   = TestUtils.RandomString(10);
            var loginProvider = TestUtils.RandomString(5);
            var providerKey   = TestUtils.RandomString(5);
            var displayName   = TestUtils.RandomString(5);
            var myCustomThing = TestUtils.RandomString(10);
            var user          = new MyIdentityUser(username)
            {
                MyCustomThing = myCustomThing
            };
            var claim = new Claim(ClaimTypes.Country, countryName);

            user.AddClaim(claim);
            var login = new UserLoginInfo(loginProvider, providerKey, displayName);

            user.AddLogin(login);
            user.SetEmail(email);

            using (var dbProvider = DynamoDbServerTestUtils.CreateDatabase())
            {
                var roleUsersStore = new DynamoRoleUsersStore <DynamoIdentityRole, MyIdentityUser>();
                await roleUsersStore.EnsureInitializedAsync(dbProvider.Client, dbProvider.Context, TestUtils.NewTableName());

                var store = new DynamoUserStore <MyIdentityUser, DynamoIdentityRole>(roleUsersStore);
                await store.EnsureInitializedAsync(dbProvider.Client, dbProvider.Context, TestUtils.NewTableName());

                // ACT, ASSERT
                var result = await store.CreateAsync(user, CancellationToken.None);

                Assert.True(result.Succeeded);

                // ACT, ASSERT
                var retrievedUser = await store.FindByIdAsync(user.Id, CancellationToken.None);

                Assert.NotNull(retrievedUser);
                Assert.Equal(username, retrievedUser.UserName);
                Assert.Equal(myCustomThing, retrievedUser.MyCustomThing);

                var countryClaim = retrievedUser.GetClaims().FirstOrDefault(x => x.Type == ClaimTypes.Country);
                Assert.NotNull(countryClaim);
                Assert.Equal(countryName, countryClaim.Value);

                var retrievedLoginProvider = retrievedUser.GetLogins().FirstOrDefault(x => x.LoginProvider == loginProvider);
                Assert.NotNull(retrievedLoginProvider);
                Assert.Equal(providerKey, retrievedLoginProvider.ProviderKey);
                Assert.Equal(displayName, retrievedLoginProvider.ProviderDisplayName);

                var userByLogin = await store.FindByLoginAsync(loginProvider, providerKey, CancellationToken.None);

                Assert.NotNull(userByLogin);
                var retrivedLogins = userByLogin.GetLogins();
                Assert.NotEmpty(retrivedLogins);
                Assert.True(retrivedLogins[0].EqualsTo(login));

                var userByEmail = await store.FindByEmailAsync(user.NormalizedEmail, CancellationToken.None);

                Assert.NotNull(userByEmail);
                var retrievedEmail = userByEmail.Email.Value;
                Assert.NotEmpty(retrievedEmail);
                Assert.True(retrievedEmail.Equals(email));

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

                Assert.NotNull(userByName);
                var retrievedUserName = userByName.UserName;
                Assert.NotEmpty(retrievedUserName);
                Assert.True(retrievedUserName.Equals(username));

                var usersForClaims = await store.GetUsersForClaimAsync(claim, CancellationToken.None);

                Assert.NotNull(usersForClaims);
                Assert.NotEmpty(usersForClaims);
                var userByClaim = usersForClaims[0];
                Assert.NotNull(userByClaim);
                var userClaims = userByClaim.GetClaims();
                Assert.NotNull(userClaims);
                Assert.NotEmpty(userClaims);
                Assert.Equal(userClaims[0].Type, claim.Type);
                Assert.Equal(userClaims[0].Value, claim.Value);
            }
        }