mirror of
https://github.com/azaion/satellite-provider.git
synced 2026-06-22 07:51:31 +00:00
[AZ-794] [AZ-795] [AZ-796] Strict input validation + z/x/y rename
AZ-794: rename inventory wire fields tileZoom/tileX/tileY -> z/x/y to match the slippy-map URL convention. Contract bumped to v2.0.0. AZ-795: shared validation infrastructure -- FluentValidation + ValidationEndpointFilter + GlobalValidatorConfig (camelCase paths). GlobalExceptionHandler now converts JsonException (UnmappedMember + JsonRequired) into RFC 7807 ValidationProblemDetails. JSON layer hardened with UnmappedMemberHandling.Disallow + camelCase naming policy. New error-shape.md contract. AZ-796: InventoryRequestValidator covers 9 rules (XOR tiles vs locationHashes, cap 1000, z 0..22, x/y in slippy bounds, hash length/charset). 16 unit tests + 16 integration tests + a manual curl probe script. Adjacent fixes uncovered by the new strict layer: - IdempotentPostTests RoutePoint payload corrected to lat/lon (the DTO has used JsonPropertyName for ages; previously silently ignored under PascalCase fallback). - TileInventoryTests slippy x/y reduced to fit z=18 bounds. - docker-compose.yml host port for Postgres moved 5432 -> 5433 to avoid sibling-project conflict; appsettings.Development + README + AGENTS + architecture + containerization docs aligned. New coderule (suite + repo): API consumer-facing OpenAPI descriptions must not contain task IDs, contract filenames, or version-bump history -- internal change tracking belongs in commits/contract docs/changelogs. Existing offending descriptions in Program.cs cleaned up. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,261 @@
|
||||
using FluentAssertions;
|
||||
using FluentValidation.TestHelper;
|
||||
using SatelliteProvider.Api.Validators;
|
||||
using SatelliteProvider.Common.DTO;
|
||||
|
||||
namespace SatelliteProvider.Tests.Validators;
|
||||
|
||||
public class InventoryRequestValidatorTests
|
||||
{
|
||||
private readonly InventoryRequestValidator _validator = new();
|
||||
|
||||
[Fact]
|
||||
public void Validate_TilesPopulated_LocationHashesNull_Passes()
|
||||
{
|
||||
// Arrange
|
||||
var request = new TileInventoryRequest
|
||||
{
|
||||
Tiles = new[] { new TileCoord { Z = 18, X = 1, Y = 1 } }
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = _validator.TestValidate(request);
|
||||
|
||||
// Assert
|
||||
result.ShouldNotHaveAnyValidationErrors();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_LocationHashesPopulated_TilesNull_Passes()
|
||||
{
|
||||
// Arrange
|
||||
var request = new TileInventoryRequest
|
||||
{
|
||||
LocationHashes = new[] { Guid.NewGuid() }
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = _validator.TestValidate(request);
|
||||
|
||||
// Assert
|
||||
result.ShouldNotHaveAnyValidationErrors();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_BothPopulated_FailsXorRule()
|
||||
{
|
||||
// Arrange
|
||||
var request = new TileInventoryRequest
|
||||
{
|
||||
Tiles = new[] { new TileCoord { Z = 18, X = 1, Y = 1 } },
|
||||
LocationHashes = new[] { Guid.NewGuid() }
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = _validator.TestValidate(request);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationErrorFor("$")
|
||||
.WithErrorMessage("Populate exactly one of `tiles` or `locationHashes` (sending both, or neither, is not allowed).");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_NeitherPopulated_FailsXorRule()
|
||||
{
|
||||
// Arrange
|
||||
var request = new TileInventoryRequest();
|
||||
|
||||
// Act
|
||||
var result = _validator.TestValidate(request);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationErrorFor("$");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_BothEmpty_FailsXorRule()
|
||||
{
|
||||
// Arrange
|
||||
var request = new TileInventoryRequest
|
||||
{
|
||||
Tiles = Array.Empty<TileCoord>(),
|
||||
LocationHashes = Array.Empty<Guid>()
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = _validator.TestValidate(request);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationErrorFor("$");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_TilesAtCap_Passes()
|
||||
{
|
||||
// Arrange
|
||||
var coords = Enumerable.Range(0, TileInventoryLimits.MaxEntriesPerRequest)
|
||||
.Select(_ => new TileCoord { Z = 18, X = 1, Y = 1 })
|
||||
.ToArray();
|
||||
var request = new TileInventoryRequest { Tiles = coords };
|
||||
|
||||
// Act
|
||||
var result = _validator.TestValidate(request);
|
||||
|
||||
// Assert
|
||||
result.ShouldNotHaveValidationErrorFor("tiles");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_TilesOverCap_FailsCapRule()
|
||||
{
|
||||
// Arrange
|
||||
var coords = Enumerable.Range(0, TileInventoryLimits.MaxEntriesPerRequest + 1)
|
||||
.Select(_ => new TileCoord { Z = 18, X = 1, Y = 1 })
|
||||
.ToArray();
|
||||
var request = new TileInventoryRequest { Tiles = coords };
|
||||
|
||||
// Act
|
||||
var result = _validator.TestValidate(request);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationErrorFor("tiles")
|
||||
.WithErrorMessage($"`tiles` must contain at most {TileInventoryLimits.MaxEntriesPerRequest} entries.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_LocationHashesOverCap_FailsCapRule()
|
||||
{
|
||||
// Arrange
|
||||
var hashes = Enumerable.Range(0, TileInventoryLimits.MaxEntriesPerRequest + 1)
|
||||
.Select(_ => Guid.NewGuid())
|
||||
.ToArray();
|
||||
var request = new TileInventoryRequest { LocationHashes = hashes };
|
||||
|
||||
// Act
|
||||
var result = _validator.TestValidate(request);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationErrorFor("locationHashes")
|
||||
.WithErrorMessage($"`locationHashes` must contain at most {TileInventoryLimits.MaxEntriesPerRequest} entries.");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(-1)]
|
||||
[InlineData(23)]
|
||||
[InlineData(100)]
|
||||
public void Validate_TileZoomOutOfRange_FailsRangeRule(int z)
|
||||
{
|
||||
// Arrange
|
||||
var request = new TileInventoryRequest
|
||||
{
|
||||
Tiles = new[] { new TileCoord { Z = z, X = 0, Y = 0 } }
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = _validator.TestValidate(request);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationErrorFor("tiles[0].z");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0)]
|
||||
[InlineData(18)]
|
||||
[InlineData(22)]
|
||||
public void Validate_TileZoomInRange_PassesRangeRule(int z)
|
||||
{
|
||||
// Arrange
|
||||
var maxAxis = (1 << z) - 1;
|
||||
var request = new TileInventoryRequest
|
||||
{
|
||||
Tiles = new[] { new TileCoord { Z = z, X = maxAxis, Y = maxAxis } }
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = _validator.TestValidate(request);
|
||||
|
||||
// Assert
|
||||
result.ShouldNotHaveValidationErrorFor("tiles[0].z");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_TileXNegative_FailsRangeRule()
|
||||
{
|
||||
// Arrange
|
||||
var request = new TileInventoryRequest
|
||||
{
|
||||
Tiles = new[] { new TileCoord { Z = 18, X = -1, Y = 0 } }
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = _validator.TestValidate(request);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationErrorFor("tiles[0].x");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_TileXAtUpperBound_FailsRangeRule()
|
||||
{
|
||||
// Arrange — at z=2, valid x is 0..3, so x=4 is invalid
|
||||
var request = new TileInventoryRequest
|
||||
{
|
||||
Tiles = new[] { new TileCoord { Z = 2, X = 4, Y = 0 } }
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = _validator.TestValidate(request);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationErrorFor("tiles[0].x")
|
||||
.WithErrorMessage("`x` must be < 2^z = 4 for z=2.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_TileYNegative_FailsRangeRule()
|
||||
{
|
||||
// Arrange
|
||||
var request = new TileInventoryRequest
|
||||
{
|
||||
Tiles = new[] { new TileCoord { Z = 18, X = 0, Y = -1 } }
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = _validator.TestValidate(request);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationErrorFor("tiles[0].y");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_TileYAtUpperBound_FailsRangeRule()
|
||||
{
|
||||
// Arrange — at z=0, valid y is 0..0, so y=1 is invalid
|
||||
var request = new TileInventoryRequest
|
||||
{
|
||||
Tiles = new[] { new TileCoord { Z = 0, X = 0, Y = 1 } }
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = _validator.TestValidate(request);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationErrorFor("tiles[0].y");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_AxesAtMaxForZoom_Passes()
|
||||
{
|
||||
// Arrange — at z=18, valid x/y is 0..(2^18 - 1) = 0..262143
|
||||
var request = new TileInventoryRequest
|
||||
{
|
||||
Tiles = new[] { new TileCoord { Z = 18, X = 262_143, Y = 262_143 } }
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = _validator.TestValidate(request);
|
||||
|
||||
// Assert
|
||||
result.ShouldNotHaveAnyValidationErrors();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user