2017-09-13 08:47:34 +02:00
|
|
|
|
using BTCPayServer.Configuration;
|
|
|
|
|
using BTCPayServer.Hosting;
|
2017-10-20 21:06:37 +02:00
|
|
|
|
using BTCPayServer.Services.Invoices;
|
2017-09-15 09:06:57 +02:00
|
|
|
|
using BTCPayServer.Services.Rates;
|
2017-09-13 08:47:34 +02:00
|
|
|
|
using BTCPayServer.Tests.Logging;
|
|
|
|
|
using BTCPayServer.Tests.Mocks;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Http.Internal;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Controllers;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Routing;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using NBitcoin;
|
|
|
|
|
using NBXplorer;
|
|
|
|
|
using NBXplorer.DerivationStrategy;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
using System.Security.Principal;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace BTCPayServer.Tests
|
|
|
|
|
{
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public class BTCPayServerTester : IDisposable
|
|
|
|
|
{
|
|
|
|
|
private string _Directory;
|
|
|
|
|
|
|
|
|
|
public BTCPayServerTester(string scope)
|
|
|
|
|
{
|
|
|
|
|
this._Directory = scope ?? throw new ArgumentNullException(nameof(scope));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Uri NBXplorerUri
|
|
|
|
|
{
|
|
|
|
|
get; set;
|
|
|
|
|
}
|
2018-01-11 09:29:48 +01:00
|
|
|
|
|
|
|
|
|
public Uri LTCNBXplorerUri { get; set; }
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public string CookieFile
|
|
|
|
|
{
|
|
|
|
|
get; set;
|
|
|
|
|
}
|
|
|
|
|
public Uri ServerUri
|
|
|
|
|
{
|
|
|
|
|
get;
|
|
|
|
|
set;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ExtKey HDPrivateKey
|
|
|
|
|
{
|
|
|
|
|
get; set;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Postgres
|
|
|
|
|
{
|
|
|
|
|
get; set;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IWebHost _Host;
|
|
|
|
|
public int Port
|
|
|
|
|
{
|
|
|
|
|
get; set;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Start()
|
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(_Directory))
|
|
|
|
|
Directory.CreateDirectory(_Directory);
|
|
|
|
|
|
|
|
|
|
HDPrivateKey = new ExtKey();
|
|
|
|
|
StringBuilder config = new StringBuilder();
|
|
|
|
|
config.AppendLine($"regtest=1");
|
|
|
|
|
config.AppendLine($"port={Port}");
|
|
|
|
|
config.AppendLine($"explorer.url={NBXplorerUri.AbsoluteUri}");
|
2018-01-11 09:29:48 +01:00
|
|
|
|
config.AppendLine($"ltc.explorer.url={LTCNBXplorerUri.AbsoluteUri}");
|
2017-10-27 10:53:04 +02:00
|
|
|
|
config.AppendLine($"explorer.cookiefile={CookieFile}");
|
2018-01-11 09:29:48 +01:00
|
|
|
|
config.AppendLine($"ltc.explorer.cookiefile={CookieFile}");
|
2017-10-27 10:53:04 +02:00
|
|
|
|
config.AppendLine($"hdpubkey={HDPrivateKey.Neuter().ToString(Network.RegTest)}");
|
|
|
|
|
if (Postgres != null)
|
|
|
|
|
config.AppendLine($"postgres=" + Postgres);
|
|
|
|
|
File.WriteAllText(Path.Combine(_Directory, "settings.config"), config.ToString());
|
|
|
|
|
|
|
|
|
|
ServerUri = new Uri("http://" + HostName + ":" + Port + "/");
|
|
|
|
|
|
|
|
|
|
var conf = new DefaultConfiguration() { Logger = Logs.LogProvider.CreateLogger("Console") }.CreateConfiguration(new[] { "--datadir", _Directory });
|
|
|
|
|
|
|
|
|
|
_Host = new WebHostBuilder()
|
|
|
|
|
.UseConfiguration(conf)
|
|
|
|
|
.ConfigureServices(s =>
|
|
|
|
|
{
|
|
|
|
|
s.AddSingleton<IRateProvider>(new MockRateProvider(new Rate("USD", 5000m)));
|
|
|
|
|
s.AddLogging(l =>
|
|
|
|
|
{
|
|
|
|
|
l.SetMinimumLevel(LogLevel.Information)
|
|
|
|
|
.AddFilter("Microsoft", LogLevel.Error)
|
|
|
|
|
.AddFilter("Hangfire", LogLevel.Error)
|
|
|
|
|
.AddProvider(Logs.LogProvider);
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.UseKestrel()
|
|
|
|
|
.UseStartup<Startup>()
|
|
|
|
|
.Build();
|
|
|
|
|
_Host.Start();
|
2017-12-16 17:04:20 +01:00
|
|
|
|
InvoiceRepository = (InvoiceRepository)_Host.Services.GetService(typeof(InvoiceRepository));
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
2017-12-16 17:04:20 +01:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public string HostName
|
|
|
|
|
{
|
|
|
|
|
get;
|
|
|
|
|
internal set;
|
|
|
|
|
}
|
2017-12-16 17:04:20 +01:00
|
|
|
|
public InvoiceRepository InvoiceRepository { get; private set; }
|
2017-10-27 10:53:04 +02:00
|
|
|
|
|
|
|
|
|
public T GetService<T>()
|
|
|
|
|
{
|
|
|
|
|
return _Host.Services.GetRequiredService<T>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T GetController<T>(string userId = null) where T : Controller
|
|
|
|
|
{
|
|
|
|
|
var context = new DefaultHttpContext();
|
|
|
|
|
context.Request.Host = new HostString("127.0.0.1");
|
|
|
|
|
context.Request.Scheme = "http";
|
|
|
|
|
context.Request.Protocol = "http";
|
|
|
|
|
if (userId != null)
|
|
|
|
|
{
|
|
|
|
|
context.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.NameIdentifier, userId) }));
|
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
}
|