Example #1
0
        public void Consumer_AuthorizeUser()
        {
            //arrange
            var provider = A.Fake<IOAuthProvider>();
            IConsumer consumer = new ConsumerFactory(options, provider)
                .CreateConsumer();

            var request_token = "requestToken";
            var request_token_secret = "requestTokenSecret";
            var authorize_url = new Uri("http://expectedUrl");
            var verifier = "verifier";
            var getRequestTokenRequestResult = string.Format("oauth_token={0}&oauth_token_secret={1}&oauth_expires_in=124&xoauth_request_auth_url={2}",
                request_token,
                request_token_secret,
                authorize_url);

            var access_token = "accessToken";
            var access_token_secret = "accessTokenSecret";
            var tokenInfo = new TokenInfo
            {
                AccessToken = access_token,
                AccessTokenSecret = access_token_secret,
            };

            var getAccessTokenRequestResult = string.Format("oauth_token={0}&oauth_token_secret={1}&oauth_expires_in=124&oauth_session_handle=12334&oauth_authorization_expires_in=1234",
                access_token,
                access_token_secret);

            A.CallTo(() => provider.GetRequestTokenRequest(
                A<string>._, A<string>._, A<string>._, A<string>._, A<SignatureMethod>._))
                .Returns(getRequestTokenRequestResult);

            A.CallTo(() => provider.GetGetAccessTokenRequest(
                A<string>._, A<string>._, A<string>._, A<string>._, A<SignatureMethod>._, A<string>._, A<string>._))
                .Returns(getAccessTokenRequestResult);

            //act
            consumer.RequestLogin();
            var actualTokenInfo = consumer.AuthorizeUser(verifier);

            //assert
            A.CallTo(() => provider.GetRequestTokenRequest(
                options.RequestTokenUrl,
                options.ConsumerKey,
                options.ConsumerSecret,
                options.RequestTokenResponseCallbackUrl,
                options.SignatureMethod));

            A.CallTo(() => provider.GetGetAccessTokenRequest(
                options.AccessTokenUrl,
                options.ConsumerKey,
                options.ConsumerSecret,
                request_token_secret,
                options.SignatureMethod,
                request_token,
                verifier));

            Assert.AreEqual(tokenInfo.AccessToken, actualTokenInfo.AccessToken);
            Assert.AreEqual(tokenInfo.AccessTokenSecret, actualTokenInfo.AccessTokenSecret);
        }
Example #2
0
        public void OAuthClient_MakeRequest_HasAccesstoken()
        {
            // arrange
            var resourcePath = new Uri(TU.GenerateLocalHostBaseAddress());
            var systemNetHttpMethod = new System.Net.Http.HttpMethod("GET");
            var webRequestResult = HttpWebRequest.CreateDefault(resourcePath);
            string resourceResult = TU.RandomAlphaNumString();
            var tokenInfo = new TokenInfo
            {
                AccessToken = TU.RandomAlphaNumString(),
                AccessTokenSecret = TU.RandomAlphaNumString()
            };

            A.CallTo(() => tokenService.GetAccessToken(A<string>._, out tokenInfo))
                .Returns(true);
            A.CallTo(() => clientConsumer.MakeHttpRequest(A<Uri>._,A<HttpMethod>._))
                .Returns(webRequestResult);
            A.CallTo(() => httpRequestReader.ReadHttpRequest(A<WebRequest>._))
                .Returns(resourceResult);

            // act
            client.Login("email");
            var actualResult = client.Request(resourcePath, systemNetHttpMethod);

            // assert
            Assert.AreEqual(actualResult, resourceResult);
            TU.MustHaveHappend(() => clientConsumer.MakeHttpRequest(
                TU.Matches(resourcePath),
                TU.Matches(systemNetHttpMethod.Convert())
                ));
            TU.MustHaveHappend(() => httpRequestReader.ReadHttpRequest(
                TU.Matches(webRequestResult)
                ));
        }
        public bool GetAccessToken(string userEmail, out TokenInfo token)
        {
            if (userEmail == null)
                throw new ArgumentNullException();

            var contents = fileIOProvider.ReadFile(filePath);
            var tokens = tokenSerializer.DeserializeTokenEmailPairs(contents);
            return tokens.TryGetValue(userEmail, out token);
        }
        public void StoreAccessToken(string userEmail, TokenInfo token)
        {
            if (userEmail == null || token == null)
                throw new ArgumentNullException();

            var contents = fileIOProvider.ReadFile(filePath);
            var tokens = tokenSerializer.DeserializeTokenEmailPairs(contents);
            tokens.Add(userEmail, token);
            var serializedTokenCollection = tokenSerializer.SerializeTokenEmailPairs(tokens);
            fileIOProvider.WriteFile(filePath, serializedTokenCollection);
        }
Example #5
0
        public bool Login(string email)
        {
            TokenInfo storageToken = null;
            if (!tokenService.GetAccessToken(email, out storageToken))
            {
                TokenInfo = consumer.RequestAccessToken();
                tokenService.StoreAccessToken(email, TokenInfo);
            } else
            {
                TokenInfo = storageToken;
                consumer.SetAccessTokenInfo(TokenInfo);
            }

            return (TokenInfo != null);
        }
        public void FileTokenStorageIntegrationTest_CanPersistAccessTokenAndRetrieveIt()
        {
            // arrange
            var fileIOProvider = new FileIOProvider();
            var tokenSerializer = new TokenCollectionSerializer();
            var tokenStorage = new FileTokenStorageService(fileIOProvider, tokenSerializer, GetRandomPath());
            var email = "*****@*****.**";
            var tokenInfo = new TokenInfo
            {
                AccessToken = TU.RandomAlphaNumString(),
                AccessTokenSecret = TU.RandomAlphaNumString()
            };

            // act
            tokenStorage.StoreAccessToken(email, tokenInfo);
            TokenInfo actualTokenInfo;
            var result = tokenStorage.GetAccessToken(email, out actualTokenInfo);
            // assert
            Assert.IsTrue(result);
            Assert.AreEqual(tokenInfo.AccessToken, actualTokenInfo.AccessToken);
            Assert.AreEqual(tokenInfo.AccessTokenSecret, actualTokenInfo.AccessTokenSecret);
        }
 public void SetAccessTokenInfo(TokenInfo token)
 {
     this.consumer.SetAccessTokenInfo(token);
 }
Example #8
0
        public void OAuthClient_ValidLogin_TokenCached_AccessTokenProvided()
        {
            // arrange
            var tokenInfo = new TokenInfo
            {
                AccessToken = TU.RandomAlphaNumString(),
                AccessTokenSecret = TU.RandomAlphaNumString()
            };
            string email = "*****@*****.**";
            A.CallTo(() => tokenService.GetAccessToken(A<string>._, out tokenInfo))
                .Returns(true);

            // act

            var result = client.Login(email);

            // assert
            Assert.IsTrue(result);
            Assert.AreEqual(tokenInfo, client.TokenInfo);
            TU.MustHaveHappend(() => tokenService.GetAccessToken(
                TU.Matches(email),
                out tokenInfo));
        }
Example #9
0
        public void OAuthClient_ValidLogin_TokenNotCached_AccessTokenProvided()
        {
            // arrange
            var token = new TokenInfo
            {
                AccessToken = TU.RandomAlphaNumString(),
                AccessTokenSecret = TU.RandomAlphaNumString()
            };
            TokenInfo storageAccessToken = null;
            string email = "*****@*****.**";

            A.CallTo(() => tokenService.GetAccessToken(A<string>._, out storageAccessToken))
                .Returns(false);
            A.CallTo(() => clientConsumer.RequestAccessToken())
                .Returns(token);

            // act
            var result = client.Login(email);

            // assert
            Assert.IsTrue(result);
            Assert.AreEqual(token.AccessToken, client.TokenInfo.AccessToken);
            Assert.AreEqual(token.AccessTokenSecret, client.TokenInfo.AccessTokenSecret);
            Assert.IsNull(storageAccessToken);

            TU.MustHaveHappend(() => tokenService.GetAccessToken(
                TU.Matches(email),
                out storageAccessToken));
            TU.MustHaveHappend(() => clientConsumer.RequestAccessToken());
            TU.MustHaveHappend(() => tokenService.StoreAccessToken(TU.Matches(email), TU.Matches(token)));
        }
 public void StoreAccessToken(string userEmail, TokenInfo token)
 {
     fileTokenService.StoreAccessToken(userEmail, token);
 }
 public bool GetAccessToken(string email, out TokenInfo token)
 {
     return fileTokenService.GetAccessToken(email, out token);
 }