2026-12-02 23:31:22 +01:00
|
|
|
using Helpdesk.Api.Controllers;
|
2026-12-02 23:56:26 +01:00
|
|
|
using System.Text.Json;
|
2026-12-02 23:31:22 +01:00
|
|
|
using Helpdesk.Infrastructure.Persistence;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using MySqlConnector;
|
|
|
|
|
using static Helpdesk.IntegrationTests.BackendFixture;
|
|
|
|
|
|
|
|
|
|
namespace Helpdesk.IntegrationTests;
|
|
|
|
|
|
|
|
|
|
public sealed class DatabaseTests(BackendFixture fixture) : IClassFixture<BackendFixture>
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task MigratorAppliesEveryScriptAndEfCanReadTheSchema()
|
|
|
|
|
{
|
|
|
|
|
using var client = fixture.CreateClient();
|
|
|
|
|
await using var scope = fixture.Services.CreateAsyncScope();
|
|
|
|
|
var db = scope.ServiceProvider.GetRequiredService<HelpdeskDbContext>();
|
|
|
|
|
var expected = Directory.EnumerateFiles(MigrationsPath, "V*__*.sql")
|
|
|
|
|
.Select(Path.GetFileName).Order(StringComparer.Ordinal).ToArray();
|
|
|
|
|
var applied = await db.Database
|
|
|
|
|
.SqlQueryRaw<string>("SELECT scriptname AS Value FROM schema_version ORDER BY schemaversionid")
|
|
|
|
|
.ToListAsync(Cancellation);
|
|
|
|
|
Assert.Equal(expected, applied);
|
|
|
|
|
var health = await ReadAsync<HealthResponse>(client.GetAsync("/health", Cancellation));
|
|
|
|
|
Assert.Equal(new HealthResponse("Healthy", expected[^1]), health);
|
|
|
|
|
|
|
|
|
|
await db.Users.AsNoTracking().ToListAsync(Cancellation);
|
|
|
|
|
await db.AuthTokens.AsNoTracking().ToListAsync(Cancellation);
|
|
|
|
|
await db.Buildings.AsNoTracking().ToListAsync(Cancellation);
|
|
|
|
|
await db.IssueCategories.AsNoTracking().ToListAsync(Cancellation);
|
|
|
|
|
await db.ProposalCategories.AsNoTracking().ToListAsync(Cancellation);
|
|
|
|
|
await db.Issues.AsNoTracking().ToListAsync(Cancellation);
|
|
|
|
|
await db.IssueEvents.AsNoTracking().ToListAsync(Cancellation);
|
|
|
|
|
await db.IssueTimings.ToListAsync(Cancellation);
|
|
|
|
|
await db.Attachments.AsNoTracking().ToListAsync(Cancellation);
|
|
|
|
|
await db.Proposals.AsNoTracking().ToListAsync(Cancellation);
|
|
|
|
|
await db.ProposalEvents.AsNoTracking().ToListAsync(Cancellation);
|
|
|
|
|
await db.ProposalVotes.AsNoTracking().ToListAsync(Cancellation);
|
|
|
|
|
await db.ProposalScores.ToListAsync(Cancellation);
|
|
|
|
|
await db.Notifications.AsNoTracking().ToListAsync(Cancellation);
|
|
|
|
|
await db.NotificationDeliveries.AsNoTracking().ToListAsync(Cancellation);
|
|
|
|
|
await db.PushDevices.AsNoTracking().ToListAsync(Cancellation);
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-02 23:56:26 +01:00
|
|
|
[Fact]
|
|
|
|
|
public async Task OpenApiDescribesMultipartIssueRequests()
|
|
|
|
|
{
|
|
|
|
|
using var client = fixture.CreateClient();
|
|
|
|
|
using var document = await ReadAsync<JsonDocument>(client.GetAsync("/swagger/v1/swagger.json", Cancellation));
|
|
|
|
|
var paths = document.RootElement.GetProperty("paths");
|
|
|
|
|
var create = paths.GetProperty("/api/v1/issues").GetProperty("post").GetProperty("requestBody").GetProperty("content");
|
|
|
|
|
Assert.True(create.TryGetProperty("application/json", out _));
|
|
|
|
|
var createSchema = create.GetProperty("multipart/form-data").GetProperty("schema");
|
|
|
|
|
Assert.Contains(createSchema.GetProperty("required").EnumerateArray(), field => field.GetString() == "categoryId");
|
|
|
|
|
var uploadSchema = paths.GetProperty("/api/v1/issues/{id}/attachments").GetProperty("post")
|
|
|
|
|
.GetProperty("requestBody").GetProperty("content").GetProperty("multipart/form-data").GetProperty("schema");
|
|
|
|
|
Assert.Equal("files", Assert.Single(uploadSchema.GetProperty("required").EnumerateArray()).GetString());
|
|
|
|
|
var files = uploadSchema.GetProperty("properties").GetProperty("files");
|
|
|
|
|
Assert.Equal(5, files.GetProperty("maxItems").GetInt32());
|
|
|
|
|
Assert.Equal("binary", files.GetProperty("items").GetProperty("format").GetString());
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-02 23:31:22 +01:00
|
|
|
[Fact]
|
|
|
|
|
public async Task DatabaseRejectsAnAssignedIssueWithoutAnAssignee()
|
|
|
|
|
{
|
|
|
|
|
await using var scope = fixture.Services.CreateAsyncScope();
|
|
|
|
|
var db = scope.ServiceProvider.GetRequiredService<HelpdeskDbContext>();
|
|
|
|
|
var reporterId = await db.Users.Select(user => user.Id).SingleAsync(Cancellation);
|
|
|
|
|
var categoryId = await db.IssueCategories.MinAsync(category => category.Id, Cancellation);
|
|
|
|
|
|
|
|
|
|
var error = await Assert.ThrowsAsync<MySqlException>(() => db.Database.ExecuteSqlInterpolatedAsync($"""
|
|
|
|
|
INSERT INTO issue (title, description, category_id, reporter_id, status)
|
|
|
|
|
VALUES ('Test', 'Test', {categoryId}, {reporterId}, 'ASSIGNED')
|
|
|
|
|
""", Cancellation));
|
|
|
|
|
Assert.Contains("ck_issue_assignee", error.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|