コード例 #1
0
        public void WhenLoginReturnsAnError()
        {
            // Arrange
            var model = new LoginRequestDto
            {
                Password = "******",
                UserName = "******"
            };

            var loggedInUser = new LoggedInUserDto
            {
                UserName = "******"
            };

            var users = new List <ApplicationUser> {
                new ApplicationUser {
                    UserName = "******"
                }
            }.AsQueryable();

            _userManager   = new FakeUserManager(_userStore, users);
            _signInManager = new FakeSignInManager(_userStore, users, Microsoft.AspNetCore.Identity.SignInResult.Failed);

            _tokenService.Setup(x => x.GenerateJwtToken(It.IsAny <ApplicationUser>())).Returns(new Logic.Models.JsonWebToken("123", 1));
            _mapper.Setup(x => x.Map <LoggedInUserDto>(It.IsAny <ApplicationUser>())).Returns(loggedInUser);

            var controller = new AccountController(_userManager, _signInManager, _mapper.Object, _tokenService.Object);

            // Act
            var result = controller.Login(model).Result;

            // Assert
            Assert.Equal(typeof(BadRequestObjectResult), result.GetType());
        }
コード例 #2
0
        public void WhenRegisterReturnsLoggedInUser()
        {
            // Arrange
            var model = new RegisterRequestDto
            {
                Password = "******",
                UserName = "******"
            };

            var loggedInUser = new LoggedInUserDto
            {
                UserName = "******"
            };

            var appUser = new ApplicationUser
            {
                UserName = "******"
            };

            var users = new List <ApplicationUser>().AsQueryable();

            _userManager = new FakeUserManager(_userStore, users);

            _signInManager = new FakeSignInManager(_userStore, users, Microsoft.AspNetCore.Identity.SignInResult.Success);

            _tokenService.Setup(x => x.GenerateJwtToken(It.IsAny <ApplicationUser>())).Returns(new Logic.Models.JsonWebToken("123", 1));
            _mapper.Setup(x => x.Map <ApplicationUser>(It.IsAny <RegisterRequestDto>())).Returns(appUser);
            _mapper.Setup(x => x.Map <LoggedInUserDto>(It.IsAny <ApplicationUser>())).Returns(loggedInUser);

            var controller = new AccountController(_userManager, _signInManager, _mapper.Object, _tokenService.Object);

            // Act
            OkObjectResult result = controller.Register(model).Result as OkObjectResult;

            var resultModel = result.Value as Logic.Models.JsonWebToken;

            // Assert
            Assert.Equal("123", resultModel.Token);
        }