Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/GZCTF.Integration.Test/Base/GZCTFApplicationFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GZCTF.Models;
using GZCTF.Models.Request.Account;
using GZCTF.Services.Container.Manager;
using GZCTF.Services.Container.Provider;
using GZCTF.Storage;
Expand All @@ -10,6 +11,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System.Net.Http.Json;
using Testcontainers.K3s;
using Testcontainers.Minio;
using Testcontainers.PostgreSql;
Expand Down Expand Up @@ -262,6 +264,25 @@ public async Task InitializeAsync()
await context.Database.MigrateAsync();
}

/// <summary>
/// Create an authenticated HTTP client for the given user
/// </summary>
public HttpClient CreateAuthenticatedClient(TestDataSeeder.SeededUser user)
{
var client = CreateClient();

// Login the user
var loginResponse = client.PostAsJsonAsync("/api/Account/LogIn", new
{
UserName = user.UserName,
Password = user.Password
}).Result;

loginResponse.EnsureSuccessStatusCode();

return client;
}

public new async Task DisposeAsync()
{
// Dispose containers
Expand Down
17 changes: 17 additions & 0 deletions src/GZCTF.Integration.Test/Base/TestDataSeeder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,23 @@ private static string NormalizeTeamName(string? teamName)
return trimmed;
}

/// <summary>
/// Create a user with specific role
/// </summary>
public static async Task<(SeededUser user, string password)> CreateUserWithRoleAsync(
IServiceProvider services,
Role role = Role.User,
CancellationToken token = default)
{
var password = $"Test{role}Pass123!";
var userName = RandomName();
var email = $"{userName}@test.com";

var user = await CreateUserAsync(services, userName, password, email, role, token);

return (user, password);
}

public record SeededUser(Guid Id, string UserName, string Email, string Password, Role Role);

public record SeededTeam(int Id, string Name, Guid OwnerId);
Expand Down
Loading
Loading