Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/Api/DeployApiEndpointsTests.cs
T
Joseph Doherty 88c9766ab5 fix(deploy): #949 ask timeout on the deploy trigger is OutcomeUnknown (202 + pre-allocated id), not a failure; slow-compose phase timings
The id is allocated before the ask and travels in StartDeployment, so a timed-out
caller can name and poll the deployment that may still go ahead. REST answers 202
OutcomeUnknown with a Location; the AdminUI shows 'could not be confirmed, may still
be created'; a retry during a running compose gets 409 naming the same id. The actor
logs per-phase timings for composes slower than 3 s.
2026-12-01 19:27:13 -05:00

91 lines
4.5 KiB
C#

using Microsoft.AspNetCore.Http;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.AdminUI.Api;
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Admin;
using ZB.MOM.WW.OtOpcUa.Commons.Types;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Api;
/// <summary>Unit tests for the pure helpers behind the headless deploy REST endpoint —
/// <see cref="DeployApiEndpoints.IsAuthorized"/> (fixed-time API-key check) and
/// <see cref="DeployApiEndpoints.ToResult"/> (outcome → HTTP status mapping).</summary>
public sealed class DeployApiEndpointsTests
{
// ── IsAuthorized ────────────────────────────────────────────────────────────
/// <summary>Verifies the exact configured key authorizes.</summary>
[Fact]
public void IsAuthorized_true_for_matching_key()
=> DeployApiEndpoints.IsAuthorized("s3cret", "s3cret").ShouldBeTrue();
/// <summary>Verifies a wrong key is rejected.</summary>
[Fact]
public void IsAuthorized_false_for_wrong_key()
=> DeployApiEndpoints.IsAuthorized("nope", "s3cret").ShouldBeFalse();
/// <summary>Verifies a missing/empty provided key is rejected.</summary>
[Theory]
[InlineData(null)]
[InlineData("")]
public void IsAuthorized_false_for_missing_provided_key(string? provided)
=> DeployApiEndpoints.IsAuthorized(provided, "s3cret").ShouldBeFalse();
/// <summary>Verifies that when no key is configured nothing authorizes (endpoint stays closed).</summary>
[Theory]
[InlineData(null)]
[InlineData("")]
public void IsAuthorized_false_when_no_key_configured(string? configured)
=> DeployApiEndpoints.IsAuthorized("anything", configured).ShouldBeFalse();
// ── ToResult ────────────────────────────────────────────────────────────────
/// <summary>Verifies each deployment outcome maps to the right HTTP status.</summary>
[Theory]
[InlineData(StartDeploymentOutcome.Accepted, StatusCodes.Status202Accepted)]
[InlineData(StartDeploymentOutcome.NoChanges, StatusCodes.Status200OK)]
[InlineData(StartDeploymentOutcome.AnotherDeploymentInFlight, StatusCodes.Status409Conflict)]
[InlineData(StartDeploymentOutcome.Rejected, StatusCodes.Status422UnprocessableEntity)]
[InlineData(StartDeploymentOutcome.OutcomeUnknown, StatusCodes.Status202Accepted)]
public void ToResult_maps_outcome_to_status(StartDeploymentOutcome outcome, int expectedStatus)
{
var result = DeployApiEndpoints.ToResult(
new StartDeploymentResult(outcome, DeploymentId: null, RevisionHash: null,
Message: "x", CorrelationId: CorrelationId.NewId()));
result.ShouldBeAssignableTo<IStatusCodeHttpResult>();
((IStatusCodeHttpResult)result).StatusCode.ShouldBe(expectedStatus);
}
/// <summary>
/// lmxopcua#949: an ask timeout is not a failure. The REST trigger answers 202 with the pre-allocated id in
/// the body and the Location, so the caller can poll for the deployment that may still go ahead.
/// </summary>
[Fact]
public void ToResult_OutcomeUnknown_is_202_with_the_preallocated_id_and_a_pollable_Location()
{
var id = Guid.NewGuid();
var result = DeployApiEndpoints.ToResult(new StartDeploymentResult(
StartDeploymentOutcome.OutcomeUnknown, new DeploymentId(id), RevisionHash: null,
Message: "no reply", CorrelationId: CorrelationId.NewId()));
var accepted = result.ShouldBeOfType<Microsoft.AspNetCore.Http.HttpResults.Accepted<DeployApiEndpoints.DeployResponse>>();
accepted.Location.ShouldBe($"/api/deployments/{id}");
accepted.Value!.Outcome.ShouldBe("OutcomeUnknown");
accepted.Value.DeploymentId.ShouldBe(id);
accepted.Value.Message.ShouldBe("no reply");
}
/// <summary>A 409 for a compose still running names the deployment that is going ahead (#949).</summary>
[Fact]
public void ToResult_InFlight_carries_the_in_flight_id()
{
var id = Guid.NewGuid();
var result = DeployApiEndpoints.ToResult(new StartDeploymentResult(
StartDeploymentOutcome.AnotherDeploymentInFlight, new DeploymentId(id), null, "busy", CorrelationId.NewId()));
var conflict = result.ShouldBeOfType<Microsoft.AspNetCore.Http.HttpResults.Conflict<DeployApiEndpoints.DeployResponse>>();
conflict.Value!.DeploymentId.ShouldBe(id);
}
}