Use block-scoped namespaces in the test suite
Converts all 73 test source files from file-scoped (namespace X;) to
block-scoped (namespace X { ... }) declarations, re-indenting their
contents accordingly. Matches the existing convention already used in
several production files (e.g. Musify.Infrastructure.Configuration,
Musify.Application.Contracts).
Mechanical change only, done with a script, verified with a full
rebuild (0 warnings, 0 errors) and test run (257/257 passing).
This commit is contained in:
@@ -5,67 +5,68 @@ using Musify.Infrastructure.Configuration;
|
||||
using Musify.Infrastructure.Services;
|
||||
using Xunit;
|
||||
|
||||
namespace Musify.Infrastructure.Tests.Services;
|
||||
|
||||
public sealed class StreamTicketServiceTests : IDisposable
|
||||
namespace Musify.Infrastructure.Tests.Services
|
||||
{
|
||||
private readonly string privateKeyPath = Path.GetTempFileName();
|
||||
private readonly RSA rsa = RSA.Create(2048);
|
||||
|
||||
public StreamTicketServiceTests() => File.WriteAllText(privateKeyPath, rsa.ExportPkcs8PrivateKeyPem());
|
||||
|
||||
public void Dispose()
|
||||
public sealed class StreamTicketServiceTests : IDisposable
|
||||
{
|
||||
rsa.Dispose();
|
||||
File.Delete(privateKeyPath);
|
||||
}
|
||||
private readonly string privateKeyPath = Path.GetTempFileName();
|
||||
private readonly RSA rsa = RSA.Create(2048);
|
||||
|
||||
private StreamTicketService CreateService(int ticketTtlSeconds = 3600) => new(new StreamTicketConfiguration
|
||||
{
|
||||
PrivateKeyPath = privateKeyPath,
|
||||
Audience = "media-gateway",
|
||||
Issuer = "musify-webapi",
|
||||
TicketTtlSeconds = ticketTtlSeconds
|
||||
});
|
||||
public StreamTicketServiceTests() => File.WriteAllText(privateKeyPath, rsa.ExportPkcs8PrivateKeyPem());
|
||||
|
||||
[Fact]
|
||||
public void IssueTicket_ReturnsATokenSignedWithTheConfiguredKeyAndClaims()
|
||||
{
|
||||
using var service = CreateService(ticketTtlSeconds: 120);
|
||||
|
||||
var ticket = service.IssueTicket(userId: 42, keyPrefix: "Tracks/ProcessedAudios/abc/");
|
||||
|
||||
Assert.Equal(120, ticket.ExpiresInSeconds);
|
||||
|
||||
var handler = new JsonWebTokenHandler();
|
||||
var jwt = handler.ReadJsonWebToken(ticket.Token);
|
||||
Assert.Equal("42", jwt.GetClaim("sub").Value);
|
||||
Assert.Equal("Tracks/ProcessedAudios/abc/", jwt.GetClaim("prefix").Value);
|
||||
Assert.Equal("musify-webapi", jwt.Issuer);
|
||||
Assert.Contains("media-gateway", jwt.Audiences);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task IssueTicket_TokenValidatesAgainstTheMatchingPublicKey()
|
||||
{
|
||||
using var service = CreateService();
|
||||
var ticket = service.IssueTicket(userId: 1, keyPrefix: "Tracks/ProcessedAudios/xyz/");
|
||||
|
||||
var publicKeyPem = rsa.ExportSubjectPublicKeyInfoPem();
|
||||
using var verifyingRsa = RSA.Create();
|
||||
verifyingRsa.ImportFromPem(publicKeyPem);
|
||||
|
||||
var handler = new JsonWebTokenHandler();
|
||||
var result = await handler.ValidateTokenAsync(ticket.Token, new TokenValidationParameters
|
||||
public void Dispose()
|
||||
{
|
||||
ValidIssuer = "musify-webapi",
|
||||
ValidAudience = "media-gateway",
|
||||
ValidateIssuer = true,
|
||||
ValidateAudience = true,
|
||||
ValidateLifetime = true,
|
||||
IssuerSigningKey = new RsaSecurityKey(verifyingRsa),
|
||||
rsa.Dispose();
|
||||
File.Delete(privateKeyPath);
|
||||
}
|
||||
|
||||
private StreamTicketService CreateService(int ticketTtlSeconds = 3600) => new(new StreamTicketConfiguration
|
||||
{
|
||||
PrivateKeyPath = privateKeyPath,
|
||||
Audience = "media-gateway",
|
||||
Issuer = "musify-webapi",
|
||||
TicketTtlSeconds = ticketTtlSeconds
|
||||
});
|
||||
|
||||
Assert.True(result.IsValid);
|
||||
[Fact]
|
||||
public void IssueTicket_ReturnsATokenSignedWithTheConfiguredKeyAndClaims()
|
||||
{
|
||||
using var service = CreateService(ticketTtlSeconds: 120);
|
||||
|
||||
var ticket = service.IssueTicket(userId: 42, keyPrefix: "Tracks/ProcessedAudios/abc/");
|
||||
|
||||
Assert.Equal(120, ticket.ExpiresInSeconds);
|
||||
|
||||
var handler = new JsonWebTokenHandler();
|
||||
var jwt = handler.ReadJsonWebToken(ticket.Token);
|
||||
Assert.Equal("42", jwt.GetClaim("sub").Value);
|
||||
Assert.Equal("Tracks/ProcessedAudios/abc/", jwt.GetClaim("prefix").Value);
|
||||
Assert.Equal("musify-webapi", jwt.Issuer);
|
||||
Assert.Contains("media-gateway", jwt.Audiences);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task IssueTicket_TokenValidatesAgainstTheMatchingPublicKey()
|
||||
{
|
||||
using var service = CreateService();
|
||||
var ticket = service.IssueTicket(userId: 1, keyPrefix: "Tracks/ProcessedAudios/xyz/");
|
||||
|
||||
var publicKeyPem = rsa.ExportSubjectPublicKeyInfoPem();
|
||||
using var verifyingRsa = RSA.Create();
|
||||
verifyingRsa.ImportFromPem(publicKeyPem);
|
||||
|
||||
var handler = new JsonWebTokenHandler();
|
||||
var result = await handler.ValidateTokenAsync(ticket.Token, new TokenValidationParameters
|
||||
{
|
||||
ValidIssuer = "musify-webapi",
|
||||
ValidAudience = "media-gateway",
|
||||
ValidateIssuer = true,
|
||||
ValidateAudience = true,
|
||||
ValidateLifetime = true,
|
||||
IssuerSigningKey = new RsaSecurityKey(verifyingRsa),
|
||||
});
|
||||
|
||||
Assert.True(result.IsValid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user