yavsc/test/WIP/YavscWorkInProgress.cs

121 lines
4 KiB
C#
Raw Normal View History

2018-07-15 00:29:06 +02:00
// // YavscWorkInProgress.cs
// /*
// paul 21/06/2018 10:11 20182018 6 21
// */
using System;
2018-07-25 02:12:13 +02:00
using System.Collections.Generic;
2018-08-02 12:28:13 +02:00
using System.Linq;
2018-07-25 02:12:13 +02:00
using System.Net;
2018-08-03 02:59:37 +02:00
using System.Text;
2018-07-25 02:12:13 +02:00
using System.Threading.Tasks;
2018-07-15 00:29:06 +02:00
using Xunit;
2018-07-21 16:15:54 +02:00
using Xunit.Abstractions;
2018-07-25 02:12:13 +02:00
using Yavsc.Authentication;
using static OAuth.AspNet.AuthServer.Constants;
2018-07-15 00:29:06 +02:00
2018-07-20 02:27:53 +02:00
namespace test
2018-07-15 00:29:06 +02:00
{
[Collection("Yavsc Work In Progress")]
2018-08-01 06:12:42 +02:00
[Trait("regres", "yes")]
2018-07-21 16:25:08 +02:00
public class YavscWorkInProgress : BaseTestContext, IClassFixture<ServerSideFixture>
2018-07-15 00:29:06 +02:00
{
2018-07-21 16:15:54 +02:00
public YavscWorkInProgress(ServerSideFixture serverFixture, ITestOutputHelper output)
2018-08-02 12:28:13 +02:00
: base(output, serverFixture)
2018-07-21 16:15:54 +02:00
{
}
2018-07-15 00:29:06 +02:00
2018-07-25 02:12:13 +02:00
[Theory]
2018-08-02 12:28:13 +02:00
[MemberData(nameof(GetLoginIntentData), parameters: 1)]
2018-07-25 02:12:13 +02:00
public async Task TestUserMayLogin
(
string clientId,
2018-08-02 12:28:13 +02:00
string clientSecret,
string scope,
string authorizeUrl,
string redirectUrl,
string accessTokenUrl,
string login,
string pass
2018-07-25 02:12:13 +02:00
)
{
2018-08-02 12:28:13 +02:00
try
{
var oauthor = new OAuthenticator(clientId, clientSecret, scope,
new Uri(authorizeUrl), new Uri(redirectUrl), new Uri(accessTokenUrl));
var query = new Dictionary<string, string>();
query[Parameters.Username] = login;
query[Parameters.Password] = pass;
query[Parameters.GrantType] = GrantTypes.Password;
var result = await oauthor.RequestAccessTokenAsync(query);
Console.WriteLine(">> Got an output");
Console.WriteLine(Parameters.AccessToken + ": " + result[Parameters.AccessToken]);
Console.WriteLine(Parameters.TokenType + ": " + result[Parameters.TokenType]);
Console.WriteLine(Parameters.ExpiresIn + ": " + result[Parameters.ExpiresIn]);
Console.WriteLine(Parameters.RefreshToken + ": " + result[Parameters.RefreshToken]);
2018-07-15 00:29:06 +02:00
2018-08-02 12:28:13 +02:00
}
catch (Exception ex)
{
var webex = ex as WebException;
if (webex != null && webex.Status == (WebExceptionStatus)400)
{
if (login == "joe")
{
2018-07-25 02:12:13 +02:00
Console.WriteLine("Bad pass joe!");
return;
2018-08-02 12:28:13 +02:00
}
}
throw;
}
2018-07-15 00:29:06 +02:00
}
2018-08-03 02:59:37 +02:00
public static string GetPassword()
2018-08-02 12:28:13 +02:00
{
2018-08-03 02:59:37 +02:00
var pwd = new StringBuilder();
while (true)
{
var len = pwd.ToString().Length;
ConsoleKeyInfo i = Console.ReadKey(true);
if (i.Key == ConsoleKey.Enter)
{
break;
}
else if (i.Key == ConsoleKey.Backspace)
{
if (pwd.Length > 0)
{
pwd.Remove(len - 1, 1);
Console.Write("\b \b");
}
}
else
{
pwd.Append(i.KeyChar);
Console.Write("*");
}
}
return pwd.ToString();
2018-08-02 12:28:13 +02:00
}
public static IEnumerable<object[]> GetLoginIntentData(int numTests)
{
2018-08-03 02:59:37 +02:00
var allData = new List<object[]>();
Console.WriteLine($"Please, enter {numTests}:");
for (int iTest=0; iTest<numTests; iTest++)
2018-08-02 12:28:13 +02:00
{
2018-08-03 02:59:37 +02:00
Console.Write("Please, enter a login:");
var login = Console.ReadLine();
Console.Write("Please, enter a pass:");
var pass = GetPassword();
2018-08-02 12:28:13 +02:00
2018-08-03 02:59:37 +02:00
allData.Add(new object[] { ServerSideFixture.ApiKey, "blouh", "profile",
"http://localhost:5000/authorize", "http://localhost:5000/oauth/success",
"http://localhost:5000/token",login, pass });
}
2018-08-02 12:28:13 +02:00
return allData.Take(numTests);
}
2018-07-15 00:29:06 +02:00
}
}