using Helpdesk.Api.Controllers; using System.Text.Json; 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 { [Fact] public async Task MigratorAppliesEveryScriptAndEfCanReadTheSchema() { using var client = fixture.CreateClient(); await using var scope = fixture.Services.CreateAsyncScope(); var db = scope.ServiceProvider.GetRequiredService(); var expected = Directory.EnumerateFiles(MigrationsPath, "V*__*.sql") .Select(Path.GetFileName).Order(StringComparer.Ordinal).ToArray(); var applied = await db.Database .SqlQueryRaw("SELECT scriptname AS Value FROM schema_version ORDER BY schemaversionid") .ToListAsync(Cancellation); Assert.Equal(expected, applied); var health = await ReadAsync(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); } [Fact] public async Task OpenApiDescribesMultipartIssueRequests() { using var client = fixture.CreateClient(); using var document = await ReadAsync(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()); } [Fact] public async Task DatabaseRejectsAnAssignedIssueWithoutAnAssignee() { await using var scope = fixture.Services.CreateAsyncScope(); var db = scope.ServiceProvider.GetRequiredService(); 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(() => 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); } }