63 lines
2.3 KiB
C#
63 lines
2.3 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.ValidateAsync(token)).ShouldBeTrue();
|
|
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.ValidateAsync(null)).ShouldBeFalse();
|
|
(await service.ValidateAsync("")).ShouldBeFalse();
|
|
(await service.ValidateAsync(token + "x")).ShouldBeFalse();
|
|
|
|
(await _store.RevokeAsync((await _store.ListAsync())[0].Id)).ShouldBeTrue();
|
|
(await service.ValidateAsync(token)).ShouldBeFalse();
|
|
}
|
|
|
|
[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.ValidateAsync("fixed-token-from-environment")).ShouldBeTrue();
|
|
(await service.ValidateAsync(stored)).ShouldBeTrue();
|
|
(await service.ValidateAsync("fixed-token")).ShouldBeFalse();
|
|
}
|
|
|
|
private McpTokenService Create(string? environmentToken) =>
|
|
new(_store, Options.Create(new McpOptions { Token = environmentToken }));
|
|
|
|
public void Dispose() => _data.Dispose();
|
|
}
|