public bool UserHasPermissions(User user, ContentPackage package)
        {
            if (package.Owner.ID == user.ID)
                return true;

            return UserHasOrder(user, package);
        }
        public static User CreateUser(string username = "******",
            string password = "******",
            string email = "email",
            string passwordQuestion = "passwordQuestion",
            string passwordAnswer = "passwordAnswer",
            bool isApproved = true,
            int id = 0)
        {
            var user = new User
                       {
                           ID = id,
                           UserName = username,
                           Password = password,
                           Email = email,
                           PasswordQuestion = passwordQuestion,
                           PasswordAnswer = passwordAnswer,
                           IsApproved = isApproved,
                           IsLocked = false,
                           LastPasswordChangedDate = DateTime.Today,
                           CreateDate = DateTime.Today,
                           LastActivityDate = DateTime.Today,
                           LastLockoutDate = DateTime.MinValue.ToLocalTime(),
                           LastLoginDate = DateTime.MinValue.ToLocalTime(),
                           Roles = new Collection<Role>()
                       };

            return user;
        }
 public bool UserHasOrder(User user, ContentPackage package)
 {
     var profileService = new ProfileService(UnitOfWork);
     var profile = profileService.GetUserProfile(user);
     var order = UnitOfWork.GetRepository<Order>().Get(o => o.ContentPackage.ID == package.ID
                                                     && o.UserProfile.ID == profile.ID)
         .FirstOrDefault();
     return order != null;
 }
        public UserProfile GetUserProfile(User user)
        {
            if (user == null)
                throw new ArgumentNullException("user");

            return UnitOfWork.GetRepository<UserProfile>()
                .Get(profile => profile.User.ID == user.ID)
                .Single();
        }
 public void CreateUserProfile(User user, string phoneNumber)
 {
     var profile = new UserProfile
                       {
                           User = user,
                           Balance = 0,
                           PhoneNumber = phoneNumber
                       };
     UnitOfWork.GetRepository<UserProfile>().Insert(profile);
     UnitOfWork.Save();
 }
        public FinanceControllerTests()
        {
            _unitOfWork = new UnitOfWorkMock();
            _controller = new FinanceController(_unitOfWork);

            _currentUser = new User {UserName = "******"};
            _currentUserProfile = new UserProfile {User = _currentUser};

            _unitOfWork.GetRepository<User>().Insert(_currentUser);
            _unitOfWork.GetRepository<UserProfile>().Insert(_currentUserProfile);
            _unitOfWork.Save();

            _controller.ControllerContext = CreateControllerContext(_currentUser);
        }
        public void UserHasPermissions_UserIsOwner_UserHasPermissions()
        {
            // Arrange
            var unitOfWork = new UnitOfWorkMock();
            var user = new User();
            var package = new ContentPackage {Owner = user};

            // Act
            var financeService = new FinanceService(unitOfWork);
            var userHasPermissions = financeService.UserHasPermissions(user, package);

            // Assert
            userHasPermissions.Should().BeTrue();
        }
        public ContentControllerTests()
        {
            _unitOfWork = new UnitOfWorkMock();
            _fileStorageManager = new FileStorageManagerMock();

            _currentUser = new User {UserName = "******"};
            _currentUserProfile = new UserProfile {User = _currentUser};
            _unitOfWork.GetRepository<User>().Insert(_currentUser);
            _unitOfWork.GetRepository<UserProfile>().Insert(_currentUserProfile);

            _controller = new ContentController(_unitOfWork,
                                                _fileStorageManager,
                                                new FinanceService(_unitOfWork));
            _controller.ControllerContext = CreateControllerContext();
            InitUnitOfWork(_unitOfWork);
        }
        public void UserDetails_Simple_UserInformationProvided()
        {
            // Arrange
            var membershipService = new Mock<IMembershipService>();
            var profileService = new Mock<IProfileService>();
            var user = new User();
            var userProfile = new UserProfile();
            membershipService.Setup(s => s.GetUser(3)).Returns(user);
            profileService.Setup(s => s.GetUserProfile(user)).Returns(userProfile);
            var controller = new AdminController(membershipService.Object,profileService.Object);

            // Act
            var result = controller.UserDetails(3);

            // Assert
            var viewModel = (UserDetailsViewModel)result.Model;
            viewModel.User.Should().Be(user);
            viewModel.UserProfile.Should().Be(userProfile);
        }
        public void UserHasPermissions_UserNotOrderedPackage_UserHasNoPermissions()
        {
            // Arrange
            var unitOfWork = new UnitOfWorkMock();
            var user = new User();
            var userProfile = new UserProfile {User = user};
            var package = new ContentPackage {Owner = new User()};
            unitOfWork.GetRepository<User>().Insert(user);
            unitOfWork.GetRepository<UserProfile>().Insert(userProfile);
            unitOfWork.GetRepository<ContentPackage>().Insert(package);
            unitOfWork.Save();

            // Act
            var financeService = new FinanceService(unitOfWork);
            var userHasPermissions = financeService.UserHasPermissions(user, package);

            // Assert
            userHasPermissions.Should().BeFalse();
        }
        public void Register_Simple_UserCreated()
        {
            // Arrange
            var unitOfWork = new UnitOfWorkMock();
            var user = new User();
            var membershipServiceMock = MembershipServiceMock(user, MembershipCreateStatus.Success);
            var roleService = new Mock<IRoleService>().Object;
            var authenticationProvider = new Mock<IAuthenticationProvider>().Object;

            var controller = new AccountController(unitOfWork,
                                                   membershipServiceMock.Object,
                                                   roleService,
                                                   new ProfileService(unitOfWork),
                                                   authenticationProvider);
            var viewModel = new RegisterViewModel
            {
                UserName = "******",
                Email = "[email protected]",
                Password = "******",
                ConfirmPassword = "******",
                PhoneNumber = "000000"
            };

            // Act
            controller.Register(viewModel);

            // Assert
            MembershipCreateStatus status;
            membershipServiceMock.Verify(x => x.CreateUser(viewModel.UserName,
                                                           viewModel.Password,
                                                           viewModel.Email,
                                                           null,
                                                           null,
                                                           true,
                                                           out status),
                                         Times.Once());
        }
 private void AddUserViaProvider(User user)
 {
     _service.CreateUser(user.UserName,
                         user.Password,
                         user.Email,
                         user.PasswordQuestion,
                         user.PasswordAnswer,
                         user.IsApproved,
                         out _membershipCreateStatus);
 }
 private void BindUserToRole(User user, Role role)
 {
     user.Roles.Add(role);
     role.Users.Add(user);
 }
 private void AddUserDirectly(User user)
 {
     _unitOfWork.GetRepository<User>().Insert(user);
     _unitOfWork.Save();
 }
        public void Register_UserCreationSucceded_UserProfileCreated()
        {
            // Arrange
            var unitOfWork = new UnitOfWorkMock();
            var user = new User();
            var roleService = new Mock<IRoleService>().Object;
            var membershipServiceMock = MembershipServiceMock(user, MembershipCreateStatus.Success);
            var authenticationProvider = new Mock<IAuthenticationProvider>().Object;

            var controller = new AccountController(unitOfWork,
                                                   membershipServiceMock.Object,
                                                   roleService,
                                                   new ProfileService(unitOfWork),
                                                   authenticationProvider);
            var viewModel = new RegisterViewModel
                            {
                                PhoneNumber = "000000"
                            };

            // Act
            controller.Register(viewModel);

            // Assert
            var userProfile = unitOfWork.GetRepository<UserProfile>().Get().Single();
            userProfile.User.Should().Be(user);
            userProfile.PhoneNumber.Should().Be(viewModel.PhoneNumber);
        }
        public User CreateUser(string username,
            string password,
            string email,
            string passwordQuestion,
            string passwordAnswer,
            bool isApproved,
            out MembershipCreateStatus status)
        {
            if (RequiresEmail)
            {
                if (string.IsNullOrEmpty(email))
                {
                    status = MembershipCreateStatus.InvalidEmail;
                    return null;
                }
            }

            if (RequiresUniqueEmail && !string.IsNullOrEmpty(email) && GetUserByEmail(email) != null)
            {
                status = MembershipCreateStatus.DuplicateEmail;
                return null;
            }

            if (RequiresQuestionAndAnswer)
            {
                if (string.IsNullOrEmpty(passwordQuestion))
                {
                    status = MembershipCreateStatus.InvalidQuestion;
                    return null;
                }
                if (string.IsNullOrEmpty(passwordAnswer))
                {
                    status = MembershipCreateStatus.InvalidAnswer;
                    return null;
                }
            }

            var existingUser = GetUser(username);
            if (existingUser != null)
            {
                status = MembershipCreateStatus.DuplicateUserName;
                return null;
            }

            var user = new User
                       {
                           UserName = username.ToLower(),
                           Password = password,
                           Email = email.ToLower(),
                           IsApproved = isApproved,
                           PasswordQuestion = passwordQuestion,
                           PasswordAnswer = passwordAnswer,
                           LastPasswordChangedDate = DateTime.Now,
                           LastActivityDate = DateTime.Now,
                           LastLockoutDate = null,
                           LastLoginDate = null,
                       };

            UnitOfWork.GetRepository<User>().Insert(user);
            UnitOfWork.Save();

            status = MembershipCreateStatus.Success;
            return user;
        }
        private ContentPackage CreatePackage(int price = 100)
        {
            _ownerUser = new User {UserName = "******"};
            _ownerProfile = new UserProfile {User = _ownerUser};
            _unitOfWork.GetRepository<User>().Insert(_ownerUser);
            _unitOfWork.GetRepository<UserProfile>().Insert(_ownerProfile);

            var package = new ContentPackage
                          {
                              Price = price,
                              Owner = _ownerUser
                          };

            return package;
        }
 private ControllerContext CreateControllerContext(User user)
 {
     var contextMock = new Mock<ControllerContext>();
     contextMock.Setup(context => context.HttpContext.User.Identity.Name).Returns(user.UserName);
     contextMock.Setup(context => context.HttpContext.User.Identity.IsAuthenticated).Returns(true);
     return contextMock.Object;
 }
        public void Register_UserCreationSucceded_UserRoleAssignedToUser()
        {
            // Arrange
            var unitOfWork = new UnitOfWorkMock();
            var user = new User();
            var membershipServiceMock = MembershipServiceMock(user, MembershipCreateStatus.Success);
            var roleService = new Mock<IRoleService>();
            var authenticationProvider = new Mock<IAuthenticationProvider>().Object;

            var controller = new AccountController(unitOfWork,
                                                   membershipServiceMock.Object,
                                                   roleService.Object,
                                                   new ProfileService(unitOfWork),
                                                   authenticationProvider);

            // Act
            controller.Register(new RegisterViewModel());

            // Assert
            roleService.Verify(rs => rs.AddUsersToRoles(new[] { user.UserName }, new[] { "user" }));
        }
 private Mock<IMembershipService> MembershipServiceMock(User user,
     MembershipCreateStatus membershipCreateStatus)
 {
     var mock = new Mock<IMembershipService>();
     var membershipServiceMock = mock;
     membershipServiceMock.Setup(
         s => s.CreateUser(It.IsAny<string>(),
                           It.IsAny<string>(),
                           It.IsAny<string>(),
                           It.IsAny<string>(),
                           It.IsAny<string>(),
                           It.IsAny<bool>(),
                           out membershipCreateStatus))
         .Returns(user);
     return membershipServiceMock;
 }
        public void Register_UserCreationFailed_UserProfileNotCreated()
        {
            // Arrange
            var unitOfWork = new UnitOfWorkMock();
            var user = new User();
            var membershipServiceMock = MembershipServiceMock(user, MembershipCreateStatus.ProviderError);
            var roleService = new Mock<IRoleService>().Object;
            var authenticationProvider = new Mock<IAuthenticationProvider>().Object;

            var controller = new AccountController(unitOfWork,
                                                   membershipServiceMock.Object,
                                                   roleService,
                                                   new ProfileService(unitOfWork),
                                                   authenticationProvider);

            // Act
            controller.Register(new RegisterViewModel());

            // Assert
            var userProfiles = unitOfWork.GetRepository<UserProfile>().Get();
            userProfiles.Should().BeEmpty();
            controller.ModelState.IsValid.Should().BeFalse();
        }