mirror of
https://github.com/azaion/admin.git
synced 2026-04-22 22:56:32 +00:00
9da34a594b
Made-with: Cursor
74 lines
2.7 KiB
C#
74 lines
2.7 KiB
C#
using System.ComponentModel.DataAnnotations;
|
|
using System.Net.Http.Headers;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Xunit;
|
|
|
|
namespace Azaion.E2E.Helpers;
|
|
|
|
public sealed class TestSettings
|
|
{
|
|
[Required] public string ApiBaseUrl { get; init; } = null!;
|
|
[Required] public string AdminEmail { get; init; } = null!;
|
|
[Required] public string AdminPassword { get; init; } = null!;
|
|
[Required] public string UploaderEmail { get; init; } = null!;
|
|
[Required] public string UploaderPassword { get; init; } = null!;
|
|
[Required] public string JwtSecret { get; init; } = null!;
|
|
}
|
|
|
|
public sealed class TestFixture : IAsyncLifetime
|
|
{
|
|
public HttpClient HttpClient { get; private set; } = null!;
|
|
public string AdminToken { get; private set; } = "";
|
|
public TestSettings Settings { get; private set; } = null!;
|
|
public string AdminEmail => Settings.AdminEmail;
|
|
public string AdminPassword => Settings.AdminPassword;
|
|
public string UploaderEmail => Settings.UploaderEmail;
|
|
public string UploaderPassword => Settings.UploaderPassword;
|
|
public string JwtSecret => Settings.JwtSecret;
|
|
public IConfiguration Configuration { get; private set; } = null!;
|
|
|
|
public async Task InitializeAsync()
|
|
{
|
|
Configuration = new ConfigurationBuilder()
|
|
.SetBasePath(AppContext.BaseDirectory)
|
|
.AddJsonFile("appsettings.test.json", optional: false)
|
|
.AddEnvironmentVariables()
|
|
.Build();
|
|
|
|
Settings = Configuration.Get<TestSettings>()
|
|
?? throw new InvalidOperationException("Failed to bind TestSettings from configuration.");
|
|
Validator.ValidateObject(Settings, new ValidationContext(Settings), validateAllProperties: true);
|
|
|
|
var baseUri = new Uri(Settings.ApiBaseUrl, UriKind.Absolute);
|
|
HttpClient = new HttpClient { BaseAddress = baseUri, Timeout = TimeSpan.FromMinutes(5) };
|
|
|
|
using var loginClient = CreateApiClient();
|
|
AdminToken = await loginClient.LoginAsync(AdminEmail, AdminPassword).ConfigureAwait(false);
|
|
HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AdminToken);
|
|
}
|
|
|
|
public Task DisposeAsync()
|
|
{
|
|
HttpClient.Dispose();
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public ApiClient CreateApiClient()
|
|
{
|
|
var client = new HttpClient { BaseAddress = new Uri(Settings.ApiBaseUrl, UriKind.Absolute), Timeout = TimeSpan.FromMinutes(5) };
|
|
return new ApiClient(client, disposeClient: true);
|
|
}
|
|
|
|
public ApiClient CreateAuthenticatedClient(string token)
|
|
{
|
|
var api = CreateApiClient();
|
|
api.SetAuthToken(token);
|
|
return api;
|
|
}
|
|
}
|
|
|
|
[CollectionDefinition("E2E")]
|
|
public sealed class E2ECollection : ICollectionFixture<TestFixture>
|
|
{
|
|
}
|