Skip to content

Commit

Permalink
Make sure end date is after start date in Crowdfund app (#4084)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
bolatovumar and dennisreimann committed Sep 9, 2022
1 parent 3b1946d commit 7106830
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
10 changes: 10 additions & 0 deletions BTCPayServer.Tests/SeleniumTests.cs
Expand Up @@ -808,6 +808,16 @@ public async Task CanCreateCrowdfundingApp()
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);

Expand Down
Expand Up @@ -126,7 +126,7 @@ public async Task<IActionResult> ContributeToCrowdfund(string appId, ContributeT
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;
Expand Down Expand Up @@ -287,12 +287,17 @@ public async Task<IActionResult> UpdateCrowdfund(string appId, UpdateCrowdfundVi

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)
{
ModelState.AddModelError(nameof(vm.ResetEveryAmount), "You must reset the goal at a minimum of 1 ");
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)
Expand Down Expand Up @@ -374,7 +379,13 @@ private async Task<string> GetStoreDefaultCurrentIfEmpty(string storeId, string
{
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();
}
Expand Down
4 changes: 2 additions & 2 deletions BTCPayServer/Views/Shared/Crowdfund/UpdateCrowdfund.cshtml
Expand Up @@ -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>
Expand All @@ -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)">
Expand Down

0 comments on commit 7106830

Please sign in to comment.