From 67eeb4b69a6f2cc18a1c72071d0bc941781c15a3 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Tue, 24 May 2022 13:18:16 +0900 Subject: [PATCH] Allow resolution of any settings via DI --- .../AltcoinTests/AltcoinTests.cs | 4 +- BTCPayServer.Tests/BTCPayServerTester.cs | 11 +++- BTCPayServer.Tests/GreenfieldAPITests.cs | 8 +-- BTCPayServer.Tests/UnitTest1.cs | 8 +-- .../Components/MainNav/Default.cshtml | 11 ++-- .../Configuration/BTCPayServerOptions.cs | 2 - .../Configuration/DefaultConfiguration.cs | 1 - ...ieldLightningNodeApiController.Internal.cs | 4 +- ...enfieldLightningNodeApiController.Store.cs | 4 +- .../GreenfieldLightningNodeApiController.cs | 10 ++-- ...ightningNetworkPaymentMethodsController.cs | 10 +++- ...ymentMethodsController.WalletGeneration.cs | 2 +- ...eldStoreOnChainPaymentMethodsController.cs | 10 +++- ...GreenfieldStoreOnChainWalletsController.cs | 10 ++-- .../GreenField/GreenfieldUsersController.cs | 5 +- .../Controllers/UIAccountController.cs | 35 ++++++------ BTCPayServer/Controllers/UIHomeController.cs | 8 +-- .../Controllers/UIServerController.Users.cs | 7 +-- .../Controllers/UIServerController.cs | 17 +++--- .../UIStoresController.LightningLike.cs | 24 ++++---- .../Controllers/UIStoresController.Onchain.cs | 3 +- .../Controllers/UIStoresController.cs | 6 +- .../SettingsRepositoryExtensions.cs | 18 ------ .../DomainMappingConstraintAttribute.cs | 3 +- .../Filters/ExperimentalRouteAttribute.cs | 2 +- BTCPayServer/Hosting/BTCPayServerServices.cs | 14 ++++- .../Services/BTCPayServerEnvironment.cs | 3 - .../Services/DefaultSwaggerProvider.cs | 7 ++- BTCPayServer/Services/ISettingsAccessor.cs | 55 +++++++++++++++++++ .../Services/Mails/EmailSenderFactory.cs | 13 +++-- BTCPayServer/Services/PoliciesSettings.cs | 2 + BTCPayServer/Views/Shared/LayoutHead.cshtml | 4 +- .../Views/Shared/LayoutHeadTheme.cshtml | 9 +-- BTCPayServer/Views/Shared/_Layout.cshtml | 4 +- BTCPayServer/Views/Shared/_LayoutPos.cshtml | 8 +-- BTCPayServer/Views/UIAccount/Login.cshtml | 4 +- .../Views/UIAppsPublic/ViewCrowdfund.cshtml | 4 +- .../ViewPaymentRequest.cshtml | 4 +- .../ShowLightningNodeInfo.cshtml | 4 +- .../UIPullPayment/ViewPullPayment.cshtml | 8 +-- BTCPayServer/Views/UIServer/Policies.cshtml | 4 ++ 41 files changed, 221 insertions(+), 149 deletions(-) delete mode 100644 BTCPayServer/Extensions/SettingsRepositoryExtensions.cs create mode 100644 BTCPayServer/Services/ISettingsAccessor.cs diff --git a/BTCPayServer.Tests/AltcoinTests/AltcoinTests.cs b/BTCPayServer.Tests/AltcoinTests/AltcoinTests.cs index 4a4e00bd8..5b301209f 100644 --- a/BTCPayServer.Tests/AltcoinTests/AltcoinTests.cs +++ b/BTCPayServer.Tests/AltcoinTests/AltcoinTests.cs @@ -72,14 +72,14 @@ namespace BTCPayServer.Tests // Setup Lightning var controller = user.GetController(); - var lightningVm = (LightningNodeViewModel)Assert.IsType(await controller.SetupLightningNode(user.StoreId, cryptoCode)).Model; + var lightningVm = (LightningNodeViewModel)Assert.IsType(controller.SetupLightningNode(user.StoreId, cryptoCode)).Model; Assert.True(lightningVm.Enabled); var response = await controller.SetLightningNodeEnabled(user.StoreId, cryptoCode, false); Assert.IsType(response); // Get enabled state from settings LightningSettingsViewModel lnSettingsModel; - response = controller.LightningSettings(user.StoreId, cryptoCode).GetAwaiter().GetResult(); + response = controller.LightningSettings(user.StoreId, cryptoCode); lnSettingsModel = (LightningSettingsViewModel)Assert.IsType(response).Model; Assert.NotNull(lnSettingsModel?.ConnectionString); Assert.False(lnSettingsModel.Enabled); diff --git a/BTCPayServer.Tests/BTCPayServerTester.cs b/BTCPayServer.Tests/BTCPayServerTester.cs index 9573bb21e..9b7cd0611 100644 --- a/BTCPayServer.Tests/BTCPayServerTester.cs +++ b/BTCPayServer.Tests/BTCPayServerTester.cs @@ -137,8 +137,6 @@ namespace BTCPayServer.Tests } if (CheatMode) config.AppendLine("cheatmode=1"); - if (Experimental) - config.AppendLine("experimental=1"); config.AppendLine($"torrcfile={TestUtils.GetTestDataFullPath("Tor/torrc")}"); config.AppendLine($"socksendpoint={SocksEndpoint}"); @@ -293,7 +291,6 @@ namespace BTCPayServer.Tests public string SSHKeyFile { get; internal set; } public string SSHConnection { get; set; } public bool NoCSP { get; set; } - public bool Experimental { get; internal set; } public T GetController(string userId = null, string storeId = null, bool isAdmin = false) where T : Controller { @@ -342,5 +339,13 @@ namespace BTCPayServer.Tests var index = coinAverageMock.ExchangeRates.FindIndex(o => o.CurrencyPair == p); coinAverageMock.ExchangeRates[index] = new PairRate(p, bidAsk); } + + public async Task EnableExperimental() + { + var r = GetService(); + var p = await r.GetSettingAsync() ?? new PoliciesSettings(); + p.Experimental = true; + await r.UpdateSetting(p); + } } } diff --git a/BTCPayServer.Tests/GreenfieldAPITests.cs b/BTCPayServer.Tests/GreenfieldAPITests.cs index 869d7e96c..a69e3caf5 100644 --- a/BTCPayServer.Tests/GreenfieldAPITests.cs +++ b/BTCPayServer.Tests/GreenfieldAPITests.cs @@ -2519,8 +2519,8 @@ namespace BTCPayServer.Tests public async Task CustodiansControllerTests() { using var tester = CreateServerTester(); - tester.PayTester.Experimental = true; await tester.StartAsync(); + await tester.PayTester.EnableExperimental(); var unauthClient = new BTCPayServerClient(tester.PayTester.ServerUri); await AssertHttpError(401, async () => await unauthClient.GetCustodians()); @@ -2539,8 +2539,8 @@ namespace BTCPayServer.Tests { using var tester = CreateServerTester(); - tester.PayTester.Experimental = true; await tester.StartAsync(); + await tester.PayTester.EnableExperimental(); var admin = tester.NewAccount(); await admin.GrantAccessAsync(true); @@ -2711,9 +2711,9 @@ namespace BTCPayServer.Tests public async Task CustodianTests() { using var tester = CreateServerTester(); - tester.PayTester.Experimental = true; await tester.StartAsync(); - + await tester.PayTester.EnableExperimental(); + var admin = tester.NewAccount(); await admin.GrantAccessAsync(true); diff --git a/BTCPayServer.Tests/UnitTest1.cs b/BTCPayServer.Tests/UnitTest1.cs index 9d808365b..f7c33ff30 100644 --- a/BTCPayServer.Tests/UnitTest1.cs +++ b/BTCPayServer.Tests/UnitTest1.cs @@ -408,7 +408,7 @@ namespace BTCPayServer.Tests var storeController = user.GetController(); var storeResponse = storeController.GeneralSettings(); Assert.IsType(storeResponse); - Assert.IsType(await storeController.SetupLightningNode(user.StoreId, "BTC")); + Assert.IsType(storeController.SetupLightningNode(user.StoreId, "BTC")); storeController.SetupLightningNode(user.StoreId, new LightningNodeViewModel { @@ -430,7 +430,7 @@ namespace BTCPayServer.Tests new LightningNodeViewModel { ConnectionString = tester.MerchantCharge.Client.Uri.AbsoluteUri }, "save", "BTC").GetAwaiter().GetResult()); - storeResponse = storeController.LightningSettings(user.StoreId, "BTC").GetAwaiter().GetResult(); + storeResponse = storeController.LightningSettings(user.StoreId, "BTC"); var storeVm = Assert.IsType(Assert .IsType(storeResponse).Model); @@ -1571,7 +1571,7 @@ namespace BTCPayServer.Tests // enable unified QR code in settings var vm = Assert.IsType(Assert - .IsType(await user.GetController().LightningSettings(user.StoreId, cryptoCode)).Model + .IsType(user.GetController().LightningSettings(user.StoreId, cryptoCode)).Model ); vm.OnChainWithLnInvoiceFallback = true; Assert.IsType( @@ -1629,7 +1629,7 @@ namespace BTCPayServer.Tests // Activating LNUrl, we should still have only 1 payment criteria that can be set. user.RegisterLightningNode(cryptoCode, LightningConnectionType.Charge); - var lnSettingsVm = await user.GetController().LightningSettings(user.StoreId, cryptoCode).AssertViewModelAsync(); + var lnSettingsVm = user.GetController().LightningSettings(user.StoreId, cryptoCode).AssertViewModel(); lnSettingsVm.LNURLEnabled = true; lnSettingsVm.LNURLStandardInvoiceEnabled = true; Assert.IsType(user.GetController().LightningSettings(lnSettingsVm).Result); diff --git a/BTCPayServer/Components/MainNav/Default.cshtml b/BTCPayServer/Components/MainNav/Default.cshtml index 449fa9538..4f5dd5bd9 100644 --- a/BTCPayServer/Components/MainNav/Default.cshtml +++ b/BTCPayServer/Components/MainNav/Default.cshtml @@ -8,16 +8,15 @@ @using BTCPayServer.Abstractions.Extensions @using BTCPayServer.Abstractions.Contracts @using BTCPayServer.Client +@using BTCPayServer.Services @inject BTCPayServer.Services.BTCPayServerEnvironment Env @inject SignInManager SignInManager -@inject ISettingsRepository SettingsRepository +@inject PoliciesSettings PoliciesSettings +@inject ThemeSettings Theme @model BTCPayServer.Components.MainNav.MainNavViewModel @addTagHelper *, BundlerMinifier.TagHelpers -@{ - var theme = await SettingsRepository.GetTheme(); -}