2020-06-29 04:44:35 +02:00
|
|
|
using System;
|
2020-06-28 10:55:27 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
2018-10-31 05:06:17 +01:00
|
|
|
using System.Linq;
|
2020-06-28 10:55:27 +02:00
|
|
|
using System.Net.Http;
|
|
|
|
using System.Security.Claims;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using BTCPayServer.Configuration;
|
2018-10-28 14:10:37 +01:00
|
|
|
using BTCPayServer.HostedServices;
|
2017-09-13 08:47:34 +02:00
|
|
|
using BTCPayServer.Hosting;
|
2018-05-02 20:32:42 +02:00
|
|
|
using BTCPayServer.Rating;
|
2020-06-28 10:55:27 +02:00
|
|
|
using BTCPayServer.Services;
|
2017-10-20 21:06:37 +02:00
|
|
|
using BTCPayServer.Services.Invoices;
|
2017-09-15 09:06:57 +02:00
|
|
|
using BTCPayServer.Services.Rates;
|
2018-04-29 19:33:42 +02:00
|
|
|
using BTCPayServer.Services.Stores;
|
2017-09-13 08:47:34 +02:00
|
|
|
using BTCPayServer.Tests.Logging;
|
|
|
|
using BTCPayServer.Tests.Mocks;
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2020-06-28 10:55:27 +02:00
|
|
|
using Microsoft.AspNetCore.Hosting.Server.Features;
|
2017-09-13 08:47:34 +02:00
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-01-24 12:12:47 +01:00
|
|
|
using Microsoft.Extensions.Configuration;
|
2020-06-28 10:55:27 +02:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
2017-09-13 08:47:34 +02:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using NBitcoin;
|
|
|
|
using NBXplorer;
|
2020-11-17 13:46:23 +01:00
|
|
|
using AuthenticationSchemes = BTCPayServer.Abstractions.Constants.AuthenticationSchemes;
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
|
|
namespace BTCPayServer.Tests
|
|
|
|
{
|
2017-10-27 10:53:04 +02:00
|
|
|
public class BTCPayServerTester : IDisposable
|
|
|
|
{
|
2024-07-24 13:16:20 +02:00
|
|
|
internal readonly string _Directory;
|
2021-11-22 09:16:08 +01:00
|
|
|
public ILoggerProvider LoggerProvider { get; }
|
|
|
|
|
|
|
|
ILog TestLogs;
|
|
|
|
public BTCPayServerTester(ILog testLogs, ILoggerProvider loggerProvider, string scope)
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
2021-11-22 09:16:08 +01:00
|
|
|
this.LoggerProvider = loggerProvider;
|
|
|
|
this.TestLogs = testLogs;
|
2017-10-27 10:53:04 +02:00
|
|
|
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; }
|
2019-12-24 08:20:44 +01:00
|
|
|
public Uri LBTCNBXplorerUri { get; set; }
|
2018-04-15 14:18:51 +02:00
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
public Uri ServerUri
|
|
|
|
{
|
|
|
|
get;
|
|
|
|
set;
|
|
|
|
}
|
2024-05-09 02:18:02 +02:00
|
|
|
public Uri ServerUriWithIP
|
|
|
|
{
|
|
|
|
get;
|
|
|
|
set;
|
|
|
|
}
|
2017-10-27 10:53:04 +02:00
|
|
|
|
|
|
|
public string Postgres
|
|
|
|
{
|
|
|
|
get; set;
|
|
|
|
}
|
2022-07-05 07:39:50 +02:00
|
|
|
public string ExplorerPostgres
|
|
|
|
{
|
|
|
|
get; set;
|
|
|
|
}
|
2017-10-27 10:53:04 +02:00
|
|
|
|
|
|
|
IWebHost _Host;
|
|
|
|
public int Port
|
|
|
|
{
|
|
|
|
get; set;
|
|
|
|
}
|
|
|
|
|
2024-07-24 13:16:20 +02:00
|
|
|
public async Task RestartStartupTask<T>()
|
|
|
|
{
|
|
|
|
var startupTask = GetService<IServiceProvider>().GetServices<Abstractions.Contracts.IStartupTask>()
|
|
|
|
.Single(task => task is T);
|
|
|
|
await startupTask.ExecuteAsync();
|
|
|
|
}
|
|
|
|
|
2018-04-15 14:18:51 +02:00
|
|
|
public bool MockRates { get; set; } = true;
|
2020-04-08 15:40:41 +02:00
|
|
|
public string SocksEndpoint { get; set; }
|
2018-04-15 14:18:51 +02:00
|
|
|
|
2020-06-28 10:55:27 +02:00
|
|
|
public HashSet<string> Chains { get; set; } = new HashSet<string>() { "BTC" };
|
2019-12-24 10:11:21 +01:00
|
|
|
public bool UseLightning { get; set; }
|
2021-10-11 05:32:09 +02:00
|
|
|
public bool CheatMode { get; set; } = true;
|
2020-03-19 05:30:53 +01:00
|
|
|
public bool DisableRegistration { get; set; } = false;
|
2019-10-07 09:04:25 +02:00
|
|
|
public async Task StartAsync()
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
|
|
|
if (!Directory.Exists(_Directory))
|
|
|
|
Directory.CreateDirectory(_Directory);
|
2021-01-27 06:39:38 +01:00
|
|
|
string chain = NBXplorerDefaultSettings.GetFolderName(ChainName.Regtest);
|
2018-01-12 08:00:31 +01:00
|
|
|
string chainDirectory = Path.Combine(_Directory, chain);
|
|
|
|
if (!Directory.Exists(chainDirectory))
|
|
|
|
Directory.CreateDirectory(chainDirectory);
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
StringBuilder config = new StringBuilder();
|
2018-01-12 08:00:31 +01:00
|
|
|
config.AppendLine($"{chain.ToLowerInvariant()}=1");
|
2019-05-14 09:13:55 +02:00
|
|
|
if (InContainer)
|
|
|
|
{
|
2019-05-14 09:56:03 +02:00
|
|
|
config.AppendLine($"bind=0.0.0.0");
|
2019-05-14 09:13:55 +02:00
|
|
|
}
|
2019-05-14 09:56:03 +02:00
|
|
|
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");
|
2019-12-24 10:11:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (UseLightning)
|
|
|
|
{
|
2021-03-02 03:11:58 +01:00
|
|
|
config.AppendLine($"btc.lightning={IntegratedLightning}");
|
2019-12-24 08:20:44 +01:00
|
|
|
var localLndBackupFile = Path.Combine(_Directory, "walletunlock.json");
|
|
|
|
File.Copy(TestUtils.GetTestDataFullPath("LndSeedBackup/walletunlock.json"), localLndBackupFile, true);
|
|
|
|
config.AppendLine($"btc.external.lndseedbackup={localLndBackupFile}");
|
|
|
|
}
|
2018-01-11 14:52:28 +01:00
|
|
|
|
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");
|
|
|
|
}
|
2021-10-11 05:32:09 +02:00
|
|
|
if (CheatMode)
|
|
|
|
config.AppendLine("cheatmode=1");
|
2020-06-28 10:55:27 +02:00
|
|
|
|
2019-11-08 08:10:49 +01:00
|
|
|
config.AppendLine($"torrcfile={TestUtils.GetTestDataFullPath("Tor/torrc")}");
|
2020-04-08 15:40:41 +02:00
|
|
|
config.AppendLine($"socksendpoint={SocksEndpoint}");
|
2019-10-31 04:29:59 +01:00
|
|
|
config.AppendLine($"debuglog=debug.log");
|
2021-09-09 14:51:28 +02:00
|
|
|
config.AppendLine($"nocsp={NoCSP.ToString().ToLowerInvariant()}");
|
2019-12-24 08:20:44 +01:00
|
|
|
|
2019-09-06 09:51:49 +02: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}");
|
2018-02-26 10:58:02 +01:00
|
|
|
|
2024-08-30 01:34:23 +02:00
|
|
|
if (!String.IsNullOrEmpty(Postgres))
|
2017-10-27 10:53:04 +02:00
|
|
|
config.AppendLine($"postgres=" + Postgres);
|
2022-07-05 07:39:50 +02:00
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(ExplorerPostgres))
|
|
|
|
config.AppendLine($"explorer.postgres=" + ExplorerPostgres);
|
2018-01-12 08:00:31 +01:00
|
|
|
var confPath = Path.Combine(chainDirectory, "settings.config");
|
2019-10-07 09:25:27 +02:00
|
|
|
await File.WriteAllTextAsync(confPath, config.ToString());
|
2017-10-27 10:53:04 +02:00
|
|
|
|
|
|
|
ServerUri = new Uri("http://" + HostName + ":" + Port + "/");
|
2024-05-09 02:18:02 +02:00
|
|
|
ServerUriWithIP = new Uri("http://127.0.0.1:" + Port + "/");
|
2019-03-25 04:59:42 +01:00
|
|
|
HttpClient = new HttpClient();
|
|
|
|
HttpClient.BaseAddress = ServerUri;
|
2018-02-26 10:58:02 +01:00
|
|
|
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
|
2023-01-24 12:12:47 +01:00
|
|
|
var confBuilder = new DefaultConfiguration() { Logger = LoggerProvider.CreateLogger("Console") }.CreateConfigurationBuilder(new[] { "--datadir", _Directory, "--conf", confPath, "--disable-registration", DisableRegistration ? "true" : "false" });
|
2024-10-03 08:16:21 +02:00
|
|
|
// This make sure that tests work outside of this assembly (ie, test project it a plugin)
|
2024-10-03 09:04:16 +02:00
|
|
|
confBuilder.SetBasePath(TestUtils.TestDirectory);
|
2023-01-24 12:12:47 +01:00
|
|
|
#if DEBUG
|
|
|
|
confBuilder.AddJsonFile("appsettings.dev.json", true, false);
|
|
|
|
#endif
|
|
|
|
var conf = confBuilder.Build();
|
2017-10-27 10:53:04 +02:00
|
|
|
_Host = new WebHostBuilder()
|
|
|
|
.UseConfiguration(conf)
|
2019-04-28 16:11:24 +02:00
|
|
|
.UseContentRoot(FindBTCPayServerDirectory())
|
2019-10-06 16:38:57 +02:00
|
|
|
.UseWebRoot(Path.Combine(FindBTCPayServerDirectory(), "wwwroot"))
|
2017-10-27 10:53:04 +02:00
|
|
|
.ConfigureServices(s =>
|
|
|
|
{
|
|
|
|
s.AddLogging(l =>
|
|
|
|
{
|
2019-03-07 11:29:32 +01:00
|
|
|
l.AddFilter("System.Net.Http.HttpClient", LogLevel.Critical);
|
2017-10-27 10:53:04 +02:00
|
|
|
l.SetMinimumLevel(LogLevel.Information)
|
|
|
|
.AddFilter("Microsoft", LogLevel.Error)
|
2024-09-09 12:02:45 +02:00
|
|
|
.AddFilter("Microsoft.EntityFrameworkCore.Migrations", LogLevel.Information)
|
2021-04-27 08:18:01 +02:00
|
|
|
.AddFilter("Fido2NetLib.DistributedCacheMetadataService", LogLevel.Error)
|
2021-11-22 09:16:08 +01:00
|
|
|
.AddProvider(LoggerProvider);
|
2017-10-27 10:53:04 +02:00
|
|
|
});
|
|
|
|
})
|
2020-04-06 12:18:49 +02:00
|
|
|
.ConfigureServices(services =>
|
|
|
|
{
|
|
|
|
services.TryAddSingleton<IFeeProviderFactory>(new BTCPayServer.Services.Fees.FixedFeeProvider(new FeeRate(100L, 1)));
|
|
|
|
})
|
2017-10-27 10:53:04 +02:00
|
|
|
.UseKestrel()
|
|
|
|
.UseStartup<Startup>()
|
|
|
|
.Build();
|
2019-10-07 09:04:25 +02:00
|
|
|
await _Host.StartWithTasksAsync();
|
2019-05-14 09:56:03 +02:00
|
|
|
|
|
|
|
var urls = _Host.ServerFeatures.Get<IServerAddressesFeature>().Addresses;
|
|
|
|
foreach (var url in urls)
|
|
|
|
{
|
2021-11-22 09:16:08 +01:00
|
|
|
TestLogs.LogInformation("Listening on " + url);
|
2019-05-14 09:56:03 +02:00
|
|
|
}
|
2021-11-22 09:16:08 +01:00
|
|
|
TestLogs.LogInformation("Server URI " + ServerUri);
|
2019-05-14 09:56:03 +02:00
|
|
|
|
2017-12-16 17:04:20 +01:00
|
|
|
InvoiceRepository = (InvoiceRepository)_Host.Services.GetService(typeof(InvoiceRepository));
|
2018-08-22 17:24:33 +02:00
|
|
|
StoreRepository = (StoreRepository)_Host.Services.GetService(typeof(StoreRepository));
|
2019-05-09 09:11:09 +02:00
|
|
|
Networks = (BTCPayNetworkProvider)_Host.Services.GetService(typeof(BTCPayNetworkProvider));
|
2018-05-02 20:32:42 +02:00
|
|
|
|
2018-08-22 17:24:33 +02:00
|
|
|
if (MockRates)
|
2018-05-02 20:32:42 +02:00
|
|
|
{
|
2018-08-22 17:24:33 +02:00
|
|
|
var rateProvider = (RateProviderFactory)_Host.Services.GetService(typeof(RateProviderFactory));
|
|
|
|
rateProvider.Providers.Clear();
|
|
|
|
|
2020-06-24 10:51:00 +02:00
|
|
|
coinAverageMock = new MockRateProvider();
|
2020-01-17 10:11:05 +01:00
|
|
|
coinAverageMock.ExchangeRates.Add(new PairRate(CurrencyPair.Parse("BTC_USD"), new BidAsk(5000m)));
|
2021-10-20 16:17:40 +02:00
|
|
|
coinAverageMock.ExchangeRates.Add(new PairRate(CurrencyPair.Parse("BTC_EUR"), new BidAsk(4000m)));
|
2020-01-17 10:11:05 +01:00
|
|
|
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)));
|
2020-01-10 14:50:39 +01:00
|
|
|
rateProvider.Providers.Add("coingecko", coinAverageMock);
|
2018-08-22 17:24:33 +02:00
|
|
|
|
|
|
|
var bitflyerMock = new MockRateProvider();
|
2020-01-17 10:11:05 +01:00
|
|
|
bitflyerMock.ExchangeRates.Add(new PairRate(CurrencyPair.Parse("BTC_JPY"), new BidAsk(700000m)));
|
2018-08-22 17:24:33 +02:00
|
|
|
rateProvider.Providers.Add("bitflyer", bitflyerMock);
|
|
|
|
|
2020-11-02 13:10:55 +01:00
|
|
|
var ndax = new MockRateProvider();
|
|
|
|
ndax.ExchangeRates.Add(new PairRate(CurrencyPair.Parse("BTC_CAD"), new BidAsk(6000m)));
|
|
|
|
rateProvider.Providers.Add("ndax", ndax);
|
2018-08-22 17:24:33 +02:00
|
|
|
|
2019-12-24 08:20:44 +01:00
|
|
|
var bitfinex = new MockRateProvider();
|
2020-01-17 10:11:05 +01:00
|
|
|
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 10:55:27 +02: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)));
|
2023-12-12 17:38:28 +01:00
|
|
|
bitpay.ExchangeRates.Add(new PairRate(CurrencyPair.Parse("DOGE_BTC"), new BidAsk(0.004m)));
|
2020-03-20 09:31:22 +01:00
|
|
|
rateProvider.Providers.Add("bitpay", bitpay);
|
2020-07-28 11:30:23 +02:00
|
|
|
var kraken = new MockRateProvider();
|
|
|
|
kraken.ExchangeRates.Add(new PairRate(CurrencyPair.Parse("ETH_BTC"), new BidAsk(0.1m)));
|
|
|
|
rateProvider.Providers.Add("kraken", kraken);
|
2018-08-22 17:24:33 +02:00
|
|
|
}
|
2019-05-14 11:35:22 +02:00
|
|
|
|
2024-01-31 06:45:54 +01:00
|
|
|
// reset test server policies
|
|
|
|
var settings = GetService<SettingsRepository>();
|
|
|
|
await settings.UpdateSetting(new PoliciesSettings { LockSubscription = false, RequiresUserApproval = false });
|
2019-05-14 11:35:22 +02:00
|
|
|
|
2021-11-22 09:16:08 +01:00
|
|
|
TestLogs.LogInformation("Waiting site is operational...");
|
2019-10-07 09:04:25 +02:00
|
|
|
await WaitSiteIsOperational();
|
2021-11-22 09:16:08 +01:00
|
|
|
TestLogs.LogInformation("Site is now operational");
|
2019-05-14 11:35:22 +02:00
|
|
|
}
|
2020-06-24 10:51:00 +02:00
|
|
|
MockRateProvider coinAverageMock;
|
2019-05-14 11:35:22 +02:00
|
|
|
private async Task WaitSiteIsOperational()
|
|
|
|
{
|
2020-03-10 09:24:38 +01:00
|
|
|
_ = HttpClient.GetAsync("/").ConfigureAwait(false);
|
2022-01-14 09:50:29 +01:00
|
|
|
using var cts = new CancellationTokenSource(20_000);
|
|
|
|
var synching = WaitIsFullySynched(cts.Token);
|
|
|
|
await Task.WhenAll(synching).ConfigureAwait(false);
|
2020-03-10 09:24:38 +01:00
|
|
|
// Opportunistic call to wake up view compilation in debug mode, we don't need to await.
|
2019-05-14 11:35:22 +02:00
|
|
|
}
|
|
|
|
|
2019-08-20 09:10:44 +02:00
|
|
|
private async Task WaitIsFullySynched(CancellationToken cancellationToken)
|
2019-05-14 11:35:22 +02:00
|
|
|
{
|
|
|
|
var dashBoard = GetService<NBXplorerDashboard>();
|
|
|
|
while (!dashBoard.IsFullySynched())
|
|
|
|
{
|
2019-08-20 09:10:44 +02:00
|
|
|
await Task.Delay(10, cancellationToken).ConfigureAwait(false);
|
2019-05-14 11:35:22 +02:00
|
|
|
}
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
2018-04-15 14:18:51 +02:00
|
|
|
|
2019-04-28 16:11:24 +02:00
|
|
|
private string FindBTCPayServerDirectory()
|
|
|
|
{
|
2024-10-03 09:04:16 +02:00
|
|
|
var solutionDirectory = TestUtils.TryGetSolutionDirectoryInfo();
|
2019-04-28 16:11:24 +02:00
|
|
|
return Path.Combine(solutionDirectory.FullName, "BTCPayServer");
|
|
|
|
}
|
|
|
|
|
2019-03-25 04:59:42 +01:00
|
|
|
public HttpClient HttpClient { get; set; }
|
|
|
|
|
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; }
|
2018-07-19 12:31:17 +02:00
|
|
|
public StoreRepository StoreRepository { get; private set; }
|
2019-05-09 09:11:09 +02:00
|
|
|
public BTCPayNetworkProvider Networks { get; private set; }
|
2021-03-02 03:11:58 +01:00
|
|
|
public string IntegratedLightning { get; internal set; }
|
2018-03-20 16:31:19 +01:00
|
|
|
public bool InContainer { get; internal set; }
|
2017-10-27 10:53:04 +02:00
|
|
|
|
|
|
|
public T GetService<T>()
|
|
|
|
{
|
|
|
|
return _Host.Services.GetRequiredService<T>();
|
|
|
|
}
|
|
|
|
|
2019-09-06 09:51:49 +02:00
|
|
|
public IServiceProvider ServiceProvider => _Host.Services;
|
2019-05-29 16:33:31 +02:00
|
|
|
|
2019-09-06 09:51:49 +02:00
|
|
|
public string SSHPassword { get; internal set; }
|
|
|
|
public string SSHKeyFile { get; internal set; }
|
|
|
|
public string SSHConnection { get; set; }
|
2021-09-09 14:51:28 +02:00
|
|
|
public bool NoCSP { get; set; }
|
|
|
|
|
2019-10-12 13:35:30 +02:00
|
|
|
public T GetController<T>(string userId = null, string storeId = null, bool isAdmin = false) where T : Controller
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
|
|
|
var context = new DefaultHttpContext();
|
2018-10-09 16:32:47 +02:00
|
|
|
context.Request.Host = new HostString("127.0.0.1", Port);
|
2017-10-27 10:53:04 +02:00
|
|
|
context.Request.Scheme = "http";
|
|
|
|
context.Request.Protocol = "http";
|
|
|
|
if (userId != null)
|
|
|
|
{
|
2018-10-26 16:07:39 +02: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 13:35:30 +02:00
|
|
|
if (isAdmin)
|
|
|
|
claims.Add(new Claim(ClaimTypes.Role, Roles.ServerAdmin));
|
|
|
|
context.User = new ClaimsPrincipal(new ClaimsIdentity(claims.ToArray(), AuthenticationSchemes.Cookie));
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
2018-08-22 17:24:33 +02:00
|
|
|
if (storeId != null)
|
2018-04-29 19:33:42 +02:00
|
|
|
{
|
|
|
|
context.SetStoreData(GetService<StoreRepository>().FindStore(storeId, userId).GetAwaiter().GetResult());
|
|
|
|
}
|
2017-10-27 10:53:04 +02:00
|
|
|
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 10:51:00 +02: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);
|
|
|
|
}
|
2022-05-24 06:18:16 +02:00
|
|
|
|
|
|
|
public async Task EnableExperimental()
|
|
|
|
{
|
|
|
|
var r = GetService<SettingsRepository>();
|
|
|
|
var p = await r.GetSettingAsync<PoliciesSettings>() ?? new PoliciesSettings();
|
|
|
|
p.Experimental = true;
|
|
|
|
await r.UpdateSetting(p);
|
|
|
|
}
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
}
|