Skip to content

Commit efb0d34

Browse files
Add user integration tests
- Introduced `UserTest` class with scenarios for user creation and login using email or username. - Enhanced `TestWebApplicationFactory` to configure in-memory database and testing environment variables. - Verified JWT key and issuer setup for testing within integration tests.
1 parent e0c211f commit efb0d34

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

sparkly-server.test/TestWebAppliactionFactory.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.AspNetCore.Hosting;
22
using Microsoft.AspNetCore.Mvc.Testing;
33
using Microsoft.EntityFrameworkCore;
4+
using Microsoft.Extensions.Configuration;
45
using Microsoft.Extensions.DependencyInjection;
56
using sparkly_server.Infrastructure;
67

@@ -11,6 +12,17 @@ public class TestWebApplicationFactory : WebApplicationFactory<Program>
1112
protected override void ConfigureWebHost(IWebHostBuilder builder)
1213
{
1314
builder.UseEnvironment("Testing");
15+
16+
builder.ConfigureAppConfiguration((context, config) =>
17+
{
18+
var settings = new Dictionary<string, string>
19+
{
20+
["SPARKLY_JWT_KEY"] = "this-is-very-long-test-jwt-key-123456",
21+
["SPARKLY_JWT_ISSUER"] = "sparkly-test-issuer"
22+
};
23+
24+
config.AddInMemoryCollection(settings);
25+
});
1426

1527
builder.ConfigureServices(services =>
1628
{
@@ -26,6 +38,13 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
2638
{
2739
options.UseInMemoryDatabase("sparkly-tests");
2840
});
41+
42+
var sp = services.BuildServiceProvider();
43+
44+
using var scope = sp.CreateScope();
45+
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
46+
db.Database.EnsureDeleted();
47+
db.Database.EnsureCreated();
2948
});
3049
}
3150
}

sparkly-server.test/UserTest.cs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using sparkly_server.DTO;
3+
using sparkly_server.Infrastructure;
4+
using System.Text;
5+
using System.Text.Json;
6+
7+
namespace sparkly_server.test
8+
{
9+
public class UserTest : IClassFixture<TestWebApplicationFactory>, IAsyncLifetime
10+
{
11+
private readonly HttpClient _client;
12+
private readonly TestWebApplicationFactory _factory;
13+
14+
public UserTest(TestWebApplicationFactory factory)
15+
{
16+
_factory = factory;
17+
_client = factory.CreateClient();
18+
}
19+
20+
public async Task InitializeAsync()
21+
{
22+
using var scope = _factory.Services.CreateScope();
23+
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
24+
25+
db.Users.RemoveRange(db.Users);
26+
db.Projects.RemoveRange(db.Projects);
27+
await db.SaveChangesAsync();
28+
}
29+
30+
public Task DisposeAsync() => Task.CompletedTask;
31+
32+
private async Task RegisterTestUserAsync(string userName, string email, string password)
33+
{
34+
var payload = new RegisterRequest(Username: userName, Email: email, Password: password);
35+
36+
var content = new StringContent(
37+
JsonSerializer.Serialize(payload),
38+
Encoding.UTF8,
39+
"application/json"
40+
);
41+
42+
var response = await _client.PostAsync("/api/v1/auth/register", content);
43+
response.EnsureSuccessStatusCode();
44+
}
45+
46+
[Fact]
47+
public async Task User_CanBeCreated()
48+
{
49+
var userName = $"user_{Guid.NewGuid():N}";
50+
var email = $"{Guid.NewGuid():N}@example.com";
51+
var password = "UserPassword";
52+
53+
await RegisterTestUserAsync(userName, email, password);
54+
}
55+
56+
[Fact]
57+
public async Task User_CanLoginByEmail()
58+
{
59+
var userName = $"user_{Guid.NewGuid():N}";
60+
var email = $"{Guid.NewGuid():N}@example.com";
61+
var password = "UserPassword";
62+
63+
await RegisterTestUserAsync(userName, email, password);
64+
65+
var payload = new LoginRequest(Identifier: email, Password: password);
66+
67+
var content = new StringContent(
68+
JsonSerializer.Serialize(payload),
69+
Encoding.UTF8,
70+
"application/json"
71+
);
72+
73+
var response = await _client.PostAsync("/api/v1/auth/login", content);
74+
75+
response.EnsureSuccessStatusCode();
76+
}
77+
78+
[Fact]
79+
public async Task User_CanLoginByUsername()
80+
{
81+
var userName = $"user_{Guid.NewGuid():N}";
82+
var email = $"{Guid.NewGuid():N}@example.com";
83+
var password = "UserPassword";
84+
85+
await RegisterTestUserAsync(userName, email, password);
86+
87+
var payload = new LoginRequest(Identifier: userName, Password: password);
88+
89+
var content = new StringContent(
90+
JsonSerializer.Serialize(payload),
91+
Encoding.UTF8,
92+
"application/json"
93+
);
94+
95+
var response = await _client.PostAsync("/api/v1/auth/login", content);
96+
97+
response.EnsureSuccessStatusCode();
98+
}
99+
}
100+
}

0 commit comments

Comments
 (0)