51 lines
2.8 KiB
C#
51 lines
2.8 KiB
C#
using System.Net.Http.Json;
|
|
using ItWorx.KnowledgeHub.Application.Audit;
|
|
using ItWorx.KnowledgeHub.Contracts.Activity;
|
|
using ItWorx.KnowledgeHub.Contracts.Knowledge;
|
|
using ItWorx.KnowledgeHub.IntegrationTests.Infrastructure;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace ItWorx.KnowledgeHub.IntegrationTests.Operations;
|
|
|
|
[Collection(PostgresTestGroup.Name)]
|
|
public sealed class ActivityFeedTests(PostgresContainerFixture postgres) : OwnerSetupTestBase(postgres)
|
|
{
|
|
[Fact]
|
|
public async Task Activity_excludes_security_events_before_pagination_and_audit_retains_them()
|
|
{
|
|
using var client = Factory.CreateClient();
|
|
await SetupHelper.LoginAsync(client, OwnerEmail, OwnerPassword);
|
|
(await SetupHelper.PostWithCsrfAsync(client, "/api/v1/pages",
|
|
new CreatePageRequest(null, "Operationeel resultaat", "WikiPage", "Inhoud.", null))).EnsureSuccessStatusCode();
|
|
using var scope = Factory.Services.CreateScope();
|
|
var audit = scope.ServiceProvider.GetRequiredService<IAuditEventRecorder>();
|
|
for (var i = 0; i < 3; i++)
|
|
await audit.RecordAsync(new AuditEventRecord(AuditActorKind.System, null, "security",
|
|
"token.rotated", "service-account", Guid.NewGuid().ToString(), "Beveiligingswijziging", null,
|
|
DateTimeOffset.UtcNow), CancellationToken.None);
|
|
var feed = await client.GetFromJsonAsync<ActivityListResponse>("/api/v1/activity?limit=1");
|
|
Assert.Single(feed!.Items);
|
|
Assert.Equal("page.created", feed.Items[0].Action);
|
|
var raw = await scope.ServiceProvider.GetRequiredService<IAuditEventReader>().ListRecentAsync(10, CancellationToken.None);
|
|
Assert.Equal(3, raw.Count(item => item.Action == "token.rotated"));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Activity_feed_returns_real_append_only_audit_events_in_reverse_chronological_order()
|
|
{
|
|
using var client = Factory.CreateClient();
|
|
await SetupHelper.LoginAsync(client, OwnerEmail, OwnerPassword);
|
|
var create = await SetupHelper.PostWithCsrfAsync(
|
|
client, "/api/v1/pages", new CreatePageRequest(null, "Auditbare pagina", "WikiPage", "Inhoud.", null));
|
|
create.EnsureSuccessStatusCode();
|
|
|
|
var activity = await client.GetFromJsonAsync<ActivityListResponse>("/api/v1/activity?limit=20");
|
|
Assert.NotNull(activity);
|
|
var pageCreated = Assert.Single(activity.Items, item => item.Action == "page.created");
|
|
Assert.Equal("page", pageCreated.TargetType);
|
|
Assert.Contains("Auditbare pagina", pageCreated.Summary, StringComparison.Ordinal);
|
|
Assert.DoesNotContain("Inhoud.", pageCreated.Summary, StringComparison.Ordinal);
|
|
Assert.Equal(activity.Items.OrderByDescending(item => item.OccurredAt).Select(item => item.Id), activity.Items.Select(item => item.Id));
|
|
}
|
|
}
|