Add MicCheck.Api.Tests.Integration project
NUnit integration tests that target a running API by URL and seed/clean data directly in Postgres. Settings (BaseUrl, DbConnectionString) come from .runsettings or environment variables so tests can be pointed at any environment. HTTP requests live in HttpFiles/*.http; assertion failures include a fresh DB snapshot of the relevant rows. First test covers the flag-enabled path.
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
using System.Text.Json;
|
||||
using MicCheck.Api.Tests.Integration.Common;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace MicCheck.Api.Tests.Integration.Flags;
|
||||
|
||||
[TestFixture]
|
||||
public class FlagEnabledTests
|
||||
{
|
||||
private IntegrationTestSettings settings = null!;
|
||||
private TestDatabase db = null!;
|
||||
private FlagApiHttpClient client = null!;
|
||||
private FlagSeed? seed;
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
{
|
||||
settings = IntegrationTestSettings.Load();
|
||||
db = new TestDatabase(settings.DbConnectionString);
|
||||
client = new FlagApiHttpClient(settings);
|
||||
}
|
||||
|
||||
[OneTimeTearDown]
|
||||
public void OneTimeTearDown() => client.Dispose();
|
||||
|
||||
[SetUp]
|
||||
public async Task SetUp()
|
||||
=> seed = await FlagSeed.InsertEnabledFlagAsync(db, testRunTag: "FlagEnabled", enabled: true, value: null);
|
||||
|
||||
[TearDown]
|
||||
public async Task TearDown()
|
||||
{
|
||||
if (seed is not null)
|
||||
{
|
||||
await seed.CleanupAsync(db);
|
||||
seed = null;
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task WhenAFeatureIsEnabledInEnvironment_ThenTheFlagApiReportsItEnabled()
|
||||
{
|
||||
var current = seed!;
|
||||
|
||||
using var response = await client.SendAsync(
|
||||
httpFile: "Flags.http",
|
||||
requestName: "GetFlags",
|
||||
variables: new Dictionary<string, string>
|
||||
{
|
||||
["baseUrl"] = settings.BaseUrl,
|
||||
["environmentKey"] = current.EnvironmentApiKey
|
||||
});
|
||||
|
||||
var body = await response.Content.ReadAsStringAsync();
|
||||
|
||||
Assert.That(
|
||||
response.IsSuccessStatusCode,
|
||||
Is.True,
|
||||
$"GET /api/v1/flags returned {(int)response.StatusCode} {response.ReasonPhrase}. Body: {body}. DB state:\n{await current.DescribeAsync(db)}");
|
||||
|
||||
var flags = JsonSerializer.Deserialize<List<FlagResponseDto>>(body, JsonOptions)
|
||||
?? throw new InvalidOperationException("Response body was not a JSON array.");
|
||||
|
||||
var match = flags.SingleOrDefault(f => f.Feature.Name == current.FeatureName);
|
||||
|
||||
Assert.That(
|
||||
match,
|
||||
Is.Not.Null,
|
||||
$"Feature '{current.FeatureName}' was not present in the flags response. Returned features: [{string.Join(", ", flags.Select(f => f.Feature.Name))}]. DB state:\n{await current.DescribeAsync(db)}");
|
||||
|
||||
Assert.That(
|
||||
match!.Enabled,
|
||||
Is.True,
|
||||
$"Feature '{current.FeatureName}' was reported as disabled, but the seeded FeatureState has Enabled=true. DB state:\n{await current.DescribeAsync(db)}");
|
||||
}
|
||||
|
||||
private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web);
|
||||
|
||||
private sealed record FlagResponseDto(int Id, FeatureSummaryDto Feature, bool Enabled, string? FeatureStateValue);
|
||||
private sealed record FeatureSummaryDto(int Id, string Name);
|
||||
}
|
||||
70
tests/api/MicCheck.Api.Tests.Integration/Flags/FlagSeed.cs
Normal file
70
tests/api/MicCheck.Api.Tests.Integration/Flags/FlagSeed.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using MicCheck.Api.Tests.Integration.Common;
|
||||
|
||||
namespace MicCheck.Api.Tests.Integration.Flags;
|
||||
|
||||
public sealed record FlagSeed(int OrganizationId, int ProjectId, int EnvironmentId, int FeatureId, int FeatureStateId, string EnvironmentApiKey, string FeatureName)
|
||||
{
|
||||
public static async Task<FlagSeed> InsertEnabledFlagAsync(TestDatabase db, string testRunTag, bool enabled, string? value)
|
||||
{
|
||||
var unique = $"{testRunTag}-{Guid.NewGuid():N}"[..40];
|
||||
var environmentApiKey = $"envkey_{Guid.NewGuid():N}";
|
||||
var featureName = $"feature_{unique}";
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
|
||||
var organizationId = await db.ExecuteScalarAsync<int>(
|
||||
"INSERT INTO \"Organizations\" (\"Name\", \"CreatedAt\") VALUES (@name, @createdAt) RETURNING \"Id\"",
|
||||
("name", $"org_{unique}"),
|
||||
("createdAt", now));
|
||||
|
||||
var projectId = await db.ExecuteScalarAsync<int>(
|
||||
"INSERT INTO \"Projects\" (\"Name\", \"OrganizationId\", \"CreatedAt\", \"HideDisabledFlags\") VALUES (@name, @orgId, @createdAt, false) RETURNING \"Id\"",
|
||||
("name", $"project_{unique}"),
|
||||
("orgId", organizationId),
|
||||
("createdAt", now));
|
||||
|
||||
var environmentId = await db.ExecuteScalarAsync<int>(
|
||||
"INSERT INTO \"Environments\" (\"Name\", \"ApiKey\", \"ProjectId\", \"CreatedAt\") VALUES (@name, @apiKey, @projectId, @createdAt) RETURNING \"Id\"",
|
||||
("name", $"env_{unique}"),
|
||||
("apiKey", environmentApiKey),
|
||||
("projectId", projectId),
|
||||
("createdAt", now));
|
||||
|
||||
var featureId = await db.ExecuteScalarAsync<int>(
|
||||
"INSERT INTO \"Features\" (\"Name\", \"Type\", \"DefaultEnabled\", \"ProjectId\", \"CreatedAt\") VALUES (@name, 0, @defaultEnabled, @projectId, @createdAt) RETURNING \"Id\"",
|
||||
("name", featureName),
|
||||
("defaultEnabled", enabled),
|
||||
("projectId", projectId),
|
||||
("createdAt", now));
|
||||
|
||||
var featureStateId = await db.ExecuteScalarAsync<int>(
|
||||
"""
|
||||
INSERT INTO "FeatureStates" ("FeatureId", "EnvironmentId", "Enabled", "Value", "CreatedAt", "UpdatedAt", "Version")
|
||||
VALUES (@featureId, @environmentId, @enabled, @value, @createdAt, @updatedAt, 1)
|
||||
RETURNING "Id"
|
||||
""",
|
||||
("featureId", featureId),
|
||||
("environmentId", environmentId),
|
||||
("enabled", enabled),
|
||||
("value", (object?)value ?? DBNull.Value),
|
||||
("createdAt", now),
|
||||
("updatedAt", now));
|
||||
|
||||
return new FlagSeed(organizationId, projectId, environmentId, featureId, featureStateId, environmentApiKey, featureName);
|
||||
}
|
||||
|
||||
public async Task CleanupAsync(TestDatabase db)
|
||||
=> await db.ExecuteAsync(
|
||||
"DELETE FROM \"Organizations\" WHERE \"Id\" = @orgId",
|
||||
("orgId", OrganizationId));
|
||||
|
||||
public async Task<string> DescribeAsync(TestDatabase db)
|
||||
=> await db.SnapshotRowsAsync(
|
||||
"""
|
||||
SELECT f."Id" AS "FeatureId", f."Name", f."DefaultEnabled",
|
||||
fs."Id" AS "FeatureStateId", fs."Enabled", fs."Value", fs."EnvironmentId"
|
||||
FROM "Features" f
|
||||
JOIN "FeatureStates" fs ON fs."FeatureId" = f."Id"
|
||||
WHERE f."Id" = @featureId
|
||||
""",
|
||||
("featureId", FeatureId));
|
||||
}
|
||||
Reference in New Issue
Block a user