Files
groundsforsupport.stevanfre…/tests/GroundsForSupport.Server.Tests/Integration/PaymentTests.cs
T

94 lines
2.5 KiB
C#
Raw Normal View History

2025-12-26 08:00:04 -06:00
using System.Net;
using System.Net.Http.Json;
2025-12-27 08:38:31 -06:00
using GroundsForSupport.Server.Payments;
using GroundsForSupport.Server.Payments.Stripe;
using GroundsForSupport.Server.Tests.Integration.Infra;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Moq;
2025-12-26 08:00:04 -06:00
2026-01-04 22:16:27 -06:00
[assembly: CaptureConsole]
2025-12-26 08:00:04 -06:00
namespace GroundsForSupport.API.Tests.Integration;
public sealed class PaymentTests(TestApi api) : IClassFixture<TestApi>
{
private readonly TestApi _api = api;
[Fact]
public async Task CreatePaymentIntent_WhenGivenInvalidAmount_ItShouldReturnBadRequest()
{
var client = _api.CreateClient();
2025-12-27 08:38:31 -06:00
var request = new
{
2025-12-26 08:00:04 -06:00
amount = 0,
email = string.Empty,
};
2026-01-04 21:13:08 -06:00
var response = await client.PostAsJsonAsync("/payments/create-intent", request, TestContext.Current.CancellationToken);
2025-12-26 08:00:04 -06:00
2026-01-04 22:16:27 -06:00
Console.WriteLine(await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken));
2025-12-26 08:00:04 -06:00
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
}
2025-12-27 08:38:31 -06:00
[Fact]
public async Task CreatePaymentIntent_WhenGivenInvalidEmail_ItShouldReturnBadRequest()
{
var client = _api.CreateClient();
var request = new
{
amount = 5000,
email = "invalid-email",
};
2026-01-04 21:13:08 -06:00
var response = await client.PostAsJsonAsync("/payments/create-intent", request, TestContext.Current.CancellationToken);
2025-12-27 08:38:31 -06:00
2026-01-04 22:16:27 -06:00
Console.WriteLine(await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken));
2025-12-27 08:38:31 -06:00
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
}
[Fact]
public async Task CreatePaymentIntent_WhenGivenValidRequest_ItShouldReturnCreated()
{
var mockStripeService = new Mock<IStripeService>();
mockStripeService
.Setup(s => s.CreatePaymentIntentAsync(
It.IsAny<string>(),
It.IsAny<decimal>(),
It.IsAny<string?>(),
It.IsAny<string?>(),
It.IsAny<CancellationToken>()
)
)
.ReturnsAsync((true, new Intent("pi_12345_secret_67890")));
var api = _api.WithWebHostBuilder(
b => b.ConfigureTestServices(
s => s.AddSingleton(mockStripeService.Object)
)
);
var client = api.CreateClient();
2025-12-27 08:38:31 -06:00
var request = new
{
2026-01-04 21:13:08 -06:00
name = "Test User",
2025-12-27 08:38:31 -06:00
amount = 5000,
email = "test@test.com",
};
2026-01-04 21:13:08 -06:00
var response = await client.PostAsJsonAsync("/payments/create-intent", request, TestContext.Current.CancellationToken);
2025-12-27 08:38:31 -06:00
2026-01-04 22:16:27 -06:00
Console.WriteLine(await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken));
2025-12-27 08:38:31 -06:00
response.StatusCode.Should().Be(HttpStatusCode.OK);
}
2025-12-26 08:00:04 -06:00
}