Пример #1
0
        public async Task ShouldReturnNormalizedNameOfRole()
        {
            DocumentDbRoleStore <DocumentDbIdentityRole> store = CreateRoleStore();
            DocumentDbIdentityRole targetRole = DocumentDbIdentityRoleBuilder.Create().WithId().WithNormalizedRoleName();

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

            Assert.Equal(targetRole.NormalizedName, result);
        }
        public async Task ShouldReturnIdOfRole()
        {
            DocumentDbRoleStore <TestIdentityRole> store = CreateRoleStore();
            TestIdentityRole targetRole = DocumentDbIdentityRoleBuilder.Create().WithId();

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

            Assert.Equal(targetRole.Id, result);
        }
Пример #3
0
        public async Task ShouldSetNewNormalizedRoleName()
        {
            DocumentDbRoleStore <DocumentDbIdentityRole> store = CreateRoleStore();
            DocumentDbIdentityRole targetRole = DocumentDbIdentityRoleBuilder.Create().WithId().WithNormalizedRoleName();
            string newNormalizedRoleName      = Guid.NewGuid().ToString();

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

            Assert.Equal(targetRole.NormalizedName, newNormalizedRoleName);
        }
Пример #4
0
        public async Task ShouldRemoveClaimFromRole()
        {
            Claim claimToRemove = new Claim(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());

            DocumentDbRoleStore <DocumentDbIdentityRole> store = CreateRoleStore();
            DocumentDbIdentityRole 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));
        }
Пример #5
0
        public async Task ShouldAddClaimToRole()
        {
            Claim newClaim = new Claim(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());

            DocumentDbRoleStore <DocumentDbIdentityRole> store = CreateRoleStore();
            DocumentDbIdentityRole targetRole = DocumentDbIdentityRoleBuilder.Create().WithId();

            await store.AddClaimAsync(targetRole, newClaim);

            Assert.Contains(targetRole.Claims, c => c.Type.Equals(newClaim.Type));
        }
Пример #6
0
        public async Task ShouldThrowExceptionWhenPassingNotNormalizedNameToAddToRole()
        {
            DocumentDbUserStore <DocumentDbIdentityUser> userStore = CreateUserStore();
            DocumentDbRoleStore <DocumentDbIdentityRole> roleStore = CreateRoleStore();
            DocumentDbIdentityUser targetUser = DocumentDbIdentityUserBuilder.Create();
            DocumentDbIdentityRole 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));
        }
Пример #7
0
        public async Task ShouldThrowExceptionOnAddingUserToNonexistantRole()
        {
            DocumentDbUserStore <DocumentDbIdentityUser> userStore = CreateUserStore();
            DocumentDbRoleStore <DocumentDbIdentityRole> roleStore = CreateRoleStore();
            DocumentDbIdentityUser targetUser          = DocumentDbIdentityUserBuilder.Create();
            DocumentDbIdentityRole 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));
        }
Пример #8
0
        public async Task ShouldCreateNewRoleInDatabase()
        {
            DocumentDbIdentityRole newRole = DocumentDbIdentityRoleBuilder.Create().WithId();
            DocumentDbRoleStore <DocumentDbIdentityRole> 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
            DocumentDbIdentityRole queriedRole = await store.FindByIdAsync(newRole.Id, CancellationToken.None);

            Assert.True(result.Succeeded);
            Assert.Equal(queriedRole, newRole, new DocumentDbIdentityRoleComparer());
        }
Пример #9
0
        public async Task ShouldDeleteRoleFromDb()
        {
            DocumentDbRoleStore <DocumentDbIdentityRole> store = CreateRoleStore();
            DocumentDbIdentityRole 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);
        }
Пример #10
0
        public async Task ShouldReturnRoleByName()
        {
            DocumentDbRoleStore <DocumentDbIdentityRole> store = CreateRoleStore();
            DocumentDbIdentityRole targetRole = DocumentDbIdentityRoleBuilder.Create().WithId().WithNormalizedRoleName();

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

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

            Assert.Equal(targetRole.Id, queriedRole.Id);
        }
Пример #11
0
        public async Task ShouldAddUserToRole()
        {
            DocumentDbUserStore <DocumentDbIdentityUser> userStore = CreateUserStore();
            DocumentDbRoleStore <DocumentDbIdentityRole> roleStore = CreateRoleStore();
            DocumentDbIdentityUser targetUser = DocumentDbIdentityUserBuilder.Create();
            DocumentDbIdentityRole 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));
        }
Пример #12
0
        public async Task ShouldReturnQueriedClaimFromRole()
        {
            string firstClaimType  = Guid.NewGuid().ToString();
            string secondClaimType = Guid.NewGuid().ToString();
            string thirdClaimType  = Guid.NewGuid().ToString();

            DocumentDbRoleStore <DocumentDbIdentityRole> store = CreateRoleStore();
            DocumentDbIdentityRole 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));
        }
Пример #13
0
        public async Task ShouldUpdateExistingRoleInDatabase()
        {
            DocumentDbRoleStore <DocumentDbIdentityRole> store = CreateRoleStore();
            DocumentDbIdentityRole 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
            DocumentDbIdentityRole queriedRole = await store.FindByIdAsync(existingRole.Id, CancellationToken.None);

            Assert.True(result.Succeeded);
            Assert.Equal(existingRole, queriedRole, new DocumentDbIdentityRoleComparer());
        }