public async Task AuthenticateUserThrowsExceptionIfGoogleApiFails() { // Arrange var config = CreateProviderConfig(); var(restClientFactory, restClient) = CreateRestClientAndFactory(); // Arrnage - Calling Google API for token succeeds SetupTokenResultSuccess(restClient, "token", "secret"); // Arrange - Calling Google API fails var response = Substitute.For <IRestResponse <AccessTokenResult> >(); response.IsSuccessful.Returns(false); restClient.ExecuteAsync <AccessTokenResult>(Arg.Any <RestRequest>()).Returns(Task.FromResult(response)); var provider = new GoogleProvider(config, restClientFactory, "url1", "url2"); var http = Substitute.For <HttpRequest>(); http.Query.Returns(new QueryCollection(new Dictionary <string, StringValues> { { "state", new StringValues("TestState") }, { "code", new StringValues("TestCode") }, })); // Act / Assert await Assert.ThrowsAsync <NogginNetCoreAuthException>(() => provider.AuthenticateUser(http, "secret")); }
public async Task AuthenticateUserReturnsUserInfo() { // Arrange var config = CreateProviderConfig(); var(restClientFactory, restClient) = CreateRestClientAndFactory(); // Arrange - Calling Google API for token SetupTokenResultSuccess(restClient, "token", "secret"); // Arrange - Calling Google for user details var googleResponse = Substitute.For <IRestResponse <UserInfoResult> >(); googleResponse.IsSuccessful.Returns(true); googleResponse.StatusCode.Returns(HttpStatusCode.OK); googleResponse.Data.Returns(new UserInfoResult { Id = "TestId", DisplayName = "RichardG2268", Name = new Name { FamilyName = "Garside", GivenName = "Richard" }, Language = "en-GB", Image = new Image { Url = "hotdang.jpg" } }); restClient.ExecuteAsync <UserInfoResult>(Arg.Any <RestRequest>()).Returns(Task.FromResult(googleResponse)); var provider = new GoogleProvider(config, restClientFactory, "url1", "url2"); var http = Substitute.For <HttpRequest>(); http.Query.Returns(new QueryCollection(new Dictionary <string, StringValues> { { "state", new StringValues("TestState") }, { "code", new StringValues("TestCode") }, })); // Act var authenticatedUser = await provider.AuthenticateUser(http, "secret"); // Assert Assert.Equal("Richard Garside", authenticatedUser.Name); Assert.Equal("RichardG2268", authenticatedUser.UserName); Assert.Equal("hotdang.jpg", authenticatedUser.Picture); }