Files
Flypaper/test/Flypaper.Tests/Mcp/McpTokenServiceTests.cs
T
TomandClaude Opus 5.5 e88dc415f9
Build and deploy / build (push) Successful in 1m50s
Build and deploy / deploy (push) Successful in 21s
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>
2026-11-17 18:27:35 +01:00

78 lines
3.0 KiB
C#

using Flypaper.Storage.Settings;
using Flypaper.Tests.Fakes;
using Flypaper.Web.Mcp;
using Microsoft.Extensions.Options;
using Shouldly;
namespace Flypaper.Tests.Mcp;
public sealed class McpTokenServiceTests : IDisposable
{
private readonly McpTestData _data = new();
private readonly McpTokenStore _store;
public McpTokenServiceTests() => _store = new McpTokenStore(_data.Factory, new FixedTimeProvider(McpTestData.Now));
[Fact]
public async Task Created_token_is_valid_and_stored_only_as_hash()
{
var service = Create(environmentToken: null);
var token = await service.CreateAsync("Claude Desktop");
token.Length.ShouldBeGreaterThan(40);
(await service.AuthenticateAsync(token)).ShouldBe(McpAccess.Read);
var stored = (await _store.ListAsync()).ShouldHaveSingleItem();
stored.Name.ShouldBe("Claude Desktop");
stored.TokenHash.ShouldBe(McpTokenService.Hash(token));
stored.TokenHash.ShouldNotContain(token);
stored.LastUsedAt.ShouldBe(McpTestData.Now.UtcDateTime);
}
[Fact]
public async Task Wrong_missing_and_revoked_tokens_are_rejected()
{
var service = Create(environmentToken: null);
var token = await service.CreateAsync("test");
(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.AuthenticateAsync(token)).ShouldBeNull();
}
[Fact]
public async Task Environment_token_is_accepted_alongside_stored_ones()
{
var service = Create(environmentToken: "fixed-token-from-environment");
var stored = await service.CreateAsync("test");
service.UsesEnvironmentToken.ShouldBeTrue();
(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) =>
new(_store, Options.Create(new McpOptions { Token = environmentToken }));
public void Dispose() => _data.Dispose();
}