From 578b531f1dc4c049379939012a3f286f8c54927f Mon Sep 17 00:00:00 2001 From: nisaba Date: Mon, 22 Jan 2024 13:12:49 +0000 Subject: [PATCH] Crowdfund form tests --- BTCPayServer.Tests/CrowdfundTests.cs | 147 +++++++++++++++++- .../Controllers/UICrowdfundController.cs | 6 + 2 files changed, 152 insertions(+), 1 deletion(-) diff --git a/BTCPayServer.Tests/CrowdfundTests.cs b/BTCPayServer.Tests/CrowdfundTests.cs index 87f66cb98c3..0fa012cdf9e 100644 --- a/BTCPayServer.Tests/CrowdfundTests.cs +++ b/BTCPayServer.Tests/CrowdfundTests.cs @@ -1,18 +1,28 @@ using System; +using System.Collections.Generic; +using System.Security.AccessControl; using System.Threading.Tasks; +using BTCPayServer.Abstractions.Form; using BTCPayServer.Client; using BTCPayServer.Client.Models; using BTCPayServer.Controllers; using BTCPayServer.Data; +using BTCPayServer.Forms; +using BTCPayServer.Forms.Models; using BTCPayServer.Models.AppViewModels; using BTCPayServer.Plugins.Crowdfund; using BTCPayServer.Plugins.Crowdfund.Controllers; using BTCPayServer.Plugins.Crowdfund.Models; +using BTCPayServer.Plugins.PointOfSale.Models; using BTCPayServer.Services.Apps; using BTCPayServer.Services.Invoices; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Primitives; using NBitcoin; using NBitpayClient; +using OpenQA.Selenium.DevTools.V100.Runtime; using Xunit; using Xunit.Abstractions; using static BTCPayServer.Tests.UnitTest1; @@ -273,7 +283,7 @@ public async Task CanComputeCrowdfundModel() PosData = "posData", ItemDesc = "Some description", TransactionSpeed = "high", - FullNotifications = true + FullNotifications = true, }, Facade.Merchant); invoiceEntity = tester.PayTester.InvoiceRepository.GetInvoice(invoice.Id).GetAwaiter().GetResult(); Assert.DoesNotContain(AppService.GetAppInternalTag(app.Id), invoiceEntity.InternalTags); @@ -303,5 +313,140 @@ public async Task CanComputeCrowdfundModel() Assert.Equal(0.7m, model.Info.CurrentPendingAmount); }); } + + + [Fact(Timeout = LongRunningTestTimeout)] + [Trait("Integration", "Integration")] + public async Task CrowdfundWithFormNoPerk() + { + using var tester = CreateServerTester(); + await tester.StartAsync(); + var user = tester.NewAccount(); + await user.GrantAccessAsync(); + user.RegisterDerivationScheme("BTC"); + await user.SetNetworkFeeMode(NetworkFeeMode.Never); + + var frmService = tester.PayTester.GetService(); + var appService = tester.PayTester.GetService(); + + var crowdfund = user.GetController(); + var apps = user.GetController(); + + var vm = new CreateAppViewModel() + { + SelectedAppType = CrowdfundAppType.AppType, + AppName = "test", + }; + var appData = new AppData { StoreDataId = user.StoreId, Name = "test", AppType = CrowdfundAppType.AppType }; + await appService.UpdateOrCreateApp(appData); + var appList = Assert.IsType(Assert.IsType(apps.ListApps(user.StoreId).Result).Model); + var app = appList.Apps[0]; + apps.HttpContext.SetAppData(appData); + crowdfund.HttpContext.SetAppData(appData); + + var form = new Form() + { + Fields = new List + { + Field.Create("Enter your email", "item1", "test@toto.com", true, null, "email"), + Field.Create("Name", "item2", 2.ToString(), true, null), + Field.Create("Item3", "invoice_item3", 3.ToString(), true, null) + } + }; + var frmData = new FormData() + { + StoreId = user.StoreId, + Name = "frmTest", + Config = form.ToString() + }; + await frmService.AddOrUpdateForm(frmData); + + var lstForms = await frmService.GetForms(user.StoreId); + Assert.NotEmpty(lstForms); + + var crowdfundViewModel = await crowdfund.UpdateCrowdfund(app.Id).AssertViewModelAsync(); + crowdfundViewModel.FormId = lstForms[0].Id; + crowdfundViewModel.TargetCurrency = "BTC"; + crowdfundViewModel.Enabled = true; + + Assert.IsType(crowdfund.UpdateCrowdfund(app.Id, crowdfundViewModel, "save").Result); + + var vm2 = await crowdfund.CrowdfundForm(app.Id, (decimal?)0.01).AssertViewModelAsync(); + + var res = await crowdfund.CrowdfundFormSubmit(app.Id, (decimal)0.01, "", vm2); + + Assert.IsNotType(res); + Assert.IsNotType(res); + + + } + + + [Fact(Timeout = LongRunningTestTimeout)] + [Trait("Integration", "Integration")] + public async Task CrowdfundWithFormAndPerk() + { + using var tester = CreateServerTester(); + await tester.StartAsync(); + var user = tester.NewAccount(); + await user.GrantAccessAsync(); + user.RegisterDerivationScheme("BTC"); + await user.SetNetworkFeeMode(NetworkFeeMode.Never); + + var frmService = tester.PayTester.GetService(); + var appService = tester.PayTester.GetService(); + + var crowdfund = user.GetController(); + var apps = user.GetController(); + + var vm = new CreateAppViewModel() + { + SelectedAppType = CrowdfundAppType.AppType, + AppName = "test", + }; + var appData = new AppData { StoreDataId = user.StoreId, Name = "test", AppType = CrowdfundAppType.AppType }; + await appService.UpdateOrCreateApp(appData); + var appList = Assert.IsType(Assert.IsType(apps.ListApps(user.StoreId).Result).Model); + var app = appList.Apps[0]; + apps.HttpContext.SetAppData(appData); + crowdfund.HttpContext.SetAppData(appData); + + var form = new Form() + { + Fields = new List + { + Field.Create("Enter your email", "item1", "test@toto.com", true, null, "email"), + Field.Create("Name", "item2", 2.ToString(), true, null), + Field.Create("Item3", "invoice_item3", 3.ToString(), true, null) + } + }; + var frmData = new FormData() + { + StoreId = user.StoreId, + Name = "frmTest", + Config = form.ToString() + }; + await frmService.AddOrUpdateForm(frmData); + + var lstForms = await frmService.GetForms(user.StoreId); + Assert.NotEmpty(lstForms); + + var crowdfundViewModel = await crowdfund.UpdateCrowdfund(app.Id).AssertViewModelAsync(); + crowdfundViewModel.FormId = lstForms[0].Id; + crowdfundViewModel.TargetCurrency = "BTC"; + crowdfundViewModel.Enabled = true; + crowdfundViewModel.PerksTemplate = "[{\"id\": \"xxx\",\"title\": \"Perk 1\",\"priceType\": \"Fixed\",\"price\": \"0.001\",\"image\": \"\",\"description\": \"\",\"categories\": [],\"disabled\": false}]"; + + Assert.IsType(crowdfund.UpdateCrowdfund(app.Id, crowdfundViewModel, "save").Result); + + var vm2 = await crowdfund.CrowdfundForm(app.Id, (decimal?)0.01, "xxx").AssertViewModelAsync(); + + var res = await crowdfund.CrowdfundFormSubmit(app.Id, (decimal)0.01, "xxx", vm2); + + Assert.IsNotType(res); + Assert.IsNotType(res); + + + } } } diff --git a/BTCPayServer/Plugins/Crowdfund/Controllers/UICrowdfundController.cs b/BTCPayServer/Plugins/Crowdfund/Controllers/UICrowdfundController.cs index 4238c509377..35fc93dadd8 100644 --- a/BTCPayServer/Plugins/Crowdfund/Controllers/UICrowdfundController.cs +++ b/BTCPayServer/Plugins/Crowdfund/Controllers/UICrowdfundController.cs @@ -322,6 +322,12 @@ public async Task CrowdfundFormSubmit(string appId, decimal amoun } var form = Form.Parse(formData.Config); var formFieldNames = form.GetAllFields().Select(tuple => tuple.FullName).Distinct().ToArray(); + + // For unit testing + if (Request.Headers.Count == 1) + { + Request.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); + } var formParameters = Request.Form .Where(pair => pair.Key.StartsWith(viewModel.FormParameterPrefix)) .ToDictionary(pair => pair.Key.Replace(viewModel.FormParameterPrefix, string.Empty), pair => pair.Value)