mirror of
https://github.com/azaion/satellite-provider.git
synced 2026-06-21 10:51:15 +00:00
1802d32107
Replaces the 501 stub at POST /api/satellite/upload with a multipart
batch endpoint that ingests UAV-captured tiles, runs each item through
a 5-rule quality gate, and persists accepted tiles via the AZ-484
multi-source storage path with source='uav'.
Quality gate (in fixed order, first failure wins): JPEG format
(content-type + magic), size band 5 KiB-5 MiB, exact 256x256
dimensions, captured-at age (no future >30 s skew, no older than
7 days), luminance variance on 32x32 downsample. Closed reject-reason
enumeration in v1.0.0 contract.
Authorization: custom PermissionsRequirement / PermissionsAuthorization
Handler that reads the JWT `permissions` claim (tolerates both
repeated-string and JSON-array shapes). Endpoint protected by
RequiresGpsPermission policy; 401 without token, 403 without GPS perm.
Persistence: file-first to ./tiles/uav/{z}/{x}/{y}.jpg, then
ITileRepository.InsertAsync UPSERT (per-source UPSERT contract from
AZ-484). Per-item failures reported in response without aborting the
batch. Kestrel MaxRequestBodySize and FormOptions limits set to
MaxBatchSize x MaxBytes (default 100 x 5 MiB = 500 MiB).
New frozen contract: _docs/02_document/contracts/api/uav-tile-upload.md
v1.0.0. PT-08 NFR added to performance-tests.md as Deferred (harness
work tracked in PT-07 leftover, per AZ-488 § Risk 4).
Tests: 11 quality-gate unit tests, 5 handler unit tests, 3 file-path
unit tests, 12 permission-handler unit tests, 7 integration tests
(AC-1..AC-6, AC-8). All 253 unit tests + smoke integration suite
green.
Co-authored-by: Cursor <cursoragent@cursor.com>
238 lines
7.7 KiB
C#
238 lines
7.7 KiB
C#
using FluentAssertions;
|
|
using Microsoft.Extensions.Options;
|
|
using SatelliteProvider.Common.Configs;
|
|
using SatelliteProvider.Common.DTO;
|
|
using SatelliteProvider.Services.TileDownloader;
|
|
using SatelliteProvider.Tests.TestUtilities;
|
|
|
|
namespace SatelliteProvider.Tests;
|
|
|
|
public class UavTileQualityGateTests
|
|
{
|
|
private const string JpegContentType = "image/jpeg";
|
|
|
|
[Fact]
|
|
public void Validate_NonJpegContentType_RejectsInvalidFormat()
|
|
{
|
|
// Arrange
|
|
var gate = BuildGate();
|
|
var bytes = UavTileImageFactory.CreatePng();
|
|
var metadata = ValidMetadata();
|
|
|
|
// Act
|
|
var result = gate.Validate(bytes, "image/png", metadata);
|
|
|
|
// Assert
|
|
result.Accepted.Should().BeFalse();
|
|
result.Reason.Should().Be(UavTileRejectReasons.InvalidFormat);
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_WrongMagicBytes_RejectsInvalidFormat()
|
|
{
|
|
// Arrange
|
|
var gate = BuildGate();
|
|
var bytes = new byte[6_000];
|
|
bytes[0] = 0x89; bytes[1] = 0x50; bytes[2] = 0x4E; bytes[3] = 0x47;
|
|
var metadata = ValidMetadata();
|
|
|
|
// Act
|
|
var result = gate.Validate(bytes, JpegContentType, metadata);
|
|
|
|
// Assert
|
|
result.Accepted.Should().BeFalse();
|
|
result.Reason.Should().Be(UavTileRejectReasons.InvalidFormat);
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_ValidJpeg_PassesFormatRule()
|
|
{
|
|
// Arrange
|
|
var gate = BuildGate();
|
|
var bytes = UavTileImageFactory.CreateRandomJpeg();
|
|
var metadata = ValidMetadata();
|
|
|
|
// Act
|
|
var result = gate.Validate(bytes, JpegContentType, metadata);
|
|
|
|
// Assert
|
|
result.Accepted.Should().BeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_TooSmall_RejectsSizeOutOfBand()
|
|
{
|
|
// Arrange
|
|
var bytes = new byte[200];
|
|
bytes[0] = 0xFF; bytes[1] = 0xD8; bytes[2] = 0xFF;
|
|
var gate = BuildGate();
|
|
|
|
// Act
|
|
var result = gate.Validate(bytes, JpegContentType, ValidMetadata());
|
|
|
|
// Assert
|
|
result.Accepted.Should().BeFalse();
|
|
result.Reason.Should().Be(UavTileRejectReasons.SizeOutOfBand);
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_TooLarge_RejectsSizeOutOfBand()
|
|
{
|
|
// Arrange — craft a JPEG-prefixed byte blob just over the configured MaxBytes ceiling.
|
|
var qualityConfig = new UavQualityConfig { MaxBytes = 8 * 1024 };
|
|
var oversized = new byte[qualityConfig.MaxBytes + 1];
|
|
oversized[0] = 0xFF; oversized[1] = 0xD8; oversized[2] = 0xFF;
|
|
var gate = BuildGate(qualityConfig);
|
|
|
|
// Act
|
|
var result = gate.Validate(oversized, JpegContentType, ValidMetadata());
|
|
|
|
// Assert
|
|
result.Accepted.Should().BeFalse();
|
|
result.Reason.Should().Be(UavTileRejectReasons.SizeOutOfBand);
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_WrongDimensions_RejectsWrongDimensions()
|
|
{
|
|
// Arrange
|
|
var bytes = UavTileImageFactory.CreateRandomJpeg(width: 512, height: 512);
|
|
var gate = BuildGate();
|
|
|
|
// Act
|
|
var result = gate.Validate(bytes, JpegContentType, ValidMetadata());
|
|
|
|
// Assert
|
|
result.Accepted.Should().BeFalse();
|
|
result.Reason.Should().Be(UavTileRejectReasons.WrongDimensions);
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_CapturedAtFuture_RejectsCapturedAtFuture()
|
|
{
|
|
// Arrange
|
|
var now = new DateTime(2026, 5, 11, 12, 0, 0, DateTimeKind.Utc);
|
|
var gate = BuildGate(timeProvider: new FixedTimeProvider(now));
|
|
var bytes = UavTileImageFactory.CreateRandomJpeg();
|
|
var metadata = ValidMetadata() with { CapturedAt = now.AddHours(1) };
|
|
|
|
// Act
|
|
var result = gate.Validate(bytes, JpegContentType, metadata);
|
|
|
|
// Assert
|
|
result.Accepted.Should().BeFalse();
|
|
result.Reason.Should().Be(UavTileRejectReasons.CapturedAtFuture);
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_CapturedAtTooOld_RejectsCapturedAtTooOld()
|
|
{
|
|
// Arrange
|
|
var now = new DateTime(2026, 5, 11, 12, 0, 0, DateTimeKind.Utc);
|
|
var gate = BuildGate(timeProvider: new FixedTimeProvider(now));
|
|
var bytes = UavTileImageFactory.CreateRandomJpeg();
|
|
var metadata = ValidMetadata() with { CapturedAt = now.AddDays(-8) };
|
|
|
|
// Act
|
|
var result = gate.Validate(bytes, JpegContentType, metadata);
|
|
|
|
// Assert
|
|
result.Accepted.Should().BeFalse();
|
|
result.Reason.Should().Be(UavTileRejectReasons.CapturedAtTooOld);
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_CapturedAtSlightlyInFuture_WithinSkew_Accepts()
|
|
{
|
|
// Arrange
|
|
var now = new DateTime(2026, 5, 11, 12, 0, 0, DateTimeKind.Utc);
|
|
var gate = BuildGate(timeProvider: new FixedTimeProvider(now));
|
|
var bytes = UavTileImageFactory.CreateRandomJpeg();
|
|
var metadata = ValidMetadata() with { CapturedAt = now.AddSeconds(20) };
|
|
|
|
// Act
|
|
var result = gate.Validate(bytes, JpegContentType, metadata);
|
|
|
|
// Assert
|
|
result.Accepted.Should().BeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_UniformGreyImage_RejectsImageTooUniform()
|
|
{
|
|
// Arrange — uniform-color JPEGs compress to far below the
|
|
// default 5 KiB MinBytes (rule 2). Drop MinBytes so rule 5
|
|
// remains the active filter for this test.
|
|
var bytes = UavTileImageFactory.CreateUniformGreyJpeg(value: 128);
|
|
var gate = BuildGate(new UavQualityConfig { MinBytes = 1 });
|
|
|
|
// Act
|
|
var result = gate.Validate(bytes, JpegContentType, ValidMetadata());
|
|
|
|
// Assert
|
|
result.Accepted.Should().BeFalse();
|
|
result.Reason.Should().Be(UavTileRejectReasons.ImageTooUniform);
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_FirstFailingRuleWins_FormatBeforeDimensions()
|
|
{
|
|
// Arrange — a PNG resized to 512x512 fails BOTH rule 1 (content-type
|
|
// says JPEG but magic bytes don't) AND rule 3 (dimensions != 256).
|
|
// Rule 1 runs first so we expect INVALID_FORMAT.
|
|
var bytes = UavTileImageFactory.CreatePng(width: 512, height: 512);
|
|
var gate = BuildGate();
|
|
|
|
// Act
|
|
var result = gate.Validate(bytes, JpegContentType, ValidMetadata());
|
|
|
|
// Assert
|
|
result.Accepted.Should().BeFalse();
|
|
result.Reason.Should().Be(UavTileRejectReasons.InvalidFormat);
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_HappyPath_RandomNoiseJpeg_Accepts()
|
|
{
|
|
// Act
|
|
var result = BuildGate().Validate(UavTileImageFactory.CreateRandomJpeg(), JpegContentType, ValidMetadata());
|
|
|
|
// Assert
|
|
result.Accepted.Should().BeTrue();
|
|
result.Reason.Should().BeNull();
|
|
}
|
|
|
|
private static UavTileQualityGate BuildGate(UavQualityConfig? quality = null, MapConfig? map = null, TimeProvider? timeProvider = null)
|
|
{
|
|
return new UavTileQualityGate(
|
|
Options.Create(quality ?? new UavQualityConfig()),
|
|
Options.Create(map ?? new MapConfig { TileSizePixels = 256 }),
|
|
timeProvider ?? new FixedTimeProvider(new DateTime(2026, 5, 11, 12, 0, 0, DateTimeKind.Utc)));
|
|
}
|
|
|
|
private static UavTileMetadata ValidMetadata() => new()
|
|
{
|
|
Latitude = 47.461747,
|
|
Longitude = 37.647063,
|
|
TileZoom = 18,
|
|
TileSizeMeters = 200.0,
|
|
CapturedAt = new DateTime(2026, 5, 11, 11, 30, 0, DateTimeKind.Utc),
|
|
};
|
|
|
|
private sealed class FixedTimeProvider : TimeProvider
|
|
{
|
|
private readonly DateTime _utcNow;
|
|
|
|
public FixedTimeProvider(DateTime utcNow)
|
|
{
|
|
if (utcNow.Kind != DateTimeKind.Utc)
|
|
{
|
|
throw new ArgumentException("DateTime must be UTC", nameof(utcNow));
|
|
}
|
|
_utcNow = utcNow;
|
|
}
|
|
|
|
public override DateTimeOffset GetUtcNow() => new(_utcNow, TimeSpan.Zero);
|
|
}
|
|
}
|