コード例 #1
0
        /// <summary>
        /// Changes password using as parameter <see cref="UserToChangePassword"/>.
        /// </summary>
        /// <param name="user">User for log in</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        public void ChangePassword(UserToChangePassword user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user), "User can not be null.");
            }

            if (!IsPasswordConfirmed(user.Username, user.OldPassword))
            {
                throw new InvalidOperationException("Wrong password.");
            }

            var query = @"UPDATE [User]
                        SET [Password] = @newPassword
                        WHERE [Username] = @name";

            using (SqlConnection connection = new SqlConnection(_connectionString))
            {
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@name", user.Username);

                if (user.NewPassword != user.ConfirmNewPassword)
                {
                    throw new InvalidOperationException("Confirmation password is not equal to the password.");
                }

                string hashedPassword = BCrypt.Net.BCrypt.HashPassword(user.NewPassword);

                parameters.Add("@newPassword", hashedPassword);

                connection.QueryFirstOrDefault <UserToChangePassword>(query, parameters);
            }
        }
        public void ChangePassword_NullValue_Test()
        {
            var accountServiceMock = new Mock <IAccountService>();

            var user = new UserToChangePassword();

            accountServiceMock.Setup(x => x.ChangePassword(user))
            .Throws <ArgumentNullException>();
            var controller = new AccountController(accountServiceMock.Object);

            var actualResult = controller.ChangePassword(user);

            var badRequestResult = (BadRequestObjectResult)actualResult;
            var asJson           = JsonConvert.SerializeObject(badRequestResult.Value);
            var deserialized     = JsonConvert.DeserializeObject <Dictionary <string, object> >(asJson);

            Assert.IsTrue((bool)deserialized["Success"] == false);

            accountServiceMock.VerifyAll();
        }
        public void ChangePassword_Successfully_Test()
        {
            var accountServiceMock = new Mock <IAccountService>();

            var user = new UserToChangePassword();

            accountServiceMock.Setup(x => x.ChangePassword(user));

            var controller   = new AccountController(accountServiceMock.Object);
            var actualResult = controller.ChangePassword(user);

            var okResult = (OkObjectResult)actualResult;

            var asJson       = JsonConvert.SerializeObject(okResult.Value);
            var deserialized = JsonConvert.DeserializeObject <Dictionary <string, object> >(asJson);

            Assert.IsTrue((bool)deserialized["Success"]);

            accountServiceMock.VerifyAll();
        }
        public void ChangePassword_ConfirmedPasswordNotEqualToTheNewPassword_Test()
        {
            var accountServiceMock = new Mock <IAccountService>();

            var user = new UserToChangePassword();

            accountServiceMock.Setup(x => x.ChangePassword(user))
            .Throws(new InvalidOperationException("Confirmation password is not equal to the password."));

            var controller = new AccountController(accountServiceMock.Object);

            var actualResult = controller.ChangePassword(user);

            var badRequestResult = (BadRequestObjectResult)actualResult;
            var asJson           = JsonConvert.SerializeObject(badRequestResult.Value);
            var deserialized     = JsonConvert.DeserializeObject <Dictionary <string, object> >(asJson);

            Assert.AreEqual("Confirmation password is not equal to the password.", deserialized["Error"]);

            accountServiceMock.VerifyAll();
        }
コード例 #5
0
        public IActionResult ChangePassword(UserToChangePassword user)
        {
            try
            {
                _accountService.ChangePassword(user);

                return(Ok(new
                {
                    Success = true,
                    Message = "Successfully changed password."
                }));
            }
            catch (InvalidOperationException invalidOperationException)
            {
                return(BadRequest(new
                {
                    Success = false,
                    Error = invalidOperationException.Message
                }));
            }
            catch (ArgumentNullException argumentNullException)
            {
                return(BadRequest(new
                {
                    Success = false,
                    Error = argumentNullException.Message
                }));
            }
            catch (Exception exception)
            {
                return(new ObjectResult(new
                {
                    Success = false,
                    Error = exception.Message
                })
                {
                    StatusCode = StatusCodes.Status500InternalServerError
                });
            }
        }