btcpayserver/BTCPayServer.Tests/PaymentRequestTests.cs

239 lines
10 KiB
C#
Raw Normal View History

2019-01-14 22:43:29 +01:00
using System;
using System.Linq;
using System.Threading.Tasks;
2020-07-24 09:40:37 +02:00
using BTCPayServer.Client.Models;
2019-01-14 22:43:29 +01:00
using BTCPayServer.Controllers;
using BTCPayServer.Models.PaymentRequestViewModels;
using BTCPayServer.PaymentRequest;
2019-01-14 22:43:29 +01:00
using BTCPayServer.Services.Invoices;
using BTCPayServer.Services.PaymentRequests;
2019-01-14 22:43:29 +01:00
using BTCPayServer.Tests.Logging;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using NBitcoin;
2019-01-14 22:43:29 +01:00
using NBitpayClient;
using Xunit;
using Xunit.Abstractions;
namespace BTCPayServer.Tests
{
public class PaymentRequestTests
{
public PaymentRequestTests(ITestOutputHelper helper)
{
Logs.Tester = new XUnitLog(helper) {Name = "Tests"};
2019-01-14 22:43:29 +01:00
Logs.LogProvider = new XUnitLogProvider(helper);
}
[Fact]
[Trait("Integration", "Integration")]
2019-10-07 09:04:25 +02:00
public async Task CanCreateViewUpdateAndDeletePaymentRequest()
2019-01-14 22:43:29 +01:00
{
using (var tester = ServerTester.Create())
{
2019-10-07 09:04:25 +02:00
await tester.StartAsync();
2019-01-14 22:43:29 +01:00
var user = tester.NewAccount();
user.GrantAccess();
user.RegisterDerivationScheme("BTC");
var user2 = tester.NewAccount();
user2.GrantAccess();
var paymentRequestController = user.GetController<PaymentRequestController>();
var guestpaymentRequestController = user2.GetController<PaymentRequestController>();
var request = new UpdatePaymentRequestViewModel()
{
Title = "original juice",
Currency = "BTC",
Amount = 1,
StoreId = user.StoreId,
Description = "description"
};
var id = (Assert
.IsType<RedirectToActionResult>(await paymentRequestController.EditPaymentRequest(null, request))
.RouteValues.Values.First().ToString());
2020-06-28 10:55:27 +02:00
2019-01-14 22:43:29 +01:00
//permission guard for guests editing
Assert
2020-05-09 15:17:59 +02:00
.IsType<NotFoundResult>(await guestpaymentRequestController.EditPaymentRequest(id));
2019-01-14 22:43:29 +01:00
request.Title = "update";
2020-06-28 10:55:27 +02:00
Assert.IsType<RedirectToActionResult>(await paymentRequestController.EditPaymentRequest(id, request));
Assert.Equal(request.Title,
Assert.IsType<ViewPaymentRequestViewModel>(Assert
.IsType<ViewResult>(await paymentRequestController.ViewPaymentRequest(id)).Model).Title);
2019-01-14 22:43:29 +01:00
Assert.False(string.IsNullOrEmpty(id));
Assert.IsType<ViewPaymentRequestViewModel>(Assert
2020-05-09 15:17:59 +02:00
.IsType<ViewResult>(await paymentRequestController.ViewPaymentRequest(id)).Model);
2019-01-14 22:43:29 +01:00
2020-05-09 15:17:59 +02:00
//Archive
2019-01-14 22:43:29 +01:00
Assert
2020-05-09 15:17:59 +02:00
.IsType<RedirectToActionResult>(await paymentRequestController.TogglePaymentRequestArchival(id));
Assert.True(Assert
.IsType<ViewPaymentRequestViewModel>(Assert
.IsType<ViewResult>(await paymentRequestController.ViewPaymentRequest(id)).Model).Archived);
2020-06-28 10:55:27 +02:00
Assert.Empty(Assert
.IsType<ListPaymentRequestsViewModel>(Assert
.IsType<ViewResult>(await paymentRequestController.GetPaymentRequests()).Model).Items);
2020-06-28 10:55:27 +02:00
//unarchive
Assert
.IsType<RedirectToActionResult>(await paymentRequestController.TogglePaymentRequestArchival(id));
Assert.False(Assert
.IsType<ViewPaymentRequestViewModel>(Assert
.IsType<ViewResult>(await paymentRequestController.ViewPaymentRequest(id)).Model).Archived);
2020-06-28 10:55:27 +02:00
Assert.Single(Assert
.IsType<ListPaymentRequestsViewModel>(Assert
.IsType<ViewResult>(await paymentRequestController.GetPaymentRequests()).Model).Items);
2019-01-14 22:43:29 +01:00
}
}
2019-04-05 09:28:18 +02:00
[Fact(Timeout = 60 * 2 * 1000)]
2019-01-14 22:43:29 +01:00
[Trait("Integration", "Integration")]
public async Task CanPayPaymentRequestWhenPossible()
{
using (var tester = ServerTester.Create())
{
2019-10-07 09:04:25 +02:00
await tester.StartAsync();
2019-01-14 22:43:29 +01:00
var user = tester.NewAccount();
user.GrantAccess();
user.RegisterDerivationScheme("BTC");
var paymentRequestController = user.GetController<PaymentRequestController>();
Assert.IsType<NotFoundResult>(
await paymentRequestController.PayPaymentRequest(Guid.NewGuid().ToString()));
2019-01-14 22:43:29 +01:00
var request = new UpdatePaymentRequestViewModel()
{
Title = "original juice",
Currency = "BTC",
Amount = 1,
StoreId = user.StoreId,
Description = "description"
};
var response = Assert
.IsType<RedirectToActionResult>(paymentRequestController.EditPaymentRequest(null, request).Result)
.RouteValues.First();
var invoiceId = Assert
.IsType<OkObjectResult>(
await paymentRequestController.PayPaymentRequest(response.Value.ToString(), false)).Value
2019-01-14 22:43:29 +01:00
.ToString();
var actionResult = Assert
.IsType<RedirectToActionResult>(
await paymentRequestController.PayPaymentRequest(response.Value.ToString()));
2019-01-14 22:43:29 +01:00
Assert.Equal("Checkout", actionResult.ActionName);
Assert.Equal("Invoice", actionResult.ControllerName);
Assert.Contains(actionResult.RouteValues,
pair => pair.Key == "Id" && pair.Value.ToString() == invoiceId);
2019-01-14 22:43:29 +01:00
var invoice = user.BitPay.GetInvoice(invoiceId, Facade.Merchant);
Assert.Equal(1, invoice.Price);
2020-06-28 10:55:27 +02:00
2019-01-14 22:43:29 +01:00
request = new UpdatePaymentRequestViewModel()
{
Title = "original juice with expiry",
Currency = "BTC",
Amount = 1,
2020-06-28 10:55:27 +02:00
ExpiryDate = DateTime.Today.Subtract(TimeSpan.FromDays(2)),
2019-01-14 22:43:29 +01:00
StoreId = user.StoreId,
Description = "description"
};
2020-06-28 10:55:27 +02:00
2019-01-14 22:43:29 +01:00
response = Assert
.IsType<RedirectToActionResult>(paymentRequestController.EditPaymentRequest(null, request).Result)
.RouteValues.First();
Assert
.IsType<BadRequestObjectResult>(
await paymentRequestController.PayPaymentRequest(response.Value.ToString(), false));
2019-01-14 22:43:29 +01:00
}
}
2020-06-28 10:55:27 +02:00
[Fact(Timeout = 60 * 2 * 1000)]
[Trait("Integration", "Integration")]
public async Task CanCancelPaymentWhenPossible()
{
using (var tester = ServerTester.Create())
{
2019-10-07 09:04:25 +02:00
await tester.StartAsync();
var user = tester.NewAccount();
user.GrantAccess();
user.RegisterDerivationScheme("BTC");
var paymentRequestController = user.GetController<PaymentRequestController>();
2020-06-28 10:55:27 +02:00
Assert.IsType<NotFoundResult>(await
paymentRequestController.CancelUnpaidPendingInvoice(Guid.NewGuid().ToString(), false));
var request = new UpdatePaymentRequestViewModel()
{
Title = "original juice",
Currency = "BTC",
Amount = 1,
StoreId = user.StoreId,
Description = "description"
};
var response = Assert
.IsType<RedirectToActionResult>(paymentRequestController.EditPaymentRequest(null, request).Result)
.RouteValues.First();
var paymentRequestId = response.Value.ToString();
2020-06-28 10:55:27 +02:00
var invoiceId = Assert
.IsType<OkObjectResult>(await paymentRequestController.PayPaymentRequest(paymentRequestId, false))
.Value
.ToString();
var actionResult = Assert
.IsType<RedirectToActionResult>(
await paymentRequestController.PayPaymentRequest(response.Value.ToString()));
Assert.Equal("Checkout", actionResult.ActionName);
Assert.Equal("Invoice", actionResult.ControllerName);
Assert.Contains(actionResult.RouteValues,
pair => pair.Key == "Id" && pair.Value.ToString() == invoiceId);
var invoice = user.BitPay.GetInvoice(invoiceId, Facade.Merchant);
Assert.Equal(InvoiceState.ToString(InvoiceStatusLegacy.New), invoice.Status);
2020-06-28 10:55:27 +02:00
Assert.IsType<OkObjectResult>(await
paymentRequestController.CancelUnpaidPendingInvoice(paymentRequestId, false));
2020-06-28 10:55:27 +02:00
invoice = user.BitPay.GetInvoice(invoiceId, Facade.Merchant);
Assert.Equal(InvoiceState.ToString(InvoiceStatusLegacy.Invalid), invoice.Status);
2020-06-28 10:55:27 +02:00
Assert.IsType<BadRequestObjectResult>(await
paymentRequestController.CancelUnpaidPendingInvoice(paymentRequestId, false));
2020-06-28 10:55:27 +02:00
invoiceId = Assert
.IsType<OkObjectResult>(await paymentRequestController.PayPaymentRequest(paymentRequestId, false))
.Value
.ToString();
invoice = user.BitPay.GetInvoice(invoiceId, Facade.Merchant);
2020-06-28 10:55:27 +02:00
//a hack to generate invoices for the payment request is to manually create an invocie with an order id that matches:
user.BitPay.CreateInvoice(new Invoice(1, "USD")
{
OrderId = PaymentRequestRepository.GetOrderIdForPaymentRequest(paymentRequestId)
});
//shouldnt crash
await paymentRequestController.ViewPaymentRequest(paymentRequestId);
await paymentRequestController.CancelUnpaidPendingInvoice(paymentRequestId);
}
}
2019-01-14 22:43:29 +01:00
}
}