Exemple #1
0
        public async void ChangePassword_fail()
        {
            // Arrange
            var securityAdapter = new Mock <ISecurityAdapter>();
            var controller      = new AccountApiController(securityAdapter.Object);

            var userIdentity  = new ClaimsIdentity();
            var userPrincipal = new ClaimsPrincipal(userIdentity);

            controller.ControllerContext = new ControllerContext
            {
                HttpContext = new DefaultHttpContext {
                    User = userPrincipal
                }
            };

            // Act
            var result = await controller.ChangePassword(new ChangePasswordModel());

            // Assert
            Assert.IsType <ObjectResult>(result);

            ObjectResult objectResult = result as ObjectResult;

            Assert.True(objectResult.StatusCode == (int)HttpStatusCode.BadRequest);

            Assert.IsType <List <string> >(objectResult.Value);

            var errors = objectResult.Value as List <string>;

            Assert.True(errors.Count > 0);

            securityAdapter.Verify(x => x.ChangePassword(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()), Times.Never);
        }
Exemple #2
0
        public async void ChangePassword_success()
        {
            // Arrange
            var securityAdapter = new Mock <ISecurityAdapter>();
            var controller      = new AccountApiController(securityAdapter.Object);

            var userIdentity  = new ClaimsIdentity();
            var userPrincipal = new ClaimsPrincipal(userIdentity);

            controller.ControllerContext = new ControllerContext
            {
                HttpContext = new DefaultHttpContext {
                    User = userPrincipal
                }
            };

            var model = new ChangePasswordModel
            {
                OldPassword     = "******",
                NewPassword     = "******",
                ConfirmPassword = "******"
            };

            securityAdapter.Setup(x => x.CheckPassword(userIdentity.Name, model.OldPassword)).Returns(_BoolFromTaskFunction(true));
            securityAdapter.Setup(x => x.ChangePassword(userIdentity.Name, model.OldPassword, model.NewPassword)).Returns(_BoolFromTaskFunction(true));

            // Act
            var result = await controller.ChangePassword(model);

            // Assert
            Assert.IsType <StatusCodeResult>(result);

            securityAdapter.Verify(x => x.CheckPassword(userIdentity.Name, model.OldPassword), Times.Once);
            securityAdapter.Verify(x => x.ChangePassword(userIdentity.Name, model.OldPassword, model.NewPassword), Times.Once);
        }