public void CanGetAccessTokenValidInDb()
        {
            var restClient = new Mock <IRestClient>();
            var externalTokenRepository = new LocalRepository <ExternalToken>();
            var serverTime    = new Mock <IServerTime>();
            var jsonConverter = new Mock <IJsonConverter>();

            var api = new GoogleAuthentication(restClient.Object,
                                               externalTokenRepository,
                                               serverTime.Object,
                                               jsonConverter.Object);

            var now = new DateTime(2014, 12, 17, 10, 0, 0);

            serverTime.Setup(st => st.RequestStarted).Returns(now);
            var validAccessToken = new ExternalToken
            {
                AccessToken    = "AccessToken",
                ExpirationDate = now.AddHours(1),
                Type           = "Google_Access_Token"
            };

            externalTokenRepository.Add(validAccessToken);
            var token = api.GetAccessToken();

            Assert.AreEqual("AccessToken", token.AccessToken);
            Assert.AreEqual("Google_Access_Token", token.Type);
            Assert.AreEqual(11, token.ExpirationDate.Hour);
        }
        public void CanGetNewAccessTokenIfNotValidInDbOrForcedForNew()
        {
            var restClient = new Mock <IRestClient>();
            var externalTokenRepository = new LocalRepository <ExternalToken>();
            var serverTime    = new Mock <IServerTime>();
            var jsonConverter = new Mock <IJsonConverter>();

            var api = new GoogleAuthentication(restClient.Object,
                                               externalTokenRepository,
                                               serverTime.Object,
                                               jsonConverter.Object);

            var now = new DateTime(2014, 12, 17, 10, 0, 0);

            serverTime.Setup(st => st.RequestStarted).Returns(now);
            var validRefreshToken = new ExternalToken
            {
                AccessToken    = "RefreshToken",
                ExpirationDate = now.AddYears(100),
                Type           = "Google_Refresh_Token"
            };

            var inValidAccessToken = new ExternalToken
            {
                AccessToken    = "AccessToken",
                ExpirationDate = now.AddHours(-2),
                Type           = "Google_Access_Token"
            };

            externalTokenRepository.Add(validRefreshToken);
            externalTokenRepository.Add(inValidAccessToken);

            var restResponse = new RestResponse();

            restResponse.Content = "{ \"access_token\" : \"NewAccessToken\", \"token_type\" : \"Bearer\", \"expires_in\" : 3600}";
            restClient.Setup(rc => rc.Execute(It.IsAny <RestRequest>()))
            .Returns(restResponse);

            var googleApiTokenResponse = new GoogleApiTokenResponse
            {
                access_token = "NewAccessToken",
                expires_in   = 3600,
                token_type   = "Bearer"
            };

            jsonConverter.Setup(
                jc =>
                jc.Deserilize <GoogleApiTokenResponse>(
                    "{ \"access_token\" : \"NewAccessToken\", \"token_type\" : \"Bearer\", \"expires_in\" : 3600}"))
            .Returns(googleApiTokenResponse);

            var hasValidTokenInDb = externalTokenRepository.Any(t => t.Type == "Google_Access_Token" && t.ExpirationDate > now);

            Assert.AreEqual(false, hasValidTokenInDb);

            var token = api.GetAccessToken();

            var gotNewValidToken = externalTokenRepository.Any(t => t.Type == "Google_Access_Token" && t.ExpirationDate > now);

            Assert.AreEqual(true, gotNewValidToken);

            Assert.AreEqual("NewAccessToken", token.AccessToken);
            Assert.AreEqual("Google_Access_Token", token.Type);
            Assert.AreEqual(11, token.ExpirationDate.Hour);
        }