Exemplo n.º 1
0
        /// <summary>
        /// Returns info about the logged user
        /// according to logon request scope
        /// </summary>
        public MeResult GetMe(string accessToken)
        {
            int statusCode = 0;
            var res        = new MeResult();
            var serializer = new JavaScriptSerializer();

            try
            {
                string reqUri = (MeEndpoint
                                 + "?access_token={access_token}")
                                .Replace("{access_token}", accessToken);

                HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(reqUri);
                wr.AllowAutoRedirect = false;
                wr.KeepAlive         = true;
                HttpWebResponse retreiveResponse = (HttpWebResponse)wr.GetResponse();
                statusCode = (int)retreiveResponse.StatusCode;
                Stream       objStream = retreiveResponse.GetResponseStream();
                StreamReader objReader = new StreamReader(objStream);
                string       json      = objReader.ReadToEnd();
                retreiveResponse.Close();

                res = serializer.Deserialize <MeResult>(json);
            }
            catch (WebException wex)
            {
                HttpWebResponse wrs = (HttpWebResponse)wex.Response;
                throw new LogMeBotException((int)wrs.StatusCode, wex.Message);
            }
            catch (Exception ex)
            {
                throw new LogMeBotException(statusCode, ex.Message);
            }
            return(res);
        }
            public void GivenValidCredentials_AuthenticateClient_ReturnsAnAuthenticatedClientWithUserInformation()
            {
                // Arrange.
                var mockRestResponseAccessToken = new Mock <IRestResponse>();

                mockRestResponseAccessToken.Setup(x => x.StatusCode).Returns(HttpStatusCode.OK);
                mockRestResponseAccessToken.Setup(x => x.Content).Returns("access_token=foo&expires=1000");

                var meResult = new MeResult
                {
                    Id        = 1,
                    FirstName = "some firstname",
                    LastName  = "some lastname",
                    Link      = "http://whatever",
                    Locale    = "en-au",
                    Name      = "Hi there",
                    Timezone  = 10,
                    Username  = "******",
                    Verified  = true
                };

                var mockRestResponseApiMe = new Mock <IRestResponse <MeResult> >();

                mockRestResponseApiMe.Setup(x => x.StatusCode).Returns(HttpStatusCode.OK);
                mockRestResponseApiMe.Setup(x => x.Data).Returns(meResult);

                var mockRestClient = new Mock <IRestClient>();

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

                var facebookProvider = new FacebookProvider(new ProviderParams {
                    Key = "a", Secret = "b"
                })
                {
                    RestClientFactory = new RestClientFactory(mockRestClient.Object)
                };
                const string existingState         = "Oops! - Tasselhoff Burrfoot";
                var          queryStringParameters = new NameValueCollection
                {
                    { "state", existingState },
                    { "code", "whatever" }
                };
                var facebookAuthenticationSettings = new FacebookAuthenticationServiceSettings
                {
                    State       = existingState,
                    CallBackUri = new Uri("http://2p1s.com")
                };

                // Act.
                var result =
                    facebookProvider.AuthenticateClient(facebookAuthenticationSettings, queryStringParameters);

                // Assert.
                Assert.NotNull(result);
                Assert.Equal("facebook", result.ProviderName);
                Assert.NotNull(result.AccessToken);
                Assert.NotNull(result.UserInformation);
                Assert.False(string.IsNullOrEmpty(result.UserInformation.Id));
                Assert.NotNull(result.UserInformation.Name);
                Assert.NotNull(result.UserInformation.UserName);
            }