Beispiel #1
0
        public async Task Authenticate_should_return_token_from_provider()
        {
            // given
            string jwtToken     = "jwt token";
            string email        = "*****@*****.**";
            string password     = "******";
            var    roadkillUser = new RoadkillIdentityUser()
            {
                Id                 = "1",
                UserName           = email,
                NormalizedUserName = email.ToUpperInvariant(),
                Email              = email,
                NormalizedEmail    = email.ToUpperInvariant(),
                RoleClaims         = new List <string>()
            };

            var model = new AuthorizationRequest()
            {
                Email    = email,
                Password = password
            };

            _userManagerMock.FindByEmailAsync(email)
            .Returns(Task.FromResult(roadkillUser));

            _signinManagerMock.PasswordSignInAsync(roadkillUser, password, true, false)
            .Returns(Task.FromResult(SignInResult.Success));

            var claims = new List <Claim>()
            {
                new Claim("any", "thing")
            } as IList <Claim>;

            _userManagerMock.GetClaimsAsync(roadkillUser)
            .Returns(Task.FromResult(claims));

            _jwtTokenProvider.CreateToken(claims, roadkillUser.Email)
            .Returns(jwtToken);

            // when
            ActionResult <string> actionResult = await _authorizationController.Authenticate(model);

            // then
            actionResult.Result.ShouldBeOfType <OkObjectResult>();

            var okResult = actionResult.Result as OkObjectResult;

            okResult.Value.ShouldBe(jwtToken);
        }
Beispiel #2
0
        public async Task Authenticate_should_return_jwt_and_refresh_token_logging_ip()
        {
            // given
            string ipAddress    = "9.8.7.6";
            string refreshToken = "refresh token";
            string jwtToken     = "jwt token";
            string email        = "*****@*****.**";
            string password     = "******";

            var roadkillUser = new RoadkillIdentityUser()
            {
                Id                 = "1",
                UserName           = email,
                NormalizedUserName = email.ToUpperInvariant(),
                Email              = email,
                NormalizedEmail    = email.ToUpperInvariant(),
                RoleClaims         = new List <string>()
            };

            var model = new AuthorizationRequest()
            {
                Email    = email,
                Password = password
            };

            _userManagerMock.FindByEmailAsync(email)
            .Returns(Task.FromResult(roadkillUser));

            _signinManagerMock.PasswordSignInAsync(roadkillUser, password, true, false)
            .Returns(Task.FromResult(SignInResult.Success));

            var claims = new List <Claim>()
            {
                new Claim("any", "thing")
            } as IList <Claim>;

            _userManagerMock.GetClaimsAsync(roadkillUser)
            .Returns(Task.FromResult(claims));

            _jwtTokenService
            .CreateToken(claims, roadkillUser.Email)
            .Returns(jwtToken);

            var httpContext = new DefaultHttpContext();

            httpContext.Connection.RemoteIpAddress = IPAddress.Parse(ipAddress);
            _authorizationController.ControllerContext.HttpContext = httpContext;

            _jwtTokenService
            .CreateRefreshToken(roadkillUser.Email, ipAddress)
            .Returns(refreshToken);

            // when
            ActionResult <AuthorizationResponse> actionResult = await _authorizationController.Authenticate(model);

            // then
            actionResult.Result.ShouldBeOfType <OkObjectResult>();
            var okResult = actionResult.Result as OkObjectResult;
            var response = okResult.Value as AuthorizationResponse;

            response.ShouldNotBeNull();
            response.JwtToken.ShouldBe(jwtToken);
            response.RefreshToken.ShouldBe(refreshToken);
        }