blob: e46b88c67e9c2842bc95e9d0769ae497933edfe8 (
plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
using System;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Database.Implementations;
using Jellyfin.Database.Implementations.Locking;
using Jellyfin.Database.Providers.Sqlite;
using Jellyfin.Server.Implementations.Security;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Xunit;
namespace Jellyfin.Server.Implementations.Tests.Security
{
public sealed class AuthenticationManagerTests : IDisposable
{
private readonly SqliteConnection _connection;
private readonly DbContextOptions<JellyfinDbContext> _dbOptions;
private readonly AuthenticationManager _authenticationManager;
public AuthenticationManagerTests()
{
_connection = new SqliteConnection("Data Source=:memory:");
_connection.Open();
_dbOptions = new DbContextOptionsBuilder<JellyfinDbContext>()
.UseSqlite(_connection)
.Options;
// Create the schema
using var ctx = CreateDbContext();
ctx.Database.EnsureCreated();
var factory = new Mock<IDbContextFactory<JellyfinDbContext>>();
factory.Setup(f => f.CreateDbContext()).Returns(CreateDbContext);
factory.Setup(f => f.CreateDbContextAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(CreateDbContext);
_authenticationManager = new AuthenticationManager(factory.Object);
}
public void Dispose()
{
_connection.Dispose();
}
private JellyfinDbContext CreateDbContext()
{
return new JellyfinDbContext(
_dbOptions,
NullLogger<JellyfinDbContext>.Instance,
new SqliteDatabaseProvider(null!, NullLogger<SqliteDatabaseProvider>.Instance),
new NoLockBehavior(NullLogger<NoLockBehavior>.Instance));
}
[Fact]
public async Task CreateApiKey_ReturnsInfoMatchingStoredKey()
{
var created = await _authenticationManager.CreateApiKey("test-app");
Assert.Equal("test-app", created.AppName);
Assert.False(string.IsNullOrEmpty(created.AccessToken));
var keys = await _authenticationManager.GetApiKeys();
var stored = Assert.Single(keys);
Assert.Equal(created.AppName, stored.AppName);
Assert.Equal(created.AccessToken, stored.AccessToken);
}
[Fact]
public async Task CreateApiKey_WithMultipleKeys_ReturnsDistinctTokens()
{
var first = await _authenticationManager.CreateApiKey("app1");
var second = await _authenticationManager.CreateApiKey("app2");
Assert.NotEqual(first.AccessToken, second.AccessToken);
}
}
}
|