Ejemplo n.º 1
0
        public async void Handle_GivenValidToken_ShouldSucceed()
        {
            // arrange
            var tokenServiceMock = new Mock <ITokenService>();

            tokenServiceMock.Setup(x => x.GetPrincipalFromToken(It.IsAny <string>(), It.IsAny <string>())).Returns(new ClaimsPrincipal(new[]
            {
                new ClaimsIdentity(new [] { new Claim("id", "1") })
            }));
            tokenServiceMock.Setup(x => x.GenerateEncodedToken(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <string>())).Returns(new AccessToken("", 0, ""));
            tokenServiceMock.Setup(x => x.GenerateToken(32)).Returns("");

            const string refreshToken = "1234";
            var          user         = new ACSUser("", "", "", "");

            user.AddRefreshToken(refreshToken, 0, "");

            var mockUserRepository = new Mock <IUserRepository>();

            mockUserRepository.Setup(repo => repo.FirstOrDefaultByQueryAsync(It.IsAny <UserQueryFilter>())).ReturnsAsync(user);

            var mockOutputPort = new Mock <IOutputPort <ExchangeRefreshTokenResponse> >();

            mockOutputPort.Setup(outputPort => outputPort.Handle(It.IsAny <ExchangeRefreshTokenResponse>()));

            var useCase = new ExchangeRefreshTokenUseCase(tokenServiceMock.Object, mockUserRepository.Object);

            // act
            var response = await useCase.Handle(new ExchangeRefreshTokenRequest("", refreshToken, ""), mockOutputPort.Object);

            // assert
            Assert.True(response);
        }
Ejemplo n.º 2
0
        public async Task Handle_GivenInvalidToken_ShouldFail()
        {
            // arrange
            var mockJwtTokenValidator = new Mock <IJwtValidator>();

            mockJwtTokenValidator.Setup(validator => validator.GetPrincipalFromToken(It.IsAny <string>())).Returns((ClaimsPrincipal)null);

            var useCase = new ExchangeRefreshTokenUseCase(mockJwtTokenValidator.Object, null, null, null);

            // act
            var response = await useCase.Handle(new ExchangeRefreshTokenRequest("", "", ""));

            // assert
            Assert.True(useCase.HasError);
        }
Ejemplo n.º 3
0
        public async void Handle_GivenInvalidToken_ShouldFail()
        {
            // arrange
            var mockJwtTokenValidator = new Mock <IJwtTokenValidator>();

            mockJwtTokenValidator.Setup(validator => validator.GetPrincipalFromToken(It.IsAny <string>(), It.IsAny <string>())).Returns((ClaimsPrincipal)null);

            var mockOutputPort = new Mock <IOutputPort <ExchangeRefreshTokenResponse> >();

            mockOutputPort.Setup(outputPort => outputPort.Handle(It.IsAny <ExchangeRefreshTokenResponse>()));

            var useCase = new ExchangeRefreshTokenUseCase(mockJwtTokenValidator.Object, null, null, null);

            // act
            var response = await useCase.Handle(new ExchangeRefreshTokenRequest("", "", ""), mockOutputPort.Object);

            // assert
            Assert.False(response);
        }
Ejemplo n.º 4
0
        public async void Handle_GivenValidToken_ShouldSucceed()
        {
            // arrange
            var mockJwtTokenValidator = new Mock <IJwtTokenValidator>();

            mockJwtTokenValidator.Setup(validator => validator.GetPrincipalFromToken(It.IsAny <string>(), It.IsAny <string>())).Returns(new ClaimsPrincipal(new[]
            {
                new ClaimsIdentity(new [] { new Claim("id", "111-222-333") })
            }));

            const string refreshToken = "1234";
            var          user         = new User("", "", "", "");

            user.Id = 0;
            user.AddRefreshToken(refreshToken, "");

            var mockUserRepository = new Mock <IUserRepository>();

            mockUserRepository.Setup(repo => repo.GetSingleBySpec(It.IsAny <UserSpecification>())).ReturnsAsync(user);

            var mockJwtFactory = new Mock <IJwtFactory>();

            mockJwtFactory.Setup(factory => factory.GenerateEncodedToken(It.IsAny <string>(), It.IsAny <string>())).ReturnsAsync(new AccessToken("", 0));

            var mockTokenFactory = new Mock <ITokenFactory>();

            mockTokenFactory.Setup(factory => factory.GenerateToken(32)).Returns("");

            var mockOutputPort = new Mock <IOutputPort <ExchangeRefreshTokenResponse> >();

            mockOutputPort.Setup(outputPort => outputPort.Handle(It.IsAny <ExchangeRefreshTokenResponse>()));

            var useCase = new ExchangeRefreshTokenUseCase(mockJwtTokenValidator.Object, mockUserRepository.Object, mockJwtFactory.Object, mockTokenFactory.Object);

            // act
            var response = await useCase.Handle(new ExchangeRefreshTokenRequest("", refreshToken, ""), mockOutputPort.Object);

            // assert
            Assert.True(response);
            mockJwtTokenValidator.VerifyAll();
            mockOutputPort.VerifyAll();
            mockTokenFactory.VerifyAll();
        }
Ejemplo n.º 5
0
        public async Task Handle_GivenValidToken_ShouldSucceed()
        {
            // arrange
            var mockJwtTokenValidator = new Mock <IJwtValidator>();

            mockJwtTokenValidator.Setup(validator => validator.GetPrincipalFromToken(It.IsAny <string>())).Returns(new ClaimsPrincipal(new[]
            {
                new ClaimsIdentity(new [] { new Claim("id", "111-222-333") })
            }));

            const string refreshToken = "1234";
            var          user         = new User("", "", "");

            user.AddRefreshToken(refreshToken, "");

            var mockUserRepository = new Mock <IUserRepository>();

            mockUserRepository.Setup(repo => repo.FindById(It.IsAny <string>())).ReturnsAsync(user);

            var mockJwtFactory = new Mock <IJwtFactory>();

            mockJwtFactory.Setup(factory => factory.GenerateEncodedToken(It.IsAny <string>(), It.IsAny <string>())).ReturnsAsync("");

            var mockTokenFactory = new Mock <ITokenFactory>();

            mockTokenFactory.Setup(factory => factory.GenerateToken(32)).Returns("");

            var useCase = new ExchangeRefreshTokenUseCase(mockJwtTokenValidator.Object, mockUserRepository.Object, mockJwtFactory.Object, mockTokenFactory.Object);

            // act
            var response = await useCase.Handle(new ExchangeRefreshTokenRequest("", refreshToken, ""));

            // assert
            Assert.False(useCase.HasError);
            Assert.NotNull(response);
        }