public async Task AuthenticateUserThrowsExceptionIfGitHubApiFails()
        {
            // Arrange
            var config = CreateProviderConfig();

            var(restClientFactory, restClient) = CreateRestClientAndFactory();

            // Arrnage - Calling GitHub API for token succeeds
            SetupTokenResultSuccess(restClient, "token");

            // Arrange - Calling GitHub API to get user fails
            var response = Substitute.For <IRestResponse <UserResult> >();

            response.IsSuccessful.Returns(false);
            restClient.ExecuteAsync <UserResult>(Arg.Any <RestRequest>()).Returns(Task.FromResult(response));

            var provider = new GitHubProvider(config, restClientFactory, "url1", "url2");

            var http = Substitute.For <HttpRequest>();

            http.Query.Returns(new QueryCollection(new Dictionary <string, StringValues> {
                { "code", new StringValues("TestCode") }
            }));

            // Act / Assert
            await Assert.ThrowsAsync <NogginNetCoreAuthException>(() => provider.AuthenticateUser(http, ""));
        }
        public async Task AuthenticateUserReturnsUserInfo()
        {
            // Arrange
            var config = CreateProviderConfig();

            var(restClientFactory, restClient) = CreateRestClientAndFactory();

            // Arrange - Calling GitHub API for token
            SetupTokenResultSuccess(restClient, "token");

            // Arrange - Calling GitHub for user details
            var gitHubResponse = Substitute.For <IRestResponse <UserResult> >();

            gitHubResponse.IsSuccessful.Returns(true);
            gitHubResponse.StatusCode.Returns(HttpStatusCode.OK);
            gitHubResponse.Data.Returns(new UserResult
            {
                Id        = 2268,
                Login     = "******",
                Name      = "Richard Garside",
                AvatarUrl = "lookingood.jpg"
            });
            restClient.ExecuteAsync <UserResult>(Arg.Any <RestRequest>()).Returns(Task.FromResult(gitHubResponse));

            var provider = new GitHubProvider(config, restClientFactory, "url1", "url2");

            var http = Substitute.For <HttpRequest>();

            http.Query.Returns(new QueryCollection(new Dictionary <string, StringValues> {
                { "code", new StringValues("TestCode") },
            }));

            // Act
            var authenticatedUser = await provider.AuthenticateUser(http, "secret");

            // Assert
            Assert.Equal("Richard Garside", authenticatedUser.Name);
            Assert.Equal("NogginBox", authenticatedUser.UserName);
            Assert.Equal("lookingood.jpg", authenticatedUser.Picture);
        }