2019-05-13 12:11:15 +08:00
using Longbow.Data ;
using Microsoft.AspNetCore.Hosting ;
2019-01-21 17:33:20 +08:00
using Microsoft.AspNetCore.Mvc.Testing ;
using Microsoft.AspNetCore.Mvc.Testing.Handlers ;
2019-05-12 14:28:56 +08:00
using Microsoft.Extensions.Configuration ;
2019-01-21 17:33:20 +08:00
using System ;
2019-05-13 12:11:15 +08:00
using System.Collections.Generic ;
2019-05-17 18:04:46 +08:00
using System.IO ;
2019-01-21 17:33:20 +08:00
using System.Net ;
using System.Net.Http ;
using UnitTest ;
using Xunit ;
namespace Bootstrap.Admin
{
2019-01-24 17:58:06 +08:00
[CollectionDefinition("SQLServerContext")]
2019-01-21 17:33:20 +08:00
public class BootstrapAdminTestContext : ICollectionFixture < BAWebHost >
{
}
2019-01-24 17:58:06 +08:00
[CollectionDefinition("SQLiteContext")]
public class SQLiteContext : ICollectionFixture < SQLiteBAWebHost >
{
}
[CollectionDefinition("MySqlContext")]
public class MySqlContext : ICollectionFixture < MySqlBAWebHost >
{
}
2019-05-13 12:11:15 +08:00
[CollectionDefinition("MongoContext")]
public class MongoContext : ICollectionFixture < MongoBAWebHost >
{
}
2019-01-24 17:58:06 +08:00
public class MySqlBAWebHost : BAWebHost
{
protected override void ConfigureWebHost ( IWebHostBuilder builder )
{
base . ConfigureWebHost ( builder ) ;
2019-05-13 12:11:15 +08:00
TestHelper . ConfigureWebHost ( builder , DatabaseProviderType . MySql ) ;
2019-01-24 17:58:06 +08:00
}
}
public class SQLiteBAWebHost : BAWebHost
{
protected override void ConfigureWebHost ( IWebHostBuilder builder )
{
base . ConfigureWebHost ( builder ) ;
2019-05-13 12:11:15 +08:00
TestHelper . ConfigureWebHost ( builder , DatabaseProviderType . SQLite ) ;
}
}
public class MongoBAWebHost : BAWebHost
{
protected override void ConfigureWebHost ( IWebHostBuilder builder )
{
base . ConfigureWebHost ( builder ) ;
builder . ConfigureAppConfiguration ( app = > app . AddInMemoryCollection ( new KeyValuePair < string , string > [ ] {
new KeyValuePair < string , string > ( "DB:0:Enabled" , "false" ) ,
new KeyValuePair < string , string > ( "DB:1:Enabled" , "false" ) ,
new KeyValuePair < string , string > ( "DB:2:Enabled" , "false" ) ,
new KeyValuePair < string , string > ( "DB:3:Enabled" , "false" )
} ) ) ;
2019-01-24 17:58:06 +08:00
}
}
2019-01-21 17:33:20 +08:00
/// <summary>
///
/// </summary>
public class BAWebHost : WebApplicationFactory < Startup >
{
/// <summary>
///
/// </summary>
static BAWebHost ( )
{
// Copy license
TestHelper . CopyLicense ( ) ;
}
public BAWebHost ( )
{
var client = CreateClient ( "Account/Login" ) ;
var login = client . LoginAsync ( ) ;
login . Wait ( ) ;
}
/// <summary>
/// 获得已经登录的HttpClient
/// </summary>
/// <param name="baseAddress"></param>
/// <returns></returns>
2019-03-02 15:15:47 +08:00
public HttpClient CreateClient ( string baseAddress )
{
var client = CreateDefaultClient ( new Uri ( $"http://localhost/{baseAddress}/" ) , new RedirectHandler ( 7 ) , new CookieContainerHandler ( _cookie ) ) ;
return client ;
}
2019-01-21 17:33:20 +08:00
2019-03-05 13:05:19 +08:00
protected override void ConfigureClient ( HttpClient client )
{
base . ConfigureClient ( client ) ;
2019-05-11 12:15:03 +08:00
client . DefaultRequestHeaders . Add ( "User-Agent" , "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1_UnitTest) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.1 Safari/605.1.15" ) ;
2019-03-05 13:05:19 +08:00
}
2019-01-21 17:33:20 +08:00
private readonly CookieContainer _cookie = new CookieContainer ( ) ;
protected override void ConfigureWebHost ( IWebHostBuilder builder )
{
base . ConfigureWebHost ( builder ) ;
2019-05-12 14:28:56 +08:00
var config = new ConfigurationBuilder ( ) ;
2019-05-17 18:04:46 +08:00
config . AddJsonFile ( TestHelper . RetrievePath ( $"UnitTest{Path.DirectorySeparatorChar}appsettings.json" ) , false , true ) ;
2019-05-12 14:28:56 +08:00
config . AddEnvironmentVariables ( ) ;
var con = config . Build ( ) ;
if ( con . GetValue ( "Appveyor" , false ) )
{
TestHelper . SQLServerConnectionString = con . GetConnectionString ( "sqlserver-app" ) ;
TestHelper . MySqlConnectionString = con . GetConnectionString ( "mysql-app" ) ;
TestHelper . NpgSqlConnectionString = con . GetConnectionString ( "npgsql-app" ) ;
}
else
{
TestHelper . SQLServerConnectionString = con . GetConnectionString ( "sqlserver" ) ;
TestHelper . MySqlConnectionString = con . GetConnectionString ( "mysql" ) ;
TestHelper . NpgSqlConnectionString = con . GetConnectionString ( "npgsql" ) ;
2019-05-15 11:08:31 +08:00
builder . ConfigureAppConfiguration ( app = > app . AddInMemoryCollection ( new KeyValuePair < string , string > [ ] {
new KeyValuePair < string , string > ( "MongoDB" , con . GetValue ( "MongoDB" , "UnitTest" ) )
} ) ) ;
2019-05-12 14:28:56 +08:00
}
2019-05-18 17:48:40 +08:00
builder . ConfigureAppConfiguration ( app = > app . AddJsonFile ( TestHelper . RetrievePath ( $"UnitTest{Path.DirectorySeparatorChar}appsettings.unittest.json" ) , false , true ) ) ;
2019-05-12 14:28:56 +08:00
TestHelper . SQLiteConnectionString = con . GetConnectionString ( "sqlite" ) ;
2019-01-21 17:33:20 +08:00
TestHelper . ConfigureWebHost ( builder ) ;
}
}
}