Files
Flypaper/test/Flypaper.Tests/Mcp/FlypaperToolsTests.cs
T
TomandClaude Sonnet 5.5 b7027e2a72
Build and deploy / build (push) Successful in 1m31s
Build and deploy / deploy (push) Successful in 30s
Show the strategy of each catch in the dashboard
Add a Strategie column to the catch list (searchable, filterable) and a
Strategien tab to the Top 10 card. The MCP top tool accepts strategies.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 23:34:10 +01:00

155 lines
5.4 KiB
C#

using Flypaper.Core.Abstractions;
using Flypaper.Storage.Queries;
using Flypaper.Tests.Fakes;
using Flypaper.Web.Mcp;
using ModelContextProtocol;
using NSubstitute;
using Shouldly;
namespace Flypaper.Tests.Mcp;
public sealed class FlypaperToolsTests : IDisposable
{
private readonly McpTestData _data = new();
private readonly FlypaperTools _tools;
public FlypaperToolsTests()
{
var governor = Substitute.For<IConnectionGovernor>();
governor.ActiveConnections.Returns(2);
_tools = new FlypaperTools(new CatchQueries(_data.Factory), governor, new FixedTimeProvider(McpTestData.Now));
}
[Fact]
public async Task Get_stats_summarizes_the_period()
{
var stats = await _tools.GetStats("24h");
stats.Period.ShouldBe("24h");
stats.Since.ShouldBe(McpTestData.Now.UtcDateTime.AddHours(-24));
stats.Catches.ShouldBe(3);
stats.Rejected.ShouldBe(1);
stats.UniqueIps.ShouldBe(2);
stats.TotalHoldSeconds.ShouldBe(3750);
stats.AverageHoldSeconds.ShouldBe(1250);
stats.ActiveTraps.ShouldBe(2);
stats.LongestCatch.ShouldNotBeNull().DurationSeconds.ShouldBe(3600);
stats.LongestCatch!.Country.ShouldBe("China");
stats.LongestCatch.Asn.ShouldBe("AS4134");
stats.LongestCatch.StartedAt.Kind.ShouldBe(DateTimeKind.Utc);
}
[Fact]
public async Task Get_stats_for_all_time()
{
var stats = await _tools.GetStats("all");
stats.Since.ShouldBeNull();
stats.Catches.ShouldBe(5);
stats.UniqueIps.ShouldBe(4);
}
[Theory]
[InlineData("yesterday")]
[InlineData("0h")]
[InlineData("-1d")]
public async Task Rejects_invalid_periods(string period) =>
await Should.ThrowAsync<McpException>(() => _tools.GetStats(period));
[Fact]
public async Task List_catches_returns_newest_first_with_rejected_ones()
{
var result = await _tools.ListCatches();
result.Count.ShouldBe(6);
result.Catches.Select(c => c.StartedAt).ShouldBeInOrder(SortDirection.Descending);
result.Catches.ShouldContain(c => c.Outcome == "Rejected");
}
[Theory]
[InlineData("cn", null, null, null, 2)]
[InlineData(null, "AS3462", null, null, 2)]
[InlineData(null, "google", null, null, 1)]
[InlineData(null, null, "198.51.100.7", null, 2)]
[InlineData(null, null, null, ".ENV", 2)]
[InlineData("CN", null, null, "wp-login", 1)]
public async Task List_catches_filters(string? country, string? provider, string? ip, string? target, int expected)
{
var result = await _tools.ListCatches(country: country, provider: provider, ip: ip, target_contains: target);
result.Count.ShouldBe(expected);
}
[Fact]
public async Task List_catches_filters_by_time_and_limit()
{
var since = McpTestData.Now.AddHours(-3.5).ToString("O");
var until = McpTestData.Now.AddHours(-1.5).ToString("O");
(await _tools.ListCatches(since: since, until: until)).Count.ShouldBe(2);
(await _tools.ListCatches(limit: 1)).Count.ShouldBe(1);
(await _tools.ListCatches(limit: 100_000)).Count.ShouldBe(6);
await Should.ThrowAsync<McpException>(() => _tools.ListCatches(since: "last tuesday"));
await Should.ThrowAsync<McpException>(() => _tools.ListCatches(ip: "not-an-ip"));
}
[Theory]
[InlineData("ips", "203.0.113.9", 2)]
[InlineData("countries", "CN", 2)]
[InlineData("providers", "AS4134", 2)]
[InlineData("targets", "GET /.env", 1)]
[InlineData("strategies", "html-drip", 4)]
[InlineData("user_agents", "zgrab/0.x", 2)]
public async Task Top_ranks_each_dimension(string dimension, string firstKey, int firstCount)
{
var result = await _tools.Top(dimension, "all");
result.Dimension.ShouldBe(dimension);
result.Items[0].Key.ShouldBe(firstKey);
result.Items[0].Catches.ShouldBe(firstCount);
}
[Fact]
public async Task Top_uses_common_country_names_and_rejects_unknown_dimensions()
{
var countries = await _tools.Top("countries", "24h");
countries.Items.Select(i => i.Label).ShouldBe(["China", "Taiwan"]);
await Should.ThrowAsync<McpException>(() => _tools.Top("cities"));
}
[Fact]
public async Task Longest_catches_are_ordered_by_duration()
{
var result = await _tools.LongestCatches("all", 3);
result.Catches.Select(c => c.DurationSeconds).ShouldBe([3600, 900, 120]);
}
[Fact]
public async Task Lookup_ip_returns_everything_known()
{
var result = await _tools.LookupIp("198.51.100.7");
result.Known.ShouldBeTrue();
result.Country.ShouldBe("Taiwan");
result.Asn.ShouldBe("AS3462");
result.Catches.ShouldBe(1);
result.Rejected.ShouldBe(1);
result.TotalHoldSeconds.ShouldBe(30);
result.FirstSeen.ShouldBe(McpTestData.Now.UtcDateTime.AddHours(-4));
result.LastSeen.ShouldBe(McpTestData.Now.UtcDateTime.AddHours(-3).AddSeconds(30));
result.Events.Count.ShouldBe(2);
}
[Fact]
public async Task Lookup_ip_handles_unknown_and_invalid_addresses()
{
(await _tools.LookupIp("192.0.2.99")).Known.ShouldBeFalse();
(await _tools.LookupIp("2001:0db8::0001")).Known.ShouldBeTrue();
await Should.ThrowAsync<McpException>(() => _tools.LookupIp("999.1.1.1"));
}
public void Dispose() => _data.Dispose();
}