This commit is contained in:
Paul Schneider
2021-06-06 20:32:39 +01:00
parent 6ae7333dbb
commit 43c6f31a65
20 changed files with 176 additions and 141 deletions

View File

@ -22,26 +22,27 @@ using Xunit.Abstractions;
using System.IO;
using System.Linq;
using Yavsc.Server.Models.IT.SourceCode;
using yavscTests.Settings;
namespace test
namespace yavscTests
{
[Collection("Yavsc mandatory success story")]
[Trait("regression", "oui")]
public class BatchTests: BaseTestContext, IClassFixture<ServerSideFixture>, IDisposable
{
ServerSideFixture _fixture;
private TestingSetup _testingOptions;
public BatchTests(ITestOutputHelper output, ServerSideFixture fixture) : base (output, fixture)
{
_fixture = fixture;
_testingOptions = fixture.TestingSetup;
}
[Fact]
public void GitClone()
{
Assert.True(_serverFixture.EnsureTestDb());
Assert.NotNull (_fixture.DbContext.Project);
var firstProject = _fixture.DbContext.Project.Include(p=>p.Repository).FirstOrDefault();
Assert.NotNull (_serverFixture.DbContext.Project);
var firstProject = _serverFixture.DbContext.Project.Include(p=>p.Repository).FirstOrDefault();
Assert.NotNull (firstProject);
var di = new DirectoryInfo(_serverFixture.SiteSetup.GitRepository);
if (!di.Exists) di.Create();
@ -94,19 +95,32 @@ namespace test
configurationRoot = builder.Build();
}
internal static BeforeCompileContext CreateYavscCompilationContext()
internal BeforeCompileContext CreateYavscCompilationContext()
{
BeforeCompileContext newBeforeCompileContext = null;
Assert.NotNull(_testingOptions);
try
{
var projectContext = new ProjectContext
{
Name = "Yavsc",
ProjectDirectory = "../Yavsc",
ProjectFilePath = "../Yavsc/project.json",
ProjectDirectory = _testingOptions.YavscWebPath,
ProjectFilePath = Path.Combine(_testingOptions.YavscWebPath, "project.json"),
TargetFramework = new FrameworkName("DNX", new Version(4, 5, 1)),
Configuration = Environment.GetEnvironmentVariable("ASPNET_ENV")
};
return new BeforeCompileContext(
newBeforeCompileContext = new BeforeCompileContext(
null, projectContext, () => null, () => null, () => null);
}
catch(Exception ex)
{
_output.WriteLine(ex.Message);
_output.WriteLine(ex.StackTrace);
}
return newBeforeCompileContext;
}
internal static IConfigurationRoot CreateConfiguration(string prjDir)
@ -144,19 +158,21 @@ namespace test
{
options.ResourcesPath = "Resources";
});
AppDomain.CurrentDomain.SetData("YAVSC_DB_CONNECTION", Startup.Testing.ConnectionStrings.Default);
AppDomain.CurrentDomain.SetData("YAVSC_DB_CONNECTION", Startup.TestingSetup.ConnectionStrings.Default);
serviceCollection.AddEntityFramework()
.AddNpgsql()
.AddDbContext<ApplicationDbContext>(
db => db.UseNpgsql(Startup.Testing.ConnectionStrings.Default)
db => db.UseNpgsql(Startup.TestingSetup.ConnectionStrings.Default)
);
provider = serviceCollection.BuildServiceProvider();
}
// TODO
[Fact]
public void ARequestAppDelegate()
public void ARequestDelegate()
{
try {
var services = new ServiceCollection();
services.AddTransient<IRuntimeEnvironment>(
svs => PlatformServices.Default.Runtime
@ -168,13 +184,20 @@ namespace test
IApplicationBuilder app = new ApplicationBuilder(serviceProvider);
var rtd = app.Build();
}
catch (Exception ex)
{
_output.WriteLine(ex.Message);
_output.WriteLine(ex.StackTrace);
}
}
[Fact]
public void InitApplicationBuilder()
{
try {
var services = new ServiceCollection();
services.AddTransient<IRuntimeEnvironment>(
@ -189,6 +212,17 @@ namespace test
var rtd = app.Build();
IOptions<LocalizationOptions> localOptions = ActivatorUtilities.GetServiceOrCreateInstance<IOptions<LocalizationOptions>>(provider); ;
}
catch (Exception ex)
{
_output.WriteLine(ex.Message);
_output.WriteLine(ex.StackTrace);
}
finally
{
}
}
public void Dispose()
@ -197,7 +231,7 @@ namespace test
{
Directory.Delete(Path.Combine(gitRepo,"yavsc"), true);
}
_fixture.DropTestDb();
_serverFixture.DropTestDb();
}
}
}

View File

@ -19,7 +19,7 @@ using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Filters;
using Microsoft.AspNet.Mvc.Razor;
namespace test
namespace yavscTests
{
[Collection("Yavsc Work In Progress")]
[Trait("regression", "oui")]
@ -35,11 +35,6 @@ namespace test
[Fact]
public void EnsureWeb()
{
var host = new WebHostBuilder();
host.UseEnvironment("Development")
.UseServer("Microsoft.AspNet.Server.Kestrel")
.UseStartup<test.Startup>()
.Build().Start();
}
}

View File

@ -10,13 +10,14 @@ using System.Net;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.Hosting;
using Microsoft.Extensions.OptionsModel;
using Microsoft.Extensions.PlatformAbstractions;
using Xunit;
using Xunit.Abstractions;
using Yavsc.Authentication;
using static OAuth.AspNet.AuthServer.Constants;
namespace test
namespace yavscTests
{
[Collection("Yavsc Work In Progress")]
[Trait("regression", "oui")]
@ -39,9 +40,7 @@ namespace test
string scope,
string authorizeUrl,
string redirectUrl,
string accessTokenUrl,
string login,
string pass
string accessTokenUrl
)
{
try
@ -52,8 +51,8 @@ namespace test
new Uri(authorizeUrl), new Uri(redirectUrl), new Uri(accessTokenUrl));
var query = new Dictionary<string, string>
{
[Parameters.Username] = Startup.Testing.ValidCreds[0].UserName,
[Parameters.Password] = Startup.Testing.ValidCreds[0].Password,
[Parameters.Username] = Startup.TestingSetup.ValidCreds.UserName,
[Parameters.Password] = Startup.TestingSetup.ValidCreds.Password,
[Parameters.GrantType] = GrantTypes.Password
};
@ -70,7 +69,7 @@ namespace test
var webex = ex as WebException;
if (webex != null && webex.Status == (WebExceptionStatus)400)
{
if (login == "joe")
if (Startup.TestingSetup.ValidCreds.UserName == "lame-user")
{
Console.WriteLine("Bad pass joe!");
return;
@ -85,30 +84,20 @@ namespace test
var allData = new List<object[]>();
for (int iTest=0; iTest < numTests && iTest < Startup.Testing.ValidCreds.Length; iTest++)
{
var login = Startup.Testing.ValidCreds[iTest].UserName;
var pass = Startup.Testing.ValidCreds[iTest].Password;
allData.Add(new object[] { "blouh", "profile",
"http://localhost:5000/authorize", "http://localhost:5000/oauth/success",
"http://localhost:5000/token", "http://localhost:5000/authorize"});
allData.Add(new object[] { ServerSideFixture.ApiKey, "blouh", "profile",
"http://localhost:5000/authorize", "http://localhost:5000/oauth/success",
"http://localhost:5000/token",login, pass});
}
var valid = allData.Count;
for (int iTest=0; iTest + valid < numTests && iTest < Startup.Testing.InvalidCreds.Length; iTest++)
{
var login = Startup.Testing.InvalidCreds[iTest].UserName;
var pass = Startup.Testing.InvalidCreds[iTest].Password;
allData.Add(new object[] { ServerSideFixture.ApiKey, "blouh", "profile",
"http://localhost:5000/authorize", "http://localhost:5000/oauth/success",
"http://localhost:5000/token",login, 0 });
}
return allData.Take(numTests);
allData.Add(new object[] { "blouh", "profile",
"http://localhost:5000/authorize", "http://localhost:5000/oauth/success",
"http://localhost:5000/token", "http://localhost:5000/authorize"});
return allData.Take(numTests);;
}
}
}

View File

@ -1,5 +1,5 @@
namespace test {
namespace yavscTests {
public class ResxResources {
const string resPath = "Resources/Test.TestResources.resx";
public void HaveAResxLoader()

View File

@ -10,11 +10,11 @@ using Yavsc;
using Yavsc.Models;
using Xunit;
using Npgsql;
using test.Settings;
using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Metadata.Conventions;
using yavscTests.Settings;
namespace test
namespace yavscTests
{
[Trait("regression", "II")]
public class ServerSideFixture : IDisposable
@ -26,7 +26,7 @@ namespace test
readonly ILoggerFactory _loggerFactory;
IEmailSender _mailSender;
public static string ApiKey => "53f4d5da-93a9-4584-82f9-b8fdf243b002";
public string ApiKey { get; private set; }
public ApplicationDbContext DbContext { get; private set; }
public SiteSettings SiteSetup
@ -46,7 +46,7 @@ namespace test
/// initialized by Init
/// </summary>
/// <value></value>
public static object TestingSetup { get; private set; }
public TestingSetup TestingSetup { get; private set; }
public IEmailSender MailSender
{
@ -76,9 +76,9 @@ namespace test
internal void UpgradeDb()
internal int UpgradeDb()
{
Microsoft.Data.Entity.Commands.Program.Main(
return Microsoft.Data.Entity.Commands.Program.Main(
new string[] { "database", "update" });
}
@ -95,7 +95,7 @@ namespace test
}
}
bool dbCreated;
private readonly WebHostBuilder host;
public WebHostBuilder Host { get; private set; }
private readonly IHostingEngine hostengnine;
@ -108,13 +108,13 @@ namespace test
//
public ServerSideFixture()
{
host = new WebHostBuilder();
AssertNotNull(host, nameof(host));
Host = new WebHostBuilder();
AssertNotNull(Host, nameof(Host));
hostengnine = host
.UseEnvironment("Development")
.UseServer("test")
.UseStartup<test.Startup>()
hostengnine = Host
.UseEnvironment("Testing")
.UseServer("yavscTests")
.UseStartup<Startup>()
.Build();
AssertNotNull(hostengnine, nameof(hostengnine));
@ -132,20 +132,27 @@ namespace test
_loggerFactory = App.Services.GetService(typeof(ILoggerFactory)) as ILoggerFactory;
AssertNotNull(_loggerFactory, nameof(_loggerFactory));
var siteSetup = App.Services.GetService(typeof(IOptions<SiteSettings>)) as IOptions<SiteSettings>;
AssertNotNull(siteSetup, nameof(siteSetup));
var testingSetup = App.Services.GetService(typeof(IOptions<Testing>)) as IOptions<Testing>;
var testingSetup = App.Services.GetService(typeof(IOptions<TestingSetup>)) as IOptions<TestingSetup>;
AssertNotNull(testingSetup, nameof(testingSetup));
DbContext = App.Services.GetService(typeof(ApplicationDbContext)) as ApplicationDbContext;
SiteSetup = siteSetup.Value;
TestingSetup = testingSetup.Value;
AssertNotNull(SiteSetup, nameof(SiteSetup));
TestingSetup = testingSetup.Value;
AssertNotNull(TestingSetup, nameof(TestingSetup));
Logger = _loggerFactory.CreateLogger<ServerSideFixture>();
AssertNotNull(Logger, nameof(Logger));
var builder = new DbConnectionStringBuilder
{
ConnectionString = Startup.Testing.ConnectionStrings.Default
ConnectionString = Startup.TestingSetup.ConnectionStrings.Default
};
ConventionSet conventions = new ConventionSet();
@ -155,6 +162,7 @@ namespace test
TestingDatabase = (string)builder["Database"];
AssertNotNull(TestingDatabase, nameof(TestingDatabase));
Logger.LogInformation("ServerSideFixture created.");
}
@ -167,7 +175,7 @@ namespace test
public void CheckDbExistence()
{
using (
NpgsqlConnection cx = new NpgsqlConnection(Startup.Testing.ConnectionStrings.DatabaseCtor))
NpgsqlConnection cx = new NpgsqlConnection(Startup.TestingSetup.ConnectionStrings.Default))
{
cx.Open();
_logger.LogInformation($"check db for TestingDatabase:{TestingDatabase}");
@ -183,16 +191,19 @@ namespace test
{
if (!DbCreated)
{
using (NpgsqlConnection cx = new NpgsqlConnection(Startup.Testing.ConnectionStrings.DatabaseCtor))
using (NpgsqlConnection cx =
new NpgsqlConnection(Startup.TestingSetup.ConnectionStrings.DatabaseCtor))
{
_logger.LogInformation($"create database for TestingDatabase : {TestingDatabase}");
cx.Open();
var command = cx.CreateCommand();
using (NpgsqlConnection ownercx = new NpgsqlConnection(Startup.Testing.ConnectionStrings.Default))
using (NpgsqlConnection ownercx = new NpgsqlConnection(Startup.TestingSetup.ConnectionStrings.Default))
command.CommandText = $"create database \"{TestingDatabase}\" OWNER \"{ownercx.UserName}\";";
_logger.LogInformation(command.CommandText);
command.ExecuteNonQuery();
cx.Close();
}
dbCreated = true;
@ -213,7 +224,15 @@ namespace test
}
public bool DbCreated { get {
try {
CheckDbExistence();
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
_logger.LogError(ex.StackTrace);
}
return dbCreated; } }
}
}