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;
/// Unit tests for the pure helpers behind the headless deploy REST endpoint —
/// (fixed-time API-key check) and
/// (outcome → HTTP status mapping).
public sealed class DeployApiEndpointsTests
{
// ── IsAuthorized ────────────────────────────────────────────────────────────
/// Verifies the exact configured key authorizes.
[Fact]
public void IsAuthorized_true_for_matching_key()
=> DeployApiEndpoints.IsAuthorized("s3cret", "s3cret").ShouldBeTrue();
/// Verifies a wrong key is rejected.
[Fact]
public void IsAuthorized_false_for_wrong_key()
=> DeployApiEndpoints.IsAuthorized("nope", "s3cret").ShouldBeFalse();
/// Verifies a missing/empty provided key is rejected.
[Theory]
[InlineData(null)]
[InlineData("")]
public void IsAuthorized_false_for_missing_provided_key(string? provided)
=> DeployApiEndpoints.IsAuthorized(provided, "s3cret").ShouldBeFalse();
/// Verifies that when no key is configured nothing authorizes (endpoint stays closed).
[Theory]
[InlineData(null)]
[InlineData("")]
public void IsAuthorized_false_when_no_key_configured(string? configured)
=> DeployApiEndpoints.IsAuthorized("anything", configured).ShouldBeFalse();
// ── ToResult ────────────────────────────────────────────────────────────────
/// Verifies each deployment outcome maps to the right HTTP status.
[Theory]
[InlineData(StartDeploymentOutcome.Accepted, StatusCodes.Status202Accepted)]
[InlineData(StartDeploymentOutcome.NoChanges, StatusCodes.Status200OK)]
[InlineData(StartDeploymentOutcome.AnotherDeploymentInFlight, StatusCodes.Status409Conflict)]
[InlineData(StartDeploymentOutcome.Rejected, StatusCodes.Status422UnprocessableEntity)]
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)result).StatusCode.ShouldBe(expectedStatus);
}
}