コード例 #1
0
        /// <summary>
        /// Validates the user.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <exception cref="ValidationException">User is not valid.</exception>
        private static void ValidateUser(User user)
		{

			var details = new Dictionary<string, string> ();
		
			if (string.IsNullOrWhiteSpace (user.FirstName))
				details ["FirstName"] = "First name cannot be empty.";

			if (string.IsNullOrWhiteSpace (user.LastName))
				details ["LastName"] = "Last name cannot be empty.";

			if (string.IsNullOrWhiteSpace (user.PhoneNumber) || !Regex.IsMatch (user.PhoneNumber, @"^\d{3}-?\d{3}-?\d{4}$"))
				details ["PhoneNumber"] = "Enter a valid U.S. phone number. 000-000-0000";
			else
				user.PhoneNumber = user.PhoneNumber.Replace ("-", string.Empty);

			if (string.IsNullOrWhiteSpace (user.Email) || !Regex.IsMatch (user.Email, "^.*[@]{1}\\w*[.]{1}\\w{2,4}$"))
				details ["Email"] = "Please enter a valid email.";

			if (string.IsNullOrWhiteSpace (user.Password) || !Regex.IsMatch (user.Password, "^.{8,25}$"))
				details["Password"] = "******";
				
			if (details.Count > 0)
				throw new ValidationException (ErrorCodes.EntityValidationError, details, "User is not valid.");
		}
コード例 #2
0
        public void LoginTest()
        {

            const string userName = "******";
            const string password = "******";

            var dataAccess = Mock.Interface<IDataAccess>();

            var expectedUser = new User
            {
                    Email = userName,
                    Password = PasswordHash.HashPassword (password),
                    PhoneNumber = "000-000-0000",
            };

            var users = new [] { expectedUser };
            Expect.MethodCall( () => dataAccess.All<User>() ).Returns( users.AsQueryable() );

            var userService = new UserService(dataAccess);

            User actualUser;
            userService.Login(userName, password, out actualUser);

            Assert.IsTrue(actualUser == expectedUser);

        }
コード例 #3
0
        public void InvalidCredsTest()
        {

            const string userName = "******";
            const string password = "******";

            var dataAccess = Mock.Interface<IDataAccess>();
            var userService = new UserService(dataAccess);

            var users = new User[0];
            Expect.MethodCall( () => dataAccess.All<User>() ).Returns( users.AsQueryable() );

            Assert.Throws<BadCredentialsException>(() =>
                {
                    User user;
                    userService.Login(userName, password, out user);
                });

        }
コード例 #4
0
        /// <summary>
        /// Creates the user.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <exception cref="System.ArgumentNullException">user</exception>
        /// <exception cref="Hangout.BusinessLogic.DuplicateEntityException">
        /// PhoneNumber
        /// or
        /// Email
        /// </exception>
        public void CreateUser (User user)
		{

			if (user == null)
				throw new ArgumentNullException ("user");

			ValidateUser (user);

			var existingUser = _dataAccess.All<User> ().FirstOrDefault (x => x.Email == user.Email || x.PhoneNumber == user.PhoneNumber);
			if (existingUser != null) 
			{
				if(existingUser.PhoneNumber == user.PhoneNumber)
					throw new DuplicateEntityException (ErrorCodes.EntityDuplicatedError, "PhoneNumber", string.Format ("Phone number '{0}' is taken.", user.PhoneNumber));

				if(existingUser.Email == user.Email)
					throw new DuplicateEntityException (ErrorCodes.EntityDuplicatedError, "Email", string.Format ("Email '{0}' is taken.", user.Email));
			}

			user.Password = PasswordHash.HashPassword (user.Password);

			_dataAccess.Add (user);

		}
コード例 #5
0
        /// <summary>
        /// Logins the specified username.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <param name="user">The user.</param>
        /// <exception cref="System.ArgumentNullException">
        /// password
        /// or
        /// username
        /// </exception>
        /// <exception cref="Hangout.BusinessLogic.BadCredentialsException">Wrong email and password!</exception>
        public void Login (string username, string password, out User user)
		{
		
			if (string.IsNullOrWhiteSpace (password))
				throw new ArgumentNullException ("password");

			if (string.IsNullOrWhiteSpace (username))
				throw new ArgumentNullException ("username");

			password = PasswordHash.HashPassword (password);

			user = _dataAccess.All<User> ().FirstOrDefault (x => x.Email == username && x.Password == password);

			if (user == null)
				throw new BadCredentialsException (ErrorCodes.BadCredentialsError, "Wrong email and password!");

		}
コード例 #6
0
        public void InvalidUserCreationTet()
        {

            var dataAccess = Mock.Interface<IDataAccess>();
            var userService = new UserService(dataAccess);

            Assert.Throws<ValidationException>(() =>
            {
                var newUser = new User();
                userService.CreateUser(newUser);
            });

        }