public void WhenValidatingUserWithLongDisplayName_ThenValidationFails()
 {
     var user = new User()
                    {AuthorizationId = "authId", DisplayName = new string('a', 16)};
     var exception = Assert.Throws<ValidationException>(() => ValidateUser(user));
     Assert.Contains("DisplayName", exception.ValidationResult.MemberNames);
 }
        public VehicleControllerFixture()
        {
            serviceLocator = new Mock <IServiceLocator>();

            chartDataServiceMock = new Mock <IChartDataService>();

            countryServicesMock = new Mock <ICountryServices>();
            countryServicesMock
            .Setup(r => r.GetCountriesAndRegionsList())
            .Returns(() => new ReadOnlyCollection <string>(new[] { "a", "b" }));

            defaultUser = new User {
                AuthorizationId = "TestClaimsIdentifier", UserId = 5
            };
            defaultUserInfo = new UserInfo
            {
                ClaimsIdentifier = defaultUser.AuthorizationId,
                UserId           = defaultUser.UserId
            };

            userServicesMock = new Mock <IUserServices>();
            userServicesMock
            .Setup(s => s.GetUserByClaimedIdentifier(defaultUser.AuthorizationId))
            .Returns(defaultUser);
        }
        public void WhenJsonGetFleetStatisticSeries_ThenReturnsJsonData()
        {
            var user = new User {UserId = 1, AuthorizationId = "AuthorizationId", DisplayName = "DisplayName"};

            userServicesMock.Setup(r => r.GetUserByClaimedIdentifier(It.IsAny<string>())).Returns(user);

            var target = new HomeController(
                userServicesMock.Object,
                chartDataServiceMock.Object);

            target.SetFakeControllerContext();
            target.SetUserIdentity(new MileageStatsIdentity(this.user.AuthorizationId,
                                                            this.user.DisplayName,
                                                            this.user.UserId));

            var series = new StatisticSeries();

            chartDataServiceMock.Setup(x => x.CalculateSeriesForUser(user.UserId, null, null)).Returns(
                series);

            ActionResult actual = target.JsonGetFleetStatisticSeries();

            var actualJsonResult = actual as JsonResult;
            Assert.NotNull(actualJsonResult);
            Assert.Same(series, actualJsonResult.Data);
        }
        public User GetOrCreateUser(string claimedId)
        {
            User user = null;
            try
            {
                user = this.GetUserByClaimedIdentifier(claimedId);
            }
            catch (BusinessServicesException)
            {
                user = null;
            }

            if (user == null)
            {
                user = new User()
                           {
                               AuthorizationId = claimedId,
                               DisplayName = Resources.NewUserDefaultDisplayName,
                           };

                user = this.CreateUser(user);
            }

            return user;
        }
 public void WhenValidatingUserWithLongPostalCode_ThenValidationFails()
 {
     var user = new User()
                    {AuthorizationId = "authId", DisplayName = "name", PostalCode = "12345678901234567890"};
     var exception = Assert.Throws<ValidationException>(() => ValidateUser(user));
     Assert.Contains("PostalCode", exception.ValidationResult.MemberNames);
 }
        public void WhenCountryIsNotSet_ThenAlphaNumericPostalCodeAllowed()
        {
            User user = new User() {DisplayName = "Name", AuthorizationId = "AuthId", PostalCode = "a1b2c3"};

            List<ValidationResult> results = new List<ValidationResult>();
            bool isValid = ValidateUserPostalCode(user, ref results);
            Assert.True(isValid);
            Assert.Equal(0, results.Count);
        }
        public void InvokesUserRepository()
        {
            var user = new User {};

            var handler = new CreateUser(_userRepo.Object);
            handler.Execute("id");

            _userRepo
                .Verify(r => r.Create(It.Is<User>(u => u.AuthorizationId == "id")), Times.Once());
        }
        public void WhenCountryIsNotSetNorIsPostalCodeSet_ThenCustomerIsValid()
        {
            User user = new User() {DisplayName = "Name", AuthorizationId = "AuthId"};

            List<ValidationResult> results = new List<ValidationResult>();
            bool isValid = ValidateUserPostalCode(user, ref results);

            Assert.True(isValid);
            Assert.Equal(0, results.Count);
        }
 public void WhenValidatingUserWithLongCountry_ThenValidationFails()
 {
     var user = new User()
                    {
                        DisplayName = "name",
                        AuthorizationId = "authId",
                        Country = "ThisIsAVeryVeryVeryVeryVeryLongStringThatShouldNotBeAllowed"
                    };
     var exception = Assert.Throws<ValidationException>(() => ValidateUser(user));
     Assert.Contains("Country", exception.ValidationResult.MemberNames);
 }
Exemplo n.º 10
0
 public void WhenValidatingUserWithCountryAsUSAndFiveDigitNumericPostalCode_ThenValidationSucceeds()
 {
     var user = new User()
                    {
                        AuthorizationId = "authId",
                        DisplayName = "name",
                        Country = "United States",
                        PostalCode = "12345"
                    };
     ValidateUser(user);
 }
        private void InitializeFixture()
        {
            user = new User {UserId = 1};
            userServicesMock = new Mock<IUserServices>();
            userServicesMock.Setup(r => r.GetUserByClaimedIdentifier(It.IsAny<string>())).Returns(user);
            chartDataServiceMock = new Mock<IChartDataService>();

            countryServicesMock = new Mock<ICountryServices>();
            countryServicesMock.Setup(r => r.GetCountriesAndRegionsList()).Returns(
                () => new ReadOnlyCollection<string>(new List<string> {"a", "b"}));
        }
        private static UserInfo CreateUserContextFromUser(User user)
        {
            var userContext = new UserInfo
            {
                UserId = user.UserId,
                DisplayName = user.DisplayName,
                ClaimsIdentifier = user.AuthorizationId
            };

            return userContext;
        }
        private void InitializeFixture()
        {
            this.defaultTestUser = new User()
                                       {
                                           AuthorizationId = "TestAuthorizationId",
                                           DisplayName = "DefaultTestUser"
                                       };

            var repository = new UserRepository();
            repository.Create(this.defaultTestUser);
        }
Exemplo n.º 14
0
 public void WhenValidatingUserWithCountryAsUSAndShortNumericPostalCode_ThenValidationFails()
 {
     var user = new User()
                    {
                        AuthorizationId = "authId",
                        DisplayName = "name",
                        Country = "United States",
                        PostalCode = "123"
                    };
     var exception = Assert.Throws<ValidationException>(() => ValidateUser(user));
     Assert.Contains("PostalCode", exception.ValidationResult.MemberNames);
 }
Exemplo n.º 15
0
 public void WhenValidatingUserWithCountryAsUSAndAlphaNumericPostalCode_ThenValidationFails()
 {
     var user = new User()
                    {
                        AuthorizationId = "authId",
                        DisplayName = "name",
                        TwoLetterCountryCode = "US",
                        PostalCode = "a1b2c3"
                    };
     var exception = Assert.Throws<ValidationException>(() => ValidateUser(user));
     Assert.Contains("PostalCode", exception.ValidationResult.MemberNames);
 }
        public ReminderControllerFixture()
        {
            _serviceLocator = new Mock <IServiceLocator>();

            _defaultUser = new User {
                AuthorizationId = "TestClaimsIdentifier", UserId = 5
            };

            _mockUserServices = new Mock <IUserServices>();
            _mockUserServices
            .Setup(s => s.GetUserByClaimedIdentifier(_defaultUser.AuthorizationId))
            .Returns(_defaultUser);
        }
        /// <summary>
        /// Creates a new <see cref="FormsAuthenticationTicket"/> from a user.
        /// </summary>
        /// <param name="user"></param>
        /// <param name="issueDate"></param>
        /// <param name="isPersistent"></param>
        /// <returns></returns>
        /// <remarks>
        /// Encodes the <see cref="UserInfo"/> into the <see cref="FormsAuthenticationTicket.UserData"/> property
        /// of the authentication ticket.  This can be recovered by using the <see cref="UserInfo.FromString"/> method.
        /// </remarks>
        public static FormsAuthenticationTicket CreateAuthenticationTicket(User user, DateTime issueDate, bool isPersistent)
        {
            UserInfo userInfo = CreateUserContextFromUser(user);

            var ticket = new FormsAuthenticationTicket(
                1,
                user.AuthorizationId,
                issueDate,
                DateTime.Now.Add(FormsAuthentication.Timeout),
                isPersistent,
                userInfo.ToString());

            return ticket;
        }
        /// <summary>
        /// Creates a new <see cref="FormsAuthenticationTicket"/> from a user.
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        /// <remarks>
        /// Encodes the <see cref="UserInfo"/> into the <see cref="FormsAuthenticationTicket.UserData"/> property
        /// of the authentication ticket.  This can be recovered by using the <see cref="UserInfo.FromString"/> method.
        /// </remarks>
        public static FormsAuthenticationTicket CreateAuthenticationTicket(User user)
        {
            UserInfo userInfo = CreateUserContextFromUser(user);

            var ticket = new FormsAuthenticationTicket(
                1,
                user.AuthorizationId,
                DateTime.Now,
                DateTime.Now.Add(FormsAuthentication.Timeout),
                false,
                userInfo.ToString());

            return ticket;
        }
 public User CreateUser(User newUser)
 {
     if (newUser == null) throw new ArgumentNullException("newUser");
     try
     {
         Model.User userToAdd = ToDataModelUser(newUser);
         this.userRepository.Create(userToAdd);
         return ToServiceUser(userToAdd);
     }
     catch (InvalidOperationException ex)
     {
         throw new BusinessServicesException(Resources.UnableToCreateUserExceptionMessage, ex);
     }
 }
        public void WhenRequestingAvailableUserByAuthenticatedId_ThenReturnsUserFromRepository()
        {
            var userData = new User
                                {
                                    AuthorizationId = "TestId",
                                    DisplayName = "TestDisplayName",
                                };

            var userRepository = new UserRepository();
            userRepository.Create(userData);

            var retrievedUser = userRepository.GetByAuthenticatedId(userData.AuthorizationId);

            Assert.NotNull(retrievedUser);
        }
Exemplo n.º 21
0
        public void ThenUserReturned()
        {
            var user = new User { UserId = 1, DisplayName = "a friendly name" };

            _userRepo
                .Setup(ur => ur.GetByAuthenticatedId("1"))
                .Returns(new User { UserId = 1, DisplayName = "a friendly name" });

            var handler = new GetUserByClaimId(_userRepo.Object);

            var retrievedUser = handler.Execute("1");

            Assert.NotNull(retrievedUser);
            Assert.Equal(user.UserId, retrievedUser.UserId);
        }
        public void WhenAddingUser_ThenUserPersists()
        {
            var userRepository = new UserRepository();

            var newUser = new User
                              {
                                  AuthorizationId = "AnAuthorizationId",
                                  DisplayName = "TheDisplayName",
                              };

            userRepository.Create(newUser);

            Assert.NotNull(userRepository.Set
                .Where(u => u.AuthorizationId == newUser.AuthorizationId).First());
        }
        public void WhenCountryIsUnitedStates_ThenAlphaNumericPostalCodeIsNotAllowed()
        {
            User user = new User()
                            {
                                DisplayName = "Name",
                                AuthorizationId = "AuthId",
                                TwoLetterCountryCode = "US",
                                PostalCode = "a1b2c3"
                            };
            List<ValidationResult> results = new List<ValidationResult>();
            bool isValid = ValidateUserPostalCode(user, ref results);

            Assert.False(isValid);
            Assert.Contains("PostalCode", results[0].MemberNames);
        }
Exemplo n.º 24
0
        public virtual void Execute(User user)
        {
            if (user == null) throw new ArgumentNullException("user");

            User userToUpdate = new User
            {
                UserId = user.UserId,
                AuthorizationId = user.AuthorizationId,
                DisplayName = user.DisplayName,
                Country = user.Country,
                HasRegistered = user.HasRegistered,
            };

            _userRepository.Update(userToUpdate);
        }
Exemplo n.º 25
0
        public void Seed(int? userId)
        {
            if(!userId.HasValue)
            {
                var user = new User
                {
                    AuthorizationId = "http://not/a/real/openid/url",
                    DisplayName = "Sample User",
                    Country = "United States"
                };
                _users.Create(user);
                userId = user.UserId;
            }

            SeedVehicles(userId.Value);
        }
        public void WhenAddingUser_ThenUserReturnsPopulatedNewUser()
        {
            var userRepository = new UserRepository();

            const string authorizationId = "AnAuthorizationId";
            const string displayName = "TheDisplayName";
            var newUser = new User
                              {
                                  AuthorizationId = authorizationId,
                                  DisplayName = displayName,
                              };

            userRepository.Create(newUser);

            Assert.NotNull(newUser);
            Assert.Equal(authorizationId, newUser.AuthorizationId);
            Assert.Equal(displayName, newUser.DisplayName);
        }
        private void InitializeFixture()
        {
            defaultTestUser = new User()
                                       {
                                           AuthorizationId = "TestAuthorizationId",
                                           DisplayName = "DefaultTestUser"
                                       };

            var userRepository = new UserRepository();
            userRepository.Create(defaultTestUser);

            int userId = defaultTestUser.UserId;

            var vehicleRepository = new VehicleRepository();
            defaultVehicle = new Vehicle()
                                      {
                                          Name = "Test Vehicle"
                                      };
            vehicleRepository.Create(defaultTestUser.UserId, defaultVehicle);
        }
        public ActionResult Edit(User user, string action = null)
        {
            if (action == "cancel")
            {
                //Reset user profile with current user info
                user.Country = CurrentUser.Country;
                user.DisplayName = CurrentUser.DisplayName;
            }
                
            user.UserId = CurrentUserId;
            user.HasRegistered = true;
                
            _updateUser.Execute(user);

            var ticket = _formsAuthentication.GetAuthenticationTicket(HttpContext);

            if (ticket != null)
            {
                _formsAuthentication.SetAuthCookie(HttpContext,
                    UserAuthenticationTicketBuilder.CreateAuthenticationTicket(
                    user, ticket.IssueDate, ticket.IsPersistent));
            }

            if (ModelState.IsValid || action == "cancel")
            {
                if (Request.IsAjaxRequest())
                    return new HttpStatusCodeResult((int)HttpStatusCode.OK, Messages.ProfileController_ProfileUpdated);

                if(action != "cancel")
                {
                    this.SetConfirmationMessage(Messages.ProfileController_ProfileUpdated);
                }

                return RedirectToAction("Index", "Dashboard");
            }

            if (Request.IsAjaxRequest())
                return new HttpStatusCodeResult((int)HttpStatusCode.BadRequest, Messages.ProfileController_InvalidData);

            return SetupProfileForm(user);
        }
Exemplo n.º 29
0
        public virtual User Execute(string authorizationId)
        {
            if (string.IsNullOrEmpty(authorizationId))
                throw new ArgumentNullException(authorizationId);

            var user = new User 
            { 
                AuthorizationId = authorizationId,
            };

            this._userRepository.Create(user);

            return new User
                {
                    UserId = user.UserId,
                    Country = user.Country,
                    HasRegistered = user.HasRegistered,
                    AuthorizationId = user.AuthorizationId,
                    DisplayName = user.DisplayName
                };
        }
        public void InvokesUserRepository()
        {
            var user = new User
            {
                AuthorizationId = "id",
                Country = "country",
                DisplayName = "displayName",
                HasRegistered = true,
                UserId = 1
            };

            var handler = new UpdateUser(_userRepo.Object);
            handler.Execute(user);

            _userRepo
                .Verify(r => r.Update(It.Is<User>(u => 
                    u.AuthorizationId == user.AuthorizationId &&
                    u.Country == user.Country &&
                    u.DisplayName == user.DisplayName &&
                    u.HasRegistered == user.HasRegistered &&
                    u.UserId == user.UserId)), Times.Once());
        }
        public void WhenCountryIsUnitedStates_ThenFiveDigitNumericPostalCodeIsAllowed()
        {
            User user = new User()
                            {
                                DisplayName = "Name",
                                AuthorizationId = "AuthId",
                                TwoLetterCountryCode = "US",
                                PostalCode = "12345"
                            };

            List<ValidationResult> results = new List<ValidationResult>();
            bool isValid = ValidateUserPostalCode(user, ref results);
            Assert.True(isValid);
            Assert.Equal(0, results.Count);
        }
 private static bool ValidateUserPostalCode(User user, ref List<ValidationResult> results)
 {
     var context = new ValidationContext(user, null, null);
     return Validator.TryValidateObject(user, context, results, true);
 }