Skip to content

Commit

Permalink
#625 AuthorizationFormHtmlParser.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
inyutin-maxim committed Mar 9, 2019
1 parent 23e2c29 commit de9de47
Show file tree
Hide file tree
Showing 18 changed files with 2,010 additions and 35 deletions.
109 changes: 109 additions & 0 deletions VkNet.Tests/Infrastructure/AuthorizationFormHtmlParserTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Flurl;
using Flurl.Http.Testing;
using Moq.AutoMock;
using NUnit.Framework;
using VkNet.Infrastructure.Authorization.ImplicitFlow;

namespace VkNet.Tests.Infrastructure
{
[TestFixture]
public class AuthorizationFormHtmlParserTests
{
[Test]
public async Task LoginForm()
{
using (var httpTest = new HttpTest())
{
httpTest.RespondWith(ReadHtmlFile("login"));
var mocker = new AutoMocker();

var parser = mocker.CreateInstance<AuthorizationFormHtmlParser>();
var result = await parser.GetFormAsync(new Url("https://m.vk.com/login?act=authcheck&m=442"));

Assert.NotNull(result);
Assert.AreEqual("post", result.Method);
Assert.AreEqual("https://login.vk.com/?act=login&amp;soft=1&amp;utf8=1", result.Action);
Assert.IsNotEmpty(result.Fields);
}
}

[Test]
public async Task CaptchaForm()
{
using (var httpTest = new HttpTest())
{
httpTest.RespondWith(ReadHtmlFile("captcha"));
var mocker = new AutoMocker();

var parser = mocker.CreateInstance<AuthorizationFormHtmlParser>();
var result = await parser.GetFormAsync(new Url("https://m.vk.com/login?act=authcheck&m=442"));

Assert.NotNull(result);
Assert.AreEqual("post", result.Method);
Assert.AreEqual("https://login.vk.com/?act=login&amp;soft=1&amp;utf8=1", result.Action);
Assert.IsNotEmpty(result.Fields);
}
}

[Test]
public async Task TwoFaForm()
{
using (var httpTest = new HttpTest())
{
httpTest.RespondWith(ReadHtmlFile("twofa"));
var mocker = new AutoMocker();

var parser = mocker.CreateInstance<AuthorizationFormHtmlParser>();
var result = await parser.GetFormAsync(new Url("https://m.vk.com/login?act=authcheck&m=442"));

Assert.NotNull(result);
Assert.AreEqual("post", result.Method);
Assert.AreEqual("https://m.vk.com/login?act=authcheck_code&amp;hash=1552162040_c98f31a6e83d3c91c1", result.Action);
Assert.IsNotEmpty(result.Fields);
}
}

[Test]
public async Task ConsentForm()
{
using (var httpTest = new HttpTest())
{
httpTest.RespondWith(ReadHtmlFile("consent"));
var mocker = new AutoMocker();

var parser = mocker.CreateInstance<AuthorizationFormHtmlParser>();
var result = await parser.GetFormAsync(new Url("https://m.vk.com/login?act=authcheck&m=442"));

Assert.NotNull(result);
Assert.AreEqual("post", result.Method);
Assert.AreEqual("https://login.vk.com/?act=grant_access&amp;client_id=4268118&amp;settings=140492255&amp;redirect_uri=https%3A%2F%2Foauth.vk.com%2Fblank.html&amp;response_type=token&amp;group_ids=&amp;token_type=0&amp;v=&amp;state=123&amp;display=mobile&amp;ip_h=5a4524d95f3521be68&amp;hash=1552162134_98614f6fce5d86d252&amp;https=1", result.Action);
Assert.IsNotEmpty(result.Fields);
}
}

private string ReadHtmlFile(string fileNameWithoutExtension)
{
var folders = new List<string>
{
AppContext.BaseDirectory,
"TestData",
"Authorization",
fileNameWithoutExtension
};

var path = Path.Combine(folders.ToArray()) + ".html";

if (!File.Exists(path))
{
throw new FileNotFoundException(path);
}

return File.ReadAllText(path, Encoding.UTF8);
}
}
}
41 changes: 37 additions & 4 deletions VkNet.Tests/Infrastructure/ImplicitFlowTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
using System;
using System.Text;
using System.Threading.Tasks;
using Flurl;
using Moq;
using Moq.AutoMock;
using NUnit.Framework;
using VkNet.Abstractions.Core;
using VkNet.Enums;
using VkNet.Enums.Filters;
using VkNet.Enums.SafetyEnums;
using VkNet.Exception;
using VkNet.Infrastructure.Authorization;
using VkNet.Infrastructure.Authorization.ImplicitFlow;
using VkNet.Model;
using VkNet.Utils;

Expand All @@ -18,8 +23,8 @@ public class ImplicitFlowTests
[Test]
public void CreateAuthorizeUrl()
{
const int clientId = 1;
var scope = Settings.Notify.ToUInt64();
const int clientId = 4268118;
var scope = Settings.All|Settings.Offline;
const string state = "123";
var display = Display.Mobile;
var builder = new StringBuilder("https://oauth.vk.com/authorize?");
Expand All @@ -28,7 +33,7 @@ public void CreateAuthorizeUrl()
builder.Append("redirect_uri=https://oauth.vk.com/blank.html&");

builder.Append($"display={display}&");
builder.Append($"scope={scope}&");
builder.Append($"scope={scope.ToUInt64()}&");
builder.Append("response_type=token&");
builder.Append("v=5.92&");

Expand All @@ -41,7 +46,7 @@ public void CreateAuthorizeUrl()

var implicitFlow = mocker.CreateInstance<ImplicitFlow>();

var authorizeUrl = implicitFlow.CreateAuthorizeUrl(clientId, scope, display, state);
var authorizeUrl = implicitFlow.CreateAuthorizeUrl(clientId, scope.ToUInt64(), display, state);

Assert.AreEqual(new Url(expected), authorizeUrl);
}
Expand All @@ -58,6 +63,34 @@ public async Task Authorize()

mocker.Setup<IVkApiVersionManager, string>(x => x.Version).Returns("5.92");

mocker.Setup<IAuthorizationForm, Task<AuthorizationFormResult>>(x => x.ExecuteAsync(It.IsAny<Url>()))
.ReturnsAsync(new AuthorizationFormResult
{
ResponseUrl = "https://m.vk.com/login?act=authcheck&m=442",
RequestUrl = "https://m.vk.com/login?act=authcheck&m=442",
Cookies = new Cookies()
});

mocker.Setup<IAuthorizationFormFactory, IAuthorizationForm>(x => x.Create(It.IsAny<ImplicitFlowPageType>()))
.Returns(mocker.Get<IAuthorizationForm>());

mocker.GetMock<IVkAuthorization<ImplicitFlowPageType>>()
.SetupSequence(x => x.GetPageType(It.IsAny<Uri>()))
.Returns(ImplicitFlowPageType.LoginPassword)
.Returns(ImplicitFlowPageType.TwoFactor)
.Returns(ImplicitFlowPageType.Consent)
.Returns(ImplicitFlowPageType.Result);

mocker.GetMock<IVkAuthorization<ImplicitFlowPageType>>()
.Setup(x => x.GetAuthorizationResult(It.IsAny<Uri>()))
.Returns(new AuthorizationResult
{
AccessToken = "access_token",
UserId = 123,
ExpiresIn = 0,
State = "123"
});

var implicitFlow = mocker.CreateInstance<ImplicitFlow>();
var result = await implicitFlow.AuthorizeAsync().ConfigureAwait(false);

Expand Down
Loading

0 comments on commit de9de47

Please sign in to comment.