-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathPolpTestFixture.cs
More file actions
56 lines (44 loc) · 1.88 KB
/
Copy pathPolpTestFixture.cs
File metadata and controls
56 lines (44 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
namespace NpgsqlRestTests.Setup;
[CollectionDefinition("PolpTestFixture")]
public class PolpTestFixtureCollection : ICollectionFixture<PolpTestFixture> { }
/// <summary>
/// Test fixture for PoLP (Principle of Least Privilege) tests.
/// Creates a separate web application that uses test_user credentials
/// for BOTH metadata discovery AND function execution.
/// </summary>
public class PolpTestFixture : IDisposable
{
private readonly WebApplication _app;
private readonly HttpClient _client;
public HttpClient Client => _client;
public PolpTestFixture()
{
// Ensure the database is created with superuser (postgres)
// This creates all the schemas, types, functions, and grants needed for testing
Database.Create();
var connectionString = Database.CreatePolpConnection(); // Uses test_user
var builder = WebApplication.CreateBuilder();
builder.WebHost.UseUrls("http://127.0.0.1:0"); // Use random available port
_app = builder.Build();
_app.UseNpgsqlRest(new NpgsqlRestOptions(connectionString)
{
// Only include polp_schema - test_user only has USAGE on this schema
IncludeSchemas = ["polp_schema"],
CommentsMode = CommentsMode.ParseAll,
});
_app.StartAsync().GetAwaiter().GetResult();
var serverAddress = _app.Urls.First();
_client = new HttpClient { BaseAddress = new Uri(serverAddress) };
_client.Timeout = TimeSpan.FromHours(1);
}
#pragma warning disable CA1816 // Dispose methods should call SuppressFinalize
public void Dispose()
#pragma warning restore CA1816 // Dispose methods should call SuppressFinalize
{
_client.Dispose();
_app.StopAsync().GetAwaiter().GetResult();
_app.DisposeAsync().GetAwaiter().GetResult();
}
}