コード例 #1
0
        public async Task ShouldReturnAllUsersWithAdminRole()
        {
            TestIdentityRole adminRole = DocumentDbIdentityRoleBuilder.Create().WithNormalizedRoleName();

            DocumentDbUserStore <TestIdentityUser, TestIdentityRole> store = CreateUserStore();

            TestIdentityUser firstAdmin  = DocumentDbIdentityUserBuilder.Create().WithId().WithNormalizedUserName().AddRole(adminRole).AddRole();
            TestIdentityUser secondAdmin = DocumentDbIdentityUserBuilder.Create().WithId().WithNormalizedUserName().AddRole(adminRole).AddRole().AddRole();
            TestIdentityUser thirdAdmin  = DocumentDbIdentityUserBuilder.Create().WithId().WithNormalizedUserName().AddRole(adminRole);

            CreateDocument(firstAdmin);
            CreateDocument(secondAdmin);
            CreateDocument(DocumentDbIdentityUserBuilder.Create().AddRole().AddRole());
            CreateDocument(DocumentDbIdentityUserBuilder.Create().AddRole().AddRole().AddRole());
            CreateDocument(thirdAdmin);
            CreateDocument(DocumentDbIdentityUserBuilder.Create());
            CreateDocument(DocumentDbIdentityUserBuilder.Create().AddRole());

            IList <TestIdentityUser> adminUsers = await store.GetUsersInRoleAsync(adminRole.NormalizedName, CancellationToken.None);

            Assert.Collection(
                adminUsers,
                u => u.Id.Equals(firstAdmin.Id),
                u => u.Id.Equals(secondAdmin.Id),
                u => u.Id.Equals(thirdAdmin.Id));
        }
コード例 #2
0
        public async Task ShouldReturnNormalizedNameOfRole()
        {
            DocumentDbRoleStore <TestIdentityRole> store = CreateRoleStore();
            TestIdentityRole targetRole = DocumentDbIdentityRoleBuilder.Create().WithId().WithNormalizedRoleName();

            string result = await store.GetNormalizedRoleNameAsync(targetRole, CancellationToken.None);

            Assert.Equal(targetRole.NormalizedName, result);
        }
コード例 #3
0
        public async Task ShouldReturnUserIsInRole()
        {
            var userStore = CreateUserStore();
            TestIdentityRole targetRole = DocumentDbIdentityRoleBuilder.Create().WithNormalizedRoleName();
            TestIdentityUser targetUser = DocumentDbIdentityUserBuilder.Create().AddRole(targetRole).AddRole();

            bool isInRole = await userStore.IsInRoleAsync(targetUser, targetRole.NormalizedName, CancellationToken.None);

            Assert.True(isInRole);
        }
コード例 #4
0
        public async Task ShouldSetNewNormalizedRoleName()
        {
            DocumentDbRoleStore <TestIdentityRole> store = CreateRoleStore();
            TestIdentityRole targetRole            = DocumentDbIdentityRoleBuilder.Create().WithId().WithNormalizedRoleName();
            string           newNormalizedRoleName = Guid.NewGuid().ToString();

            await store.SetNormalizedRoleNameAsync(targetRole, newNormalizedRoleName, CancellationToken.None);

            Assert.Equal(targetRole.NormalizedName, newNormalizedRoleName);
        }
コード例 #5
0
        public async Task ShouldRemoveClaimFromRole()
        {
            Claim claimToRemove = new Claim(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());

            DocumentDbRoleStore <TestIdentityRole> store = CreateRoleStore();
            TestIdentityRole targetRole = DocumentDbIdentityRoleBuilder.Create().WithId().AddClaim().AddClaim(claimToRemove).AddClaim();

            await store.RemoveClaimAsync(targetRole, claimToRemove, CancellationToken.None);

            Assert.DoesNotContain(targetRole.Claims, c => c.Type.Equals(claimToRemove.Type));
        }
コード例 #6
0
        public async Task ShouldAddClaimToRole()
        {
            Claim newClaim = new Claim(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());

            var store = CreateRoleStore();
            TestIdentityRole targetRole = DocumentDbIdentityRoleBuilder.Create().WithId();

            await store.AddClaimAsync(targetRole, newClaim);

            Assert.Contains(targetRole.Claims, c => c.Type.Equals(newClaim.Type));
        }
コード例 #7
0
        public async Task ShouldReturnUserIsNotInRoleWhenPassingNotNormalizedRoleNameToIsInRole()
        {
            var userStore = CreateUserStore();
            TestIdentityRole targetRole = DocumentDbIdentityRoleBuilder.Create().WithNormalizedRoleName();
            TestIdentityUser targetUser = DocumentDbIdentityUserBuilder.Create().AddRole(targetRole).AddRole();

            // Pass not normalized name which should lead to not locating the target role
            bool isInRole = await userStore.IsInRoleAsync(targetUser, targetRole.Name, CancellationToken.None);

            Assert.False(isInRole);
        }
コード例 #8
0
        public async Task ShouldReturnNoUsersWithAdminRoleWhenPassingNotNormalizedRoleNameToGetUsersInRole()
        {
            TestIdentityRole adminRole = DocumentDbIdentityRoleBuilder.Create().WithNormalizedRoleName();
            DocumentDbUserStore <TestIdentityUser, TestIdentityRole> store = CreateUserStore();
            TestIdentityUser firstAdmin = DocumentDbIdentityUserBuilder.Create().WithId().WithNormalizedUserName().AddRole(adminRole).AddRole();

            CreateDocument(firstAdmin);
            CreateDocument(DocumentDbIdentityUserBuilder.Create().AddRole().AddRole());

            IList <TestIdentityUser> adminUsers = await store.GetUsersInRoleAsync(adminRole.Name, CancellationToken.None);

            Assert.Empty(adminUsers);
        }
コード例 #9
0
        public async Task ShouldThrowExceptionWhenPassingNotNormalizedNameToAddToRole()
        {
            DocumentDbUserStore <TestIdentityUser, TestIdentityRole> userStore = CreateUserStore();
            DocumentDbRoleStore <TestIdentityRole> roleStore = CreateRoleStore();
            TestIdentityUser targetUser = DocumentDbIdentityUserBuilder.Create();
            TestIdentityRole targetRole = DocumentDbIdentityRoleBuilder.Create().WithId().WithNormalizedRoleName();

            // Create sample data role
            await roleStore.CreateAsync(targetRole, CancellationToken.None);

            // Add the user to the created role, but pass the not normalized name, expecting an exception
            await Assert.ThrowsAsync(typeof(ArgumentException), async() => await userStore.AddToRoleAsync(targetUser, targetRole.Name, CancellationToken.None));
        }
コード例 #10
0
        public async Task ShouldThrowExceptionOnAddingUserToNonexistantRole()
        {
            DocumentDbUserStore <TestIdentityUser, TestIdentityRole> userStore = CreateUserStore();
            DocumentDbRoleStore <TestIdentityRole> roleStore = CreateRoleStore();
            TestIdentityUser targetUser          = DocumentDbIdentityUserBuilder.Create();
            TestIdentityRole someNotTargetedRole = DocumentDbIdentityRoleBuilder.Create().WithId().WithNormalizedRoleName();

            // Create a role so there is a differently named role in the store
            await roleStore.CreateAsync(someNotTargetedRole, CancellationToken.None);

            // Add the user to a role name different than the role created before, expecting an exception
            await Assert.ThrowsAsync(typeof(ArgumentException), async() => await userStore.AddToRoleAsync(targetUser, "NotExistantRole", CancellationToken.None));
        }
コード例 #11
0
        public async Task ShouldCreateNewRoleInDatabase()
        {
            TestIdentityRole newRole = DocumentDbIdentityRoleBuilder.Create().WithId();
            var store = CreateRoleStore();

            // Create the new role
            IdentityResult result = store.CreateAsync(newRole, CancellationToken.None).Result;

            // Get it again from the DB to check if it was created correctly
            IdentityRole queriedRole = await store.FindByIdAsync(newRole.Id, CancellationToken.None);

            Assert.True(result.Succeeded);
            Assert.Equal(queriedRole, newRole, new DocumentDbIdentityRoleComparer());
        }
コード例 #12
0
        public async Task ShouldRemoveRoleFromUser()
        {
            DocumentDbUserStore <TestIdentityUser, TestIdentityRole> userStore = CreateUserStore();
            TestIdentityRole firstRole  = DocumentDbIdentityRoleBuilder.Create().WithNormalizedRoleName();
            TestIdentityRole secondRole = DocumentDbIdentityRoleBuilder.Create().WithNormalizedRoleName();
            TestIdentityUser targetUser = DocumentDbIdentityUserBuilder.Create().AddRole(firstRole).AddRole(secondRole);

            // Remove the second role
            await userStore.RemoveFromRoleAsync(targetUser, secondRole.NormalizedName, CancellationToken.None);

            // Assert second role has been removed while first one is still there
            Assert.DoesNotContain(targetUser.Roles, r => r.NormalizedName.Equals(secondRole.NormalizedName));
            Assert.Contains(targetUser.Roles, r => r.NormalizedName.Equals(firstRole.NormalizedName));
        }
コード例 #13
0
        public async Task ShouldDeleteRoleFromDb()
        {
            DocumentDbRoleStore <TestIdentityRole> store = CreateRoleStore();
            TestIdentityRole targetRole = DocumentDbIdentityRoleBuilder.Create().WithId();

            // Create sample data in DB
            CreateDocument(DocumentDbIdentityRoleBuilder.Create());
            CreateDocument(DocumentDbIdentityRoleBuilder.Create());
            CreateDocument(targetRole);
            CreateDocument(DocumentDbIdentityRoleBuilder.Create());

            IdentityResult result = await store.DeleteAsync(targetRole, CancellationToken.None);

            Assert.True(result.Succeeded);
        }
コード例 #14
0
        public async Task ShouldNotRemoveRoleFromUserWhenPassingNotNormalizedRoleNameToRemoveFromRole()
        {
            var userStore = CreateUserStore();
            TestIdentityRole firstRole  = DocumentDbIdentityRoleBuilder.Create().WithNormalizedRoleName();
            TestIdentityRole secondRole = DocumentDbIdentityRoleBuilder.Create().WithNormalizedRoleName();
            TestIdentityUser targetUser = DocumentDbIdentityUserBuilder.Create().AddRole(firstRole).AddRole(secondRole);

            // Try remove the second role with a not normalized role name
            await userStore.RemoveFromRoleAsync(targetUser, secondRole.Name, CancellationToken.None);

            // Assert both roles are still here, as lookup without normalized name should have failed
            Assert.Collection(targetUser.Roles,
                              r => r.NormalizedName.Equals(firstRole.NormalizedName),
                              r => r.NormalizedName.Equals(secondRole.NormalizedName));
        }
コード例 #15
0
        public async Task ShouldAddUserToRole()
        {
            DocumentDbUserStore <TestIdentityUser, TestIdentityRole> userStore = CreateUserStore();
            DocumentDbRoleStore <TestIdentityRole> roleStore = CreateRoleStore();
            TestIdentityUser targetUser = DocumentDbIdentityUserBuilder.Create();
            TestIdentityRole targetRole = DocumentDbIdentityRoleBuilder.Create("RoleName").WithId().WithNormalizedRoleName();

            // Create sample data role
            await roleStore.CreateAsync(targetRole, CancellationToken.None);

            // Add the created sample data role to the user
            await userStore.AddToRoleAsync(targetUser, targetRole.NormalizedName, CancellationToken.None);

            Assert.Contains(targetUser.Roles, r => r.NormalizedName.Equals(targetRole.NormalizedName));
        }
コード例 #16
0
        public async Task ShouldReturnRoleByName()
        {
            DocumentDbRoleStore <TestIdentityRole> store = CreateRoleStore();
            TestIdentityRole targetRole = DocumentDbIdentityRoleBuilder.Create().WithId().WithNormalizedRoleName();

            // Create sample data in DB
            CreateDocument(DocumentDbIdentityRoleBuilder.Create().WithNormalizedRoleName());
            CreateDocument(DocumentDbIdentityRoleBuilder.Create().WithNormalizedRoleName());
            CreateDocument(targetRole);
            CreateDocument(DocumentDbIdentityRoleBuilder.Create().WithNormalizedRoleName());

            IdentityRole queriedRole = await store.FindByNameAsync(targetRole.NormalizedName, CancellationToken.None);

            Assert.Equal(targetRole.Id, queriedRole.Id);
        }
コード例 #17
0
        public async Task ShouldReturnQueriedClaimFromRole()
        {
            string firstClaimType  = Guid.NewGuid().ToString();
            string secondClaimType = Guid.NewGuid().ToString();
            string thirdClaimType  = Guid.NewGuid().ToString();

            DocumentDbRoleStore <TestIdentityRole> store = CreateRoleStore();
            TestIdentityRole targetRole = DocumentDbIdentityRoleBuilder.Create().WithId().AddClaim(firstClaimType).AddClaim(secondClaimType).AddClaim(thirdClaimType);

            IList <Claim> returnedClaims = await store.GetClaimsAsync(targetRole, CancellationToken.None);

            Assert.Collection(
                returnedClaims,
                c => c.Type.Equals(firstClaimType),
                c => c.Type.Equals(secondClaimType),
                c => c.Type.Equals(thirdClaimType));
        }
コード例 #18
0
        public async Task ShouldUpdateExistingRoleInDatabase()
        {
            DocumentDbRoleStore <TestIdentityRole> store = CreateRoleStore();
            TestIdentityRole existingRole = DocumentDbIdentityRoleBuilder.Create().WithId();

            // Create sample data in DB
            CreateDocument(existingRole);

            // Change property to upate on sample data and call the update mehtod
            existingRole.Name = Guid.NewGuid().ToString();
            IdentityResult result = await store.UpdateAsync(existingRole, CancellationToken.None);

            // Get it again from the DB to check if it was created correctly
            IdentityRole queriedRole = await store.FindByIdAsync(existingRole.Id, CancellationToken.None);

            Assert.True(result.Succeeded);
            Assert.Equal(existingRole, queriedRole, new DocumentDbIdentityRoleComparer());
        }
コード例 #19
0
        public DocumentDbIdentityUserBuilder AddRole(TestIdentityRole role = null)
        {
            if (role == null)
            {
                LookupNormalizer normalizer  = new LookupNormalizer();
                string           newRoleName = Guid.NewGuid().ToString().ToUpper();

                role = new TestIdentityRole
                {
                    Id             = Guid.NewGuid().ToString().ToUpper(),
                    Name           = newRoleName,
                    NormalizedName = normalizer.Normalize(newRoleName)
                };
            }
            else
            {
                identityUser.Roles.Add(role);
            }

            return(this);
        }
コード例 #20
0
 public DocumentDbIdentityRoleBuilder(TestIdentityRole identityRole)
 {
     this.identityRole = identityRole;
 }
コード例 #21
0
 protected void CreateDocument(TestIdentityRole role)
 {
     _repoRoles.Create(role);
 }