backend + web: Protect Swagger UI behind admin login

This commit is contained in:
GonnaHetzMe committed 2026-12-03 00:49:36 +01:00
1 parent d8de714481
commit eddf5837eb
16 files changed
+158 -28

No files matched your search

@@ -1,5 +1,6 @@
using Helpdesk.Api.Controllers;
using System.Net;
using System.Text.Json;
using Helpdesk.Api.Controllers;
using Helpdesk.Infrastructure.Persistence;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
@@ -44,10 +45,18 @@ public sealed class DatabaseTests(BackendFixture fixture) : IClassFixture<Backen
}
[Fact]
public async Task OpenApiDescribesMultipartIssueRequests()
public async Task OnlyAdministratorsCanReadTheOpenApiDocument()
{
using var client = fixture.CreateClient();
using var document = await ReadAsync<JsonDocument>(client.GetAsync("/swagger/v1/swagger.json", Cancellation));
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 _));
@@ -66,7 +75,7 @@ public sealed class DatabaseTests(BackendFixture fixture) : IClassFixture<Backen
{
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 reporterId = await db.Users.Where(user => user.Email == AdminEmail).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($"""