Make sure end date is after start date in Crowdfund app (#4084)

* Make sure end date is after start date in Crowdfund app

* Add null checks

* Add test case

Co-authored-by: Dennis Reimann <mail@dennisreimann.de>
This commit is contained in:
Umar Bolatov
2022-09-09 03:26:11 -07:00
committed by GitHub
parent 3b1946d65c
commit 7106830be9
3 changed files with 27 additions and 6 deletions

View File

@@ -808,6 +808,16 @@ namespace BTCPayServer.Tests
s.Driver.FindElement(By.Id("TargetCurrency")).Clear();
s.Driver.FindElement(By.Id("TargetCurrency")).SendKeys("JPY");
s.Driver.FindElement(By.Id("TargetAmount")).SendKeys("700");
// test wrong dates
s.Driver.ExecuteJavaScript("const now = new Date();document.getElementById('StartDate').value = now.toISOString();" +
"const yst = new Date(now.setDate(now.getDate() -1));document.getElementById('EndDate').value = yst.toISOString()");
s.Driver.FindElement(By.Id("SaveSettings")).Click();
Assert.Contains("End date cannot be before start date", s.Driver.PageSource);
Assert.DoesNotContain("App updated", s.Driver.PageSource);
// unset end date
s.Driver.ExecuteJavaScript("document.getElementById('EndDate').value = ''");
s.Driver.FindElement(By.Id("SaveSettings")).Click();
Assert.Contains("App updated", s.FindAlertMessage().Text);

View File

@@ -126,7 +126,7 @@ namespace BTCPayServer.Plugins.Crowdfund.Controllers
if (!string.IsNullOrEmpty(request.ChoiceKey))
{
var choices = _appService.GetPOSItems(settings.PerksTemplate, settings.TargetCurrency);
choice = choices.FirstOrDefault(c => c.Id == request.ChoiceKey);
choice = choices?.FirstOrDefault(c => c.Id == request.ChoiceKey);
if (choice == null)
return NotFound("Incorrect option provided");
title = choice.Title;
@@ -287,7 +287,7 @@ namespace BTCPayServer.Plugins.Crowdfund.Controllers
if (Enum.Parse<CrowdfundResetEvery>(vm.ResetEvery) != CrowdfundResetEvery.Never && !vm.StartDate.HasValue)
{
ModelState.AddModelError(nameof(vm.StartDate), "A start date is needed when the goal resets every X amount of time.");
ModelState.AddModelError(nameof(vm.StartDate), "A start date is needed when the goal resets every X amount of time");
}
if (Enum.Parse<CrowdfundResetEvery>(vm.ResetEvery) != CrowdfundResetEvery.Never && vm.ResetEveryAmount <= 0)
@@ -295,6 +295,11 @@ namespace BTCPayServer.Plugins.Crowdfund.Controllers
ModelState.AddModelError(nameof(vm.ResetEveryAmount), "You must reset the goal at a minimum of 1");
}
if (vm.StartDate != null && vm.EndDate != null && DateTime.Compare((DateTime)vm.StartDate, (DateTime)vm.EndDate) > 0)
{
ModelState.AddModelError(nameof(vm.EndDate), "End date cannot be before start date");
}
if (vm.DisplayPerksRanking)
{
vm.SortPerksByPopularity = true;
@@ -374,7 +379,13 @@ namespace BTCPayServer.Plugins.Crowdfund.Controllers
{
if (string.IsNullOrWhiteSpace(currency))
{
currency = (await _storeRepository.FindStore(storeId)).GetStoreBlob().DefaultCurrency;
var store = await _storeRepository.FindStore(storeId);
if (store == null)
{
throw new Exception($"Could not find store with id {storeId}");
}
currency = store.GetStoreBlob().DefaultCurrency;
}
return currency.Trim().ToUpperInvariant();
}

View File

@@ -108,7 +108,6 @@
<vc:icon symbol="close"/>
</button>
</div>
<span asp-validation-for="StartDate" class="text-danger"></span>
</div>
<div class="form-group mb-0 w-250px">
<label asp-for="EndDate" class="form-label"></label>
@@ -121,8 +120,9 @@
<vc:icon symbol="close"/>
</button>
</div>
<span asp-validation-for="EndDate" class="text-danger"></span>
</div>
<span asp-validation-for="StartDate" class="text-danger"></span>
<span asp-validation-for="EndDate" class="text-danger"></span>
</div>
<div class="form-group mt-4" id="ResetRow" hidden="@(Model.StartDate == null)">