public void CreatePropertyState_StateNotExists_StateCreated()
        {
            // Arrange
            var unitOfWork = new UnitOfWorkMock();
            var service = new PropertyStateService(unitOfWork);
            var property = CreateProperty();
            unitOfWork.GetRepository<Property>().Insert(property);
            unitOfWork.GetRepository<Property>().Insert(property);
            unitOfWork.Save();

            // Act
            service.CreateState(property, "newValue");

            // Assert
            unitOfWork.GetRepository<PropertyState>().Get().Should().Contain(x => x.Value == "newValue");
        }
        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 CreatePropertyState_StateNotExists_NewStateReturned()
        {
            // Arrange
            var unitOfWork = new UnitOfWorkMock();
            var service = new PropertyStateService(unitOfWork);
            var property = CreateProperty();
            unitOfWork.GetRepository<Property>().Insert(property);
            unitOfWork.GetRepository<Property>().Insert(property);
            unitOfWork.Save();

            // Act
            var newState = service.CreateState(property, "newValue");

            // Assert
            newState.Should().Be(unitOfWork.GetRepository<PropertyState>().Get().Last());
        }
        public void CreatePropertyState_StateOfSamePropertyExists_StateHasUniquePropertyStateIndex()
        {
            // Arrange
            var unitOfWork = new UnitOfWorkMock();
            var service = new PropertyStateService(unitOfWork);
            var property = CreateProperty();
            var propertyState = CreatePropertyState(property);
            propertyState.Index = 2;
            unitOfWork.GetRepository<PropertyState>().Insert(propertyState);
            unitOfWork.Save();

            // Act
            service.CreateState(property, "newValue");

            // Assert
            unitOfWork.GetRepository<PropertyState>().Get().Last().Index.Should().Be(3);
        }
        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 CreatePropertyState_StateExists_ExceptionThrown()
        {
            // Arrange
            var unitOfWork = new UnitOfWorkMock();
            var service = new PropertyStateService(unitOfWork);
            var property = CreateProperty();
            var propertyState = CreatePropertyState(property);
            unitOfWork.GetRepository<PropertyState>().Insert(propertyState);

            // Act, Assert
            service.Invoking(srv => srv.CreateState(property, propertyState.Value))
                .ShouldThrow<InvalidOperationException>();
        }
        private void InitUnitOfWork(UnitOfWorkMock unitOfWork)
        {
            var property1 = new Property {Name = "property1", Order = 1, ID = 1};
            var property2 = new Property {Name = "property2", Order = 2, ID = 2};

            var state1 = new PropertyState {Property = property1, Value = "state1"};
            var state2 = new PropertyState {Property = property1, Value = "state2"};
            var state3 = new PropertyState {Property = property2, Value = "state3"};
            var state4 = new PropertyState {Property = property2, Value = "state4"};

            property1.States = new List<PropertyState> {state1, state2};
            property2.States = new List<PropertyState> {state3, state4};

            var package1 = new ContentPackage
                           {
                               ID = 1,
                               PropertyStates = new Collection<PropertyState> {state1, state3}
                           };

            var package2 = new ContentPackage
                           {
                               ID = 2,
                               PropertyStates = new Collection<PropertyState> {state2, state4}
                           };

            state1.ContentPackages = new Collection<ContentPackage> {package1};
            state2.ContentPackages = new Collection<ContentPackage> {package2};
            state3.ContentPackages = new Collection<ContentPackage> {package1};
            state4.ContentPackages = new Collection<ContentPackage> {package2};

            unitOfWork.GetRepository<Property>().Insert(property1);
            unitOfWork.GetRepository<Property>().Insert(property2);

            unitOfWork.GetRepository<PropertyState>().Insert(state1);
            unitOfWork.GetRepository<PropertyState>().Insert(state2);
            unitOfWork.GetRepository<PropertyState>().Insert(state3);
            unitOfWork.GetRepository<PropertyState>().Insert(state4);

            unitOfWork.GetRepository<ContentPackage>().Insert(package1);
            unitOfWork.GetRepository<ContentPackage>().Insert(package2);

            unitOfWork.Save();
        }
        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();
        }
        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 void CreatePropertyState_StateOfSamePropertyNotExists_StateHasUniquePropertyStateIndex()
        {
            // Arrange
            var unitOfWork = new UnitOfWorkMock();
            var service = new PropertyStateService(unitOfWork);
            var propertyState = CreatePropertyState(CreateProperty(id: 1));
            unitOfWork.GetRepository<PropertyState>().Insert(propertyState);
            unitOfWork.Save();

            // Act
            service.CreateState(CreateProperty(id: 2), "newValue");

            // Assert
            unitOfWork.GetRepository<PropertyState>().Get().Should().Contain(x => x.Index == 1);
        }
        public void GetState_StateExists_StateReturned()
        {
            // Arrange
            var unitOfWork = new UnitOfWorkMock();
            var propertyStateService = new PropertyStateService(unitOfWork);
            var property = CreateProperty();
            var propertyState = CreatePropertyState(property);
            unitOfWork.GetRepository<PropertyState>().Insert(propertyState);
            unitOfWork.Save();

            // Act
            var state = propertyStateService.GetState(property.ID, propertyState.Value);

            // Assert
            state.Should().Be(propertyState);
        }
        public void GetState_PropertyNotExists_NullReturned()
        {
            // Arrange
            var unitOfWork = new UnitOfWorkMock();
            var propertyStateService = new PropertyStateService(unitOfWork);
            var property = CreateProperty();
            var propertyState = CreatePropertyState(property);
            unitOfWork.GetRepository<PropertyState>().Insert(propertyState);

            // Act
            var state = propertyStateService.GetState(2, "value");

            // Assert
            state.Should().BeNull();
        }