Пример #1
0
            public async Task Remove_existing_claim_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, ClaimValue = claim1.Value
                };
                var identityClaim2 = new IdentityClaim {
                    ClaimType = claim2.Type, ClaimValue = claim2.Value
                };

                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);
            }
Пример #2
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 {
                    ClaimType = claim1.Type, ClaimValue = claim1.Value
                };
                var identityClaim2 = new IdentityClaim {
                    ClaimType = claim2.Type, ClaimValue = claim2.Value
                };

                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);
            }
Пример #3
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 {
                    ClaimType = claim1.Type, ClaimValue = claim1.Value
                };
                var identityClaim2 = new IdentityClaim {
                    ClaimType = claim2.Type, ClaimValue = claim2.Value
                };

                // 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);
            }
Пример #4
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);
            }
                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);
                }
Пример #6
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 {
                    ClaimType = claim.Type, ClaimValue = claim.Value
                };

                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);
            }
                public async Task Remove_existing_claim_updates_database_user_record()
                {
                    // arrange
                    var user = new IdentityUser("Remove_existing_claim_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 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);
        }
        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);
            IdentityUserLoginAssert.Equal(expected.Logins, actual.Logins);
        }