Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
- 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.
72 lines
2.2 KiB
C#
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));
|
|
}
|
|
}
|