mirror of
https://github.com/azaion/admin.git
synced 2026-04-22 19:26:33 +00:00
[AZ-189] [AZ-190] [AZ-191] [AZ-192] [AZ-193] [AZ-194] [AZ-195] Add e2e blackbox test suite
Made-with: Cursor
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
using System.Net.Http.Headers;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Azaion.E2E.Helpers;
|
||||
|
||||
public sealed class ApiClient : IDisposable
|
||||
{
|
||||
private static readonly JsonSerializerOptions JsonOptions = new()
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly bool _disposeClient;
|
||||
|
||||
public ApiClient(HttpClient httpClient, bool disposeClient = false)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_disposeClient = disposeClient;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposeClient)
|
||||
_httpClient.Dispose();
|
||||
}
|
||||
|
||||
public void SetAuthToken(string token)
|
||||
{
|
||||
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
|
||||
}
|
||||
|
||||
public async Task<string> LoginAsync(string email, string password, CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var response = await PostAsync("/login", new { email, password }, cancellationToken).ConfigureAwait(false);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var body = await response.Content.ReadFromJsonAsync<LoginResponse>(JsonOptions, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
if (body?.Token is not { Length: > 0 } t)
|
||||
throw new InvalidOperationException("Login response did not contain a token.");
|
||||
return t;
|
||||
}
|
||||
|
||||
public Task<HttpResponseMessage> PostAsync<T>(string url, T body, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var json = JsonSerializer.Serialize(body, JsonOptions);
|
||||
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
return _httpClient.PostAsync(url, content, cancellationToken);
|
||||
}
|
||||
|
||||
public Task<HttpResponseMessage> GetAsync(string url, CancellationToken cancellationToken = default) =>
|
||||
_httpClient.GetAsync(url, cancellationToken);
|
||||
|
||||
public Task<HttpResponseMessage> PutAsync(string url, CancellationToken cancellationToken = default) =>
|
||||
_httpClient.PutAsync(url, null, cancellationToken);
|
||||
|
||||
public Task<HttpResponseMessage> PutAsync<T>(string url, T body, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var json = JsonSerializer.Serialize(body, JsonOptions);
|
||||
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
return _httpClient.PutAsync(url, content, cancellationToken);
|
||||
}
|
||||
|
||||
public Task<HttpResponseMessage> DeleteAsync(string url, CancellationToken cancellationToken = default) =>
|
||||
_httpClient.DeleteAsync(url, cancellationToken);
|
||||
|
||||
public Task<HttpResponseMessage> UploadFileAsync(string url, byte[] fileContent, string fileName,
|
||||
string formFieldName = "data", CancellationToken cancellationToken = default)
|
||||
{
|
||||
var content = new MultipartFormDataContent();
|
||||
var fileContentBytes = new ByteArrayContent(fileContent);
|
||||
fileContentBytes.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
|
||||
content.Add(fileContentBytes, formFieldName, fileName);
|
||||
return _httpClient.PostAsync(url, content, cancellationToken);
|
||||
}
|
||||
|
||||
private sealed class LoginResponse
|
||||
{
|
||||
public string Token { get; init; } = "";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
using System.Net.Http.Headers;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Xunit;
|
||||
|
||||
namespace Azaion.E2E.Helpers;
|
||||
|
||||
public sealed class TestFixture : IAsyncLifetime
|
||||
{
|
||||
private string _baseUrl = "";
|
||||
|
||||
public HttpClient HttpClient { get; private set; } = null!;
|
||||
public string AdminToken { get; private set; } = "";
|
||||
public string AdminEmail { get; private set; } = "";
|
||||
public string AdminPassword { get; private set; } = "";
|
||||
public string UploaderEmail { get; private set; } = "";
|
||||
public string UploaderPassword { get; private set; } = "";
|
||||
public string JwtSecret { get; private set; } = "";
|
||||
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();
|
||||
|
||||
_baseUrl = Configuration["ApiBaseUrl"]
|
||||
?? throw new InvalidOperationException("Configuration value ApiBaseUrl is required.");
|
||||
AdminEmail = Configuration["AdminEmail"]
|
||||
?? throw new InvalidOperationException("Configuration value AdminEmail is required.");
|
||||
AdminPassword = Configuration["AdminPassword"]
|
||||
?? throw new InvalidOperationException("Configuration value AdminPassword is required.");
|
||||
UploaderEmail = Configuration["UploaderEmail"]
|
||||
?? throw new InvalidOperationException("Configuration value UploaderEmail is required.");
|
||||
UploaderPassword = Configuration["UploaderPassword"]
|
||||
?? throw new InvalidOperationException("Configuration value UploaderPassword is required.");
|
||||
JwtSecret = Configuration["JwtSecret"]
|
||||
?? throw new InvalidOperationException("Configuration value JwtSecret is required.");
|
||||
|
||||
var baseUri = new Uri(_baseUrl, 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(_baseUrl, 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>
|
||||
{
|
||||
}
|
||||
Reference in New Issue
Block a user