Exemplo n.º 1
0
        public async Task <IActionResult> UpdateUser([FromBody] UserUpdateViewModel UserInfo, string Id)
        {
            if (UserInfo == null)
            {
                return(BadRequest("User info cannot be null."));
            }

            var User = await _repository.Users.Get(u => u.Id.Equals(Id));

            if (User == null)
            {
                return(NotFound(Id));
            }

            var Errors = await _repository.Users.UpdateUser(UserInfo, User);

            if (Errors == null)
            {
                await _repository.Complete();

                var updatedUser = await _repository.Users.GetUserByUserName(UserInfo.UserName);

                var tokenString  = _cookieManager.GenerateJSONWebToken(updatedUser);
                var cookieOption = _cookieManager.CreateCookieOption();
                Response.Cookies.Append("tokenCookie", tokenString, cookieOption);
                return(Ok());
            }

            return(BadRequest(Errors));
        }
        public async void UpdateUser_CalledWithValidParameters_ReturnsOk()
        {
            IUnitOfWork       _unitOfWork    = Substitute.For <IUnitOfWork>();
            ICookieManager    _cookieManager = Substitute.For <ICookieManager>();
            IUserStore <User> _usersStore    = Substitute.For <IUserStore <User> >();
            var context = Substitute.For <HttpContext>();


            var testId = "1";
            UserUpdateViewModel userInfo = new UserUpdateViewModel()
            {
                UserName = "******"
            };
            ErrorContext nullError = null;
            User         user      = new User()
            {
                UserName = "******"
            };
            CookieOptions option = new CookieOptions();

            _unitOfWork.Users.UpdateUser(userInfo, user).Returns(nullError);
            _unitOfWork.Users.Get(Arg.Any <Expression <Func <User, bool> > >()).Returns(user);
            _unitOfWork.Users.GetUserByUserName(userInfo.UserName).Returns(user);
            _unitOfWork.Complete().Returns(2);
            _cookieManager.GenerateJSONWebToken(user).Returns("Token");
            _cookieManager.CreateCookieOption().Returns(option);

            var usersController = new UsersController(_unitOfWork, new UserManager <User>(_usersStore, null, null, null, null, null, null, null, null), _cookieManager);

            usersController.ControllerContext = new ControllerContext()
            {
                HttpContext = context
            };

            var result = await usersController.UpdateUser(userInfo, testId);

            var okResult = Assert.IsType <OkResult>(result);

            Assert.Equal(200, okResult.StatusCode);
        }
        public async void Login_CalledWithValidModel_ReturnsOk()
        {
            IUnitOfWork       _unitOfWork    = Substitute.For <IUnitOfWork>();
            ICookieManager    _cookieManager = Substitute.For <ICookieManager>();
            IUserStore <User> _usersStore    = Substitute.For <IUserStore <User> >();
            var context = Substitute.For <HttpContext>();
            var manager = Substitute.For <UserManager <User> >(_usersStore, null, null, null, null, null, null, null, null);

            UserLoginViewModel model = new UserLoginViewModel()
            {
                UserName = "******"
            };
            User user = new User {
                UserName = "******"
            };
            CookieOptions option = new CookieOptions();

            manager.CheckPasswordAsync(Arg.Any <User>(), Arg.Any <string>()).Returns(true);

            _cookieManager.GenerateJSONWebToken(user).Returns("Token");
            _cookieManager.CreateCookieOption().Returns(option);
            _unitOfWork.Users.GetUserByUserName(model.UserName).Returns(user);

            var usersController = new UsersController(_unitOfWork, manager, _cookieManager);

            usersController.ControllerContext = new ControllerContext()
            {
                HttpContext = context
            };

            var result = await usersController.Login(model);

            var okResult = Assert.IsType <OkResult>(result);

            Assert.Equal(200, okResult.StatusCode);
        }