Files

87 lines
5.1 KiB
C#
Raw Permalink Normal View History

using System.Net;
using System.Text.Json;
using Helpdesk.Api.Controllers;
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);
}
[Fact]
public async Task OnlyAdministratorsCanReadTheOpenApiDocument()
{
using var client = fixture.CreateClient();
const string path = "/api/v1/admin/openapi/v1.json";
await ExpectAsync(client.GetAsync(path, Cancellation), HttpStatusCode.Unauthorized);
var student = await fixture.RegisterAsync(client);
using var studentClient = fixture.CreateClient((await LoginAsync(client, student.Email)).AccessToken);
await ExpectAsync(studentClient.GetAsync(path, Cancellation), HttpStatusCode.Forbidden);
using var admin = fixture.CreateClient((await LoginAsync(client, AdminEmail)).AccessToken);
await ExpectAsync(admin.GetAsync("/swagger/index.html", Cancellation), HttpStatusCode.NotFound);
await ExpectAsync(admin.GetAsync("/swagger/v1/swagger.json", Cancellation), HttpStatusCode.NotFound);
using var document = await ReadAsync<JsonDocument>(admin.GetAsync(path, 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.Where(user => user.Email == AdminEmail).Select(user => user.Id).SingleAsync(Cancellation);
2026-12-02 23:31:22 +01:00
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);
}
}