From bda7ffaae83ab4942f5dcb2aab628d91180f375f Mon Sep 17 00:00:00 2001 From: iven <99438726+ivenos@users.noreply.github.com> Date: Wed, 7 Oct 2026 18:47:59 +0200 Subject: Return the created API key from POST /Auth/Keys (#17155) * Return the created API key from POST /Auth/Keys * Keep the other API key fields in the DTO --- .../Security/AuthenticationManagerTests.cs | 80 ++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 tests/Jellyfin.Server.Implementations.Tests/Security/AuthenticationManagerTests.cs (limited to 'tests/Jellyfin.Server.Implementations.Tests/Security/AuthenticationManagerTests.cs') diff --git a/tests/Jellyfin.Server.Implementations.Tests/Security/AuthenticationManagerTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Security/AuthenticationManagerTests.cs new file mode 100644 index 0000000000..e46b88c67e --- /dev/null +++ b/tests/Jellyfin.Server.Implementations.Tests/Security/AuthenticationManagerTests.cs @@ -0,0 +1,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 _dbOptions; + private readonly AuthenticationManager _authenticationManager; + + public AuthenticationManagerTests() + { + _connection = new SqliteConnection("Data Source=:memory:"); + _connection.Open(); + + _dbOptions = new DbContextOptionsBuilder() + .UseSqlite(_connection) + .Options; + + // Create the schema + using var ctx = CreateDbContext(); + ctx.Database.EnsureCreated(); + + var factory = new Mock>(); + factory.Setup(f => f.CreateDbContext()).Returns(CreateDbContext); + factory.Setup(f => f.CreateDbContextAsync(It.IsAny())) + .ReturnsAsync(CreateDbContext); + + _authenticationManager = new AuthenticationManager(factory.Object); + } + + public void Dispose() + { + _connection.Dispose(); + } + + private JellyfinDbContext CreateDbContext() + { + return new JellyfinDbContext( + _dbOptions, + NullLogger.Instance, + new SqliteDatabaseProvider(null!, NullLogger.Instance), + new NoLockBehavior(NullLogger.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); + } + } +} -- cgit v1.2.3