Ejemplo n.º 1
0
        public async Task ShouldReturnAllUsersWithAdminRole()
        {
            DocumentDbIdentityRole adminRole = DocumentDbIdentityRoleBuilder.Create().WithNormalizedRoleName();

            DocumentDbUserStore <DocumentDbIdentityUser> store = CreateUserStore();

            DocumentDbIdentityUser <DocumentDbIdentityRole> firstAdmin  = DocumentDbIdentityUserBuilder.Create().WithId().WithNormalizedUserName().AddRole(adminRole).AddRole();
            DocumentDbIdentityUser <DocumentDbIdentityRole> secondAdmin = DocumentDbIdentityUserBuilder.Create().WithId().WithNormalizedUserName().AddRole(adminRole).AddRole().AddRole();
            DocumentDbIdentityUser <DocumentDbIdentityRole> 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 <DocumentDbIdentityUser> 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));
        }
Ejemplo n.º 2
0
 async Task AddFacultyRoleAsync()
 {
     DocumentDbIdentityRole role = new DocumentDbIdentityRole()
     {
         Name = config["DataStore:Containers:Identities:Roles:Faculty:Name"]
     };
     await roleManager.CreateAsync(role);
 }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        public async Task ShouldThrowExceptionOnAddingUserToNonexistantRole()
        {
            DocumentDbUserStore <DocumentDbIdentityUser <DocumentDbIdentityRole>, DocumentDbIdentityRole> userStore = CreateUserStore();
            DocumentDbIdentityUser <DocumentDbIdentityRole> targetUser = DocumentDbIdentityUserBuilder.Create();
            DocumentDbIdentityRole targetRole = DocumentDbIdentityRoleBuilder.Create().WithId().WithNormalizedRoleName();
            string roleName = Guid.NewGuid().ToString();

            // Add the created sample data role to the user
            await Assert.ThrowsAsync(typeof(ArgumentException), async() => await userStore.AddToRoleAsync(targetUser, targetRole.Name, CancellationToken.None));
        }
Ejemplo n.º 5
0
        public async Task ShouldReturnUserIsInRole()
        {
            DocumentDbUserStore <DocumentDbIdentityUser> userStore = CreateUserStore();
            DocumentDbIdentityRole targetRole = DocumentDbIdentityRoleBuilder.Create().WithNormalizedRoleName();
            DocumentDbIdentityUser targetUser = DocumentDbIdentityUserBuilder.Create().AddRole(targetRole).AddRole();

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

            Assert.True(isInRole);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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));
        }
Ejemplo n.º 8
0
        public async Task ShouldReturnUserIsInRole()
        {
            DocumentDbIdentityRole role = DocumentDbIdentityRoleBuilder.Create();

            DocumentDbUserStore <DocumentDbIdentityUser <DocumentDbIdentityRole>, DocumentDbIdentityRole> store = CreateUserStore();
            DocumentDbIdentityUser <DocumentDbIdentityRole> user = DocumentDbIdentityUserBuilder.Create().WithId().AddRole(role).AddRole().AddRole();

            bool result = await store.IsInRoleAsync(user, role.Name, CancellationToken.None);

            Assert.True(result);
        }
Ejemplo n.º 9
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));
        }
Ejemplo n.º 10
0
        public async Task ShouldReturnUserIsNotInRoleWhenPassingNotNormalizedRoleNameToIsInRole()
        {
            DocumentDbUserStore <DocumentDbIdentityUser> userStore = CreateUserStore();
            DocumentDbIdentityRole targetRole = DocumentDbIdentityRoleBuilder.Create().WithNormalizedRoleName();
            DocumentDbIdentityUser 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);
        }
Ejemplo n.º 11
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));
        }
Ejemplo n.º 12
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));
        }
Ejemplo n.º 13
0
        public async Task ShouldReturnNoUsersWithAdminRoleWhenPassingNotNormalizedRoleNameToGetUsersInRole()
        {
            DocumentDbIdentityRole adminRole = DocumentDbIdentityRoleBuilder.Create().WithNormalizedRoleName();
            DocumentDbUserStore <DocumentDbIdentityUser>    store      = CreateUserStore();
            DocumentDbIdentityUser <DocumentDbIdentityRole> firstAdmin = DocumentDbIdentityUserBuilder.Create().WithId().WithNormalizedUserName().AddRole(adminRole).AddRole();

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

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

            Assert.Empty(adminUsers);
        }
Ejemplo n.º 14
0
        public async Task ShouldRemoveRoleFromUser()
        {
            DocumentDbUserStore <DocumentDbIdentityUser> userStore = CreateUserStore();
            DocumentDbIdentityRole firstRole  = DocumentDbIdentityRoleBuilder.Create().WithNormalizedRoleName();
            DocumentDbIdentityRole secondRole = DocumentDbIdentityRoleBuilder.Create().WithNormalizedRoleName();
            DocumentDbIdentityUser 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));
        }
Ejemplo n.º 15
0
        public DocumentDbIdentityUserBuilder AddRole(DocumentDbIdentityRole role = null)
        {
            if (role == null)
            {
                role = new DocumentDbIdentityRole()
                {
                    Id   = Guid.NewGuid().ToString(),
                    Name = Guid.NewGuid().ToString()
                };
            }

            identityUser.Roles.Add(role);
            return(this);
        }
Ejemplo n.º 16
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());
        }
Ejemplo n.º 17
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);
        }
Ejemplo n.º 18
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);
        }
Ejemplo n.º 19
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));
        }
Ejemplo n.º 20
0
        public async Task ShouldNotRemoveRoleFromUserWhenPassingNotNormalizedRoleNameToRemoveFromRole()
        {
            DocumentDbUserStore <DocumentDbIdentityUser> userStore = CreateUserStore();
            DocumentDbIdentityRole firstRole  = DocumentDbIdentityRoleBuilder.Create().WithNormalizedRoleName();
            DocumentDbIdentityRole secondRole = DocumentDbIdentityRoleBuilder.Create().WithNormalizedRoleName();
            DocumentDbIdentityUser 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));
        }
Ejemplo n.º 21
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));
        }
Ejemplo n.º 22
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());
        }
Ejemplo n.º 23
0
        public DocumentDbIdentityUserBuilder AddRole(DocumentDbIdentityRole role = null)
        {
            if (role == null)
            {
                LookupNormalizer normalizer  = new LookupNormalizer();
                string           newRoleName = Guid.NewGuid().ToString().ToUpper();

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

            return(this);
        }
 public DocumentDbIdentityRoleBuilder(DocumentDbIdentityRole identityRole)
 {
     this.identityRole = identityRole;
 }