Пример #1
0
        public async Task AuthenticateUserThrowsExceptionIfFacebookApiFails()
        {
            // Arrange
            var config = CreateProviderConfig();

            var(restClientFactory, restClient) = CreateRestClientAndFactory();

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

            // Arrange - Calling Facebook API fails
            var response = Substitute.For <IRestResponse <MeResult> >();

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

            var provider = new FacebookProvider(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"));
        }
Пример #2
0
        public async Task AuthenticateUserReturnsUserInfo()
        {
            // Arrange
            var config = CreateProviderConfig();

            var(restClientFactory, restClient) = CreateRestClientAndFactory();


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

            // Arrange - Calling Facebook for user details
            var facebookResponse = Substitute.For <IRestResponse <MeResult> >();

            facebookResponse.IsSuccessful.Returns(true);
            facebookResponse.StatusCode.Returns(HttpStatusCode.OK);
            facebookResponse.Data.Returns(new MeResult
            {
                Id       = 2268,
                Username = "******",
                Name     = "Richard Garside",
                Locale   = "en-GB"
            });
            restClient.ExecuteAsync <MeResult>(Arg.Any <RestRequest>()).Returns(Task.FromResult(facebookResponse));

            var provider = new FacebookProvider(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("https://graph.facebook.com/2268/picture", authenticatedUser.Picture);
        }