Esempio n. 1
0
        public void IsValid_WhenUserByName_AlreadyExists_AndHasUserId_DifferentFromProvided()
        {
            const int userId   = 797;
            var       userName = Guid.NewGuid().ToString();
            var       queries  = new Mock <IProcessQueries>(MockBehavior.Strict);
            var       command  = new FakeMustNotFindUserByNameCommand
            {
                UserName = userName,
                UserId   = userId + 1,
            };
            var entity = new ProxiedUser(userId)
            {
                Name = command.UserName,
            };
            Expression <Func <UserBy, bool> > expectedQuery = x => x.Name == command.UserName;

            queries.Setup(x => x.Execute(It.Is(expectedQuery))).Returns(Task.FromResult(entity as User));
            var validator = new FakeMustNotFindUserByNameValidator(queries.Object);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeFalse();
            Func <ValidationFailure, bool> nameError = x => x.PropertyName == command.PropertyName(y => y.UserName);

            result.Errors.Count(nameError).ShouldEqual(1);
            result.Errors.Single(nameError).ErrorMessage.ShouldEqual(Resources.Validation_AlreadyExists
                                                                     .Replace("{PropertyName}", User.Constraints.NameLabel)
                                                                     .Replace("{PropertyValue}", command.UserName)
                                                                     );
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Once);
            validator.ShouldHaveValidationErrorFor(x => x.UserName, command);
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Exactly(2));
        }
Esempio n. 2
0
        public void IsValid_WhenUserIsFound_ByPrincipal()
        {
            string userName  = FakeData.String();
            var    principal = new Mock <IPrincipal>(MockBehavior.Strict);
            var    identity  = new Mock <IIdentity>(MockBehavior.Strict);

            identity.SetupGet(x => x.IsAuthenticated).Returns(true);
            identity.SetupGet(x => x.Name).Returns(userName);
            principal.SetupGet(x => x.Identity).Returns(identity.Object);
            var entity = new ProxiedUser(FakeData.Id())
            {
                Name = userName
            };
            var queries = new Mock <IProcessQueries>(MockBehavior.Strict);
            var command = new FakeMustFindUserByPrincipalCommand {
                Principal = principal.Object,
            };
            Expression <Func <UserBy, bool> > expectedQuery = x => x.Principal == principal.Object;

            queries.Setup(x => x.Execute(It.Is(expectedQuery))).Returns(Task.FromResult(entity as User));
            var validator = new FakeMustFindUserByPrincipalValidator(queries.Object);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeTrue();
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Once);
            validator.ShouldNotHaveValidationErrorFor(x => x.Principal, command.Principal);
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Exactly(2));
        }
Esempio n. 3
0
        public void IsValid_WhenUserByName_AlreadyExists_ButHasUserId_SameAsProvided()
        {
            const int userId   = 797;
            var       userName = Guid.NewGuid().ToString();
            var       queries  = new Mock <IProcessQueries>(MockBehavior.Strict);
            var       command  = new FakeMustNotFindUserByNameCommand
            {
                UserName = userName,
                UserId   = userId,
            };
            var entity = new ProxiedUser(userId)
            {
                Name = command.UserName,
            };
            Expression <Func <UserBy, bool> > expectedQuery = x => x.Name == command.UserName;

            queries.Setup(x => x.Execute(It.Is(expectedQuery))).Returns(Task.FromResult(entity as User));
            var validator = new FakeMustNotFindUserByNameValidator(queries.Object);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeTrue();
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Once);
            validator.ShouldNotHaveValidationErrorFor(x => x.UserName, command);
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Exactly(2));
        }
        public void Handler_ReturnsTrue_WhenFound_ByUserId()
        {
            var userId = FakeData.Id();
            var otherUserId1 = FakeData.Id(userId);
            var otherUserId2 = FakeData.Id(userId, otherUserId1);
            var user = new ProxiedUser(userId);
            var otherUser1 = new ProxiedUser(otherUserId1);
            var otherUser2 = new ProxiedUser(otherUserId2);
            var localMemberships = new[]
            {
                new LocalMembership { User = otherUser1, },
                new LocalMembership { User = user, },
                new LocalMembership { User = otherUser2, },
            };
            var data = localMemberships.AsQueryable();
            var query = new UserHasLocalMembership(user.Id);
            var dbSet = new Mock<DbSet<LocalMembership>>(MockBehavior.Strict).SetupDataAsync(data);
            var entities = new Mock<IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet<LocalMembership>(dbSet.Object, entities.Object);
            entities.Setup(x => x.Query<LocalMembership>()).Returns(entitySet);
            var handler = new HandleUserHasLocalMembershipQuery(entities.Object);

            bool result = handler.Handle(query).Result;

            result.ShouldBeTrue();
            entities.Verify(x => x.Query<LocalMembership>(), Times.Once);
        }
Esempio n. 5
0
        public void IsValid_WhenUser_IsFound_ByUserLoginInfo()
        {
            var loginProvider = FakeData.String();
            var providerKey   = FakeData.String();
            var queries       = new Mock <IProcessQueries>(MockBehavior.Strict);
            var command       = new FakeMustFindUserByLoginProviderKeyCommand
            {
                LoginProvider = loginProvider,
                ProviderKey   = providerKey,
            };
            var user = new ProxiedUser(FakeData.Id());

            user.RemoteMemberships.Add(new ProxiedRemoteMembership(loginProvider, providerKey)
            {
                User   = user,
                UserId = user.Id,
            });
            Expression <Func <UserBy, bool> > expectedQuery = x => x.UserLoginInfo.LoginProvider == loginProvider &&
                                                              x.UserLoginInfo.ProviderKey == providerKey;

            queries.Setup(x => x.Execute(It.Is(expectedQuery))).Returns(Task.FromResult(user as User));
            var validator = new FakeMustFindUserByLoginProviderKeyValidator(queries.Object);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeTrue();
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Once);
            validator.ShouldNotHaveValidationErrorFor(x => x.ProviderKey, command);
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Exactly(2));
        }
Esempio n. 6
0
        public void Handler_ReturnsClientCookie_WhenUserById_Exists()
        {
            var userId = FakeData.Id();
            var userName = FakeData.String();
            var query = new ClientCookieBy(userId);
            var user = new ProxiedUser(userId) { Name = userName };
            user.EmailAddresses.Add(new EmailAddress
            {
                HashedValue = "Email 0 hashed value",
            });
            user.EmailAddresses.Add(new EmailAddress
            {
                IsPrimary = true,
                HashedValue = "Email 1 hashed value",
            });
            var queries = new Mock<IProcessQueries>(MockBehavior.Strict);
            Expression<Func<UserBy, bool>> expectedQuery = x => x.Id == query.UserId;
            queries.Setup(x => x.Execute(It.Is(expectedQuery))).Returns(Task.FromResult(user as User));
            var handler = new HandleClientCookieByQuery(queries.Object);

            ClientCookie result = handler.Handle(query).Result;

            Assert.NotNull(result);
            result.UserId.ShouldEqual(userId);
            result.UserName.ShouldEqual(userName);
            result.GravatarHash.ShouldEqual(user.EmailAddresses.Single(x => x.IsPrimary).HashedValue);
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Once);
        }
Esempio n. 7
0
        public void IsValid_WhenAllRulesPass(EmailVerificationPurpose purpose)
        {
            var queries   = new Mock <IProcessQueries>(MockBehavior.Strict);
            var validator = new ValidateSendVerificationEmailCommand(queries.Object);
            var command   = new SendVerificationEmail
            {
                EmailAddress     = "*****@*****.**",
                IsExpectingEmail = true,
                Purpose          = purpose,
                SendFromUrl      = "[send from this url]",
                VerifyUrlFormat  = "[here is the token:' {0}']",
            };
            Expression <Func <EmailAddressBy, bool> > expectedQuery = x => x.Value == command.EmailAddress;
            User user         = new ProxiedUser(FakeData.Id());
            var  emailAddress = new EmailAddress
            {
                Value = command.EmailAddress,
                User  = user,
            };

            queries.Setup(x => x.Execute(It.Is(expectedQuery))).Returns(Task.FromResult(emailAddress));

            var result = validator.Validate(command);

            result.IsValid.ShouldBeTrue();
        }
        public void Handler_ReturnsTrue_WhenFound_ByUserId()
        {
            var userId           = FakeData.Id();
            var otherUserId1     = FakeData.Id(userId);
            var otherUserId2     = FakeData.Id(userId, otherUserId1);
            var user             = new ProxiedUser(userId);
            var otherUser1       = new ProxiedUser(otherUserId1);
            var otherUser2       = new ProxiedUser(otherUserId2);
            var localMemberships = new[]
            {
                new LocalMembership {
                    User = otherUser1,
                },
                new LocalMembership {
                    User = user,
                },
                new LocalMembership {
                    User = otherUser2,
                },
            };
            var data      = localMemberships.AsQueryable();
            var query     = new UserHasLocalMembership(user.Id);
            var dbSet     = new Mock <DbSet <LocalMembership> >(MockBehavior.Strict).SetupDataAsync(data);
            var entities  = new Mock <IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet <LocalMembership>(dbSet.Object, entities.Object);

            entities.Setup(x => x.Query <LocalMembership>()).Returns(entitySet);
            var handler = new HandleUserHasLocalMembershipQuery(entities.Object);

            bool result = handler.Handle(query).Result;

            result.ShouldBeTrue();
            entities.Verify(x => x.Query <LocalMembership>(), Times.Once);
        }
        public void Handler_ReturnsNonNullLocalMembership_WhenFound_ByUserId()
        {
            var userId = FakeData.Id();
            var user = new ProxiedUser(userId);
            var data = new[]
            {
                new LocalMembership
                {
                    User = user,
                    PasswordHash = "password hash",
                },
            }.AsQueryable();
            var query = new LocalMembershipByUser(userId);
            var dbSet = new Mock<DbSet<LocalMembership>>(MockBehavior.Strict).SetupDataAsync(data);
            var entities = new Mock<IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet<LocalMembership>(dbSet.Object, entities.Object);
            entities.Setup(x => x.Query<LocalMembership>()).Returns(entitySet);
            var handler = new HandleLocalMembershipByUserQuery(entities.Object);

            LocalMembership result = handler.Handle(query).Result;

            result.ShouldNotBeNull();
            result.User.ShouldEqual(user);
            result.PasswordHash.ShouldEqual("password hash");
            entities.Verify(x => x.Query<LocalMembership>(), Times.Once);
        }
        public void Handler_ReturnsFalse_WhenNotFound_ByPrincipal()
        {
            var userId           = new Random().Next(1, int.MaxValue);
            var user             = new ProxiedUser(userId);
            var differentUser1   = new ProxiedUser(userId + 1);
            var differentUser2   = new ProxiedUser(userId - 1);
            var localMemberships = new[]
            {
                new LocalMembership {
                    User = differentUser1,
                },
                new LocalMembership {
                    User = differentUser2,
                },
            };
            var identity = new ClaimsIdentity(new[]
            {
                new Claim(ClaimTypes.NameIdentifier, user.Id.ToString(CultureInfo.InvariantCulture)),
            });
            var principal = new GenericPrincipal(identity, null);
            var data      = localMemberships.AsQueryable();
            var query     = new UserHasLocalMembership(principal);
            var dbSet     = new Mock <DbSet <LocalMembership> >(MockBehavior.Strict).SetupDataAsync(data);
            var entities  = new Mock <IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet <LocalMembership>(dbSet.Object, entities.Object);

            entities.Setup(x => x.Query <LocalMembership>()).Returns(entitySet);
            var handler = new HandleUserHasLocalMembershipQuery(entities.Object);

            bool result = handler.Handle(query).Result;

            result.ShouldBeFalse();
            entities.Verify(x => x.Query <LocalMembership>(), Times.Once);
        }
Esempio n. 11
0
        public void Handler_ReturnsNullUserView_WhenPrincipalIdentity_HasNoNameIdentifierClaim()
        {
            var userId = new Random().Next(1, int.MaxValue - 3);
            var user   = new ProxiedUser(userId)
            {
                Name = Guid.NewGuid().ToString(),
            };
            var data = new[] { user }.AsQueryable();
            var claims = new[]
            {
                new Claim(ClaimTypes.Name, userId.ToString(CultureInfo.InvariantCulture)),
            };
            var identity  = new ClaimsIdentity(claims, "authentication type");
            var principal = new GenericPrincipal(identity, null);
            var query     = new UserViewBy(principal);
            var dbSet     = new Mock <DbSet <User> >(MockBehavior.Strict).SetupDataAsync(data);
            var entities  = new Mock <IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet <User>(dbSet.Object, entities.Object);
            IQueryable <User> toBeReturned = entitySet;

            entities.Setup(x => x.Query <User>()).Returns(toBeReturned);
            var handler = new HandleUserViewByQuery(entities.Object);

            UserView result = handler.Handle(query).Result;

            result.ShouldBeNull();
            entities.Verify(x => x.Query <User>(), Times.Once);
        }
        public void Handler_ReturnsFalse_WhenNotFound_ByUserId()
        {
            var userId           = new Random().Next(3, int.MaxValue - 3);
            var user             = new ProxiedUser(userId);
            var differentUser1   = new ProxiedUser(userId + 1);
            var differentUser2   = new ProxiedUser(userId - 1);
            var localMemberships = new[]
            {
                new LocalMembership {
                    User = differentUser1,
                },
                new LocalMembership {
                    User = differentUser2,
                },
            };
            var data      = localMemberships.AsQueryable();
            var query     = new UserHasLocalMembership(user.Id);
            var dbSet     = new Mock <DbSet <LocalMembership> >(MockBehavior.Strict).SetupDataAsync(data);
            var entities  = new Mock <IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet <LocalMembership>(dbSet.Object, entities.Object);

            entities.Setup(x => x.Query <LocalMembership>()).Returns(entitySet);
            var handler = new HandleUserHasLocalMembershipQuery(entities.Object);

            bool result = handler.Handle(query).Result;

            result.ShouldBeFalse();
            entities.Verify(x => x.Query <LocalMembership>(), Times.Once);
        }
Esempio n. 13
0
        public void Handler_ReturnsClientCookie_WhenUserById_Exists()
        {
            var userId   = new Random().Next(3, int.MaxValue);
            var userName = Guid.NewGuid().ToString();
            var query    = new ClientCookieBy(userId);
            var user     = new ProxiedUser(userId)
            {
                Name = userName
            };

            user.EmailAddresses.Add(new EmailAddress
            {
                HashedValue = "Email 0 hashed value",
            });
            user.EmailAddresses.Add(new EmailAddress
            {
                IsPrimary   = true,
                HashedValue = "Email 1 hashed value",
            });
            var queries = new Mock <IProcessQueries>(MockBehavior.Strict);
            Expression <Func <UserBy, bool> > expectedQuery = x => x.Id == query.UserId;

            queries.Setup(x => x.Execute(It.Is(expectedQuery))).Returns(Task.FromResult(user as User));
            var handler = new HandleClientCookieByQuery(queries.Object);

            ClientCookie result = handler.Handle(query).Result;

            Assert.NotNull(result);
            result.UserId.ShouldEqual(userId);
            result.UserName.ShouldEqual(userName);
            result.GravatarHash.ShouldEqual(user.EmailAddresses.Single(x => x.IsPrimary).HashedValue);
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Once);
        }
Esempio n. 14
0
        public void Handler_ReturnsNonNullLocalMembership_WhenFound_ByUserId()
        {
            var userId = FakeData.Id();
            var user   = new ProxiedUser(userId);
            var data   = new[]
            {
                new LocalMembership
                {
                    User         = user,
                    PasswordHash = "password hash",
                },
            }.AsQueryable();
            var query     = new LocalMembershipByUser(userId);
            var dbSet     = new Mock <DbSet <LocalMembership> >(MockBehavior.Strict).SetupDataAsync(data);
            var entities  = new Mock <IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet <LocalMembership>(dbSet.Object, entities.Object);

            entities.Setup(x => x.Query <LocalMembership>()).Returns(entitySet);
            var handler = new HandleLocalMembershipByUserQuery(entities.Object);

            LocalMembership result = handler.Handle(query).Result;

            result.ShouldNotBeNull();
            result.User.ShouldEqual(user);
            result.PasswordHash.ShouldEqual("password hash");
            entities.Verify(x => x.Query <LocalMembership>(), Times.Once);
        }
Esempio n. 15
0
        public void Handler_ReturnsNonNullUserView_WhenFound_ByPrincipal()
        {
            var userId = new Random().Next(1, int.MaxValue - 3);
            var user   = new ProxiedUser(userId)
            {
                Name = Guid.NewGuid().ToString(),
            };
            var primaryEmail = new EmailAddress
            {
                Value       = string.Format("{0}@domain.tld", Guid.NewGuid()),
                HashedValue = Guid.NewGuid().ToString(),
                IsPrimary   = true,
                IsVerified  = true,
            };
            var secondaryEmail1 = new EmailAddress
            {
                Value       = string.Format("{0}@domain.tld", Guid.NewGuid()),
                HashedValue = Guid.NewGuid().ToString(),
                IsVerified  = true,
            };
            var secondaryEmail2 = new EmailAddress
            {
                Value       = string.Format("{0}@domain.tld", Guid.NewGuid()),
                HashedValue = Guid.NewGuid().ToString(),
            };

            user.EmailAddresses.Add(secondaryEmail1);
            user.EmailAddresses.Add(secondaryEmail2);
            user.EmailAddresses.Add(primaryEmail);
            var data = new[] { user }.AsQueryable();
            var claims = new[]
            {
                new Claim(ClaimTypes.NameIdentifier, userId.ToString(CultureInfo.InvariantCulture)),
            };
            var identity  = new ClaimsIdentity(claims, "authentication type");
            var principal = new GenericPrincipal(identity, null);
            var query     = new UserViewBy(principal);
            var dbSet     = new Mock <DbSet <User> >(MockBehavior.Strict).SetupDataAsync(data);
            var entities  = new Mock <IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet <User>(dbSet.Object, entities.Object);
            IQueryable <User> toBeReturned = entitySet;

            entities.Setup(x => x.Query <User>()).Returns(toBeReturned);
            var handler = new HandleUserViewByQuery(entities.Object);

            UserView result = handler.Handle(query).Result;

            result.ShouldNotBeNull();
            result.UserId.ShouldEqual(user.Id);
            result.UserName.ShouldEqual(user.Name);
            result.PrimaryEmailAddress.ShouldEqual(primaryEmail.Value);
            result.PrimaryEmailHash.ShouldEqual(primaryEmail.HashedValue);
            entities.Verify(x => x.Query <User>(), Times.Once);
        }
Esempio n. 16
0
        public void Handler_ReturnsNullUserView_WhenNotFound_ByPrincipal()
        {
            var userId      = FakeData.Id();
            var otherUserId = FakeData.Id(canNotBe: userId);
            var user        = new ProxiedUser(otherUserId)
            {
                Name = FakeData.String(),
            };
            var primaryEmail = new EmailAddress
            {
                Value       = FakeData.Email(),
                HashedValue = FakeData.String(),
                IsPrimary   = true,
                IsVerified  = true,
            };
            var secondaryEmail1 = new EmailAddress
            {
                Value       = FakeData.Email(),
                HashedValue = FakeData.String(),
                IsVerified  = true,
            };
            var secondaryEmail2 = new EmailAddress
            {
                Value       = FakeData.Email(),
                HashedValue = FakeData.String(),
            };

            user.EmailAddresses.Add(secondaryEmail1);
            user.EmailAddresses.Add(secondaryEmail2);
            user.EmailAddresses.Add(primaryEmail);
            var data = new[] { user }.AsQueryable();
            var claims = new[]
            {
                new Claim(ClaimTypes.NameIdentifier, userId.ToString(CultureInfo.InvariantCulture)),
            };
            var identity  = new ClaimsIdentity(claims, "authenticationType");
            var principal = new GenericPrincipal(identity, null);
            var query     = new UserViewBy(principal);
            var dbSet     = new Mock <DbSet <User> >(MockBehavior.Strict).SetupDataAsync(data);
            var entities  = new Mock <IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet <User>(dbSet.Object, entities.Object);
            IQueryable <User> toBeReturned = entitySet;

            entities.Setup(x => x.Query <User>()).Returns(toBeReturned);
            var handler = new HandleUserViewByQuery(entities.Object);

            UserView result = handler.Handle(query).Result;

            result.ShouldBeNull();
            entities.Verify(x => x.Query <User>(), Times.Once);
        }
Esempio n. 17
0
        public void Handler_ReturnsNonNullUserView_WhenFound_ByName()
        {
            var userId   = FakeData.Id();
            var userName = FakeData.String();
            var user     = new ProxiedUser(userId)
            {
                Name = userName,
            };
            var primaryEmail = new EmailAddress
            {
                Value       = FakeData.Email(),
                HashedValue = FakeData.String(),
                IsPrimary   = true,
                IsVerified  = true,
            };
            var secondaryEmail1 = new EmailAddress
            {
                Value       = FakeData.Email(),
                HashedValue = FakeData.String(),
                IsVerified  = true,
            };
            var secondaryEmail2 = new EmailAddress
            {
                Value       = FakeData.Email(),
                HashedValue = FakeData.String(),
            };

            user.EmailAddresses.Add(secondaryEmail1);
            user.EmailAddresses.Add(secondaryEmail2);
            user.EmailAddresses.Add(primaryEmail);
            var data = new[] { user }.AsQueryable();
            var query     = new UserViewBy(userName);
            var dbSet     = new Mock <DbSet <User> >(MockBehavior.Strict).SetupDataAsync(data);
            var entities  = new Mock <IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet <User>(dbSet.Object, entities.Object);
            IQueryable <User> toBeReturned = entitySet;

            entities.Setup(x => x.Query <User>()).Returns(toBeReturned);
            var handler = new HandleUserViewByQuery(entities.Object);

            UserView result = handler.Handle(query).Result;

            result.ShouldNotBeNull();
            result.UserId.ShouldEqual(user.Id);
            result.UserName.ShouldEqual(user.Name);
            result.PrimaryEmailAddress.ShouldEqual(primaryEmail.Value);
            result.PrimaryEmailHash.ShouldEqual(primaryEmail.HashedValue);
            entities.Verify(x => x.Query <User>(), Times.Once);
        }
Esempio n. 18
0
        public void Handler_ReturnsTrue_WhenFound_ByPrincipal()
        {
            var userId       = FakeData.Id();
            var otherUserId1 = FakeData.Id(userId);
            var otherUserId2 = FakeData.Id(userId, otherUserId1);
            var user         = new ProxiedUser(userId)
            {
                Name = FakeData.String(),
            };
            var otherUser1 = new ProxiedUser(otherUserId1)
            {
                Name = FakeData.String(),
            };
            var otherUser2 = new ProxiedUser(otherUserId2)
            {
                Name = FakeData.String(),
            };
            var localMemberships = new[]
            {
                new LocalMembership {
                    User = otherUser1,
                },
                new LocalMembership {
                    User = user,
                },
                new LocalMembership {
                    User = otherUser2,
                },
            };
            var identity = new ClaimsIdentity(new[]
            {
                new Claim(ClaimTypes.NameIdentifier, user.Id.ToString(CultureInfo.InvariantCulture)),
            });
            var principal = new GenericPrincipal(identity, null);
            var data      = localMemberships.AsQueryable();
            var query     = new UserHasLocalMembership(principal);
            var dbSet     = new Mock <DbSet <LocalMembership> >(MockBehavior.Strict).SetupDataAsync(data);
            var entities  = new Mock <IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet <LocalMembership>(dbSet.Object, entities.Object);

            entities.Setup(x => x.Query <LocalMembership>()).Returns(entitySet);
            var handler = new HandleUserHasLocalMembershipQuery(entities.Object);

            bool result = handler.Handle(query).Result;

            result.ShouldBeTrue();
            entities.Verify(x => x.Query <LocalMembership>(), Times.Once);
        }
Esempio n. 19
0
        public void IsInvalid_WhenLocalMembershipIsFound_ByPrincipal()
        {
            var userId    = FakeData.Id();
            var userName  = FakeData.String();
            var principal = new Mock <IPrincipal>(MockBehavior.Strict);
            var claims    = new[]
            {
                new Claim(ClaimTypes.Name, userName),
                new Claim(ClaimTypes.NameIdentifier, userId.ToString(CultureInfo.InvariantCulture))
            };
            var identity = new ClaimsIdentity(claims);
            var user     = new ProxiedUser(userId)
            {
                Name = userName,
            };
            var localMembership = new LocalMembership {
                User = user,
            };

            principal.SetupGet(x => x.Identity).Returns(identity);
            var queries = new Mock <IProcessQueries>(MockBehavior.Strict);
            var command = new FakeMustNotFindLocalMembershipByPrincipalCommand {
                Principal = principal.Object
            };
            Expression <Func <LocalMembershipByUser, bool> > expectedQuery = x => x.UserId == userId;

            queries.Setup(x => x.Execute(It.Is(expectedQuery)))
            .Returns(Task.FromResult(localMembership));
            var validator = new FakeMustNotFindLocalMembershipByPrincipalValidator(queries.Object);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeFalse();
            Func <ValidationFailure, bool> expectedError = x => x.PropertyName == command.PropertyName(y => y.Principal);

            result.Errors.Count(expectedError).ShouldEqual(1);
            result.Errors.Single(expectedError).ErrorMessage.ShouldEqual(Resources
                                                                         .Validation_LocalMembershipByUser_AlreadyExists
                                                                         .Replace("{PropertyName}", User.Constraints.Label)
                                                                         .Replace("{PropertyValue}", userName)
                                                                         .Replace("{PasswordLabel}", LocalMembership.Constraints.Label.ToLower())
                                                                         );
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Once);
            validator.ShouldHaveValidationErrorFor(x => x.Principal, command.Principal);
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Exactly(2));
        }
Esempio n. 20
0
        public void Handler_ReturnsNullUserView_WhenNotFound_ByName()
        {
            var userId   = new Random().Next(1, int.MaxValue - 3);
            var userName = Guid.NewGuid().ToString();
            var user     = new ProxiedUser(userId)
            {
                Name = Guid.NewGuid().ToString(),
            };
            var primaryEmail = new EmailAddress
            {
                Value       = string.Format("{0}@domain.tld", Guid.NewGuid()),
                HashedValue = Guid.NewGuid().ToString(),
                IsPrimary   = true,
                IsVerified  = true,
            };
            var secondaryEmail1 = new EmailAddress
            {
                Value       = string.Format("{0}@domain.tld", Guid.NewGuid()),
                HashedValue = Guid.NewGuid().ToString(),
                IsVerified  = true,
            };
            var secondaryEmail2 = new EmailAddress
            {
                Value       = string.Format("{0}@domain.tld", Guid.NewGuid()),
                HashedValue = Guid.NewGuid().ToString(),
            };

            user.EmailAddresses.Add(secondaryEmail1);
            user.EmailAddresses.Add(secondaryEmail2);
            user.EmailAddresses.Add(primaryEmail);
            var data = new[] { user }.AsQueryable();
            var query     = new UserViewBy(userName);
            var dbSet     = new Mock <DbSet <User> >(MockBehavior.Strict).SetupDataAsync(data);
            var entities  = new Mock <IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet <User>(dbSet.Object, entities.Object);
            IQueryable <User> toBeReturned = entitySet;

            entities.Setup(x => x.Query <User>()).Returns(toBeReturned);
            var handler = new HandleUserViewByQuery(entities.Object);

            UserView result = handler.Handle(query).Result;

            result.ShouldBeNull();
            entities.Verify(x => x.Query <User>(), Times.Once);
        }
Esempio n. 21
0
        public void Handler_ReturnsNonNullUserView_WhenFound_ById()
        {
            var userId = FakeData.Id();
            var user = new ProxiedUser(userId) { Name = FakeData.String(), };
            var primaryEmail = new EmailAddress
            {
                Value = FakeData.Email(),
                HashedValue = FakeData.String(),
                IsPrimary = true,
                IsVerified = true,
            };
            var secondaryEmail1 = new EmailAddress
            {
                Value = FakeData.Email(),
                HashedValue = FakeData.String(),
                IsVerified = true,
            };
            var secondaryEmail2 = new EmailAddress
            {
                Value = FakeData.Email(),
                HashedValue = FakeData.String(),
            };
            user.EmailAddresses.Add(secondaryEmail1);
            user.EmailAddresses.Add(secondaryEmail2);
            user.EmailAddresses.Add(primaryEmail);
            var data = new[] { user }.AsQueryable();
            var query = new UserViewBy(userId);
            var dbSet = new Mock<DbSet<User>>(MockBehavior.Strict).SetupDataAsync(data);
            var entities = new Mock<IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet<User>(dbSet.Object, entities.Object);
            IQueryable<User> toBeReturned = entitySet;
            entities.Setup(x => x.Query<User>()).Returns(toBeReturned);
            var handler = new HandleUserViewByQuery(entities.Object);

            UserView result = handler.Handle(query).Result;

            result.ShouldNotBeNull();
            result.UserId.ShouldEqual(user.Id);
            result.UserName.ShouldEqual(user.Name);
            result.PrimaryEmailAddress.ShouldEqual(primaryEmail.Value);
            result.PrimaryEmailHash.ShouldEqual(primaryEmail.HashedValue);
            entities.Verify(x => x.Query<User>(), Times.Once);
        }
Esempio n. 22
0
        public void IsCalid_WhenUser_IsFoundById()
        {
            var userId  = FakeData.Id();
            var queries = new Mock <IProcessQueries>(MockBehavior.Strict);
            var command = new FakeMustFindUserByIdCommand {
                UserId = userId
            };
            var user = new ProxiedUser(userId);
            Expression <Func <UserBy, bool> > expectedQuery = x => x.Id == userId;

            queries.Setup(x => x.Execute(It.Is(expectedQuery)))
            .Returns(Task.FromResult(user as User));
            var validator = new FakeMustFindUserByIdValidator(queries.Object);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeTrue();
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Once);
            validator.ShouldNotHaveValidationErrorFor(x => x.UserId, command.UserId);
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Exactly(2));
        }
Esempio n. 23
0
        public void Handler_ReturnsNullUserView_WhenPrincipalIsNull()
        {
            var userId = new Random().Next(1, int.MaxValue - 3);
            var user   = new ProxiedUser(userId)
            {
                Name = Guid.NewGuid().ToString(),
            };
            var data = new[] { user }.AsQueryable();
            var query     = new UserViewBy(null as IPrincipal);
            var dbSet     = new Mock <DbSet <User> >(MockBehavior.Strict).SetupDataAsync(data);
            var entities  = new Mock <IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet <User>(dbSet.Object, entities.Object);
            IQueryable <User> toBeReturned = entitySet;

            entities.Setup(x => x.Query <User>()).Returns(toBeReturned);
            var handler = new HandleUserViewByQuery(entities.Object);

            UserView result = handler.Handle(query).Result;

            result.ShouldBeNull();
            entities.Verify(x => x.Query <User>(), Times.Once);
        }
        public void IsIValid_WhenUserFound_ByNameOrEmail()
        {
            var nameOrEmail = FakeData.Email();
            var queries     = new Mock <IProcessQueries>(MockBehavior.Strict);
            var command     = new FakeMustFindUserByNameOrVerifiedEmailCommand {
                NameOrEmail = nameOrEmail
            };
            var entity = new ProxiedUser(FakeData.Id())
            {
                Name = nameOrEmail
            };
            Expression <Func <UserByNameOrVerifiedEmail, bool> > expectedQuery = x => x.NameOrEmail == nameOrEmail;

            queries.Setup(x => x.Execute(It.Is(expectedQuery))).Returns(Task.FromResult(entity as User));
            var validator = new FakeMustFindUserByNameOrVerifiedEmailValidator(queries.Object);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeTrue();
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Once);
            validator.ShouldNotHaveValidationErrorFor(x => x.NameOrEmail, command.NameOrEmail);
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Exactly(2));
        }
Esempio n. 25
0
        public void Handler_ReturnsNullLocalMembership_WhenNotFound_ByUserId()
        {
            var userId = new Random().Next(3, int.MaxValue);
            var user   = new ProxiedUser(userId - 1);
            var data   = new[]
            {
                new LocalMembership {
                    User = user,
                },
            }.AsQueryable();
            var query     = new LocalMembershipByUser(userId);
            var dbSet     = new Mock <DbSet <LocalMembership> >(MockBehavior.Strict).SetupDataAsync(data);
            var entities  = new Mock <IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet <LocalMembership>(dbSet.Object, entities.Object);

            entities.Setup(x => x.Query <LocalMembership>()).Returns(entitySet);
            var handler = new HandleLocalMembershipByUserQuery(entities.Object);

            LocalMembership result = handler.Handle(query).Result;

            result.ShouldBeNull();
            entities.Verify(x => x.Query <LocalMembership>(), Times.Once);
        }
        public void IsValid_WhenFound_ByPrincipal()
        {
            var userId    = FakeData.Id();
            var userName  = FakeData.String();
            var queries   = new Mock <IProcessQueries>(MockBehavior.Strict);
            var principal = new Mock <IPrincipal>(MockBehavior.Strict);
            var claims    = new[]
            {
                new Claim(ClaimTypes.NameIdentifier, userId.ToString(CultureInfo.InvariantCulture)),
                new Claim(ClaimTypes.Name, userName),
            };
            var identity = new ClaimsIdentity(claims);

            principal.SetupGet(x => x.Identity).Returns(identity);
            var command = new FakeMustFindLocalMembershipByPrincipalCommand {
                Principal = principal.Object,
            };
            var user = new ProxiedUser(userId)
            {
                Name = userName
            };
            var localMembership = new LocalMembership {
                User = user
            };
            Expression <Func <LocalMembershipByUser, bool> > expectedQuery = x => x.UserId == userId;

            queries.Setup(x => x.Execute(It.Is(expectedQuery)))
            .Returns(Task.FromResult(localMembership));
            var validator = new FakeMustFindLocalMembershipByPrincipalValidator(queries.Object);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeTrue();
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Once);
            validator.ShouldNotHaveValidationErrorFor(x => x.Principal, command.Principal);
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Exactly(2));
        }
Esempio n. 27
0
        public void Handler_ReturnsNullUserView_WhenPrincipalIdentity_IsNotClaimsIdentity()
        {
            var userId = FakeData.Id();
            var user   = new ProxiedUser(userId)
            {
                Name = FakeData.String(),
            };
            var data = new[] { user }.AsQueryable();
            var identity  = new GenericIdentity(user.Name, "authentication type");
            var principal = new GenericPrincipal(identity, null);
            var query     = new UserViewBy(principal);
            var dbSet     = new Mock <DbSet <User> >(MockBehavior.Strict).SetupDataAsync(data);
            var entities  = new Mock <IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet <User>(dbSet.Object, entities.Object);
            IQueryable <User> toBeReturned = entitySet;

            entities.Setup(x => x.Query <User>()).Returns(toBeReturned);
            var handler = new HandleUserViewByQuery(entities.Object);

            UserView result = handler.Handle(query).Result;

            result.ShouldBeNull();
            entities.Verify(x => x.Query <User>(), Times.Once);
        }
Esempio n. 28
0
        public void Handler_ReturnsNullUserView_WhenPrincipalIsNull()
        {
            var userId = FakeData.Id();
            var user = new ProxiedUser(userId) { Name = FakeData.String(), };
            var data = new[] { user }.AsQueryable();
            var query = new UserViewBy(null as IPrincipal);
            var dbSet = new Mock<DbSet<User>>(MockBehavior.Strict).SetupDataAsync(data);
            var entities = new Mock<IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet<User>(dbSet.Object, entities.Object);
            IQueryable<User> toBeReturned = entitySet;
            entities.Setup(x => x.Query<User>()).Returns(toBeReturned);
            var handler = new HandleUserViewByQuery(entities.Object);

            UserView result = handler.Handle(query).Result;

            result.ShouldBeNull();
            entities.Verify(x => x.Query<User>(), Times.Once);
        }
        public void Handler_ReturnsTrue_WhenFound_ByPrincipal()
        {
            var userId = FakeData.Id();
            var otherUserId1 = FakeData.Id(userId);
            var otherUserId2 = FakeData.Id(userId, otherUserId1);
            var user = new ProxiedUser(userId) { Name = FakeData.String(), };
            var otherUser1 = new ProxiedUser(otherUserId1) { Name = FakeData.String(), };
            var otherUser2 = new ProxiedUser(otherUserId2) { Name = FakeData.String(), };
            var localMemberships = new[]
            {
                new LocalMembership { User = otherUser1, },
                new LocalMembership { User = user, },
                new LocalMembership { User = otherUser2, },
            };
            var identity = new ClaimsIdentity(new[]
            {
                new Claim(ClaimTypes.NameIdentifier, user.Id.ToString(CultureInfo.InvariantCulture)),
            });
            var principal = new GenericPrincipal(identity, null);
            var data = localMemberships.AsQueryable();
            var query = new UserHasLocalMembership(principal);
            var dbSet = new Mock<DbSet<LocalMembership>>(MockBehavior.Strict).SetupDataAsync(data);
            var entities = new Mock<IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet<LocalMembership>(dbSet.Object, entities.Object);
            entities.Setup(x => x.Query<LocalMembership>()).Returns(entitySet);
            var handler = new HandleUserHasLocalMembershipQuery(entities.Object);

            bool result = handler.Handle(query).Result;

            result.ShouldBeTrue();
            entities.Verify(x => x.Query<LocalMembership>(), Times.Once);
        }
        public void IsValid_WhenAllRulesPass(EmailVerificationPurpose purpose)
        {
            var queries = new Mock<IProcessQueries>(MockBehavior.Strict);
            var validator = new ValidateSendVerificationEmailCommand(queries.Object);
            var command = new SendVerificationEmail
            {
                EmailAddress = "*****@*****.**",
                IsExpectingEmail = true,
                Purpose = purpose,
                SendFromUrl = "[send from this url]",
                VerifyUrlFormat = "[here is the token:' {0}']",
            };
            Expression<Func<EmailAddressBy, bool>> expectedQuery = x => x.Value == command.EmailAddress;
            User user = new ProxiedUser(FakeData.Id());
            var emailAddress = new EmailAddress
            {
                Value = command.EmailAddress,
                User = user,
            };
            queries.Setup(x => x.Execute(It.Is(expectedQuery))).Returns(Task.FromResult(emailAddress));

            var result = validator.Validate(command);

            result.IsValid.ShouldBeTrue();
        }
Esempio n. 31
0
        public void Handler_ReturnsNullUserView_WhenNotFound_ByPrincipal()
        {
            var userId = FakeData.Id();
            var otherUserId = FakeData.Id(canNotBe: userId);
            var user = new ProxiedUser(otherUserId) { Name = FakeData.String(), };
            var primaryEmail = new EmailAddress
            {
                Value = FakeData.Email(),
                HashedValue = FakeData.String(),
                IsPrimary = true,
                IsVerified = true,
            };
            var secondaryEmail1 = new EmailAddress
            {
                Value = FakeData.Email(),
                HashedValue = FakeData.String(),
                IsVerified = true,
            };
            var secondaryEmail2 = new EmailAddress
            {
                Value = FakeData.Email(),
                HashedValue = FakeData.String(),
            };
            user.EmailAddresses.Add(secondaryEmail1);
            user.EmailAddresses.Add(secondaryEmail2);
            user.EmailAddresses.Add(primaryEmail);
            var data = new[] { user }.AsQueryable();
            var claims = new[]
            {
                new Claim(ClaimTypes.NameIdentifier, userId.ToString(CultureInfo.InvariantCulture)), 
            };
            var identity = new ClaimsIdentity(claims, "authenticationType");
            var principal = new GenericPrincipal(identity, null);
            var query = new UserViewBy(principal);
            var dbSet = new Mock<DbSet<User>>(MockBehavior.Strict).SetupDataAsync(data);
            var entities = new Mock<IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet<User>(dbSet.Object, entities.Object);
            IQueryable<User> toBeReturned = entitySet;
            entities.Setup(x => x.Query<User>()).Returns(toBeReturned);
            var handler = new HandleUserViewByQuery(entities.Object);

            UserView result = handler.Handle(query).Result;

            result.ShouldBeNull();
            entities.Verify(x => x.Query<User>(), Times.Once);
        }
Esempio n. 32
0
        public void Handler_ReturnsNullUserView_WhenPrincipalIdentity_HasNoNameIdentifierClaim()
        {
            var userId = FakeData.Id();
            var user = new ProxiedUser(userId) { Name = FakeData.String(), };
            var data = new[] { user }.AsQueryable();
            var claims = new[]
            {
                new Claim(ClaimTypes.Name, userId.ToString(CultureInfo.InvariantCulture)), 
            };
            var identity = new ClaimsIdentity(claims, "authentication type");
            var principal = new GenericPrincipal(identity, null);
            var query = new UserViewBy(principal);
            var dbSet = new Mock<DbSet<User>>(MockBehavior.Strict).SetupDataAsync(data);
            var entities = new Mock<IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet<User>(dbSet.Object, entities.Object);
            IQueryable<User> toBeReturned = entitySet;
            entities.Setup(x => x.Query<User>()).Returns(toBeReturned);
            var handler = new HandleUserViewByQuery(entities.Object);

            UserView result = handler.Handle(query).Result;

            result.ShouldBeNull();
            entities.Verify(x => x.Query<User>(), Times.Once);
        }