Example #1
0
            public void GivenAFacebookCallback_CheckCallback_RetrievesAnAccessTokenAnUserData()
            {
                // Arrange.
                const string state = "blah";
                var          mockWebClientWrapper = MoqUtilities.MockedIWebClientWrapper();
                var          facebookProvider     = new FacebookProvider("a", "b", new Uri("http://www.google.com"),
                                                                         mockWebClientWrapper.Object);
                var authenticationService = new AuthenticationService(facebookProvider);
                var mockRequestBase       = new Mock <HttpRequestBase>();

                mockRequestBase.Setup(x => x.Params).Returns(new NameValueCollection
                {
                    { "code", "aaa" },
                    { "state", state }
                });

                // Act.
                var facebookClient =
                    authenticationService.CheckCallback(mockRequestBase.Object, state) as FacebookClient;

                // Assert.
                Assert.NotNull(facebookClient);
                Assert.NotNull(facebookClient.AccessToken);
                Assert.NotNull(facebookClient.UserInformation);
                Assert.NotNull(facebookClient.UserInformation.Id);
            }
            public void GivenSomethingWeirdHappenedWhileTryingToRetrieveMeData_RetrieveAccessToken_ThrowsAnException()
            {
                // Arrange.
                var mockWebClientWrapper = MoqUtilities.MockedIWebClientWrapper(new[] { "access_token=foo&expires=1", "ohcrap" });
                var facebookProvider     = new FacebookProvider("a", "b", new Uri("http://www.google.com"),
                                                                mockWebClientWrapper.Object);
                var facebookClient = new FacebookClient
                {
                    Code  = "aa",
                    State = "bb"
                };

                // Act.
                var result = Assert.Throws <InvalidOperationException>(() => facebookProvider.RetrieveAccessToken(facebookClient));

                // Assert.
                Assert.NotNull(result);
                Assert.Equal("Failed to deserialize the json user information result from Facebook.", result.Message);
            }
            public void GivenAMissingExpiresParam_RetrieveAccessToken_ThrowsAnException()
            {
                // Arrange.
                var mockWebClientWrapper = MoqUtilities.MockedIWebClientWrapper(new[] { "access_token=foo&hi=ohnoes", null });
                var facebookProvider     = new FacebookProvider("a", "b", new Uri("http://www.google.com"),
                                                                mockWebClientWrapper.Object);
                var facebookClient = new FacebookClient
                {
                    Code  = "aa",
                    State = "bb"
                };

                // Act.
                var result = Assert.Throws <ArgumentException>(() => facebookProvider.RetrieveAccessToken(facebookClient));

                // Assert.
                Assert.NotNull(result);
                Assert.Equal("value should be equal to 2. The actual value is 1.\r\nParameter name: value", result.Message);
            }
            public void GivenSomeInvalidResult_RetrieveAccessToken_ThrowsAnException()
            {
                // Arrange.
                var mockWebClientWrapper = MoqUtilities.MockedIWebClientWrapper(new [] { "asds", null });
                var facebookProvider     = new FacebookProvider("a", "b", new Uri("http://www.google.com"),
                                                                mockWebClientWrapper.Object);
                var facebookClient = new FacebookClient
                {
                    Code  = "aa",
                    State = "bb"
                };

                // Act.
                var result = Assert.Throws <ArgumentException>(() => facebookProvider.RetrieveAccessToken(facebookClient));

                // Assert.
                Assert.NotNull(result);
                Assert.Equal("value should contain 2 elements. value contains currently 1 element.\r\nParameter name: value", result.Message);
            }
            public void GivenValidCredentials_RetrieveAccessToken_ReturnsAnAccessTokenAndUserInformation()
            {
                // Arrange.
                var mockWebClientWrapper = MoqUtilities.MockedIWebClientWrapper();
                var facebookProvider     = new FacebookProvider("a", "b", new Uri("http://www.google.com"),
                                                                mockWebClientWrapper.Object);
                var facebookClient = new FacebookClient
                {
                    Code  = "aa",
                    State = "bb"
                };

                // Act.
                facebookProvider.RetrieveAccessToken(facebookClient);

                // Assert.
                Assert.NotNull(facebookClient);
                Assert.NotNull(facebookClient.AccessToken);
                Assert.NotNull(facebookClient.Code);
                Assert.NotNull(facebookClient.UserInformation);
                Assert.True(facebookClient.UserInformation.Id > 0);
                Assert.NotNull(facebookClient.UserInformation.FirstName);
                Assert.NotNull(facebookClient.UserInformation.LastName);
            }