2021-11-22 16:49:51 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
using System.Threading.Tasks;
|
2023-11-29 10:51:40 +01:00
|
|
|
using BTCPayServer.Abstractions.Models;
|
|
|
|
using BTCPayServer.Hosting;
|
|
|
|
using BTCPayServer.Logging;
|
|
|
|
using BTCPayServer.Plugins.Bitcoin;
|
|
|
|
using BTCPayServer.Plugins;
|
2021-11-22 16:49:51 +01:00
|
|
|
using BTCPayServer.Tests.Logging;
|
2023-11-29 10:51:40 +01:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using NBXplorer;
|
2021-11-22 16:49:51 +01:00
|
|
|
using Xunit.Abstractions;
|
2023-11-29 10:51:40 +01:00
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Microsoft.Extensions.Configuration.Memory;
|
|
|
|
using NBitcoin;
|
2021-11-22 16:49:51 +01:00
|
|
|
|
|
|
|
namespace BTCPayServer.Tests
|
|
|
|
{
|
|
|
|
public class UnitTestBase
|
|
|
|
{
|
|
|
|
public UnitTestBase(ITestOutputHelper helper)
|
|
|
|
{
|
|
|
|
TestLogs = new XUnitLog(helper) { Name = "Tests" };
|
|
|
|
TestLogProvider = new XUnitLogProvider(helper);
|
2021-11-22 09:16:08 +01:00
|
|
|
BTCPayLogs = new BTCPayServer.Logging.Logs();
|
2023-11-29 10:51:40 +01:00
|
|
|
LoggerFactory = new BTCPayServer.Logging.FuncLoggerFactory((n) => new XUnitLog(helper) { Name = n });
|
|
|
|
BTCPayLogs.Configure(LoggerFactory);
|
|
|
|
}
|
|
|
|
|
2024-08-28 11:52:08 +02:00
|
|
|
public DatabaseTester CreateDBTester()
|
|
|
|
{
|
|
|
|
return new DatabaseTester(TestLogs, LoggerFactory);
|
|
|
|
}
|
|
|
|
|
2023-11-29 10:51:40 +01:00
|
|
|
public BTCPayNetworkProvider CreateNetworkProvider(ChainName chainName)
|
|
|
|
{
|
|
|
|
var conf = new ConfigurationRoot(new List<IConfigurationProvider>()
|
|
|
|
{
|
|
|
|
new MemoryConfigurationProvider(new MemoryConfigurationSource()
|
|
|
|
{
|
|
|
|
InitialData = new[] {
|
|
|
|
new KeyValuePair<string, string>("chains", "*"),
|
|
|
|
new KeyValuePair<string, string>("network", chainName.ToString())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
return CreateNetworkProvider(conf);
|
|
|
|
}
|
|
|
|
public BTCPayNetworkProvider CreateNetworkProvider(IConfiguration conf = null)
|
|
|
|
{
|
|
|
|
conf ??= new ConfigurationRoot(new List<IConfigurationProvider>()
|
|
|
|
{
|
|
|
|
new MemoryConfigurationProvider(new MemoryConfigurationSource()
|
|
|
|
{
|
|
|
|
InitialData = new[] {
|
|
|
|
new KeyValuePair<string, string>("chains", "*"),
|
|
|
|
new KeyValuePair<string, string>("network", "regtest")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
var bootstrap = Startup.CreateBootstrap(conf);
|
|
|
|
var services = new PluginServiceCollection(new ServiceCollection(), bootstrap);
|
|
|
|
var plugins = new List<BaseBTCPayServerPlugin>() { new BitcoinPlugin() };
|
2024-08-30 01:34:23 +02:00
|
|
|
|
2023-11-29 10:51:40 +01:00
|
|
|
plugins.Add(new BTCPayServer.Plugins.Altcoins.AltcoinsPlugin());
|
2024-08-30 01:34:23 +02:00
|
|
|
|
2023-11-29 10:51:40 +01:00
|
|
|
foreach (var p in plugins)
|
|
|
|
{
|
|
|
|
p.Execute(services);
|
|
|
|
}
|
|
|
|
services.AddSingleton(services.BootstrapServices.GetRequiredService<SelectedChains>());
|
|
|
|
services.AddSingleton(services.BootstrapServices.GetRequiredService<NBXplorerNetworkProvider>());
|
|
|
|
services.AddSingleton(services.BootstrapServices.GetRequiredService<Logs>());
|
|
|
|
services.AddSingleton(services.BootstrapServices.GetRequiredService<IConfiguration>());
|
|
|
|
services.AddSingleton<BTCPayNetworkProvider>();
|
|
|
|
var serviceProvider = services.BuildServiceProvider();
|
|
|
|
return serviceProvider.GetService<BTCPayNetworkProvider>();
|
2021-11-22 16:49:51 +01:00
|
|
|
}
|
|
|
|
public ILog TestLogs
|
|
|
|
{
|
|
|
|
get;
|
|
|
|
}
|
|
|
|
public XUnitLogProvider TestLogProvider
|
|
|
|
{
|
|
|
|
get;
|
|
|
|
}
|
2021-11-22 09:16:08 +01:00
|
|
|
public BTCPayServer.Logging.Logs BTCPayLogs { get; }
|
2023-11-29 10:51:40 +01:00
|
|
|
public FuncLoggerFactory LoggerFactory { get; }
|
2021-11-22 16:49:51 +01:00
|
|
|
|
|
|
|
public ServerTester CreateServerTester([CallerMemberNameAttribute] string scope = null, bool newDb = false)
|
|
|
|
{
|
2023-11-29 10:51:40 +01:00
|
|
|
return new ServerTester(scope, newDb, TestLogs, TestLogProvider, CreateNetworkProvider());
|
2021-11-22 16:49:51 +01:00
|
|
|
}
|
|
|
|
public SeleniumTester CreateSeleniumTester([CallerMemberNameAttribute] string scope = null, bool newDb = false)
|
|
|
|
{
|
2023-11-29 10:51:40 +01:00
|
|
|
return new SeleniumTester() { Server = new ServerTester(scope, newDb, TestLogs, TestLogProvider, CreateNetworkProvider()) };
|
2021-11-22 16:49:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|