btcpayserver/BTCPayServer.Tests/BTCPayServerTester.cs

331 lines
13 KiB
C#
Raw Normal View History

2020-06-28 21:44:35 -05:00
using System;
2020-06-28 17:55:27 +09:00
using System.Collections.Generic;
using System.IO;
using System.Linq;
2020-06-28 17:55:27 +09:00
using System.Net.Http;
using System.Security.Claims;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using BTCPayServer.Configuration;
using BTCPayServer.HostedServices;
2017-09-13 15:47:34 +09:00
using BTCPayServer.Hosting;
2018-05-03 03:32:42 +09:00
using BTCPayServer.Rating;
2020-06-28 17:55:27 +09:00
using BTCPayServer.Services;
2017-10-20 14:06:37 -05:00
using BTCPayServer.Services.Invoices;
using BTCPayServer.Services.Rates;
2018-04-30 02:33:42 +09:00
using BTCPayServer.Services.Stores;
2017-09-13 15:47:34 +09:00
using BTCPayServer.Tests.Logging;
using BTCPayServer.Tests.Mocks;
using Microsoft.AspNetCore.Hosting;
2020-06-28 17:55:27 +09:00
using Microsoft.AspNetCore.Hosting.Server.Features;
2017-09-13 15:47:34 +09:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
2020-06-28 17:55:27 +09:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
2017-09-13 15:47:34 +09:00
using Microsoft.Extensions.Logging;
using NBitcoin;
using NBXplorer;
2019-10-12 20:35:30 +09:00
using AuthenticationSchemes = BTCPayServer.Security.AuthenticationSchemes;
2017-09-13 15:47:34 +09:00
namespace BTCPayServer.Tests
{
public enum TestDatabases
{
Postgres,
MySQL,
}
public class BTCPayServerTester : IDisposable
{
private readonly string _Directory;
public BTCPayServerTester(string scope)
{
this._Directory = scope ?? throw new ArgumentNullException(nameof(scope));
}
public Uri NBXplorerUri
{
get; set;
}
public Uri LTCNBXplorerUri { get; set; }
2019-12-24 08:20:44 +01:00
public Uri LBTCNBXplorerUri { get; set; }
2018-04-15 21:18:51 +09:00
public Uri ServerUri
{
get;
set;
}
public string MySQL
{
get; set;
}
public string Postgres
{
get; set;
}
IWebHost _Host;
public int Port
{
get; set;
}
public TestDatabases TestDatabase
{
get; set;
}
2018-04-15 21:18:51 +09:00
public bool MockRates { get; set; } = true;
public string SocksEndpoint { get; set; }
2018-04-15 21:18:51 +09:00
2020-06-28 17:55:27 +09:00
public HashSet<string> Chains { get; set; } = new HashSet<string>() { "BTC" };
public bool UseLightning { get; set; }
public bool AllowAdminRegistration { get; set; } = true;
public bool DisableRegistration { get; set; } = false;
2019-10-07 16:04:25 +09:00
public async Task StartAsync()
{
if (!Directory.Exists(_Directory))
Directory.CreateDirectory(_Directory);
2018-04-19 16:54:25 +09:00
string chain = NBXplorerDefaultSettings.GetFolderName(NetworkType.Regtest);
2018-01-12 16:00:31 +09:00
string chainDirectory = Path.Combine(_Directory, chain);
if (!Directory.Exists(chainDirectory))
Directory.CreateDirectory(chainDirectory);
StringBuilder config = new StringBuilder();
2018-01-12 16:00:31 +09:00
config.AppendLine($"{chain.ToLowerInvariant()}=1");
if (InContainer)
{
config.AppendLine($"bind=0.0.0.0");
}
config.AppendLine($"port={Port}");
2019-12-24 08:20:44 +01:00
config.AppendLine($"chains={string.Join(',', Chains)}");
if (Chains.Contains("BTC", StringComparer.OrdinalIgnoreCase))
{
config.AppendLine($"btc.explorer.url={NBXplorerUri.AbsoluteUri}");
config.AppendLine($"btc.explorer.cookiefile=0");
}
if (UseLightning)
{
2019-12-24 08:20:44 +01:00
config.AppendLine($"btc.lightning={IntegratedLightning.AbsoluteUri}");
var localLndBackupFile = Path.Combine(_Directory, "walletunlock.json");
File.Copy(TestUtils.GetTestDataFullPath("LndSeedBackup/walletunlock.json"), localLndBackupFile, true);
config.AppendLine($"btc.external.lndseedbackup={localLndBackupFile}");
}
2019-12-24 08:20:44 +01:00
if (Chains.Contains("LTC", StringComparer.OrdinalIgnoreCase))
{
config.AppendLine($"ltc.explorer.url={LTCNBXplorerUri.AbsoluteUri}");
config.AppendLine($"ltc.explorer.cookiefile=0");
}
if (Chains.Contains("LBTC", StringComparer.OrdinalIgnoreCase))
{
config.AppendLine($"lbtc.explorer.url={LBTCNBXplorerUri.AbsoluteUri}");
config.AppendLine($"lbtc.explorer.cookiefile=0");
}
if (AllowAdminRegistration)
config.AppendLine("allow-admin-registration=1");
2020-06-28 17:55:27 +09:00
2019-11-08 16:10:49 +09:00
config.AppendLine($"torrcfile={TestUtils.GetTestDataFullPath("Tor/torrc")}");
config.AppendLine($"socksendpoint={SocksEndpoint}");
config.AppendLine($"debuglog=debug.log");
2019-12-24 08:20:44 +01:00
if (!string.IsNullOrEmpty(SSHPassword) && string.IsNullOrEmpty(SSHKeyFile))
config.AppendLine($"sshpassword={SSHPassword}");
if (!string.IsNullOrEmpty(SSHKeyFile))
config.AppendLine($"sshkeyfile={SSHKeyFile}");
if (!string.IsNullOrEmpty(SSHConnection))
config.AppendLine($"sshconnection={SSHConnection}");
if (TestDatabase == TestDatabases.MySQL && !String.IsNullOrEmpty(MySQL))
config.AppendLine($"mysql=" + MySQL);
else if (!String.IsNullOrEmpty(Postgres))
config.AppendLine($"postgres=" + Postgres);
2018-01-12 16:00:31 +09:00
var confPath = Path.Combine(chainDirectory, "settings.config");
2019-10-07 16:25:27 +09:00
await File.WriteAllTextAsync(confPath, config.ToString());
ServerUri = new Uri("http://" + HostName + ":" + Port + "/");
HttpClient = new HttpClient();
HttpClient.BaseAddress = ServerUri;
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
var conf = new DefaultConfiguration() { Logger = Logs.LogProvider.CreateLogger("Console") }.CreateConfiguration(new[] { "--datadir", _Directory, "--conf", confPath, "--disable-registration", DisableRegistration ? "true" : "false" });
_Host = new WebHostBuilder()
.UseConfiguration(conf)
.UseContentRoot(FindBTCPayServerDirectory())
.UseWebRoot(Path.Combine(FindBTCPayServerDirectory(), "wwwroot"))
.ConfigureServices(s =>
{
s.AddLogging(l =>
{
l.AddFilter("System.Net.Http.HttpClient", LogLevel.Critical);
l.SetMinimumLevel(LogLevel.Information)
.AddFilter("Microsoft", LogLevel.Error)
.AddFilter("Hangfire", LogLevel.Error)
.AddProvider(Logs.LogProvider);
});
})
2020-04-06 19:18:49 +09:00
.ConfigureServices(services =>
{
services.TryAddSingleton<IFeeProviderFactory>(new BTCPayServer.Services.Fees.FixedFeeProvider(new FeeRate(100L, 1)));
})
.UseKestrel()
.UseStartup<Startup>()
.Build();
2019-10-07 16:04:25 +09:00
await _Host.StartWithTasksAsync();
var urls = _Host.ServerFeatures.Get<IServerAddressesFeature>().Addresses;
foreach (var url in urls)
{
Logs.Tester.LogInformation("Listening on " + url);
}
Logs.Tester.LogInformation("Server URI " + ServerUri);
InvoiceRepository = (InvoiceRepository)_Host.Services.GetService(typeof(InvoiceRepository));
2018-08-23 00:24:33 +09:00
StoreRepository = (StoreRepository)_Host.Services.GetService(typeof(StoreRepository));
Networks = (BTCPayNetworkProvider)_Host.Services.GetService(typeof(BTCPayNetworkProvider));
2018-05-03 03:32:42 +09:00
2018-08-23 00:24:33 +09:00
if (MockRates)
2018-05-03 03:32:42 +09:00
{
2018-08-23 00:24:33 +09:00
var rateProvider = (RateProviderFactory)_Host.Services.GetService(typeof(RateProviderFactory));
rateProvider.Providers.Clear();
2020-06-24 17:51:00 +09:00
coinAverageMock = new MockRateProvider();
coinAverageMock.ExchangeRates.Add(new PairRate(CurrencyPair.Parse("BTC_USD"), new BidAsk(5000m)));
coinAverageMock.ExchangeRates.Add(new PairRate(CurrencyPair.Parse("BTC_CAD"), new BidAsk(4500m)));
coinAverageMock.ExchangeRates.Add(new PairRate(CurrencyPair.Parse("BTC_LTC"), new BidAsk(162m)));
coinAverageMock.ExchangeRates.Add(new PairRate(CurrencyPair.Parse("LTC_USD"), new BidAsk(500m)));
rateProvider.Providers.Add("coingecko", coinAverageMock);
2018-08-23 00:24:33 +09:00
var bitflyerMock = new MockRateProvider();
bitflyerMock.ExchangeRates.Add(new PairRate(CurrencyPair.Parse("BTC_JPY"), new BidAsk(700000m)));
2018-08-23 00:24:33 +09:00
rateProvider.Providers.Add("bitflyer", bitflyerMock);
var quadrigacx = new MockRateProvider();
quadrigacx.ExchangeRates.Add(new PairRate(CurrencyPair.Parse("BTC_CAD"), new BidAsk(6000m)));
2018-08-23 00:24:33 +09:00
rateProvider.Providers.Add("quadrigacx", quadrigacx);
var bittrex = new MockRateProvider();
bittrex.ExchangeRates.Add(new PairRate(CurrencyPair.Parse("DOGE_BTC"), new BidAsk(0.004m)));
2018-08-23 00:24:33 +09:00
rateProvider.Providers.Add("bittrex", bittrex);
2020-06-28 17:55:27 +09:00
2019-12-24 08:20:44 +01:00
var bitfinex = new MockRateProvider();
bitfinex.ExchangeRates.Add(new PairRate(CurrencyPair.Parse("UST_BTC"), new BidAsk(0.000136m)));
2019-12-24 08:20:44 +01:00
rateProvider.Providers.Add("bitfinex", bitfinex);
2020-06-28 17:55:27 +09:00
2020-03-20 09:31:22 +01:00
var bitpay = new MockRateProvider();
bitpay.ExchangeRates.Add(new PairRate(CurrencyPair.Parse("ETB_BTC"), new BidAsk(0.1m)));
rateProvider.Providers.Add("bitpay", bitpay);
2018-08-23 00:24:33 +09:00
}
Logs.Tester.LogInformation("Waiting site is operational...");
2019-10-07 16:04:25 +09:00
await WaitSiteIsOperational();
Logs.Tester.LogInformation("Site is now operational");
}
2020-06-24 17:51:00 +09:00
MockRateProvider coinAverageMock;
private async Task WaitSiteIsOperational()
{
_ = HttpClient.GetAsync("/").ConfigureAwait(false);
using (var cts = new CancellationTokenSource(20_000))
{
var synching = WaitIsFullySynched(cts.Token);
await Task.WhenAll(synching).ConfigureAwait(false);
}
// Opportunistic call to wake up view compilation in debug mode, we don't need to await.
}
private async Task WaitIsFullySynched(CancellationToken cancellationToken)
{
var dashBoard = GetService<NBXplorerDashboard>();
while (!dashBoard.IsFullySynched())
{
await Task.Delay(10, cancellationToken).ConfigureAwait(false);
}
}
2018-04-15 21:18:51 +09:00
private string FindBTCPayServerDirectory()
{
var solutionDirectory = TestUtils.TryGetSolutionDirectoryInfo(Directory.GetCurrentDirectory());
return Path.Combine(solutionDirectory.FullName, "BTCPayServer");
}
public HttpClient HttpClient { get; set; }
public string HostName
{
get;
internal set;
}
public InvoiceRepository InvoiceRepository { get; private set; }
2018-07-19 19:31:17 +09:00
public StoreRepository StoreRepository { get; private set; }
public BTCPayNetworkProvider Networks { get; private set; }
public Uri IntegratedLightning { get; internal set; }
public bool InContainer { get; internal set; }
public T GetService<T>()
{
return _Host.Services.GetRequiredService<T>();
}
public IServiceProvider ServiceProvider => _Host.Services;
public string SSHPassword { get; internal set; }
public string SSHKeyFile { get; internal set; }
public string SSHConnection { get; set; }
2019-10-12 20:35:30 +09:00
public T GetController<T>(string userId = null, string storeId = null, bool isAdmin = false) where T : Controller
{
var context = new DefaultHttpContext();
context.Request.Host = new HostString("127.0.0.1", Port);
context.Request.Scheme = "http";
context.Request.Protocol = "http";
if (userId != null)
{
2018-10-26 23:07:39 +09:00
List<Claim> claims = new List<Claim>();
2020-02-24 16:40:04 +01:00
claims.Add(new Claim(ClaimTypes.NameIdentifier, userId));
2019-10-12 20:35:30 +09:00
if (isAdmin)
claims.Add(new Claim(ClaimTypes.Role, Roles.ServerAdmin));
context.User = new ClaimsPrincipal(new ClaimsIdentity(claims.ToArray(), AuthenticationSchemes.Cookie));
}
2018-08-23 00:24:33 +09:00
if (storeId != null)
2018-04-30 02:33:42 +09:00
{
context.SetStoreData(GetService<StoreRepository>().FindStore(storeId, userId).GetAwaiter().GetResult());
}
var scope = (IServiceScopeFactory)_Host.Services.GetService(typeof(IServiceScopeFactory));
var provider = scope.CreateScope().ServiceProvider;
context.RequestServices = provider;
var httpAccessor = provider.GetRequiredService<IHttpContextAccessor>();
httpAccessor.HttpContext = context;
var controller = (T)ActivatorUtilities.CreateInstance(provider, typeof(T));
controller.Url = new UrlHelperMock(new Uri($"http://{HostName}:{Port}/"));
controller.ControllerContext = new ControllerContext()
{
HttpContext = context
};
return controller;
}
public void Dispose()
{
if (_Host != null)
_Host.Dispose();
}
2020-06-24 17:51:00 +09:00
public void ChangeRate(string pair, BidAsk bidAsk)
{
var p = CurrencyPair.Parse(pair);
var index = coinAverageMock.ExchangeRates.FindIndex(o => o.CurrencyPair == p);
coinAverageMock.ExchangeRates[index] = new PairRate(p, bidAsk);
}
}
2017-09-13 15:47:34 +09:00
}