Files
TomandClaude Opus 5.5 3671019e60
Build and deploy / build (push) Successful in 1m46s
Build and deploy / deploy (push) Successful in 22s
Add IP origin enrichment with DB-IP and world map
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 15:44:33 +01:00

116 lines
4.5 KiB
C#

using System.IO.Compression;
using System.Net;
using Flypaper.GeoIp;
using Flypaper.Tests.Fakes;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using NSubstitute;
using Shouldly;
namespace Flypaper.Tests.GeoIp;
public sealed class GeoIpUpdaterTests : IDisposable
{
private const string BaseUrl = "https://download.test/free/";
private static readonly DateTimeOffset Now = new(2026, 11, 3, 8, 0, 0, TimeSpan.Zero);
private readonly string _directory = Path.Combine(Path.GetTempPath(), $"flypaper-geoip-{Guid.NewGuid():N}");
private readonly FakeHttpHandler _http = new();
[Fact]
public async Task Falls_back_to_previous_month_when_current_is_not_released()
{
_http.Respond($"{BaseUrl}dbip-city-lite-2026-10.mmdb.gz", () => Gzip("city-october"));
_http.Respond($"{BaseUrl}dbip-asn-lite-2026-10.mmdb.gz", () => Gzip("asn-october"));
(await CreateUpdater().UpdateAsync(CancellationToken.None)).ShouldBeTrue();
File.ReadAllText(Path.Combine(_directory, "dbip-city-lite.mmdb")).ShouldBe("city-october");
File.ReadAllText(Path.Combine(_directory, "dbip-asn-lite.mmdb")).ShouldBe("asn-october");
File.ReadAllText(Path.Combine(_directory, "dbip-city-lite.mmdb.version")).ShouldBe("2026-10");
_http.Requests.ShouldContain($"{BaseUrl}dbip-city-lite-2026-11.mmdb.gz");
}
[Fact]
public async Task Prefers_current_month()
{
_http.Respond($"{BaseUrl}dbip-city-lite-2026-11.mmdb.gz", () => Gzip("city-november"));
_http.Respond($"{BaseUrl}dbip-asn-lite-2026-11.mmdb.gz", () => Gzip("asn-november"));
(await CreateUpdater().UpdateAsync(CancellationToken.None)).ShouldBeTrue();
File.ReadAllText(Path.Combine(_directory, "dbip-city-lite.mmdb.version")).ShouldBe("2026-11");
_http.Requests.ShouldNotContain($"{BaseUrl}dbip-city-lite-2026-10.mmdb.gz");
}
[Fact]
public async Task Skips_download_when_previous_month_is_installed_and_current_is_missing()
{
Install("city", "2026-10");
Install("asn", "2026-10");
(await CreateUpdater().UpdateAsync(CancellationToken.None)).ShouldBeFalse();
_http.Requests.ShouldBe([$"{BaseUrl}dbip-city-lite-2026-11.mmdb.gz", $"{BaseUrl}dbip-asn-lite-2026-11.mmdb.gz"]);
}
[Fact]
public async Task Skips_requests_when_current_month_is_installed()
{
Install("city", "2026-11");
Install("asn", "2026-11");
(await CreateUpdater().UpdateAsync(CancellationToken.None)).ShouldBeFalse();
_http.Requests.ShouldBeEmpty();
}
[Fact]
public async Task Fails_without_touching_files_when_server_errors()
{
_http.Respond($"{BaseUrl}dbip-city-lite-2026-11.mmdb.gz", () => new HttpResponseMessage(HttpStatusCode.InternalServerError));
await Should.ThrowAsync<HttpRequestException>(() => CreateUpdater().UpdateAsync(CancellationToken.None));
File.Exists(Path.Combine(_directory, "dbip-city-lite.mmdb")).ShouldBeFalse();
}
private GeoIpUpdater CreateUpdater()
{
var options = Options.Create(new GeoIpOptions { Directory = _directory, BaseUrl = BaseUrl });
var httpClientFactory = Substitute.For<IHttpClientFactory>();
httpClientFactory.CreateClient(Arg.Any<string>()).Returns(_ => new HttpClient(_http, disposeHandler: false));
var databaseFactory = Substitute.For<IGeoIpDatabaseFactory>();
var enricher = new DbIpEnricher(databaseFactory, NullLogger<DbIpEnricher>.Instance);
return new GeoIpUpdater(httpClientFactory, options, enricher, new FixedTimeProvider(Now), NullLogger<GeoIpUpdater>.Instance);
}
private void Install(string kind, string month)
{
Directory.CreateDirectory(_directory);
File.WriteAllText(Path.Combine(_directory, $"dbip-{kind}-lite.mmdb"), kind);
File.WriteAllText(Path.Combine(_directory, $"dbip-{kind}-lite.mmdb.version"), month);
}
private static HttpResponseMessage Gzip(string content)
{
using var buffer = new MemoryStream();
using (var gzip = new GZipStream(buffer, CompressionMode.Compress, leaveOpen: true))
using (var writer = new StreamWriter(gzip))
{
writer.Write(content);
}
return new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(buffer.ToArray()) };
}
public void Dispose()
{
if (Directory.Exists(_directory))
{
Directory.Delete(_directory, recursive: true);
}
}
}