Files
satellite-provider/SatelliteProvider.Tests/Validators/UavTileMetadataValidatorTests.cs
T
Oleksandr Bezdieniezhnykh 490902c80a [AZ-810] Strict validation for POST /api/satellite/upload metadata
Adds the per-endpoint child of AZ-795 ("Strict Input Validation Epic")
for the UAV upload multipart endpoint. Three new validators land under
SatelliteProvider.Api/Validators/:

- UavTileBatchMetadataPayloadValidator: items NotNull + NotEmpty +
  count <= MaxBatchSize + RuleForEach dispatching to the per-item
  validator.
- UavTileMetadataValidator: lat / lon / tileZoom range, tileSizeMeters
  > 0, capturedAt within [now - MaxAgeDays, now + future-skew]; uses an
  injectable TimeProvider so unit tests can drive a fixed clock.
- UavUploadValidationFilter: IEndpointFilter that reads the multipart
  `metadata` form field, deserializes it with the strict global
  JsonSerializerOptions (so UnmappedMemberHandling.Disallow +
  [JsonRequired] from AZ-795 are honored), runs the FluentValidation
  chain, and enforces the cross-field `items.Count == files.Count`
  envelope rule. FluentValidation errors are prefixed with `metadata.`
  so wire keys look like `errors["metadata.items[0].latitude"]`.

[JsonRequired] is added to every non-optional axis on
UavTileMetadata and UavTileBatchMetadataPayload; FlightId stays
nullable per AZ-503 anonymous-flight semantics.

Coverage: 13 unit tests + 16 integration tests + 1 curl probe script
exercise the happy path and every failure mode. All 9 ACs covered;
no regression in AZ-488 UavUploadTests payloads (traced against the
new rules).

Documentation: uav-tile-upload.md bumped v1.1.0 -> v1.2.0 with the
new validation rules section + 400-shape examples + changelog entry.
api_program.md updated to describe the three new validators + filter
+ the AddTransient<UavUploadValidationFilter>() DI registration.

Reports: batch_04_cycle8_report.md + reviews/batch_04_cycle8_review.md
record the PASS_WITH_WARNINGS verdict (2 Low DRY-in-tests findings:
FixedTimeProvider duplication crossed the cycle-2 "promote to shared"
threshold; PostBatch helper duplicated between two integration
suites). Both deferred to follow-up PBIs.

Task spec archived: _docs/02_tasks/todo/AZ-810... -> done/.
Jira: AZ-810 transitioned In Progress -> In Testing.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-23 13:32:19 +03:00

193 lines
5.5 KiB
C#

using FluentValidation.TestHelper;
using Microsoft.Extensions.Options;
using SatelliteProvider.Api.Validators;
using SatelliteProvider.Common.Configs;
using SatelliteProvider.Common.DTO;
namespace SatelliteProvider.Tests.Validators;
// AZ-810: per-item metadata validator tests. Each RuleFor in
// UavTileMetadataValidator gets at least one passing + one failing case.
// Required-field detection lives at the deserializer layer ([JsonRequired]
// on UavTileMetadata) and is exercised at the integration layer.
public class UavTileMetadataValidatorTests
{
private readonly UavTileMetadataValidator _validator;
private readonly DateTime _now;
public UavTileMetadataValidatorTests()
{
GlobalValidatorConfig.ApplyOnce();
var config = Options.Create(new UavQualityConfig
{
MaxAgeDays = 7,
CapturedAtFutureSkewSeconds = 30,
});
_now = new DateTime(2026, 5, 22, 12, 0, 0, DateTimeKind.Utc);
_validator = new UavTileMetadataValidator(config, new FixedTimeProvider(_now));
}
// Mirrors the existing pattern in UavTileUploadHandlerTests / UavTileQualityGateTests
// (those tests inline the same shape). Kept private here for SRP; if a third
// consumer appears, promote to SatelliteProvider.TestSupport.
private sealed class FixedTimeProvider : TimeProvider
{
private readonly DateTime _utcNow;
public FixedTimeProvider(DateTime utcNow) => _utcNow = utcNow;
public override DateTimeOffset GetUtcNow() => new(_utcNow, TimeSpan.Zero);
}
private static UavTileMetadata ValidMetadata(DateTime capturedAt) => new()
{
Latitude = 50.10,
Longitude = 36.10,
TileZoom = 18,
TileSizeMeters = 200.0,
CapturedAt = capturedAt,
FlightId = null,
};
[Fact]
public void Validate_AllValid_Passes()
{
// Arrange
var metadata = ValidMetadata(_now.AddMinutes(-5));
// Act
var result = _validator.TestValidate(metadata);
// Assert
result.ShouldNotHaveAnyValidationErrors();
}
[Theory]
[InlineData(-91.0)]
[InlineData(90.001)]
[InlineData(180.0)]
public void Validate_LatOutOfRange_FailsRangeRule(double lat)
{
// Arrange
var metadata = ValidMetadata(_now) with { Latitude = lat };
// Act
var result = _validator.TestValidate(metadata);
// Assert
result.ShouldHaveValidationErrorFor("latitude");
}
[Theory]
[InlineData(-181.0)]
[InlineData(180.001)]
[InlineData(360.0)]
public void Validate_LonOutOfRange_FailsRangeRule(double lon)
{
// Arrange
var metadata = ValidMetadata(_now) with { Longitude = lon };
// Act
var result = _validator.TestValidate(metadata);
// Assert
result.ShouldHaveValidationErrorFor("longitude");
}
[Theory]
[InlineData(-1)]
[InlineData(23)]
[InlineData(100)]
public void Validate_TileZoomOutOfRange_FailsRangeRule(int zoom)
{
// Arrange
var metadata = ValidMetadata(_now) with { TileZoom = zoom };
// Act
var result = _validator.TestValidate(metadata);
// Assert
result.ShouldHaveValidationErrorFor("tileZoom");
}
[Theory]
[InlineData(0.0)]
[InlineData(-1.0)]
public void Validate_TileSizeMetersNonPositive_FailsGreaterThanRule(double size)
{
// Arrange
var metadata = ValidMetadata(_now) with { TileSizeMeters = size };
// Act
var result = _validator.TestValidate(metadata);
// Assert
result.ShouldHaveValidationErrorFor("tileSizeMeters");
}
[Fact]
public void Validate_CapturedAtFuture_FailsFreshnessRule()
{
// Arrange — 60s in the future (skew limit is 30s).
var metadata = ValidMetadata(_now.AddSeconds(60));
// Act
var result = _validator.TestValidate(metadata);
// Assert
result.ShouldHaveValidationErrorFor("capturedAt")
.WithErrorMessage("`capturedAt` must be within 30s of the current time (no future-dated tiles).");
}
[Fact]
public void Validate_CapturedAtNearFutureWithinSkew_Passes()
{
// Arrange — 10s in the future (within the 30s skew window).
var metadata = ValidMetadata(_now.AddSeconds(10));
// Act
var result = _validator.TestValidate(metadata);
// Assert
result.ShouldNotHaveValidationErrorFor("capturedAt");
}
[Fact]
public void Validate_CapturedAtTooOld_FailsFreshnessRule()
{
// Arrange — 8 days ago (cap is 7 days).
var metadata = ValidMetadata(_now.AddDays(-8));
// Act
var result = _validator.TestValidate(metadata);
// Assert
result.ShouldHaveValidationErrorFor("capturedAt")
.WithErrorMessage("`capturedAt` must be within the last 7 days.");
}
[Fact]
public void Validate_FlightIdNull_Passes()
{
// Arrange — AZ-503 anonymous-flight semantics: null FlightId is valid.
var metadata = ValidMetadata(_now) with { FlightId = null };
// Act
var result = _validator.TestValidate(metadata);
// Assert
result.ShouldNotHaveValidationErrorFor("flightId");
}
[Fact]
public void Validate_FlightIdSet_Passes()
{
// Arrange
var metadata = ValidMetadata(_now) with { FlightId = Guid.NewGuid() };
// Act
var result = _validator.TestValidate(metadata);
// Assert
result.ShouldNotHaveValidationErrorFor("flightId");
}
}