public void GivenAInvalidRequestToken_RedirectToAuthenticate_ThrowsAnAuthenticationException()
            {
                // Arrange.
                var mockRestResponse = new Mock<IRestResponse>();
                mockRestResponse.Setup(x => x.StatusCode).Returns(HttpStatusCode.OK);
                mockRestResponse.Setup(x => x.Content)
                                .Returns("oauth_token=aaaaaaa&missing_an_oauth_token_secret=asdasd");
                var mockRestClient = new Mock<IRestClient>();
                mockRestClient
                    .Setup(x => x.Execute(It.IsAny<IRestRequest>()))
                    .Returns(mockRestResponse.Object);
                var twitterProvider = new TwitterProvider(new ProviderParams { Key = "a", Secret = "b" })
                {
                    RestClientFactory = new RestClientFactory(mockRestClient.Object)
                };
                var twitterAuthenticationServiceSettings = new TwitterAuthenticationServiceSettings
                {
                    CallBackUri = new Uri("http://www.pewpew.com")
                };

                // Act.
                var result =
                    Assert.Throws<AuthenticationException>(
                        () => twitterProvider.RedirectToAuthenticate(twitterAuthenticationServiceSettings));

                // Assert.
                Assert.NotNull(result);
                Assert.Null(result.InnerException);
                Assert.Equal(
                    "Retrieved a Twitter Request Token but it doesn't contain both the oauth_token and oauth_token_secret parameters.",
                    result.Message);
            }
            public void GivenARequestTokenError_RedirectToAuthenticate_ThrowsAnAuthenticationException()
            {
                // Arrange.
                var mockRestResponse = new Mock<IRestResponse>();
                mockRestResponse.Setup(x => x.StatusCode).Returns(HttpStatusCode.Unauthorized);
                mockRestResponse.Setup(x => x.StatusDescription).Returns("Unauthorized");
                var mockRestClient = new Mock<IRestClient>();
                mockRestClient
                    .Setup(x => x.Execute(It.IsAny<IRestRequest>()))
                    .Returns(mockRestResponse.Object);
                var twitterProvider = new TwitterProvider(new ProviderParams { Key = "a", Secret = "b" })
                {
                    RestClientFactory = new RestClientFactory(mockRestClient.Object)
                };
                var twitterAuthenticationServiceSettings = new TwitterAuthenticationServiceSettings
                {
                    CallBackUri = new Uri("http://www.pewpew.com")
                };

                // Act.
                var result =
                    Assert.Throws<AuthenticationException>(
                        () => twitterProvider.RedirectToAuthenticate(twitterAuthenticationServiceSettings));

                // Assert.
                Assert.NotNull(result);
                Assert.Null(result.InnerException);
                Assert.Equal(
                    "Failed to obtain a Request Token from Twitter OR the the response was not an HTTP Status 200 OK. Response Status: Unauthorized. Response Description: Unauthorized",
                    result.Message);
            }
                GivenAExceptionWhileTryingToRetrieveRequestToken_RedirectToAuthenticate_ThrowsAnAuthenticationException()
            {
                // Arrange.
                var mockRestClient = new Mock<IRestClient>();
                mockRestClient
                    .Setup(x => x.Execute(It.IsAny<IRestRequest>()))
                    .Throws(new Exception("some mock exception"));
                var twitterProvider = new TwitterProvider(new ProviderParams { Key = "a", Secret = "b" })
                {
                    RestClientFactory = new RestClientFactory(mockRestClient.Object)
                };
                var twitterAuthenticationServiceSettings = new TwitterAuthenticationServiceSettings
                {
                    CallBackUri = new Uri("http://www.pewpew.com")
                };
                // Act.
                var result =
                    Assert.Throws<AuthenticationException>(
                        () => twitterProvider.RedirectToAuthenticate(twitterAuthenticationServiceSettings));

                // Assert.
                Assert.NotNull(result);
                Assert.Equal("Failed to obtain a Request Token from Twitter.", result.Message);
                Assert.NotNull(result.InnerException);
                Assert.Equal("some mock exception", result.InnerException.Message);
            }
            public void GivenAValidRequestToken_RedirectToAuthenticate_ReturnsARedirectResult()
            {
                // Arrange.
                var redirectUri =
                    new Uri(
                        "http://api.twitter.com/oauth/authorize?oauth_token=5rlgo7AwGJt67vSoHmF227QMSHXhvmOmhN5YJpRlEo");
                var mockRestResponse = new Mock<IRestResponse>();
                mockRestResponse.Setup(x => x.StatusCode).Returns(HttpStatusCode.OK);
                mockRestResponse
                    .Setup(x => x.Content)
                    .Returns("oauth_token=aaaaaaa&oauth_token_secret=asdasd");
                var mockRestClient = new Mock<IRestClient>();
                mockRestClient
                    .Setup(x => x.BuildUri(It.IsAny<IRestRequest>()))
                    .Returns(redirectUri);
                mockRestClient
                    .Setup(x => x.Execute(It.IsAny<IRestRequest>()))
                    .Returns(mockRestResponse.Object);
                var twitterProvider = new TwitterProvider(new ProviderParams { Key = "a", Secret = "b" })
                {
                    RestClientFactory = new RestClientFactory(mockRestClient.Object)
                };
                var twitterAuthenticationServiceSettings = new TwitterAuthenticationServiceSettings
                {
                    CallBackUri = new Uri("http://www.pewpew.com")
                };

                // Act.
                var result = twitterProvider.RedirectToAuthenticate(twitterAuthenticationServiceSettings);

                // Assert.
                Assert.NotNull(result);
                Assert.Equal(result.AbsoluteUri, redirectUri.AbsoluteUri);
            }
            public void GivenARequestTokenError_RedirectToAuthenticate_ThrowsAnAuthenticationException()
            {
                // Arrange.
                var mockRestResponse = new Mock<IRestResponse>();
                mockRestResponse.Setup(x => x.StatusCode).Returns(HttpStatusCode.Unauthorized);
                mockRestResponse.Setup(x => x.StatusDescription).Returns("Unauthorized");
                var mockRestClient = new Mock<IRestClient>();
                mockRestClient
                    .Setup(x => x.Execute(It.IsAny<IRestRequest>()))
                    .Returns(mockRestResponse.Object);
                mockRestClient.Setup(x => x.BuildUri(It.IsAny<IRestRequest>()))
                              .Returns(new Uri("http://i.dont.care.about.this.uri"));
                var twitterProvider = new TwitterProvider(new ProviderParams { Key = "a", Secret = "b" })
                {
                    RestClientFactory = new RestClientFactory(mockRestClient.Object)
                };
                var twitterAuthenticationServiceSettings = new TwitterAuthenticationServiceSettings
                {
                    CallBackUri = new Uri("http://www.pewpew.com"),
                    State = "I don't always test me code changes. But when I do, it's in production."
                };

                // Act.
                var result =
                    Assert.Throws<AuthenticationException>(
                        () => twitterProvider.RedirectToAuthenticate(twitterAuthenticationServiceSettings));

                // Assert.
                Assert.NotNull(result);
                Assert.Null(result.InnerException);
                Assert.Equal(
                    "Failed to obtain a request token from the Twitter api OR the the response was not an HTTP Status 200 OK. Response Status: Unauthorized. Response Description: Unauthorized. Error Message: --no error exception--.",
                    result.Message);
            }
            public void GivenAnExceptionWhileTryingToRetrieveRequestToken_RedirectToAuthenticate_ThrowsAnAuthenticationException()
            {
                // Arrange.
                var mockRestClient = new Mock<IRestClient>();
                mockRestClient
                    .Setup(x => x.Execute(It.IsAny<IRestRequest>()))
                    .Throws(new Exception("some mock exception"));
                mockRestClient.Setup(x => x.BuildUri(It.IsAny<IRestRequest>()))
                              .Returns(new Uri("http://i.dont.care.about.this.uri"));
                var twitterProvider = new TwitterProvider(new ProviderParams { Key = "a", Secret = "b" })
                {
                    RestClientFactory = new RestClientFactory(mockRestClient.Object)
                };
                var twitterAuthenticationServiceSettings = new TwitterAuthenticationServiceSettings
                {
                    CallBackUri = new Uri("http://www.pewpew.com"),
                    State = "I don't always test me code changes. But when I do, it's in production."
                };
                // Act.
                var result =
                    Assert.Throws<AuthenticationException>(
                        () => twitterProvider.RedirectToAuthenticate(twitterAuthenticationServiceSettings));

                // Assert.
                Assert.NotNull(result);
                Assert.Equal("Failed to obtain a Request Token from Twitter.", result.Message);
                Assert.NotNull(result.InnerException);
                Assert.Equal("some mock exception", result.InnerException.Message);
            }