Exemple #1
0
            public async Task Role_has_multiple_claims_with_same_ClaimType_removing_only_removes_cliam_with_same_value()
            {
                // arrange
                var claim1         = new Claim("ClaimType", "some value");
                var claim2         = new Claim(claim1.Type, "some other value");
                var identityClaim1 = new IdentityClaim(claim1);
                var identityClaim2 = new IdentityClaim(claim2);

                var role = new IdentityRole("Role_has_multiple_claims_with_same_ClaimType_removing_only_removes_cliam_with_same_value");

                role.Claims.Add(identityClaim1);
                role.Claims.Add(identityClaim2);
                await _roleStore.CreateAsync(role);

                // act
                await _roleStore.RemoveClaimAsync(role, claim1);

                // assert

                // check role claims from memory
                IdentityClaimAssert.Equal(new List <IdentityClaim> {
                    identityClaim2
                }, role.Claims);

                // check role claims from DB
                var roleFromDb = await _roleCollection.Find(x => x.Id == role.Id).SingleOrDefaultAsync();

                IdentityClaimAssert.Equal(new List <IdentityClaim> {
                    identityClaim2
                }, roleFromDb.Claims);
            }
Exemple #2
0
            public async Task Removing_unknown_claim_does_not_change_database_role_record()
            {
                // arrange
                var identityClaim = new IdentityClaim {
                    ClaimType = "claim type", ClaimValue = "some value"
                };
                var role = new IdentityRole("Removing_unknown_claim_does_not_change_database_role_record");

                role.Claims.Add(identityClaim);

                await _roleStore.CreateAsync(role);

                // act
                var claim = new Claim("other type", "some other value");
                await _roleStore.RemoveClaimAsync(role, claim);

                // assert

                // check role claims from memory
                IdentityClaimAssert.Equal(new List <IdentityClaim> {
                    identityClaim
                }, role.Claims);

                // check role claims from DB
                var roleFromDb = await _roleCollection.Find(x => x.Id == role.Id).SingleOrDefaultAsync();

                IdentityClaimAssert.Equal(new List <IdentityClaim> {
                    identityClaim
                }, roleFromDb.Claims);
            }
Exemple #3
0
            public async Task Remove_existing_claim_different_casing_updates_database_role_record()
            {
                // arrange
                var claim1         = new Claim("ClaimType", "some value");
                var claim2         = new Claim("ClaimType2", "some other value");
                var identityClaim1 = new IdentityClaim {
                    ClaimType = claim1.Type.ToUpper(), ClaimValue = claim1.Value.ToUpper()
                };
                var identityClaim2 = new IdentityClaim(claim2);

                var role = new IdentityRole("Remove_existing_claim_updates_database_role_record");

                role.Claims.Add(identityClaim1);
                role.Claims.Add(identityClaim2);
                await _roleStore.CreateAsync(role);

                // act
                await _roleStore.RemoveClaimAsync(role, claim1);

                // assert

                // check role claims from memory
                IdentityClaimAssert.Equal(new List <IdentityClaim> {
                    identityClaim2
                }, role.Claims);

                // check role claims from DB
                var roleFromDb = await _roleCollection.Find(x => x.Id == role.Id).SingleOrDefaultAsync();

                IdentityClaimAssert.Equal(new List <IdentityClaim> {
                    identityClaim2
                }, roleFromDb.Claims);
            }
Exemple #4
0
            public async Task Adding_existing_claim_to_role_does_not_update_database_role_record()
            {
                // arrange
                var claim = new Claim("ClaimType", "some value");

                var role          = new IdentityRole("Adding_existing_claim_to_role_does_not_update_database_role_record");
                var identityClaim = new IdentityClaim(claim);

                role.Claims.Add(identityClaim);
                await _roleStore.CreateAsync(role);

                // act
                await _roleStore.AddClaimAsync(role, claim);

                // assert

                // check role claims from memory
                IdentityClaimAssert.Equal(new List <IdentityClaim> {
                    identityClaim
                }, role.Claims);

                // check role claims from DB
                var roleFromDb = await _roleCollection.Find(x => x.Id == role.Id).SingleOrDefaultAsync();

                IdentityClaimAssert.Equal(new List <IdentityClaim> {
                    identityClaim
                }, roleFromDb.Claims);
            }
Exemple #5
0
            public async Task Adding_multiple_claims_with_same_ClaimType_adds_multiple_claims_to_database()
            {
                // arrange
                var claim1 = new Claim("ClaimType", "some value");
                var claim2 = new Claim(claim1.Type, "some other value");

                var role = new IdentityRole("Adding_multiple_claims_with_same_ClaimType_adds_multiple_claims_to_database");
                await _roleStore.CreateAsync(role);

                // act
                await _roleStore.AddClaimAsync(role, claim1);

                await _roleStore.AddClaimAsync(role, claim2);

                // assert
                var identityClaim1 = new IdentityClaim(claim1);
                var identityClaim2 = new IdentityClaim(claim2);

                // check role claims from memory
                IdentityClaimAssert.Equal(new List <IdentityClaim> {
                    identityClaim1, identityClaim2
                }, role.Claims);

                // check role claims from DB
                var roleFromDb = await _roleCollection.Find(x => x.Id == role.Id).SingleOrDefaultAsync();

                IdentityClaimAssert.Equal(new List <IdentityClaim> {
                    identityClaim1, identityClaim2
                }, roleFromDb.Claims);
            }
                public async Task User_has_multiple_claims_with_same_ClaimType_removing_only_removes_claim_with_same_value()
                {
                    // arrange
                    var user =
                        new IdentityUser("User_has_multiple_claims_with_same_ClaimType_removing_only_removes_claim_with_same_value");

                    user.Claims.Add(_identityClaim1);
                    user.Claims.Add(_identityClaim1SameType);
                    await _userStore.CreateAsync(user);

                    // act
                    await _userStore.RemoveClaimsAsync(user, new List <Claim> {
                        _claim1
                    });

                    // assert

                    // check user claims from memory
                    IdentityClaimAssert.Equal(new List <IdentityClaim> {
                        _identityClaim1SameType
                    }, user.Claims);

                    // check user claims from DB
                    var userFromDb = await _userCollection.Find(x => x.Id == user.Id).SingleOrDefaultAsync();

                    IdentityClaimAssert.Equal(new List <IdentityClaim> {
                        _identityClaim1SameType
                    }, userFromDb.Claims);
                }
                public async Task Remove_multiple_existing_claims_updates_database_user_record()
                {
                    // arrange
                    var user = new IdentityUser("Remove_multiple_existing_claims_updates_database_user_record");

                    user.Claims.Add(_identityClaim1);
                    user.Claims.Add(_identityClaim2);
                    user.Claims.Add(_identityClaim3);
                    await _userStore.CreateAsync(user);

                    // act
                    await _userStore.RemoveClaimsAsync(user, new List <Claim> {
                        _claim1, _claim3
                    });

                    // assert

                    // check user claims from memory
                    IdentityClaimAssert.Equal(new List <IdentityClaim> {
                        _identityClaim2
                    }, user.Claims);

                    // check user claims from DB
                    var userFromDb = await _userCollection.Find(x => x.Id == user.Id).SingleOrDefaultAsync();

                    IdentityClaimAssert.Equal(new List <IdentityClaim> {
                        _identityClaim2
                    }, userFromDb.Claims);
                }
                public async Task Adding_multiple_claims_with_same_ClaimType_adds_multiple_claims_to_database()
                {
                    // arrange
                    var user = new IdentityUser("Adding_multiple_claims_with_same_ClaimType_adds_multiple_claims_to_database");
                    await _userStore.CreateAsync(user);

                    // act
                    await _userStore.AddClaimsAsync(user, new List <Claim> {
                        _claim1
                    });

                    await _userStore.AddClaimsAsync(user, new List <Claim> {
                        _claim1SameType
                    });

                    // assert
                    // check user claims from memory
                    IdentityClaimAssert.Equal(new List <IdentityClaim> {
                        _identityClaim1, _identityClaim1SameType
                    }, user.Claims);

                    // check user claims from DB
                    var userFromDb = await _userCollection.Find(x => x.Id == user.Id).SingleOrDefaultAsync();

                    IdentityClaimAssert.Equal(new List <IdentityClaim> {
                        _identityClaim1, _identityClaim1SameType
                    }, userFromDb.Claims);
                }
                public async Task Adding_new_claim_to_user_with_null_claims_updates_database_user_record()
                {
                    // arrange

                    var user = new IdentityUser("Adding_new_claim_to_user_with_null_claims_updates_database_user_record");

                    user.Claims = null;

                    await _userStore.CreateAsync(user);

                    // act
                    await _userStore.AddClaimsAsync(user, new List <Claim> {
                        _claim1, _claim2
                    });

                    // assert

                    // check user claims from memory
                    IdentityClaimAssert.Equal(new List <IdentityClaim> {
                        _identityClaim1, _identityClaim2
                    }, user.Claims);

                    // check user claims from DB
                    var userFromDb = await _userCollection.Find(x => x.Id == user.Id).SingleOrDefaultAsync();

                    IdentityClaimAssert.Equal(new List <IdentityClaim> {
                        _identityClaim1, _identityClaim2
                    }, userFromDb.Claims);
                }
                public async Task Matching_claims_are_replaced_and_updated_database()
                {
                    // arrange
                    var user = new IdentityUser("Matching_claims_are_replaced_and_updated_database");

                    user.Claims.Add(_identityClaim1);
                    user.Claims.Add(_identityClaim2);

                    await _userStore.CreateAsync(user);

                    // act
                    await _userStore.ReplaceClaimAsync(user, _claim1, _claim3);

                    // assert

                    // check user claims from memory
                    IdentityClaimAssert.Equal(new List <IdentityClaim> {
                        _identityClaim3, _identityClaim2
                    }, user.Claims);

                    // check user claims from DB
                    var userFromDb = await _userCollection.Find(x => x.Id == user.Id).SingleOrDefaultAsync();

                    IdentityClaimAssert.Equal(new List <IdentityClaim> {
                        _identityClaim3, _identityClaim2
                    }, userFromDb.Claims);
                }
Exemple #11
0
        public static void Equal(IdentityRole expected, IdentityRole actual)
        {
            Assert.True((expected == null && actual == null) || (expected != null && actual != null));

            Assert.Equal(expected.Id, actual.Id);
            Assert.Equal(expected.Name, actual.Name);
            Assert.Equal(expected.NormalizedName, actual.NormalizedName);

            IdentityClaimAssert.Equal(expected.Claims, actual.Claims);
        }
Exemple #12
0
        public static void Equal(IdentityUser expected, IdentityUser actual)
        {
            Assert.True((expected == null && actual == null) || (expected != null && actual != null));

            Assert.Equal(expected.Id, actual.Id);
            Assert.Equal(expected.UserName, actual.UserName);
            Assert.Equal(expected.NormalizedUserName, actual.NormalizedUserName);
            Assert.Equal(expected.Email, actual.Email);
            Assert.Equal(expected.NormalizedEmail, actual.NormalizedEmail);
            Assert.Equal(expected.EmailConfirmed, actual.EmailConfirmed);
            Assert.Equal(expected.PasswordHash, actual.PasswordHash);
            Assert.Equal(expected.SecurityStamp, actual.SecurityStamp);
            Assert.Equal(expected.PhoneNumber, actual.PhoneNumber);
            Assert.Equal(expected.PhoneNumberConfirmed, actual.PhoneNumberConfirmed);
            Assert.Equal(expected.TwoFactorEnabled, actual.TwoFactorEnabled);
            Assert.Equal(expected.LockoutEnd, actual.LockoutEnd);
            Assert.Equal(expected.LockoutEnabled, actual.LockoutEnabled);
            Assert.Equal(expected.AccessFailedCount, actual.AccessFailedCount);

            IdentityRoleAssert.Equal(expected.Roles.Cast <IdentityRole>(), actual.Roles.Cast <IdentityRole>());
            IdentityClaimAssert.Equal(expected.Claims, actual.Claims);
            IdentityClaimAssert.Equal(expected.AllClaims, actual.AllClaims);
            UserLoginInfoAssert.Equal(expected.Logins, actual.Logins);
        }
Exemple #13
0
            public async Task When_User_has_Role_with_claims_and_claim_removed_from_Role_should_remove_cliam_from_User_Role_as_well()
            {
                // arrange
                var identityClaim1 = new IdentityClaim {
                    ClaimType = "ClaimType1", ClaimValue = "val"
                };
                var identityClaim2 = new IdentityClaim {
                    ClaimType = "ClaimType2", ClaimValue = "some val"
                };
                var role1 = new IdentityRole {
                    Name = "Role1", Claims = new List <IdentityClaim> {
                        identityClaim1, identityClaim2
                    }
                };
                var role2 = new IdentityRole {
                    Name = "Role2", Claims = new List <IdentityClaim> {
                        identityClaim1
                    }
                };

                await _roleStore.CreateAsync(role1);

                await _roleStore.CreateAsync(role2);

                var user1 = new IdentityUser {
                    UserName = "******", Roles = new List <IdentityRole <string> > {
                        role1, role2
                    }
                };
                var user2 = new IdentityUser {
                    UserName = "******", Roles = new List <IdentityRole <string> > {
                        role1
                    }
                };
                var user3 = new IdentityUser {
                    UserName = "******", Roles = new List <IdentityRole <string> > {
                        role2
                    }
                };

                await _userStore.CreateAsync(user1);

                await _userStore.CreateAsync(user2);

                await _userStore.CreateAsync(user3);

                // act
                await _roleStore.RemoveClaimAsync(role1, new Claim(identityClaim1.ClaimType, identityClaim1.ClaimValue));

                // assert
                var user1FromDb = await _userCollection.Find(x => x.Id == user1.Id).SingleOrDefaultAsync();

                var user2FromDb = await _userCollection.Find(x => x.Id == user2.Id).SingleOrDefaultAsync();

                var user3FromDb = await _userCollection.Find(x => x.Id == user3.Id).SingleOrDefaultAsync();

                // USER 1
                Assert.Equal(user1.Roles.Count, user1FromDb.Roles.Count);
                var user1FromDbRole1 = user1FromDb.Roles.Single(x => x.Id == role1.Id);

                IdentityClaimAssert.Equal(new List <IdentityClaim> {
                    identityClaim2
                }, user1FromDbRole1.Claims);

                // we were specifically removing the identityClaim1 from role1 - so role2 should still have identityClaim1 in role2
                var user1FromDbRole2 = user1FromDb.Roles.Single(x => x.Id == role2.Id);

                IdentityClaimAssert.Equal(role2.Claims, user1FromDbRole2.Claims);


                // USER 2
                Assert.Equal(user2.Roles.Count, user2FromDb.Roles.Count);
                var user2FromDbRole1 = user2FromDb.Roles.Single();

                IdentityClaimAssert.Equal(new List <IdentityClaim> {
                    identityClaim2
                }, user2FromDbRole1.Claims);


                // USER 3
                Assert.Equal(user3.Roles.Count, user3FromDb.Roles.Count);
                // we were specifically removing the identityClaim1 from role1 - so role2 should still have identityClaim1 in role2
                var user3FromDbRole2 = user3FromDb.Roles.Single();

                IdentityClaimAssert.Equal(role2.Claims, user3FromDbRole2.Claims);
            }
Exemple #14
0
            public async Task When_User_has_Role_and_Role_is_updated_should_update_User_Role_as_well()
            {
                // arrange
                var identityClaim1 = new IdentityClaim {
                    ClaimType = "ClaimType1", ClaimValue = "val"
                };
                var identityClaim2 = new IdentityClaim {
                    ClaimType = "ClaimType2", ClaimValue = "some val"
                };
                var role1 = new IdentityRole {
                    Name = "Role1", Claims = new List <IdentityClaim> {
                        identityClaim1, identityClaim2
                    }
                };
                var role2 = new IdentityRole {
                    Name = "Role2", Claims = new List <IdentityClaim> {
                        identityClaim1
                    }
                };

                await _roleStore.CreateAsync(role1);

                await _roleStore.CreateAsync(role2);

                var user1 = new IdentityUser {
                    UserName = "******", Roles = new List <IdentityRole <string> > {
                        role1, role2
                    }
                };
                var user2 = new IdentityUser {
                    UserName = "******", Roles = new List <IdentityRole <string> > {
                        role1
                    }
                };
                var user3 = new IdentityUser {
                    UserName = "******", Roles = new List <IdentityRole <string> > {
                        role2
                    }
                };

                await _userStore.CreateAsync(user1);

                await _userStore.CreateAsync(user2);

                await _userStore.CreateAsync(user3);

                // act
                role1.Name = "Bob";
                await _roleStore.UpdateAsync(role1);

                // assert
                var user1FromDb = await _userCollection.Find(x => x.Id == user1.Id).SingleOrDefaultAsync();

                var user2FromDb = await _userCollection.Find(x => x.Id == user2.Id).SingleOrDefaultAsync();

                var user3FromDb = await _userCollection.Find(x => x.Id == user3.Id).SingleOrDefaultAsync();

                // USER 1
                Assert.Equal(user1.Roles.Count, user1FromDb.Roles.Count);
                var user1FromDbRole1 = user1FromDb.Roles.Single(x => x.Id == role1.Id);

                Assert.Equal(role1.Name, user1FromDbRole1.Name);
                IdentityClaimAssert.Equal(role1.Claims, user1FromDbRole1.Claims);

                var user1FromDbRole2 = user1FromDb.Roles.Single(x => x.Id == role2.Id);

                IdentityRoleAssert.Equal(role2, (IdentityRole)user1FromDbRole2);


                // USER 2
                Assert.Equal(user2.Roles.Count, user2FromDb.Roles.Count);
                var user2FromDbRole1 = user2FromDb.Roles.Single();

                Assert.Equal(role1.Name, user2FromDbRole1.Name);
                IdentityClaimAssert.Equal(role1.Claims, user2FromDbRole1.Claims);


                // USER 3
                Assert.Equal(user3.Roles.Count, user3FromDb.Roles.Count);
                var user3FromDbRole2 = user3FromDb.Roles.Single();

                IdentityRoleAssert.Equal(role2, (IdentityRole)user3FromDbRole2);
            }