Esempio n. 1
0
        public void UserRoleStoreInterface_AddToRoleAsync_AddsInstancesToCollectionProperties_ForBothSidesOfEntityRelationship()
        {
            var          entities = new Mock <IWriteEntities>(MockBehavior.Strict);
            var          instance = new SecurityStore(entities.Object) as IUserRoleStore <User, int>;
            const string roleName = "test";
            var          user     = new User();
            var          data     = new[]
            {
                new Permission {
                    Name = Guid.NewGuid().ToString()
                },
                new Permission {
                    Name = roleName
                },
                new Permission {
                    Name = Guid.NewGuid().ToString()
                },
            }.AsQueryable();
            var dbSet = new Mock <DbSet <Permission> >(MockBehavior.Strict).SetupDataAsync(data);

            entities.Setup(x => x.Get <Permission>()).Returns(dbSet.Object);

            instance.AddToRoleAsync(user, roleName).Wait();

            user.Permissions.ShouldContain(data.ElementAt(1));
            data.ElementAt(1).Users.ShouldContain(user);
        }
Esempio n. 2
0
        public void UserRoleStoreInterface_GetRolesAsync_DelegatesToUserCollection(string roleName, bool expectInResult)
        {
            var instance = new SecurityStore(null) as IUserRoleStore <User, int>;
            var user     = new User();

            user.Permissions.Add(new Permission {
                Name = "role1"
            });
            user.Permissions.Add(new Permission {
                Name = "role2"
            });
            user.Permissions.Add(new Permission {
                Name = "role3"
            });

            var result = instance.GetRolesAsync(user).Result;

            result.Count.ShouldEqual(3);
            if (expectInResult)
            {
                result.ShouldContain(roleName);
            }
            else
            {
                result.ShouldNotContain(roleName);
            }
        }
Esempio n. 3
0
        public void UserStoreInterface_FindByNameAsync_DelegatesToDataDependency(string userName, bool expectFound)
        {
            var entities = new Mock <IWriteEntities>(MockBehavior.Strict);
            var instance = new SecurityStore(entities.Object) as IUserStore <User, int>;
            var data     = new[]
            {
                new User {
                    Name = "user1"
                },
                new User {
                    Name = "user2"
                },
                new User {
                    Name = "user3"
                },
            }.AsQueryable();
            var dbSet = new Mock <DbSet <User> >(MockBehavior.Strict).SetupDataAsync(data);

            entities.Setup(x => x.Get <User>()).Returns(dbSet.Object);
            Expression <Func <IQueryable <User>, bool> > expectedData = x => ReferenceEquals(x, dbSet.Object);

            entities.Setup(x => x.EagerLoad(It.Is(expectedData), It.IsAny <Expression <Func <User, object> > >())).Returns(dbSet.Object);

            var result = instance.FindByNameAsync(userName).Result;

            (result != null).ShouldEqual(expectFound);
            if (expectFound)
            {
                result.ShouldEqual(data.Single(x => userName.Equals(x.Name)));
            }
            entities.Verify(x => x.Get <User>(), Times.Once);
        }
Esempio n. 4
0
        public void UserRoleStoreInterface_AddToRoleAsync_ThrowsInvalidOperation_WhenRoleNameDoesNotExist()
        {
            var          entities = new Mock <IWriteEntities>(MockBehavior.Strict);
            var          instance = new SecurityStore(entities.Object) as IUserRoleStore <User, int>;
            const string roleName = "test";
            var          data     = new[]
            {
                new Permission {
                    Name = Guid.NewGuid().ToString()
                },
                new Permission {
                    Name = Guid.NewGuid().ToString()
                },
                new Permission {
                    Name = Guid.NewGuid().ToString()
                },
            }.AsQueryable();
            var dbSet = new Mock <DbSet <Permission> >(MockBehavior.Strict).SetupDataAsync(data);

            entities.Setup(x => x.Get <Permission>()).Returns(dbSet.Object);
            var exception = Assert.Throws <InvalidOperationException>(() =>
                                                                      instance.AddToRoleAsync(new User(), roleName).GetAwaiter().GetResult());

            exception.ShouldNotBeNull();
            exception.Message.ShouldEqual(string.Format(Resources.Exception_InvalidOperation_DoesNotExist,
                                                        Permission.Constraints.Label, roleName));
        }
Esempio n. 5
0
        public void UserRoleStoreInterface_AddToRoleAsync_GivesUserPermission()
        {
            using (var dbContext = new EntityDbContext())
            {
                var user = new User {
                    Name = Guid.NewGuid().ToString()
                };
                var permission1 = new Permission {
                    Name = Guid.NewGuid().ToString()
                };
                var permission2 = new Permission {
                    Name = Guid.NewGuid().ToString()
                };
                var permission3 = new Permission {
                    Name = Guid.NewGuid().ToString()
                };
                dbContext.Create(user);
                dbContext.Create(permission1);
                dbContext.Create(permission2);
                dbContext.Create(permission3);
                dbContext.SaveChangesAsync().GetAwaiter().GetResult();

                var securityStore = new SecurityStore(dbContext);
                securityStore.AddToRoleAsync(user, permission2.Name).Wait();
                dbContext.SaveChangesAsync().Wait();

                user.Permissions.ShouldContain(permission2);
                permission2.Users.ShouldContain(user);
            }
        }
Esempio n. 6
0
        public void UserPasswordStoreInterface_HasPasswordAsync_ReturnsFalse_WhenUserHasNoLocalMembership()
        {
            var instance = new SecurityStore(null) as IUserPasswordStore <User, int>;
            var user     = new User();
            var result   = instance.HasPasswordAsync(user).Result;

            result.ShouldBeFalse();
        }
Esempio n. 7
0
        public void UserClaimStoreInterface_RemoveClaimAsync_ThrowsArgumentNullException_WhenClaimIsNull()
        {
            var instance  = new SecurityStore(null) as IUserClaimStore <User, int>;
            var exception = Assert.Throws <ArgumentNullException>(() =>
                                                                  instance.RemoveClaimAsync(new User(), null).GetAwaiter().GetResult());

            exception.ShouldNotBeNull();
            exception.ParamName.ShouldEqual("claim");
        }
Esempio n. 8
0
        public void UserLoginStoreInterface_RemoveLoginAsync_ThrowsArgumentNullException_WhenUserIsNull()
        {
            var instance  = new SecurityStore(null) as IUserLoginStore <User, int>;
            var exception = Assert.Throws <ArgumentNullException>(() =>
                                                                  instance.RemoveLoginAsync(null, new UserLoginInfo("provider", "key")).GetAwaiter().GetResult());

            exception.ShouldNotBeNull();
            exception.ParamName.ShouldEqual("user");
        }
Esempio n. 9
0
        public void UserClaimStoreInterface_GetClaimsAsync_ThrowsArgumentNullException_WhenUserIsNull()
        {
            var instance  = new SecurityStore(null) as IUserClaimStore <User, int>;
            var exception = Assert.Throws <ArgumentNullException>(() =>
                                                                  instance.GetClaimsAsync(null).Result);

            exception.ShouldNotBeNull();
            exception.ParamName.ShouldEqual("user");
        }
Esempio n. 10
0
        public void UserPasswordStoreInterface_SetPasswordHashAsync_ThrowsArgumentNullException_WhenUserIsNull()
        {
            var instance  = new SecurityStore(null) as IUserPasswordStore <User, int>;
            var exception = Assert.Throws <ArgumentNullException>(() =>
                                                                  instance.SetPasswordHashAsync(null, null).GetAwaiter().GetResult());

            exception.ShouldNotBeNull();
            exception.ParamName.ShouldEqual("user");
        }
Esempio n. 11
0
        public void UserSecurityStampStoreInterface_SetSecurityStampAsync_SetsUserProperty()
        {
            var instance = new SecurityStore(null) as IUserSecurityStampStore <User, int>;
            var user     = new User();
            var stamp    = Guid.NewGuid().ToString();

            instance.SetSecurityStampAsync(user, stamp).Wait();
            user.SecurityStamp.ShouldEqual(stamp);
        }
Esempio n. 12
0
        public void UserLoginStoreInterface_AddLoginAsync_ThrowsArgumentNullException_WhenUserLoginInfoIsNull()
        {
            var instance  = new SecurityStore(null) as IUserLoginStore <User, int>;
            var exception = Assert.Throws <ArgumentNullException>(() =>
                                                                  instance.AddLoginAsync(new User(), null).Wait());

            exception.ShouldNotBeNull();
            exception.ParamName.ShouldEqual("login");
        }
Esempio n. 13
0
        public void UserStoreInterface_DeleteAsync_DeletesUserEntity()
        {
            var entities = new Mock <IWriteEntities>(MockBehavior.Strict);
            var instance = new SecurityStore(entities.Object) as IUserStore <User, int>;
            var entity   = new User();

            entities.Setup(x => x.Delete(entity));
            instance.DeleteAsync(entity);
            entities.Verify(x => x.Delete(entity), Times.Once);
        }
Esempio n. 14
0
        public void QueryableUserStoreInterface_UsersPropertyGetter_ThrowsWhenDisposed()
        {
            var instance = new SecurityStore(null) as IQueryableUserStore <User, int>;

            instance.Dispose();
            var exception = Assert.Throws <ObjectDisposedException>(() => instance.Users);

            exception.ShouldNotBeNull();
            exception.ObjectName.ShouldEqual(instance.GetType().Name);
        }
Esempio n. 15
0
        public void QueryableUserStoreInterface_UsersPropertyGetter_DelegatesToDataDependency()
        {
            var entities = new Mock <IWriteEntities>(MockBehavior.Strict);
            var instance = new SecurityStore(entities.Object) as IQueryableUserStore <User, int>;
            var data = new[] { new FakeUser(1), new FakeUser(2), new FakeUser(3) }.AsQueryable();

            entities.Setup(x => x.Query <User>()).Returns(data);
            instance.Users.ShouldEqual(data);
            entities.Verify(x => x.Query <User>(), Times.Once);
        }
Esempio n. 16
0
        public void UserClaimStoreInterface_AddClaimAsync_AddsClaimToUserCollectionProperty()
        {
            var instance = new SecurityStore(null) as IUserClaimStore <User, int>;
            var user     = new User();
            var claim    = new Claim(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());

            instance.AddClaimAsync(user, claim).Wait();
            user.Claims.Count.ShouldEqual(1);
            user.Claims.Any(x => x.ClaimType == claim.Type && x.ClaimValue == claim.Value).ShouldBeTrue();
        }
Esempio n. 17
0
        public void UserRoleStoreInterface_IsInRoleAsync_ThrowsArgumentException_WhenRoleIsNullOrWhiteSpace(string roleName)
        {
            var instance  = new SecurityStore(null) as IUserRoleStore <User, int>;
            var exception = Assert.Throws <ArgumentException>(() =>
                                                              instance.IsInRoleAsync(new User(), roleName).Result);

            exception.ShouldNotBeNull();
            exception.ParamName.ShouldEqual("roleName");
            exception.Message.ShouldStartWith(Resources.Exception_Argument_CannotBeNullOrEmpty);
        }
Esempio n. 18
0
        public void UserPasswordStoreInterface_SetPasswordHashAsync_CreatesLocalMembership_WhenUserDoesNotHaveOne()
        {
            var instance     = new SecurityStore(null) as IUserPasswordStore <User, int>;
            var user         = new User();
            var passwordHash = Guid.NewGuid().ToString();

            instance.SetPasswordHashAsync(user, passwordHash).Wait();

            user.LocalMembership.ShouldNotBeNull();
            user.LocalMembership.PasswordHash.ShouldEqual(passwordHash);
        }
Esempio n. 19
0
        public void UserLoginStoreInterface_AddLoginAsync_ThrowsWhenDisposed()
        {
            var instance = new SecurityStore(null) as IUserLoginStore <User, int>;

            instance.Dispose();
            var exception = Assert.Throws <ObjectDisposedException>(() =>
                                                                    instance.AddLoginAsync(null, null).Wait());

            exception.ShouldNotBeNull();
            exception.ObjectName.ShouldEqual(instance.GetType().Name);
        }
Esempio n. 20
0
        public void UserStoreInterface_FindByIdAsync_ThrowsWhenDisposed()
        {
            var instance = new SecurityStore(null) as IUserStore <User, int>;

            instance.Dispose();
            var exception = Assert.Throws <ObjectDisposedException>(() =>
                                                                    instance.FindByIdAsync(new Random().Next(0, int.MaxValue)).Result);

            exception.ShouldNotBeNull();
            exception.ObjectName.ShouldEqual(instance.GetType().Name);
        }
Esempio n. 21
0
        public void UserStoreInterface_FindByNameAsync_ThrowsWhenDisposed()
        {
            var instance = new SecurityStore(null) as IUserStore <User, int>;

            instance.Dispose();
            var exception = Assert.Throws <ObjectDisposedException>(() =>
                                                                    instance.FindByNameAsync(Guid.NewGuid().ToString()).Result);

            exception.ShouldNotBeNull();
            exception.ObjectName.ShouldEqual(instance.GetType().Name);
        }
Esempio n. 22
0
        public void UserPasswordStoreInterface_GetPasswordHashAsync_ThrowsWhenDisposed()
        {
            var instance = new SecurityStore(null) as IUserPasswordStore <User, int>;

            instance.Dispose();
            var exception = Assert.Throws <ObjectDisposedException>(() =>
                                                                    instance.GetPasswordHashAsync(null).Result);

            exception.ShouldNotBeNull();
            exception.ObjectName.ShouldEqual(instance.GetType().Name);
        }
Esempio n. 23
0
        public void UserRoleStoreInterface_RemoveFromRoleAsync_DoesNothing_WhenUserIsNotInRole()
        {
            var instance = new SecurityStore(null) as IUserRoleStore <User, int>;
            var user     = new User();

            user.Permissions.Add(new Permission {
                Name = "test1"
            });
            instance.RemoveFromRoleAsync(new User(), "test2");
            user.Permissions.Count.ShouldEqual(1);
        }
Esempio n. 24
0
        public void UserClaimStoreInterface_RemoveClaimAsync_ThrowsWhenDisposed()
        {
            var instance = new SecurityStore(null) as IUserClaimStore <User, int>;

            instance.Dispose();
            var exception = Assert.Throws <ObjectDisposedException>(() =>
                                                                    instance.RemoveClaimAsync(null, null).GetAwaiter().GetResult());

            exception.ShouldNotBeNull();
            exception.ObjectName.ShouldEqual(instance.GetType().Name);
        }
Esempio n. 25
0
        public void UserLoginStoreInterface_FindAsync_ThrowsWhenDisposed()
        {
            var instance = new SecurityStore(null) as IUserLoginStore <User, int>;

            instance.Dispose();
            var exception = Assert.Throws <ObjectDisposedException>(() =>
                                                                    instance.FindAsync(new UserLoginInfo("provider", "key")).GetAwaiter().GetResult());

            exception.ShouldNotBeNull();
            exception.ObjectName.ShouldEqual(instance.GetType().Name);
        }
Esempio n. 26
0
        public void UserSecurityStampStoreInterface_GetSecurityStampAsync_GetsUserProperty()
        {
            var instance = new SecurityStore(null) as IUserSecurityStampStore <User, int>;
            var user     = new User();
            var stamp    = Guid.NewGuid().ToString();

            user.SecurityStamp = stamp;
            var result = instance.GetSecurityStampAsync(user).Result;

            result.ShouldEqual(stamp);
        }
Esempio n. 27
0
        public void UserLoginStoreInterface_AddLoginAsync_DelegatesToDataDependency()
        {
            var entities = new Mock <IWriteEntities>(MockBehavior.Strict);
            var instance = new SecurityStore(entities.Object) as IUserLoginStore <User, int>;
            var user     = new FakeUser(6);
            var login    = new UserLoginInfo(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
            Expression <Func <RemoteMembership, bool> > expectedEntity = x => x.UserId == user.Id &&
                                                                         x.LoginProvider == login.LoginProvider && x.ProviderKey == login.ProviderKey;

            entities.Setup(x => x.Create(It.Is(expectedEntity)));
            instance.AddLoginAsync(user, login);
            entities.Verify(x => x.Create(It.Is(expectedEntity)), Times.Once);
        }
Esempio n. 28
0
        public void UserPasswordStoreInterface_SetPasswordHashAsync_DelegatesToUserLocalMembership()
        {
            var instance = new SecurityStore(null) as IUserPasswordStore <User, int>;
            var user     = new User
            {
                LocalMembership = new LocalMembership()
            };
            var passwordHash = Guid.NewGuid().ToString();

            instance.SetPasswordHashAsync(user, passwordHash).Wait();

            user.LocalMembership.PasswordHash.ShouldEqual(passwordHash);
        }
Esempio n. 29
0
        public void UserPasswordStoreInterface_HasPasswordAsync_DelegatesToUserLocalMembershipProperty(string passwordHash, bool expect)
        {
            var instance        = new SecurityStore(null) as IUserPasswordStore <User, int>;
            var user            = new User();
            var localMembership = new LocalMembership {
                PasswordHash = passwordHash
            };

            user.LocalMembership = localMembership;
            var result = instance.HasPasswordAsync(user).Result;

            result.ShouldEqual(expect);
        }
Esempio n. 30
0
        public void UserPasswordStoreInterface_GetPasswordHashAsync_DelegatesToUserLocalMembershipProperty()
        {
            var instance        = new SecurityStore(null) as IUserPasswordStore <User, int>;
            var user            = new User();
            var localMembership = new LocalMembership {
                PasswordHash = Guid.NewGuid().ToString()
            };

            user.LocalMembership = localMembership;
            var result = instance.GetPasswordHashAsync(user).Result;

            result.ShouldEqual(localMembership.PasswordHash);
            result.ShouldEqual(user.LocalMembership.PasswordHash);
        }