-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMcpApiTokenServiceTests.cs
More file actions
54 lines (42 loc) · 2.21 KB
/
McpApiTokenServiceTests.cs
File metadata and controls
54 lines (42 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using EssentialCSharp.Web.Services;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
namespace EssentialCSharp.Web.Tests;
[NotInParallel("McpTests")]
[ClassDataSource<WebApplicationFactory>(Shared = SharedType.PerClass)]
public class McpApiTokenServiceTests(WebApplicationFactory factory)
{
[Test]
public async Task CreateTokenAsync_WithoutExpiry_UsesSixMonthDefault()
{
string userId = await McpTestHelper.CreateUserAsync(factory, "mcp-default-expiry");
using var scope = factory.Services.CreateScope();
var tokenService = scope.ServiceProvider.GetRequiredService<McpApiTokenService>();
(_, var entity) = await tokenService.CreateTokenAsync(userId, "default-expiry");
await Assert.That(entity.ExpiresAt).IsNotNull();
await Assert.That(entity.ExpiresAt!.Value)
.IsEqualTo(McpApiTokenService.GetDefaultExpirationUtc(entity.CreatedAt));
}
[Test]
public async Task CreateTokenAsync_WithExpiryWithinSixMonths_UsesRequestedExpiry()
{
string userId = await McpTestHelper.CreateUserAsync(factory, "mcp-custom-expiry");
using var scope = factory.Services.CreateScope();
var tokenService = scope.ServiceProvider.GetRequiredService<McpApiTokenService>();
DateTime requestedExpiry = DateTime.UtcNow.AddMonths(3);
(_, var entity) = await tokenService.CreateTokenAsync(userId, "custom-expiry", requestedExpiry);
await Assert.That(entity.ExpiresAt).IsNotNull();
await Assert.That(entity.ExpiresAt!.Value).IsEqualTo(requestedExpiry);
}
[Test]
public async Task CreateTokenAsync_WithExpiryBeyondSixMonths_Throws()
{
string userId = await McpTestHelper.CreateUserAsync(factory, "mcp-max-expiry");
using var scope = factory.Services.CreateScope();
var tokenService = scope.ServiceProvider.GetRequiredService<McpApiTokenService>();
DateTime requestedExpiry = McpApiTokenService.GetDefaultExpirationUtc(DateTime.UtcNow).AddDays(1);
await Assert.That(() => tokenService.CreateTokenAsync(userId, "too-long", requestedExpiry))
.Throws<ArgumentOutOfRangeException>()
.WithMessageContaining("6 months");
}
}