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);
            }
        }
Exemplo n.º 2
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_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);
            }
        }
        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);
            }
        }
Exemplo n.º 5
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);
            }
        }
Exemplo n.º 6
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);
            }
        }
Exemplo n.º 7
0
        public Program(ILoggerFactory loggerFactory,
                       DynamoUserStore <DynamoIdentityUser, DynamoIdentityRole> userStore,
                       DynamoRoleStore <DynamoIdentityRole> roleStore,
                       DynamoRoleUsersStore <DynamoIdentityRole, DynamoIdentityUser> roleUsersStore,
                       ILookupNormalizer keyNormalizer,
                       string dbUrl,
                       string regionName,
                       string tableNamePrefix)
        {
            this.logger         = loggerFactory.CreateLogger <Program>();
            this.userStore      = userStore;
            this.roleStore      = roleStore;
            this.roleUsersStore = roleUsersStore;
            this.keyNormalizer  = keyNormalizer;

            var dbConfig = new AmazonDynamoDBConfig
            {
                ServiceURL = dbUrl
            };

            if (regionName != null)
            {
                var region = RegionEndpoint.GetBySystemName(regionName);
                dbConfig.RegionEndpoint = region;
            }

            var client = new AmazonDynamoDBClient(dbConfig);

            var contextConfig = new DynamoDBContextConfig
            {
                TableNamePrefix = tableNamePrefix
            };

            var context = new DynamoDBContext(client, contextConfig);

            var prefix = tableNamePrefix ?? "";

            roleUsersTableName = $"{prefix}roleUsers";
            usersTableName     = $"{prefix}users";
            rolesTableName     = $"{prefix}roles";

            var tables = client.ListTablesAsync().Result;

            if (!tables.TableNames.Contains(usersTableName))
            {
                throw new Exception($"can't find table {usersTableName}");
            }

            if (!tables.TableNames.Contains(rolesTableName))
            {
                throw new Exception($"can't find table {rolesTableName}");
            }

            if (!tables.TableNames.Contains(roleUsersTableName))
            {
                throw new Exception($"can't find table {roleUsersTableName}");
            }

            roleUsersStore.EnsureInitializedAsync(client, context, roleUsersTableName).Wait();
            userStore.EnsureInitializedAsync(client, context, usersTableName).Wait();
            roleStore.EnsureInitializedAsync(client, context, rolesTableName).Wait();
        }
Exemplo n.º 8
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);
            }
        }