Add research page with MCP write tools, longer hold for repeat offenders and svn/hg decoys
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a25072e6fb
commit
e88dc415f9
48 files changed
+1983
-57
No files matched your search
@@ -18,6 +18,19 @@ public sealed class FileContentTests
|
||||
[InlineData("/docker-compose.yml", "yaml-drip")]
|
||||
[InlineData("/backup/db.sql", "sql-drip")]
|
||||
[InlineData("/.git/config", "git-drip")]
|
||||
[InlineData("/.git/HEAD", "git-drip")]
|
||||
[InlineData("/app/.git/logs/HEAD", "git-drip")]
|
||||
[InlineData("/.git", "git-drip")]
|
||||
[InlineData("/.gitconfig", "git-drip")]
|
||||
[InlineData("/.git-credentials", "git-drip")]
|
||||
[InlineData("/home/.GITCONFIG", "git-drip")]
|
||||
[InlineData("/.svn/format", "svn-drip")]
|
||||
[InlineData("/.svn/wc.db", "svn-drip")]
|
||||
[InlineData("/.svn/entries", "svn-drip")]
|
||||
[InlineData("/site/.svn", "svn-drip")]
|
||||
[InlineData("/.hg/requires", "hg-drip")]
|
||||
[InlineData("/.hg/store/requires", "hg-drip")]
|
||||
[InlineData("/.hg", "hg-drip")]
|
||||
[InlineData("/SDK/webLanguage", "hikvision-drip")]
|
||||
[InlineData("/ISAPI/System/deviceInfo", "hikvision-drip")]
|
||||
[InlineData("/wp-login.php", "wordpress-drip")]
|
||||
@@ -29,6 +42,9 @@ public sealed class FileContentTests
|
||||
[InlineData("/")]
|
||||
[InlineData("/index.php")]
|
||||
[InlineData("/sdkfoo")]
|
||||
[InlineData("/.svnfoo")]
|
||||
[InlineData("/.hgrc-not")]
|
||||
[InlineData("/legit.git/x")]
|
||||
[InlineData("/environment")]
|
||||
[InlineData("")]
|
||||
[InlineData(null)]
|
||||
@@ -41,6 +57,11 @@ public sealed class FileContentTests
|
||||
[InlineData("/docker-compose.yml", "version:")]
|
||||
[InlineData("/dump.sql", "-- MySQL dump")]
|
||||
[InlineData("/.git/config", "[core]")]
|
||||
[InlineData("/.git-credentials", "[core]")]
|
||||
[InlineData("/.svn/entries", "12\n")]
|
||||
[InlineData("/.svn/wc.db", "12\n")]
|
||||
[InlineData("/.hg/requires", "dotencode\n")]
|
||||
[InlineData("/.hg/store/requires", "dotencode\n")]
|
||||
[InlineData("/SDK/webLanguage", "<?xml")]
|
||||
[InlineData("/wp-login.php", "<!DOCTYPE html>")]
|
||||
public void Generates_an_endless_file_that_starts_like_the_real_thing(string path, string start)
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace Flypaper.Tests.Core;
|
||||
|
||||
public sealed class HoldMethodParserTests
|
||||
{
|
||||
private static readonly HoldMethodInput Valid = new("Alternating", "50", "1000", "1", "64", "2048", true, "80", "/Backup-2024, /admin-old/x", "01:00:00");
|
||||
private static readonly HoldMethodInput Valid = new("Alternating", "50", "1000", "1", "64", "2048", true, "80", "/Backup-2024, /admin-old/x", "01:00:00", "7", "1.00:00:00");
|
||||
|
||||
[Fact]
|
||||
public void Parses_valid_input()
|
||||
@@ -108,6 +108,37 @@ public sealed class HoldMethodParserTests
|
||||
public void Rejects_invalid_hold_durations(string value) =>
|
||||
RuntimeSettingsParser.ParseHoldMethod(Valid with { MaxHoldDuration = value }).Errors.Keys.ShouldBe([nameof(HoldMethodInput.MaxHoldDuration)]);
|
||||
|
||||
[Theory]
|
||||
[InlineData("0")]
|
||||
[InlineData("365")]
|
||||
public void Accepts_repeat_window(string value) =>
|
||||
RuntimeSettingsParser.ParseHoldMethod(Valid with { RepeatWindowDays = value }).Settings.ShouldNotBeNull().RepeatWindowDays.ShouldBe(int.Parse(value));
|
||||
|
||||
[Theory]
|
||||
[InlineData("")]
|
||||
[InlineData("-1")]
|
||||
[InlineData("366")]
|
||||
[InlineData("abc")]
|
||||
public void Rejects_invalid_repeat_window(string value) =>
|
||||
RuntimeSettingsParser.ParseHoldMethod(Valid with { RepeatWindowDays = value }).Errors.Keys.ShouldBe([nameof(HoldMethodInput.RepeatWindowDays)]);
|
||||
|
||||
[Theory]
|
||||
[InlineData("10")]
|
||||
[InlineData("00:00:00")]
|
||||
[InlineData("24:00:00")]
|
||||
[InlineData("8.00:00:00")]
|
||||
public void Rejects_invalid_repeat_hold_durations(string value) =>
|
||||
RuntimeSettingsParser.ParseHoldMethod(Valid with { RepeatHoldDuration = value }).Errors.Keys.ShouldBe([nameof(HoldMethodInput.RepeatHoldDuration)]);
|
||||
|
||||
[Fact]
|
||||
public void Parses_repeat_settings()
|
||||
{
|
||||
var settings = RuntimeSettingsParser.ParseHoldMethod(Valid).Settings.ShouldNotBeNull();
|
||||
|
||||
settings.RepeatWindowDays.ShouldBe(7);
|
||||
settings.RepeatHoldDuration.ShouldBe(TimeSpan.FromHours(24));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Computes_average_bandwidth()
|
||||
{
|
||||
|
||||
@@ -99,6 +99,58 @@ public sealed class TrapRunnerTests
|
||||
_sink.Received(1).Publish(Arg.Is<TrapEvent>(e => e.RemoteIp.Equals(IPAddress.Parse("192.168.1.20"))));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Holds_ips_with_an_earlier_timeout_longer_and_marks_the_strategy()
|
||||
{
|
||||
AllowAcquire();
|
||||
var lookup = Substitute.For<IRepeatOffenderLookup>();
|
||||
lookup.HasTimedOutSinceAsync(Arg.Any<IPAddress>(), Arg.Any<DateTime>(), Arg.Any<CancellationToken>()).Returns(true);
|
||||
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(500));
|
||||
|
||||
var outcome = await CreateRunner(TimeSpan.FromMilliseconds(100), repeatOffenders: lookup, repeatWindowDays: 7, repeatHoldDuration: TimeSpan.FromMinutes(1))
|
||||
.RunAsync(Context(), Open, cts.Token);
|
||||
|
||||
outcome.ShouldBe(TrapOutcome.ClientAborted);
|
||||
await lookup.Received(1).HasTimedOutSinceAsync(
|
||||
IPAddress.Parse("203.0.113.9"),
|
||||
Arg.Is<DateTime>(since => Math.Abs((DateTime.UtcNow.AddDays(-7) - since).TotalSeconds) < 30),
|
||||
Arg.Any<CancellationToken>());
|
||||
_sink.Received(1).Publish(Arg.Is<TrapEvent>(e => e.Strategy.EndsWith(" repeat")));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keeps_the_normal_limit_for_new_ips()
|
||||
{
|
||||
AllowAcquire();
|
||||
var lookup = Substitute.For<IRepeatOffenderLookup>();
|
||||
lookup.HasTimedOutSinceAsync(Arg.Any<IPAddress>(), Arg.Any<DateTime>(), Arg.Any<CancellationToken>()).Returns(false);
|
||||
|
||||
var outcome = await CreateRunner(TimeSpan.FromMilliseconds(100), repeatOffenders: lookup, repeatWindowDays: 7)
|
||||
.RunAsync(Context(), Open, CancellationToken.None);
|
||||
|
||||
outcome.ShouldBe(TrapOutcome.Timeout);
|
||||
_sink.Received(1).Publish(Arg.Is<TrapEvent>(e => !e.Strategy.EndsWith(" repeat")));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0, "00:00:00.100")]
|
||||
[InlineData(7, "00:00:00.050")]
|
||||
public async Task Does_not_ask_when_the_feature_is_off_or_would_not_extend_the_hold(int windowDays, string repeatHold)
|
||||
{
|
||||
AllowAcquire();
|
||||
var lookup = Substitute.For<IRepeatOffenderLookup>();
|
||||
|
||||
await CreateRunner(TimeSpan.FromMilliseconds(100), repeatOffenders: lookup, repeatWindowDays: windowDays, repeatHoldDuration: TimeSpan.Parse(repeatHold))
|
||||
.RunAsync(Context(), Open, CancellationToken.None);
|
||||
|
||||
await lookup.DidNotReceiveWithAnyArgs().HasTimedOutSinceAsync(default!, default, default);
|
||||
}
|
||||
|
||||
private sealed class NullRepeatLookup : IRepeatOffenderLookup
|
||||
{
|
||||
public ValueTask<bool> HasTimedOutSinceAsync(IPAddress address, DateTime since, CancellationToken ct) => ValueTask.FromResult(false);
|
||||
}
|
||||
|
||||
private IDisposable AllowAcquire()
|
||||
{
|
||||
var lease = Substitute.For<IDisposable>();
|
||||
@@ -113,7 +165,12 @@ public sealed class TrapRunnerTests
|
||||
|
||||
private ValueTask<ITrapChannel> Open(ITarpitStrategy strategy, CancellationToken ct) => ValueTask.FromResult<ITrapChannel>(_channel);
|
||||
|
||||
private TrapRunner CreateRunner(TimeSpan maxHoldDuration, bool ignorePrivateNetworks = true)
|
||||
private TrapRunner CreateRunner(
|
||||
TimeSpan maxHoldDuration,
|
||||
bool ignorePrivateNetworks = true,
|
||||
IRepeatOffenderLookup? repeatOffenders = null,
|
||||
int repeatWindowDays = 0,
|
||||
TimeSpan? repeatHoldDuration = null)
|
||||
{
|
||||
var dripOptions = TestOptions.Monitor(new DripOptions { MinDelay = TimeSpan.FromMilliseconds(5), MaxDelay = TimeSpan.FromMilliseconds(20) });
|
||||
var random = new SeededRandomSource(7);
|
||||
@@ -122,7 +179,14 @@ public sealed class TrapRunnerTests
|
||||
_governor,
|
||||
new ConfiguredStrategySelector([drip], dripOptions, random),
|
||||
_sink,
|
||||
TestOptions.Monitor(new FlypaperOptions { MaxHoldDuration = maxHoldDuration, IgnorePrivateNetworks = ignorePrivateNetworks }),
|
||||
repeatOffenders ?? new NullRepeatLookup(),
|
||||
TestOptions.Monitor(new FlypaperOptions
|
||||
{
|
||||
MaxHoldDuration = maxHoldDuration,
|
||||
IgnorePrivateNetworks = ignorePrivateNetworks,
|
||||
RepeatWindowDays = repeatWindowDays,
|
||||
RepeatHoldDuration = repeatHoldDuration ?? TimeSpan.FromHours(24)
|
||||
}),
|
||||
TimeProvider.System,
|
||||
NullLogger<TrapRunner>.Instance);
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ public sealed class McpEndpointTests : IDisposable
|
||||
var tools = await ReadAsync(await client.SendAsync(Rpc("tools/list", token)));
|
||||
var listed = tools.GetProperty("result").GetProperty("tools").EnumerateArray().ToList();
|
||||
listed.Select(t => t.GetProperty("name").GetString())
|
||||
.ShouldBe(["get_stats", "list_catches", "longest_catches", "lookup_ip", "top"], ignoreOrder: true);
|
||||
.ShouldBe(["get_stats", "list_catches", "list_entries", "list_ideas", "longest_catches", "lookup_ip", "top"], ignoreOrder: true);
|
||||
listed.ShouldAllBe(t => t.GetProperty("annotations").GetProperty("readOnlyHint").GetBoolean());
|
||||
listed.Where(t => !t.TryGetProperty("outputSchema", out var schema) || schema.ValueKind != JsonValueKind.Object).ShouldBeEmpty();
|
||||
|
||||
@@ -87,7 +87,11 @@ public sealed class McpEndpointTests : IDisposable
|
||||
{ "top", """{"dimension":"user_agents","period":"all"}""" },
|
||||
{ "longest_catches", """{"period":"all"}""" },
|
||||
{ "lookup_ip", """{"ip":"203.0.113.9"}""" },
|
||||
{ "lookup_ip", """{"ip":"192.0.2.1"}""" }
|
||||
{ "lookup_ip", """{"ip":"192.0.2.1"}""" },
|
||||
{ "list_entries", """{}""" },
|
||||
{ "list_entries", """{"kind":"experiment","strategy":"git-drip","limit":5}""" },
|
||||
{ "list_ideas", """{}""" },
|
||||
{ "list_ideas", """{"status":"in_progress"}""" }
|
||||
};
|
||||
|
||||
[Theory]
|
||||
@@ -134,6 +138,41 @@ public sealed class McpEndpointTests : IDisposable
|
||||
result.GetProperty("content")[0].GetProperty("text").GetString().ShouldNotBeNull().ShouldContain(expected, Case.Sensitive);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Write_tools_are_only_offered_to_and_usable_by_tokens_with_write_permission()
|
||||
{
|
||||
using var client = _factory.CreateClient();
|
||||
var tokens = _factory.Services.GetRequiredService<McpTokenService>();
|
||||
var readOnly = await tokens.CreateAsync("read");
|
||||
var writer = await tokens.CreateAsync("write", canWrite: true);
|
||||
|
||||
var writerTools = (await ReadAsync(await client.SendAsync(Rpc("tools/list", writer)))).GetProperty("result").GetProperty("tools")
|
||||
.EnumerateArray().Select(t => t.GetProperty("name").GetString()).ToList();
|
||||
writerTools.ShouldBe(
|
||||
["get_stats", "list_catches", "list_entries", "list_ideas", "longest_catches", "lookup_ip", "top", "create_entry", "update_entry", "create_idea", "update_idea", "set_idea_status"],
|
||||
ignoreOrder: true);
|
||||
|
||||
var denied = (await ReadAsync(await client.SendAsync(Rpc("tools/call", readOnly, new
|
||||
{
|
||||
name = "create_entry",
|
||||
arguments = new { title = "Test", kind = "observation", text = "Text" }
|
||||
})))).GetProperty("result");
|
||||
denied.GetProperty("isError").GetBoolean().ShouldBeTrue();
|
||||
denied.GetProperty("content")[0].GetProperty("text").GetString().ShouldNotBeNull().ShouldContain("write permission");
|
||||
|
||||
var created = (await ReadAsync(await client.SendAsync(Rpc("tools/call", writer, new
|
||||
{
|
||||
name = "create_entry",
|
||||
arguments = new { title = "Test", kind = "observation", text = "**Text**", strategies = new[] { "git-drip" } }
|
||||
})))).GetProperty("result");
|
||||
(created.TryGetProperty("isError", out var isError) && isError.GetBoolean()).ShouldBeFalse(created.ToString());
|
||||
created.GetProperty("structuredContent").GetProperty("kind").GetString().ShouldBe("observation");
|
||||
|
||||
var listed = (await ReadAsync(await client.SendAsync(Rpc("tools/call", readOnly, new { name = "list_entries", arguments = new { } }))))
|
||||
.GetProperty("result").GetProperty("structuredContent");
|
||||
listed.GetProperty("count").GetInt32().ShouldBe(1);
|
||||
}
|
||||
|
||||
private static async Task<Dictionary<string, JsonElement>> OutputSchemasAsync(HttpClient client, string token)
|
||||
{
|
||||
var tools = await ReadAsync(await client.SendAsync(Rpc("tools/list", token)));
|
||||
|
||||
@@ -21,7 +21,7 @@ public sealed class McpTokenServiceTests : IDisposable
|
||||
var token = await service.CreateAsync("Claude Desktop");
|
||||
|
||||
token.Length.ShouldBeGreaterThan(40);
|
||||
(await service.ValidateAsync(token)).ShouldBeTrue();
|
||||
(await service.AuthenticateAsync(token)).ShouldBe(McpAccess.Read);
|
||||
var stored = (await _store.ListAsync()).ShouldHaveSingleItem();
|
||||
stored.Name.ShouldBe("Claude Desktop");
|
||||
stored.TokenHash.ShouldBe(McpTokenService.Hash(token));
|
||||
@@ -35,12 +35,12 @@ public sealed class McpTokenServiceTests : IDisposable
|
||||
var service = Create(environmentToken: null);
|
||||
var token = await service.CreateAsync("test");
|
||||
|
||||
(await service.ValidateAsync(null)).ShouldBeFalse();
|
||||
(await service.ValidateAsync("")).ShouldBeFalse();
|
||||
(await service.ValidateAsync(token + "x")).ShouldBeFalse();
|
||||
(await service.AuthenticateAsync(null)).ShouldBeNull();
|
||||
(await service.AuthenticateAsync("")).ShouldBeNull();
|
||||
(await service.AuthenticateAsync(token + "x")).ShouldBeNull();
|
||||
|
||||
(await _store.RevokeAsync((await _store.ListAsync())[0].Id)).ShouldBeTrue();
|
||||
(await service.ValidateAsync(token)).ShouldBeFalse();
|
||||
(await service.AuthenticateAsync(token)).ShouldBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -50,9 +50,24 @@ public sealed class McpTokenServiceTests : IDisposable
|
||||
var stored = await service.CreateAsync("test");
|
||||
|
||||
service.UsesEnvironmentToken.ShouldBeTrue();
|
||||
(await service.ValidateAsync("fixed-token-from-environment")).ShouldBeTrue();
|
||||
(await service.ValidateAsync(stored)).ShouldBeTrue();
|
||||
(await service.ValidateAsync("fixed-token")).ShouldBeFalse();
|
||||
(await service.AuthenticateAsync("fixed-token-from-environment")).ShouldBe(McpAccess.Read);
|
||||
(await service.AuthenticateAsync(stored)).ShouldBe(McpAccess.Read);
|
||||
(await service.AuthenticateAsync("fixed-token")).ShouldBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Write_permission_is_granted_per_token_and_can_be_withdrawn()
|
||||
{
|
||||
var service = Create(environmentToken: null);
|
||||
var readOnly = await service.CreateAsync("read");
|
||||
var writer = await service.CreateAsync("write", canWrite: true);
|
||||
|
||||
(await service.AuthenticateAsync(readOnly)).ShouldBe(McpAccess.Read);
|
||||
(await service.AuthenticateAsync(writer)).ShouldBe(McpAccess.Write);
|
||||
|
||||
var id = (await _store.ListAsync()).Single(t => t.Name == "write").Id;
|
||||
(await _store.SetCanWriteAsync(id, false)).ShouldBeTrue();
|
||||
(await service.AuthenticateAsync(writer)).ShouldBe(McpAccess.Read);
|
||||
}
|
||||
|
||||
private McpTokenService Create(string? environmentToken) =>
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
using System.Net;
|
||||
using Flypaper.Core.Models;
|
||||
using Flypaper.Storage.Entities;
|
||||
using Flypaper.Storage.Queries;
|
||||
using Flypaper.Tests.Mcp;
|
||||
using Shouldly;
|
||||
|
||||
namespace Flypaper.Tests.Storage;
|
||||
|
||||
public sealed class RepeatOffenderLookupTests : IDisposable
|
||||
{
|
||||
private readonly McpTestData _data = new();
|
||||
|
||||
[Fact]
|
||||
public async Task Finds_only_timeouts_of_the_same_ip_inside_the_window()
|
||||
{
|
||||
using (var db = _data.Factory.CreateDbContext())
|
||||
{
|
||||
Add(db, "203.0.113.50", hoursAgo: 48, TrapOutcome.Timeout);
|
||||
Add(db, "203.0.113.51", hoursAgo: 2, TrapOutcome.ClientAborted);
|
||||
Add(db, "2001:db8::7", hoursAgo: 3, TrapOutcome.Timeout);
|
||||
Add(db, "203.0.113.52", hoursAgo: 24 * 30, TrapOutcome.Timeout);
|
||||
db.SaveChanges();
|
||||
}
|
||||
|
||||
var lookup = new RepeatOffenderLookup(_data.Factory);
|
||||
var since = McpTestData.Now.UtcDateTime.AddDays(-7);
|
||||
|
||||
(await lookup.HasTimedOutSinceAsync(IPAddress.Parse("203.0.113.50"), since, default)).ShouldBeTrue();
|
||||
(await lookup.HasTimedOutSinceAsync(IPAddress.Parse("2001:db8::7"), since, default)).ShouldBeTrue();
|
||||
(await lookup.HasTimedOutSinceAsync(IPAddress.Parse("203.0.113.51"), since, default)).ShouldBeFalse();
|
||||
(await lookup.HasTimedOutSinceAsync(IPAddress.Parse("203.0.113.52"), since, default)).ShouldBeFalse();
|
||||
(await lookup.HasTimedOutSinceAsync(IPAddress.Parse("203.0.113.99"), since, default)).ShouldBeFalse();
|
||||
}
|
||||
|
||||
private static void Add(Flypaper.Storage.FlypaperDbContext db, string ip, int hoursAgo, TrapOutcome outcome)
|
||||
{
|
||||
var started = McpTestData.Now.AddHours(-hoursAgo);
|
||||
db.TrapEvents.Add(TrapEventEntity.From(new TrapEvent(
|
||||
Guid.NewGuid(), TrapProtocol.Http, IPAddress.Parse(ip), "html-drip", started, started.AddSeconds(3600), 100, outcome, "GET /", null)));
|
||||
}
|
||||
|
||||
public void Dispose() => _data.Dispose();
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
using Flypaper.Storage.Entities;
|
||||
using Flypaper.Storage.Research;
|
||||
using Flypaper.Tests.Fakes;
|
||||
using Flypaper.Tests.Mcp;
|
||||
using Shouldly;
|
||||
|
||||
namespace Flypaper.Tests.Storage;
|
||||
|
||||
public sealed class ResearchStoreTests : IDisposable
|
||||
{
|
||||
private readonly McpTestData _data = new();
|
||||
private readonly ResearchStore _store;
|
||||
|
||||
public ResearchStoreTests() => _store = new ResearchStore(_data.Factory, new FixedTimeProvider(McpTestData.Now));
|
||||
|
||||
[Fact]
|
||||
public async Task Entries_are_listed_newest_first_and_filtered_by_kind_and_strategy()
|
||||
{
|
||||
await _store.CreateEntryAsync(new LabEntryInput("Alt", LabEntryKind.Experiment, "a", new DateTime(2026, 11, 10), Strategies: ["git-drip"]));
|
||||
await _store.CreateEntryAsync(new LabEntryInput("Neu", LabEntryKind.Observation, "b", new DateTime(2026, 11, 14), Strategies: ["maze-drip", "git-drip"]));
|
||||
await _store.CreateEntryAsync(new LabEntryInput("Heute", LabEntryKind.Experiment, "c"));
|
||||
|
||||
(await _store.ListEntriesAsync()).Select(e => e.Title).ShouldBe(["Heute", "Neu", "Alt"]);
|
||||
(await _store.ListEntriesAsync(LabEntryKind.Experiment)).Select(e => e.Title).ShouldBe(["Heute", "Alt"]);
|
||||
(await _store.ListEntriesAsync(strategy: "GIT-drip")).Select(e => e.Title).ShouldBe(["Neu", "Alt"]);
|
||||
(await _store.ListEntriesAsync(limit: 1)).ShouldHaveSingleItem().Title.ShouldBe("Heute");
|
||||
(await _store.ListEntriesAsync()).First().Date.ShouldBe(McpTestData.Now.UtcDateTime.Date);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Update_changes_only_given_fields_and_empty_values_clear_optional_ones()
|
||||
{
|
||||
var entry = await _store.CreateEntryAsync(new LabEntryInput("Titel", LabEntryKind.Hypothesis, "Text", Commit: "ABCDEF1", Strategies: ["git-drip"]));
|
||||
entry.Commit.ShouldBe("abcdef1");
|
||||
|
||||
var changed = await _store.UpdateEntryAsync(entry.Id, new LabEntryInput(Text: "Neu"));
|
||||
changed.ShouldNotBeNull();
|
||||
(changed.Title, changed.Text, changed.Commit, changed.Strategies).ShouldBe(("Titel", "Neu", "abcdef1", "git-drip"));
|
||||
|
||||
var cleared = await _store.UpdateEntryAsync(entry.Id, new LabEntryInput(Commit: "", Strategies: []));
|
||||
cleared.ShouldNotBeNull();
|
||||
(cleared.Commit, cleared.Strategies).ShouldBe((null, null));
|
||||
|
||||
(await _store.UpdateEntryAsync(999, new LabEntryInput(Title: "x"))).ShouldBeNull();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("", "Text", "abc1", "git-drip")]
|
||||
[InlineData("Titel", " ", "abc1", "git-drip")]
|
||||
[InlineData("Titel", "Text", "not-a-hash", "git-drip")]
|
||||
[InlineData("Titel", "Text", "abc1", "Git Drip!")]
|
||||
public async Task Invalid_entries_are_rejected(string title, string text, string commit, string strategy) =>
|
||||
await Should.ThrowAsync<ArgumentException>(() =>
|
||||
_store.CreateEntryAsync(new LabEntryInput(title, LabEntryKind.Method, text, Commit: commit, Strategies: [strategy])));
|
||||
|
||||
[Fact]
|
||||
public async Task Ideas_are_sorted_by_effort_then_efficiency_and_status_sets_implementation_date()
|
||||
{
|
||||
var hard = await _store.CreateIdeaAsync(new IdeaInput("Schwer", IdeaLevel.High, IdeaLevel.High, "r"));
|
||||
var cheap = await _store.CreateIdeaAsync(new IdeaInput("Billig", IdeaLevel.Low, IdeaLevel.Medium, "r"));
|
||||
var cheapBetter = await _store.CreateIdeaAsync(new IdeaInput("Billig und gut", IdeaLevel.Low, IdeaLevel.High, "r"));
|
||||
|
||||
(await _store.ListIdeasAsync()).Select(i => i.Title).ShouldBe(["Billig und gut", "Billig", "Schwer"]);
|
||||
|
||||
var done = await _store.UpdateIdeaAsync(cheap.Id, new IdeaInput(Status: IdeaStatus.Done, Commit: "abc123"));
|
||||
done.ShouldNotBeNull();
|
||||
(done.Status, done.ImplementedOn, done.Commit).ShouldBe((IdeaStatus.Done, McpTestData.Now.UtcDateTime.Date, "abc123"));
|
||||
|
||||
var reopened = await _store.UpdateIdeaAsync(cheap.Id, new IdeaInput(Status: IdeaStatus.Open));
|
||||
reopened.ShouldNotBeNull().ImplementedOn.ShouldBeNull();
|
||||
|
||||
(await _store.ListIdeasAsync(IdeaStatus.Open)).Count.ShouldBe(3);
|
||||
hard.Status.ShouldBe(IdeaStatus.Open);
|
||||
cheapBetter.Status.ShouldBe(IdeaStatus.Open);
|
||||
(await _store.UpdateIdeaAsync(999, new IdeaInput(Status: IdeaStatus.Done))).ShouldBeNull();
|
||||
}
|
||||
|
||||
public void Dispose() => _data.Dispose();
|
||||
}
|
||||
@@ -89,7 +89,7 @@ public sealed class SettingsOverrideTests : IDisposable
|
||||
var store = host.Services.GetRequiredService<RuntimeSettingsStore>();
|
||||
string[] holdKeys = [.. SettingKeys.HoldMethod];
|
||||
|
||||
await store.SaveAsync(new HoldMethodSettings(DripContent.Text, TimeSpan.FromMilliseconds(200), TimeSpan.FromSeconds(2), 8, 16, 256, false, 0, "/trap-a/", TimeSpan.FromMinutes(5)));
|
||||
await store.SaveAsync(new HoldMethodSettings(DripContent.Text, TimeSpan.FromMilliseconds(200), TimeSpan.FromSeconds(2), 8, 16, 256, false, 0, "/trap-a/", TimeSpan.FromMinutes(5), 7, TimeSpan.FromHours(24)));
|
||||
await store.SaveAsync(new RuntimeSettings(50, 4, 90, true));
|
||||
|
||||
drip.CurrentValue.Content.ShouldBe(DripContent.Text);
|
||||
@@ -103,7 +103,7 @@ public sealed class SettingsOverrideTests : IDisposable
|
||||
core.CurrentValue.MaxConnections.ShouldBe(50);
|
||||
(await store.HasOverridesAsync(holdKeys)).ShouldBeTrue();
|
||||
|
||||
await store.SaveAsync(new HoldMethodSettings(DripContent.Text, TimeSpan.FromMilliseconds(200), TimeSpan.FromSeconds(2), 8, 16, 256, false, 0, "", TimeSpan.FromMinutes(5)));
|
||||
await store.SaveAsync(new HoldMethodSettings(DripContent.Text, TimeSpan.FromMilliseconds(200), TimeSpan.FromSeconds(2), 8, 16, 256, false, 0, "", TimeSpan.FromMinutes(5), 0, TimeSpan.FromHours(2)));
|
||||
drip.CurrentValue.BaitPaths.ShouldBe("");
|
||||
|
||||
await store.ResetAsync(holdKeys);
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
using Flypaper.Web.Components.Shared;
|
||||
using Shouldly;
|
||||
|
||||
namespace Flypaper.Tests.Web;
|
||||
|
||||
public sealed class MarkdownRendererTests
|
||||
{
|
||||
[Fact]
|
||||
public void Renders_basic_markdown()
|
||||
{
|
||||
var html = MarkdownRenderer.Render("# Titel\n\n**fett** und `code`\n\n- eins\n- zwei").Value;
|
||||
|
||||
html.ShouldContain("<h1>Titel</h1>");
|
||||
html.ShouldContain("<strong>fett</strong>");
|
||||
html.ShouldContain("<code>code</code>");
|
||||
html.ShouldContain("<li>eins</li>");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("<script>alert(1)</script>")]
|
||||
[InlineData("<img src=x onerror=alert(1)>")]
|
||||
[InlineData("text <b onclick=\"x()\">fett</b>")]
|
||||
public void Raw_html_is_escaped(string markdown)
|
||||
{
|
||||
var html = MarkdownRenderer.Render(markdown).Value;
|
||||
|
||||
html.ShouldNotContain("<script");
|
||||
html.ShouldNotContain("<img");
|
||||
html.ShouldNotContain("<b ");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("[x](javascript:alert(1))")]
|
||||
[InlineData("[x](JaVaScRiPt:alert(1))")]
|
||||
[InlineData("[x](data:text/html;base64,AAAA)")]
|
||||
[InlineData(")")]
|
||||
[InlineData("<javascript:alert(1)>")]
|
||||
public void Unsafe_link_schemes_are_neutralized(string markdown) =>
|
||||
MarkdownRenderer.Render(markdown).Value.ToLowerInvariant().ShouldNotContain("javascript:");
|
||||
|
||||
[Theory]
|
||||
[InlineData("[x](https://example.com/a)", "https://example.com/a")]
|
||||
[InlineData("[x](/erkenntnisse?art=experiment)", "/erkenntnisse?art=experiment")]
|
||||
[InlineData("[x](mailto:a@example.com)", "mailto:a@example.com")]
|
||||
public void Safe_links_are_kept(string markdown, string url) =>
|
||||
MarkdownRenderer.Render(markdown).Value.ShouldContain($"href=\"{url}\"");
|
||||
|
||||
[Fact]
|
||||
public void Empty_input_renders_nothing() => MarkdownRenderer.Render(null).Value.ShouldBeEmpty();
|
||||
}
|
||||
Reference in new issue
Block a user