public void WhenAuthorizationCodeHasExpired_ThenThrowException()
        {
            var mocker = new AutoMoqer();
            mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns(GrantType.AuthorizationCode);
            mocker.GetMock<IConfiguration>().Setup(x => x.AuthorizationTokenExpirationLength).Returns(300);
            mocker.SetInstance<IOAuthIssuer>(new OAuthIssuer());
            var issuer = new OAuthIssuer();
            var token = issuer.GenerateAuthorizationToken(new TokenData { ConsumerId = 1, Timestamp = DateTime.UtcNow.AddHours(-1).Ticks });
            mocker.GetMock<IOAuthRequest>().Setup(x => x.AuthorizationCode).Returns(token);

            var authorizer = mocker.Resolve<AuthorizationCodeAuthorizer>();

            try
            {
                authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object);
                Assert.Fail("Exception not thrown");
            }
            catch (OAuthException ex)
            {
                Assert.AreEqual(ErrorCode.InvalidRequest, ex.ErrorCode);
                Assert.IsTrue(ex.ErrorDescription.HasValue());
            }
        }
        public void WhenRedirectUriDoesNotMatch_ThenExceptionIsThrown()
        {
            var mocker = new AutoMoqer();
            mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns(GrantType.AuthorizationCode);
            mocker.GetMock<IConfiguration>().Setup(x => x.AuthorizationTokenExpirationLength).Returns(300);
            mocker.GetMock<IConfiguration>().Setup(x => x.AccessTokenExpirationLength).Returns(500);
            var issuer = new OAuthIssuer();
            mocker.SetInstance<IOAuthIssuer>(issuer);
            var token = issuer.GenerateAuthorizationToken(new TokenData { ConsumerId = 1, Timestamp = DateTime.UtcNow.Ticks, RedirectUri = "http://test.com" });
            mocker.GetMock<IOAuthRequest>().Setup(x => x.AuthorizationCode).Returns(token);

            var authorizer = mocker.Resolve<AuthorizationCodeAuthorizer>();

            try
            {
                authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object);
                Assert.Fail("Exception not thrown");
            }
            catch (OAuthException ex)
            {
                Assert.AreEqual(ErrorCode.InvalidRequest, ex.ErrorCode);
                Assert.IsTrue(ex.ErrorDescription.HasValue());
            }

            mocker.GetMock<IOAuthRequest>().Setup(x => x.RedirectUri).Returns("http://test.com");
            var result = authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object);

            Assert.IsNotNull(result);
            Assert.IsTrue(result.AccessToken.HasValue());
            Assert.AreEqual(500, result.ExpiresIn);
            Assert.IsTrue(result.RefreshToken.HasValue());
        }
        public void ReturnsAccessToken()
        {
            var mocker = new AutoMoqer();
            mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns(GrantType.AuthorizationCode);
            mocker.GetMock<IConfiguration>().Setup(x => x.AuthorizationTokenExpirationLength).Returns(300);
            mocker.GetMock<IConfiguration>().Setup(x => x.AccessTokenExpirationLength).Returns(500);
            var issuer = new OAuthIssuer();
            mocker.SetInstance<IOAuthIssuer>(issuer);
            var token = issuer.GenerateAuthorizationToken(new TokenData {ConsumerId = 1, Timestamp = DateTime.UtcNow.Ticks});
            mocker.GetMock<IOAuthRequest>().Setup(x => x.AuthorizationCode).Returns(token);

            var authorizer = mocker.Resolve<AuthorizationCodeAuthorizer>();
            var result = authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object);

            Assert.IsNotNull(result);
            Assert.IsTrue(result.AccessToken.HasValue());
            Assert.AreEqual(500, result.ExpiresIn);
            Assert.IsTrue(result.RefreshToken.HasValue());
        }