예제 #1
0
        public void Init()
        {
            var browser = new Mock <IBrowser>();

            browser.Setup(m => m.GetJson(It.Is <string>(s => s == Url), It.IsAny <IEnumerable <KeyValuePair <string, string> > >()))
            .Callback(Callback)
            .Returns(() =>
            {
                if (string.IsNullOrWhiteSpace(Json))
                {
                    throw new ArgumentNullException(nameof(Json), @"Json не может быть равен null. Обновите значение поля Json");
                }
                return(Json);
            });

            browser.Setup(o => o.Authorize(It.IsAny <IApiAuthParams>())
                          )
            .Returns(VkAuthorization.From("https://vk.com/auth?__q_hash=qwerty&access_token=token&expires_in=1000&user_id=1"));
            Api = new VkApi
            {
                Browser = browser.Object
            };
            Api.Authorize(new ApiAuthParams
            {
                ApplicationId = 1,
                Login         = "******",
                Password      = "******",
                Settings      = Settings.All
            });
            Api.RequestsPerSecond = 10000;     // Чтобы тесты быстрее выполнялись
        }
예제 #2
0
        public AuthorizationResult Validate(string validateUrl, string phoneNumber)
        {
            var dlg = new AuthenticationView();

            dlg.webBrowser.Navigate(validateUrl);

            dlg.webBrowser.Navigated += (sender, args) =>
            {
                var result = VkAuthorization.From(args.Uri.AbsoluteUri);

                if (!result.IsAuthorized)
                {
                    return;
                }

                dlg.Auth = new AuthorizationResult
                {
                    AccessToken = result.AccessToken,
                    ExpiresIn   = result.ExpiresIn,
                    UserId      = result.UserId,
                    State       = result.State
                };

                dlg.Close();
            };

            dlg.ShowDialog();

            return(dlg.Auth);
        }
예제 #3
0
        public AuthorizationResult Authorize()
        {
            var dlg = new AuthenticationView();

            dlg.webBrowser.Navigate(
                CreateAuthorizeUrl(_authParams.ApplicationId, _authParams.Settings.ToUInt64(), Display.Mobile, "123456"));

            dlg.webBrowser.Navigated += (sender, args) =>
            {
                var result = VkAuthorization.From(args.Uri.AbsoluteUri);

                if (!result.IsAuthorized)
                {
                    return;
                }

                dlg.Auth = new AuthorizationResult
                {
                    AccessToken = result.AccessToken,
                    ExpiresIn   = result.ExpiresIn,
                    UserId      = result.UserId,
                    State       = result.State
                };

                dlg.Close();
            };

            dlg.ShowDialog();

            return(dlg.Auth);
        }
예제 #4
0
        public void Authorize_BadUserId_ThrowVkApiException()
        {
            const string urlWithBadUserId = "http://oauth.vk.com/blank.html#access_token=token&expires_in=86400&user_id=4793858sd";

            var authorization = VkAuthorization.From(new Uri(urlWithBadUserId));

            long userId = authorization.UserId;
        }
예제 #5
0
        public void Authorize_InvalidLoginOrPassword_NotAuthorizedAndAuthorizationNotRequired()
        {
            const string urlWithBadLoginOrPassword = "******";

            var authorization = VkAuthorization.From(new Uri(urlWithBadLoginOrPassword));

            Assert.IsFalse(authorization.IsAuthorized);
            Assert.IsFalse(authorization.IsAuthorizationRequired);
        }
예제 #6
0
        public void CorrectParseInputString()
        {
            var auth = VkAuthorization.From(Input);

            Assert.AreEqual(auth.AccessToken,
                            "bf81b33c4f88f45c5e2fa3874054c3c7ae1cb0c632583ca3d9d0d949d4123d8c2c4d7069d9066eceaf815");
            Assert.AreEqual(auth.ExpiresIn, 86400);
            Assert.AreEqual(auth.UserId, 32190123L);
            Assert.AreEqual(auth.Email, "*****@*****.**");
        }
예제 #7
0
        public void GetExpiresIn_Exception()
        {
            var auth  = VkAuthorization.From(Input.Replace("86400", "qwe"));
            var error = Assert.Throws <VkApiException>(() =>
            {
                var expiresIn = auth.ExpiresIn;
                Assert.NotZero(expiresIn);
            });

            Assert.AreEqual(error.Message, "ExpiresIn is not integer value.");
        }
예제 #8
0
        public void GetUserId_Exception()
        {
            var auth  = VkAuthorization.From(Input.Replace("32190123", "qwe"));
            var error = Assert.Throws <VkApiException>(() =>
            {
                var authUserId = auth.UserId;
                Assert.NotZero(authUserId);
            });

            Assert.AreEqual(error.Message, "UserId is not long value.");
        }
        public void CorrectParseInputString()
        {
            var auth = VkAuthorization.From(Input);

            Assert.AreEqual("token"
                            , auth.AccessToken);

            Assert.AreEqual(86400, auth.ExpiresIn);
            Assert.AreEqual(32190123L, auth.UserId);
            Assert.AreEqual("*****@*****.**", auth.Email);
        }
예제 #10
0
        public void CorrectParseInputString()
        {
            var auth = VkAuthorization.From(uriFragment: Input);

            Assert.AreEqual(expected: "bf81b33c4f88f45c5e2fa3874054c3c7ae1cb0c632583ca3d9d0d949d4123d8c2c4d7069d9066eceaf815"
                            , actual: auth.AccessToken);

            Assert.AreEqual(expected: 86400, actual: auth.ExpiresIn);
            Assert.AreEqual(expected: 32190123L, actual: auth.UserId);
            Assert.AreEqual(expected: "*****@*****.**", actual: auth.Email);
        }
예제 #11
0
        public void CorrectParseInputString()
        {
            var auth = VkAuthorization.From(uriFragment: Input);

            Assert.AreEqual(expected: "token"
                            , actual: auth.AccessToken);

            Assert.AreEqual(expected: 86400, actual: auth.ExpiresIn);
            Assert.AreEqual(expected: 32190123L, actual: auth.UserId);
            Assert.AreEqual(expected: "*****@*****.**", actual: auth.Email);
        }
예제 #12
0
        public void Authorize_RightInput_AccessToken()
        {
            const string returnUrl = "http://oauth.vk.com/blank.html#access_token=token&expires_in=86400&user_id=4793858";

            var authorization = VkAuthorization.From(new Uri(returnUrl));

            Assert.IsTrue(authorization.IsAuthorized);

            Assert.That(authorization.AccessToken, Is.EqualTo("token"));
            Assert.That(authorization.ExpiresIn, Is.EqualTo("86400"));
            Assert.That(authorization.UserId, Is.EqualTo(4793858));
        }
예제 #13
0
        public void GetUserId_Exception()
        {
            var auth = VkAuthorization.From(uriFragment: Input.Replace(oldValue: "32190123", newValue: "qwe"));

            var error = Assert.Throws <VkApiException>(code: () =>
            {
                var authUserId = auth.UserId;
                Assert.NotZero(actual: authUserId);
            });

            Assert.AreEqual(expected: "UserId is not long value.", actual: error.Message);
        }
예제 #14
0
        public void GetExpiresIn_Exception()
        {
            var auth = VkAuthorization.From(uriFragment: Input.Replace(oldValue: "86400", newValue: "qwe"));

            var error = Assert.Throws <VkApiException>(code: () =>
            {
                var expiresIn = auth.ExpiresIn;
                Assert.NotZero(actual: expiresIn);
            });

            Assert.AreEqual(expected: "ExpiresIn is not integer value.", actual: error.Message);
        }
예제 #15
0
        public void Authorize_BadUserId_ThrowVkApiException()
        {
            // TODO [ExpectedException(typeof(VkApiException), ExpectedMessage = "UserId is not integer value.")]
            const string urlWithBadUserId = "http://oauth.vk.com/blank.html#access_token=token&expires_in=86400&user_id=4793858sd";

            var ex = Assert.Throws <VkApiException>(() =>
            {
                var authorization = VkAuthorization.From(new Uri(urlWithBadUserId));

                var userId = authorization.UserId;
            });

            Assert.That(ex.Message, Is.EqualTo("UserId is not integer value."));
        }
예제 #16
0
        public void IsAuthorizationRequired_True()
        {
            const string uriQuery = "https://oauth.vk.com/authorize" +
                                    "?client_id=4268118" +
                                    "&redirect_uri=https:%2F%2Foauth.vk.com%2Fblank.html" +
                                    "&response_type=token" +
                                    "&scope=140426399" +
                                    "&v=" +
                                    "&state=" +
                                    "&display=page" +
                                    "&__q_hash=90f3ddf308ca69fca660e32b09e3617b";
            var auth = VkAuthorization.From(uriQuery);

            Assert.IsTrue(auth.IsAuthorizationRequired);
        }
예제 #17
0
        public void Authorize_InvalidLoginOrPassword_NotAuthorizedAndAuthorizationNotRequired()
        {
            const string urlWithBadLoginOrPassword = "******"
                                                     + "?client_id=1"
                                                     + "&redirect_uri=http%3A%2F%2Foauth.vk.com%2Fblank.html"
                                                     + "&response_type=token"
                                                     + "&scope=2"
                                                     + "&v="
                                                     + "&state="
                                                     + "&display=wap"
                                                     + "&m=4"
                                                     + "&email=mail";

            var authorization = VkAuthorization.From(uriFragment: urlWithBadLoginOrPassword);

            Assert.IsFalse(condition: authorization.IsAuthorized);
            Assert.IsFalse(condition: authorization.IsAuthorizationRequired);
        }
예제 #18
0
파일: BaseTest.cs 프로젝트: slay9090/vk
        public void Init()
        {
            var browser = new Mock <IBrowser>();

            browser.Setup(m => m.GetJson(It.Is <string>(s => s == Url),
                                         It.IsAny <IEnumerable <KeyValuePair <string, string> > >()))
            .Callback(Callback)
            .Returns(() =>
            {
                if (string.IsNullOrWhiteSpace(Json))
                {
                    throw new NullReferenceException(@"Json не может быть равен null. Обновите значение поля Json");
                }

                return(Json);
            });

            browser.Setup(o => o.Authorize(It.IsAny <IApiAuthParams>()))
            .Returns(VkAuthorization.From("https://vk.com/auth?__q_hash=qwerty&access_token=token&expires_in=1000&user_id=1"));

            browser.Setup(m => m.Validate(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(VkAuthorization.From("https://oauth.vk.com/blank.html#success=1&access_token=token&user_id=1"));

            var restClient = new Mock <IRestClient>();

            restClient.Setup(x =>
                             x.PostAsync(It.Is <Uri>(s => s == new Uri(Url)),
                                         It.IsAny <IEnumerable <KeyValuePair <string, string> > >()))
            .Callback(Callback)
            .Returns(() =>
            {
                if (string.IsNullOrWhiteSpace(Json))
                {
                    throw new NullReferenceException(@"Json не может быть равен null. Обновите значение поля Json");
                }

                return(Task.FromResult(HttpResponse <string> .Success(HttpStatusCode.OK,
                                                                      Json,
                                                                      Url)));
            });

            restClient.Setup(x => x.PostAsync(It.Is <Uri>(s => string.IsNullOrWhiteSpace(Url)),
                                              It.IsAny <IEnumerable <KeyValuePair <string, string> > >()))
            .Throws <ArgumentException>();

            Api = new VkApi
            {
                Browser    = browser.Object,
                RestClient = restClient.Object
            };

            Api.Authorize(new ApiAuthParams
            {
                ApplicationId = 1,
                Login         = "******",
                Password      = "******",
                Settings      = Settings.All
            });

            Api.RequestsPerSecond = 100000;             // Чтобы тесты быстрее выполнялись
        }
예제 #19
0
        public void IsAuthorized_Success()
        {
            var auth = VkAuthorization.From(Input);

            Assert.IsTrue(auth.IsAuthorized);
        }
예제 #20
0
        public void IsAuthorizationRequired_False()
        {
            var auth = VkAuthorization.From(uriFragment: Input);

            Assert.IsFalse(condition: auth.IsAuthorizationRequired);
        }
예제 #21
0
        public void IsAuthorized_Success()
        {
            var auth = VkAuthorization.From(uriFragment: Input);

            Assert.IsTrue(condition: auth.IsAuthorized);
        }
예제 #22
0
        public void IsAuthorized_Failed()
        {
            var auth = VkAuthorization.From(uriFragment: Input.Replace(oldValue: "access_token", newValue: "qwe"));

            Assert.IsFalse(condition: auth.IsAuthorized);
        }
예제 #23
0
        public void IsAuthorized_Failed()
        {
            var auth = VkAuthorization.From(Input.Replace("access_token", "qwe"));

            Assert.IsFalse(auth.IsAuthorized);
        }
예제 #24
0
        public void IsAuthorizationRequired_False()
        {
            var auth = VkAuthorization.From(Input);

            Assert.IsFalse(auth.IsAuthorizationRequired);
        }
예제 #25
0
        public void Authorize_BadLoginOrPasswrod_ThrowVkApiAuthorizationException()
        {
            const string urlWithBadLoginOrPassword = "******";
            var          browser = new Mock <IBrowser>();

            browser.Setup(b => b.Authorize(AppId, Email, Password, Settings.Friends)).Returns(VkAuthorization.From(new Uri(urlWithBadLoginOrPassword)));

            _vk.Browser = browser.Object;
            var ex = This.Action(() => _vk.Authorize(AppId, Email, Password, Settings.Friends)).Throws <VkApiAuthorizationException>();

            ex.Message.ShouldEqual(VkApi.InvalidAuthorization);
        }