public async Task Callback_Should_GetCounters()
        {
            var authResponse = new AuthorizationResponse()
            {
                access_token  = TestDoubles.Token,
                refresh_token = TestDoubles.RefreshToken
            };

            cacheServiceMock   = new Mock <ICacheService>();
            secureServiceMock  = new Mock <ISecureService>();
            counterServiceMock = new Mock <ICounterService>();
            cacheServiceMock.Setup(cache => cache.GetValue <AuthorizationResponse>(KEYS.ACCESS_TOKEN))
            .Returns(() => null);
            cacheServiceMock.Setup(cache => cache.SetValue(KEYS.ACCESS_TOKEN, It.IsAny <AuthorizationResponse>()))
            .Verifiable();
            secureServiceMock.Setup(service => service.GetToken(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(Task.FromResult(Result.Ok(authResponse)));
            getTokenUseCase = new GetTokenUseCase(secureServiceMock.Object, cacheServiceMock.Object);
            counterServiceMock.Setup(service => service.GetCounters())
            .Returns(Task.FromResult(Result.Ok(new List <Counter>())));
            getCountersUseCase = new GetCountersUseCase(counterServiceMock.Object);
            renewTokenUseCase  = new RenewTokenUseCase(secureServiceMock.Object, cacheServiceMock.Object);
            controller         = new AppController(
                getTokenUseCase, getCountersUseCase,
                renewTokenUseCase, cacheServiceMock.Object
                );

            var result = await controller.Callback(TestDoubles.Code, TestDoubles.State);

            Assert.IsNotNull(result);
        }
 public AppController(
     GetTokenUseCase GetTokenUseCase,
     GetCountersUseCase GetCountersUseCase,
     RenewTokenUseCase RenewTokenUseCase,
     ICacheService CacheService)
 {
     getTokenUseCase    = GetTokenUseCase;
     getCountersUseCase = GetCountersUseCase;
     renewTokenUseCase  = RenewTokenUseCase;
     cacheService       = CacheService;
 }
        public async Task Execute_WhenRefreshTokenIsNull_ShouldFail()
        {
            var useCase = new RenewTokenUseCase(secureServiceMock.Object, cacheServiceMock.Object);

            useCase.RefreshToken = null;

            var result = await useCase.Execute();

            Assert.IsTrue(result.DidFail);
            secureServiceMock.Verify(service => service.RenewToken(It.IsAny <string>()), Times.Never);
        }
        public async Task RenewToken_WhenRefreshTokenIsNull_ShouldRedirectHome()
        {
            cacheServiceMock = new Mock <ICacheService>();
            cacheServiceMock.Setup(cache => cache.GetValue <string>(KEYS.REFRESH_TOKEN))
            .Returns(() => null);
            renewTokenUseCase = new RenewTokenUseCase(null, cacheServiceMock.Object);
            renewTokenUseCase.RefreshToken = null;
            controller = new AppController(
                null, null, renewTokenUseCase, null
                );

            var result = await controller.RenewToken() as RedirectResult;

            Assert.AreEqual("/", result.Url);
        }
        public async Task Execute_Should_RenewToken()
        {
            cacheServiceMock.Setup(cache => cache.SetValue(KEYS.REFRESH_TOKEN, It.IsAny <string>()))
            .Verifiable();
            cacheServiceMock.Setup(cache => cache.GetValue <string>(KEYS.REFRESH_TOKEN))
            .Returns(TestDoubles.RefreshToken);
            secureServiceMock.Setup(service => service.RenewToken(It.IsAny <string>()))
            .Returns(Task.FromResult(TestDoubles.authResult))
            .Verifiable();
            var useCase = new RenewTokenUseCase(secureServiceMock.Object, cacheServiceMock.Object);

            var result = await useCase.Execute();

            Assert.IsTrue(result.DidSucceed);
            secureServiceMock.Verify(service => service.RenewToken(It.IsAny <string>()), Times.Once);
        }
        public async Task RenewToken_WhenRefreshTokenIsNotNull_ShouldGetAuthResponse()
        {
            var refreshToken = "refresh_token";

            cacheServiceMock = new Mock <ICacheService>();
            cacheServiceMock.Setup(cache => cache.GetValue <string>(KEYS.REFRESH_TOKEN))
            .Returns(() => refreshToken);
            secureServiceMock = new Mock <ISecureService>();
            secureServiceMock.Setup(service => service.RenewToken(refreshToken))
            .Returns(Task.FromResult(Result.Ok(new AuthorizationResponse())));
            renewTokenUseCase = new RenewTokenUseCase(secureServiceMock.Object, cacheServiceMock.Object);
            renewTokenUseCase.RefreshToken = refreshToken;
            controller = new AppController(
                null, null, renewTokenUseCase, null
                );

            var result = await controller.RenewToken() as OkObjectResult;

            Assert.IsInstanceOfType(result.Value, typeof(AuthorizationResponse));
        }