Files
git.stella-ops.org/src/Cli/__Tests/StellaOps.Cli.Tests/Configuration/EgressPolicyHttpMessageHandlerTests.cs
master a1ce3f74fa
Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
Implement MongoDB-based storage for Pack Run approval, artifact, log, and state management
- Added MongoPackRunApprovalStore for managing approval states with MongoDB.
- Introduced MongoPackRunArtifactUploader for uploading and storing artifacts.
- Created MongoPackRunLogStore to handle logging of pack run events.
- Developed MongoPackRunStateStore for persisting and retrieving pack run states.
- Implemented unit tests for MongoDB stores to ensure correct functionality.
- Added MongoTaskRunnerTestContext for setting up MongoDB test environment.
- Enhanced PackRunStateFactory to correctly initialize state with gate reasons.
2025-11-07 10:01:47 +02:00

72 lines
2.2 KiB
C#

using Xunit;
using System;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging.Abstractions;
using StellaOps.AirGap.Policy;
using StellaOps.Cli.Configuration;
namespace StellaOps.Cli.Tests.Configuration;
public sealed class EgressPolicyHttpMessageHandlerTests
{
[Fact]
public async Task SendAsync_AllowsRequestWhenPolicyPermits()
{
var options = new EgressPolicyOptions
{
Mode = EgressPolicyMode.Sealed
};
options.AddAllowRule("example.com");
var policy = new EgressPolicy(options);
var handler = new EgressPolicyHttpMessageHandler(
policy,
NullLogger.Instance,
component: "cli-tests",
intent: "allow-test")
{
InnerHandler = new StubHandler()
};
using var client = new HttpClient(handler, disposeHandler: true);
var response = await client.GetAsync("https://example.com/resource", CancellationToken.None);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
[Fact]
public async Task SendAsync_ThrowsWhenPolicyBlocksRequest()
{
var options = new EgressPolicyOptions
{
Mode = EgressPolicyMode.Sealed
};
var policy = new EgressPolicy(options);
var handler = new EgressPolicyHttpMessageHandler(
policy,
NullLogger.Instance,
component: "cli-tests",
intent: "deny-test")
{
InnerHandler = new StubHandler()
};
using var client = new HttpClient(handler, disposeHandler: true);
var exception = await Assert.ThrowsAsync<AirGapEgressBlockedException>(
() => client.GetAsync("https://blocked.example", CancellationToken.None));
Assert.Contains(AirGapEgressBlockedException.ErrorCode, exception.Message, StringComparison.OrdinalIgnoreCase);
}
private sealed class StubHandler : HttpMessageHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
=> Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK));
}
}