Skip to content

Commit e4cd0e6

Browse files
committed
feat: add integration tests for ProductController and enhance logging utility; update various test methods for consistency and accuracy
1 parent cc9a162 commit e4cd0e6

File tree

11 files changed

+327
-16
lines changed

11 files changed

+327
-16
lines changed

Tests/IntegrationTests/Api/AuthenticationControllerTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace Ecommerce.IntegrationTests.Api;
1919

20+
[Collection("Sequential")]
2021
public class AuthenticationControllerTests : IntegrationTestBase
2122
{
2223
public AuthenticationControllerTests(EcommerceWebApplicationFactoryFixture factoryFixture)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System.Net;
2+
using System.Net.Http.Json;
3+
using System.Text.Json;
4+
using System.Threading.Tasks;
5+
using Ecommerce.Application.Common.Errors;
6+
using Ecommerce.Application.UseCases.Users.Common;
7+
using Ecommerce.Contracts.Authentication;
8+
using Ecommerce.Infrastructure.Persistence.EfCore;
9+
using Ecommerce.Tests.Shared;
10+
using FluentAssertions;
11+
using Microsoft.AspNetCore.Http;
12+
using Microsoft.AspNetCore.Mvc;
13+
using Microsoft.EntityFrameworkCore.Infrastructure;
14+
using Microsoft.EntityFrameworkCore.Storage;
15+
using Microsoft.Extensions.DependencyInjection;
16+
using Xunit.Sdk;
17+
18+
namespace Ecommerce.IntegrationTests.Api;
19+
20+
[Collection("Sequential")]
21+
public class ProductControllerTests : IntegrationTestBase
22+
{
23+
public ProductControllerTests(EcommerceWebApplicationFactoryFixture factoryFixture)
24+
: base(factoryFixture) { }
25+
26+
// create empty test methods for CreateProduct, GetProducts, and DeleteProduct
27+
28+
[Fact]
29+
public async Task CreateProduct_ShouldReturnCreated_WhenCreationIsSuccessful()
30+
{
31+
await Authenticate();
32+
33+
//arrange
34+
var multiPartData = Utils.Product.CreateProductRequest();
35+
36+
//act
37+
var response = await _httpClient.PostAsync("products/", multiPartData);
38+
39+
LogPretty.Log(await response.Content.ReadFromJsonAsync<ProblemDetails>());
40+
41+
//assert
42+
response.StatusCode.Should().Be(HttpStatusCode.Created);
43+
}
44+
45+
// [Fact]
46+
// public async Task GetProducts_ShouldReturnOk_WhenProductsExist()
47+
// {
48+
// //arrange
49+
// var request = Utils.Product.CreateProductRequest();
50+
51+
// //act
52+
// var response = await _httpClient.PostAsJsonAsync("products/", request);
53+
54+
// //assert
55+
// response.StatusCode.Should().Be(HttpStatusCode.Created);
56+
// }
57+
58+
// [Fact]
59+
// public async Task DeleteProduct_ShouldReturnOk_WhenDeletionIsSuccessful()
60+
// {
61+
// //arrange
62+
// var request = Utils.Product.CreateProductRequest();
63+
64+
// //act
65+
// var response = await _httpClient.PostAsJsonAsync("products/", request);
66+
67+
// //assert
68+
// response.StatusCode.Should().Be(HttpStatusCode.Created);
69+
// }
70+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Ecommerce.Application.Common.Interfaces.Providers.Payment.Stripe;
4+
using Ecommerce.Domain.PaymentAggregate.Enums;
5+
using Ecommerce.Infrastructure.Common;
6+
using Ecommerce.Infrastructure.Services.Providers.Payment;
7+
using Ecommerce.Infrastructure.Services.Providers.Payment.Stripe;
8+
using Ecommerce.IntegrationTests;
9+
using Ecommerce.Tests.Shared;
10+
using FluentAssertions;
11+
using Microsoft.Extensions.Options;
12+
using Moq;
13+
using Xunit;
14+
15+
namespace Ecommerce_Platform.NET.Tests.IntegrationTests.Infrastructure.Services.Providers.Payment
16+
{
17+
public class StripeServiceTests : IntegrationTestBase
18+
{
19+
private readonly IStripeService _stripeService;
20+
21+
// private readonly Mock<IOptions<ClientSettings>> _clientSettingsMock;
22+
// private readonly Mock<IOptions<PaymentOptions>> _paymentOptionsMock;
23+
24+
private readonly ClientSettings _clientSettings;
25+
private readonly PaymentOptions _paymentOptions;
26+
27+
public StripeServiceTests(
28+
IOptions<ClientSettings> clientSettings,
29+
IOptions<PaymentOptions> paymentOptions,
30+
EcommerceWebApplicationFactoryFixture fixture
31+
)
32+
: base(fixture)
33+
{
34+
// _clientSettingsMock = new Mock<IOptions<ClientSettings>>();
35+
// _paymentOptionsMock = new Mock<IOptions<PaymentOptions>>();
36+
37+
// _clientSettingsMock.Setup(x => clientSettings);
38+
39+
// _paymentOptionsMock.Setup(x => paymentOptions);
40+
41+
_clientSettings = clientSettings.Value;
42+
_paymentOptions = paymentOptions.Value;
43+
_stripeService = new StripeService(clientSettings, paymentOptions);
44+
}
45+
46+
[Fact]
47+
public async Task CreateCheckoutSessionShould_ReturnOk_WhenCheckoutSessionRequestIsCorrect()
48+
{
49+
// Arrange
50+
var request = new CheckoutSessionRequest
51+
{
52+
lineItems = [new OrderItemCheckout("line-item-1", 1, 10000)],
53+
paymentMethod = PaymentMethod.CreditCard,
54+
paymentMode = PaymentMode.Payment,
55+
};
56+
57+
// Act
58+
var result = await _stripeService.CreateCheckoutSessionAsync(request);
59+
60+
// Assert
61+
result.IsSuccess.Should().BeTrue();
62+
result
63+
.Value.redirectUrl.Should()
64+
.Be(_clientSettings.ClientBaseUrl + "/payment/checkout/success");
65+
result.Value.sessionId.Should().NotBeNull();
66+
}
67+
}
68+
}

Tests/IntegrationTests/IntegrationTestBase.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
using System.Net.Http.Headers;
2+
using System.Net.Http.Json;
3+
using System.Text.Json;
4+
using Ecommerce.Contracts.Authentication;
15
using Ecommerce.Infrastructure.Persistence.EfCore;
6+
using Ecommerce.Tests.Shared;
27
using Microsoft.EntityFrameworkCore.Infrastructure;
38
using Microsoft.EntityFrameworkCore.Storage;
49
using Microsoft.Extensions.DependencyInjection;
@@ -27,10 +32,32 @@ public async Task InitializeAsync()
2732
// makes sure each test method starts with clean slate db to avoid conflicts
2833
await _db.EnsureDeletedAsync();
2934
await _db.EnsureCreatedAsync();
35+
_httpClient.DefaultRequestHeaders.Authorization = null; // reset auth before each test
3036
}
3137

3238
public async Task DisposeAsync()
3339
{
3440
await Task.CompletedTask;
3541
}
42+
43+
public async Task Authenticate()
44+
{
45+
await _httpClient.PostAsJsonAsync("auth/register", Utils.User.CreateRegisterRequest());
46+
var response = await _httpClient.PostAsJsonAsync("auth/login", Utils.User.CreateLoginRequest());
47+
48+
var authR = JsonSerializer.Deserialize<AuthenticationResponse>(
49+
await response.Content.ReadAsStreamAsync()
50+
);
51+
52+
if (authR is null)
53+
{
54+
System.Console.WriteLine("Couldn't authenticate user before test.");
55+
return;
56+
}
57+
58+
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
59+
"Bearer",
60+
authR.Token
61+
);
62+
}
3663
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Text.Json;
2+
3+
namespace Ecommerce.IntegrationTests;
4+
5+
public static class LogPretty
6+
{
7+
public static void Log<T>(T o)
8+
{
9+
Console.WriteLine("\n\n LOG PRETTY \n\n");
10+
Console.WriteLine(
11+
JsonSerializer.Serialize(o, new JsonSerializerOptions { WriteIndented = true })
12+
);
13+
Console.WriteLine("\n\n");
14+
}
15+
}

Tests/Shared/Utils/Utils.Cart.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static GetCartQuery CreateGetCartQuery()
1919
public static CartResult CreateCartResult(int i = 1)
2020
{
2121
var cartItemResults = CartItem.CreateCartItemResults(i);
22-
var totalPrice = cartItemResults.Sum(x => x.Product.PriceValue * x.Quantity);
22+
var totalPrice = cartItemResults.Sum(x => x.Product.PriceValueInCents * x.Quantity);
2323
return new CartResult
2424
{
2525
Items = CartItem.CreateCartItemResults(i),

0 commit comments

Comments
 (0)