示例#1
0
        public async Task RefreshTokenUserTestSuccess()
        {
            #region Mock Success

            TestRefreshSuccess.prarameter.Token        = jwtService.GenerateToken(TestRefreshSuccess.user.Email, TestRefreshSuccess.user.Id.ToString());
            TestRefreshSuccess.prarameter.RefreshToken = jwtService.GenerateRefreshToken();

            var claimPrincipa = jwtService.GetPrincipalFromExpiredToken(TestRefreshSuccess.prarameter.Token);
            _jwtServiceMocking.Setup(s => s.GetPrincipalFromExpiredToken(TestRefreshSuccess.prarameter.Token))
            .Returns(claimPrincipa);


            var emailFromClaim = jwtService.GetValueFromClaimType(claimPrincipa, JwtRegisteredClaimNames.Email);
            _jwtServiceMocking.Setup(s => s.GetValueFromClaimType(claimPrincipa, JwtRegisteredClaimNames.Email))
            .Returns(emailFromClaim);

            var userIdFromClaim = jwtService.GetValueFromClaimType(claimPrincipa, JwtRegisteredClaimNames.NameId);
            _jwtServiceMocking.Setup(s => s.GetValueFromClaimType(claimPrincipa, JwtRegisteredClaimNames.NameId))
            .Returns(userIdFromClaim);


            TestRefreshSuccess.response.Token        = jwtService.GenerateToken(claimPrincipa.Claims.ToList());
            TestRefreshSuccess.response.RefreshToken = jwtService.GenerateRefreshToken();

            _jwtServiceMocking.Setup(s => s.GenerateToken(emailFromClaim, userIdFromClaim))
            .Returns(TestRefreshSuccess.response.Token);

            _jwtServiceMocking.Setup(s => s.GenerateRefreshToken())
            .Returns(TestRefreshSuccess.response.RefreshToken);

            _userLogicMocking.Setup(s => s.ValidateRefreshToken(TestRefreshSuccess.user.Email, TestRefreshSuccess.prarameter.RefreshToken))
            .Returns(Task.FromResult(PandaResponse.CreateSuccessResponse(true)));

            _userLogicMocking.Setup(s => s.UpdateRefreshTokenToUser(TestRefreshSuccess.user.Email, TestRefreshSuccess.response.RefreshToken))
            .Returns(Task.FromResult(PandaResponse.CreateSuccessResponse(true)));

            userFacade = new UserFacade(_jwtServiceMocking.Object, _userLogicMocking.Object);

            #endregion

            var result = await userFacade.RefreshToken(TestRefreshSuccess.prarameter);

            _jwtServiceMocking.Verify(v => v.GetPrincipalFromExpiredToken(TestRefreshSuccess.prarameter.Token));
            _jwtServiceMocking.Verify(v => v.GetValueFromClaimType(claimPrincipa, JwtRegisteredClaimNames.NameId));
            _jwtServiceMocking.Verify(v => v.GenerateToken(emailFromClaim, userIdFromClaim));
            _jwtServiceMocking.Verify(v => v.GenerateRefreshToken());

            _userLogicMocking.Verify(v => v.ValidateRefreshToken(TestRefreshSuccess.user.Email, TestRefreshSuccess.prarameter.RefreshToken));

            _userLogicMocking.Verify(v => v.UpdateRefreshTokenToUser(TestRefreshSuccess.user.Email, TestRefreshSuccess.response.RefreshToken));


            Assert.Equal(TestRefreshSuccess.response.RefreshToken, result.Data.RefreshToken);
            Assert.Equal(TestRefreshSuccess.response.Token, result.Data.Token);
        }