Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions BTCPayServer.Tests/SeleniumTests.cs
Expand Up @@ -800,6 +800,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");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed a period character at the end since other errors don't have it.

}

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");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was an extra trailing space here which I removed.

}

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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

store can be null here technically so we need to check for that.

{
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>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Placed these below both of the elements so the layout doesn't get messed up if there is an error.

<span asp-validation-for="EndDate" class="text-danger"></span>
</div>

<div class="form-group mt-4" id="ResetRow" hidden="@(Model.StartDate == null)">
Expand Down