public void GivenAnInvalidRequestToken_AuthenticateClient_ThrowsAnAuthenticationException()
            {
                // Arrange.
                var mockRestClient = new Mock<IRestClient>();
                var mockRestResponse = new Mock<IRestResponse>();
                mockRestResponse.Setup(x => x.StatusCode).Returns(HttpStatusCode.Unauthorized);
                mockRestResponse.Setup(x => x.StatusDescription).Returns("Unauthorized");
                mockRestClient
                    .Setup(x => x.Execute(It.IsAny<IRestRequest>()))
                    .Returns(mockRestResponse.Object);
                var flickrProvider = new FlickrProvider("a", "b", null, new RestClientFactory(mockRestClient.Object));
                var nameValueCollection = new NameValueCollection
                {
                    {"oauth_token", "aaa"},
                    {"oauth_verifier", "bbbb"}
                };
                // Act.
                var result =
                    Assert.Throws<AuthenticationException>(
                        () => flickrProvider.AuthenticateClient(nameValueCollection, "asas"));

                // Assert.
                Assert.NotNull(result);
                Assert.Equal(
                    "Failed to obtain an Access Token from Flickr OR the the response was not an HTTP Status 200 OK. Response Status: Unauthorized. Response Description: Unauthorized",
                    result.Message);
            }
            public void GivenAnInvalidVerifyLogin_RetrieveUserInformation_ThrowsAnAuthenticationException()
            {
                // Arrange.
                var mockRestClient = new Mock<IRestClient>();

                var mockRestResponseRetrieveRequestToken = new Mock<IRestResponse>();
                mockRestResponseRetrieveRequestToken.Setup(x => x.StatusCode).Returns(HttpStatusCode.OK);
                mockRestResponseRetrieveRequestToken.Setup(x => x.Content)
                                                    .Returns("oauth_token=aaa&oauth_token_secret=ccc");

                var mockRestResponseVerifyCredentials = new Mock<IRestResponse<FlickrLoginResult>>();
                mockRestResponseVerifyCredentials.Setup(x => x.StatusCode).Returns(HttpStatusCode.Unauthorized);
                mockRestResponseVerifyCredentials.Setup(x => x.StatusDescription).Returns("Unauthorized");

                mockRestClient
                    .Setup(x => x.Execute(It.IsAny<IRestRequest>()))
                    .Returns(mockRestResponseRetrieveRequestToken.Object);
                mockRestClient
                    .Setup(x => x.Execute<FlickrLoginResult>(It.IsAny<IRestRequest>()))
                    .Returns(mockRestResponseVerifyCredentials.Object);

                var flickrProvider = new FlickrProvider("a", "b", null, new RestClientFactory(mockRestClient.Object));
                var nameValueCollection = new NameValueCollection
                {
                    {"oauth_token", "aaa"},
                    {"oauth_verifier", "bbb"}
                };
                // Act.
                var result =
                    Assert.Throws<AuthenticationException>(
                        () => flickrProvider.AuthenticateClient(nameValueCollection, "asd"));

                // Assert.
                Assert.NotNull(result);
                Assert.Equal(
                    "Failed to retrieve Login json data OR the the response was not an HTTP Status 200 OK. Response Status: Unauthorized. Response Description: Unauthorized",
                    result.Message);
            }
            public void GivenAnRequestTokenWithMissingParameters_AuthenticateClient_ThrowsAnAuthenticationException()
            {
                // Arrange.
                var mockRestClient = new Mock<IRestClient>();
                var mockRestResponse = new Mock<IRestResponse>();
                mockRestResponse.Setup(x => x.StatusCode).Returns(HttpStatusCode.OK);
                mockRestClient
                    .Setup(x => x.Execute(It.IsAny<IRestRequest>()))
                    .Returns(mockRestResponse.Object);
                var flickrProvider = new FlickrProvider("a", "b", null, new RestClientFactory(mockRestClient.Object));
                var nameValueCollection = new NameValueCollection
                {
                    {"oauth_token", "aaa"}
                }; // Missing oauth_secret.
                // Act.
                var result =
                    Assert.Throws<AuthenticationException>(
                        () => flickrProvider.AuthenticateClient(nameValueCollection, "asd"));

                // Assert.
                Assert.NotNull(result);
                Assert.Equal(
                    "Failed to retrieve an oauth_token and an oauth_token_secret after the client has signed and approved via Flickr.",
                    result.Message);
            }
            public void GivenTheCallbackParamtersAreInvalid_AuthenticateClient_ThrowsAnAuthenticationException()
            {
                // Arrange.
                var mockRestClient = new Mock<IRestClient>();
                mockRestClient
                    .Setup(x => x.Execute(It.IsAny<IRestRequest>()))
                    .Throws<Exception>();
                var flickrProvider = new FlickrProvider("a", "b", null, new RestClientFactory(mockRestClient.Object));
                var nameValueCollection = new NameValueCollection(); // Missing 2x required params.

                // Act.
                var result =
                    Assert.Throws<ArgumentOutOfRangeException>(
                        () => flickrProvider.AuthenticateClient(nameValueCollection, "aa"));

                // Assert.
                Assert.NotNull(result);
                Assert.Equal("Specified argument was out of the range of valid values.\r\nParameter name: parameters",
                    result.Message);
            }
            public void GivenSomeValidGetUserInfo_RetrieveUserInformation_ReturnsAnAuthenticatedClient()
            {
                // Arrange.
                var mockRestClient = new Mock<IRestClient>();

                var mockRestResponseRetrieveRequestToken = new Mock<IRestResponse>();
                mockRestResponseRetrieveRequestToken.Setup(x => x.StatusCode).Returns(HttpStatusCode.OK);
                mockRestResponseRetrieveRequestToken.Setup(x => x.Content)
                                                    .Returns("oauth_token=aaa&oauth_token_secret=ccc");

                var verifyLogin = new FlickrLoginResult
                {
                    User = new FlickrLoginResult.UserInner
                    {
                        Id = "some-id",
                        Username = new FlickrLoginResult.UserInner.UsernameInner
                        {
                            _content = "some-name"
                        }
                    }
                };
                var userData = new FlickrUserDataResult
                {
                    Person = new FlickrUserDataResult.PersonInner
                    {
                        Id = "some-id",
                        Username = new FlickrUserDataResult.PersonInner.ContentInner
                        {
                            _content = "some-name"
                        },
                        Realname = new FlickrUserDataResult.PersonInner.ContentInner
                        {
                            _content = "some-name"
                        },
                    }
                };

                var verifyLoginMock = new Mock<IRestResponse<FlickrLoginResult>>();
                verifyLoginMock.Setup(x => x.Data).Returns(verifyLogin);
                verifyLoginMock.Setup(x => x.StatusCode).Returns(HttpStatusCode.OK);

                var userDataMock = new Mock<IRestResponse<FlickrUserDataResult>>();
                userDataMock.Setup(x => x.Data).Returns(userData);
                userDataMock.Setup(x => x.StatusCode).Returns(HttpStatusCode.OK);

                mockRestClient
                    .Setup(x => x.Execute(It.IsAny<IRestRequest>()))
                    .Returns(mockRestResponseRetrieveRequestToken.Object);

                mockRestClient
                    .Setup(x => x.Execute<FlickrLoginResult>(It.IsAny<IRestRequest>()))
                    .Returns(verifyLoginMock.Object);

                mockRestClient
                    .Setup(x => x.Execute<FlickrUserDataResult>(It.IsAny<IRestRequest>()))
                    .Returns(userDataMock.Object);

                var flickrProvider = new FlickrProvider("a", "b", null, new RestClientFactory(mockRestClient.Object));
                var nameValueCollection = new NameValueCollection
                {
                    {"oauth_token", "aaa"},
                    {"oauth_verifier", "bbb"}
                };

                // Act.
                var result = flickrProvider.AuthenticateClient(nameValueCollection, "asd");

                // Assert.
                Assert.NotNull(result);
                Assert.Equal("flickr", result.ProviderName);
                Assert.NotNull(result.UserInformation);
                Assert.NotNull(result.UserInformation.Id);
                Assert.NotNull(result.UserInformation.Name);
                Assert.NotNull(result.UserInformation.UserName);
            }
            public void GivenExecutingVerifyLoginThrowsAnException_AuthenticateClient_ThrowsAnAuthenticationException()
            {
                // Arrange.
                const string exceptionMessage = "some mock exception.";
                var mockRestClient = new Mock<IRestClient>();

                var mockRestResponseRetrieveRequestToken = new Mock<IRestResponse>();
                mockRestResponseRetrieveRequestToken.Setup(x => x.StatusCode).Returns(HttpStatusCode.OK);
                mockRestResponseRetrieveRequestToken.Setup(x => x.Content)
                                                    .Returns("oauth_token=aaa&oauth_token_secret=ccc");

                var verifyLogin = new FlickrLoginResult
                {
                    User = new FlickrLoginResult.UserInner
                    {
                        Id = "some-id",
                        Username =  new FlickrLoginResult.UserInner.UsernameInner
                        {
                            _content = "some-name"
                        }
                    }
                };
                var mockRestResponseVerifyCredentials = new Mock<IRestResponse<FlickrLoginResult>>();
                mockRestResponseVerifyCredentials.Setup(x => x.Data).Returns(verifyLogin);

                mockRestClient
                    .Setup(x => x.Execute(It.IsAny<IRestRequest>()))
                    .Returns(mockRestResponseRetrieveRequestToken.Object);
                mockRestClient
                    .Setup(x => x.Execute<FlickrLoginResult>(It.IsAny<IRestRequest>()))
                    .Throws(new Exception(exceptionMessage));

                var flickrProvider = new FlickrProvider("a", "b", null, new RestClientFactory(mockRestClient.Object));
                var nameValueCollection = new NameValueCollection
                {
                    {"oauth_token", "aaa"},
                    {"oauth_verifier", "bbb"}
                };
                // Act.
                var result =
                    Assert.Throws<AuthenticationException>(
                        () => flickrProvider.AuthenticateClient(nameValueCollection, "asd"));

                // Assert.
                Assert.NotNull(result);
                Assert.Equal("Failed to retrieve Login json data from the Flickr Api.", result.Message);
            }
            public void GivenExecutingARequestToRetrieveARequestTokenThrowsAnException_AuthenticateClient_ThrowsAnAuthenticationException()
            {
                // Arrange.
                var mockRestClient = new Mock<IRestClient>();
                mockRestClient
                    .Setup(x => x.Execute(It.IsAny<IRestRequest>()))
                    .Throws(new Exception("one does not simply 'get a job'."));
                var flickrProvider = new FlickrProvider("a", "b", null, new RestClientFactory(mockRestClient.Object));
                var nameValueCollection = new NameValueCollection
                {
                    {"oauth_token", "aaa"},
                    {"oauth_verifier", "bbbb"}
                };
                // Act.
                var result =
                    Assert.Throws<AuthenticationException>(
                        () => flickrProvider.AuthenticateClient(nameValueCollection, "aa"));

                // Assert.
                Assert.NotNull(result);
                Assert.Equal("Failed to convert Request Token to an Access Token, from Flickr.", result.Message);
                Assert.NotNull(result.InnerException);
                Assert.Equal("one does not simply 'get a job'.", result.InnerException.Message);
            }