Skip to content

Commit

Permalink
Crowdfund form tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nisaba committed Jan 22, 2024
1 parent 833ccb7 commit 578b531
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 1 deletion.
147 changes: 146 additions & 1 deletion BTCPayServer.Tests/CrowdfundTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<FormDataService>();
var appService = tester.PayTester.GetService<AppService>();

var crowdfund = user.GetController<UICrowdfundController>();
var apps = user.GetController<UIAppsController>();

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<ListAppsViewModel>(Assert.IsType<ViewResult>(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>
{
Field.Create("Enter your email", "item1", "[email protected]", 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<UpdateCrowdfundViewModel>();
crowdfundViewModel.FormId = lstForms[0].Id;
crowdfundViewModel.TargetCurrency = "BTC";
crowdfundViewModel.Enabled = true;

Assert.IsType<RedirectToActionResult>(crowdfund.UpdateCrowdfund(app.Id, crowdfundViewModel, "save").Result);

var vm2 = await crowdfund.CrowdfundForm(app.Id, (decimal?)0.01).AssertViewModelAsync<FormViewModel>();

var res = await crowdfund.CrowdfundFormSubmit(app.Id, (decimal)0.01, "", vm2);

Assert.IsNotType<NotFoundObjectResult>(res);
Assert.IsNotType<BadRequest>(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<FormDataService>();
var appService = tester.PayTester.GetService<AppService>();

var crowdfund = user.GetController<UICrowdfundController>();
var apps = user.GetController<UIAppsController>();

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<ListAppsViewModel>(Assert.IsType<ViewResult>(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>
{
Field.Create("Enter your email", "item1", "[email protected]", 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<UpdateCrowdfundViewModel>();
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<RedirectToActionResult>(crowdfund.UpdateCrowdfund(app.Id, crowdfundViewModel, "save").Result);

var vm2 = await crowdfund.CrowdfundForm(app.Id, (decimal?)0.01, "xxx").AssertViewModelAsync<FormViewModel>();

var res = await crowdfund.CrowdfundFormSubmit(app.Id, (decimal)0.01, "xxx", vm2);

Assert.IsNotType<NotFoundObjectResult>(res);
Assert.IsNotType<BadRequest>(res);


}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,12 @@ public async Task<IActionResult> 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)
Expand Down

0 comments on commit 578b531

Please sign in to comment.