mirror of
https://github.com/bitwarden/server
synced 2025-12-19 09:43:25 +00:00
Inject playId services in tests and don't fail if it's not found
This commit is contained in:
@@ -14,16 +14,31 @@ public class PlayIdService(IHostEnvironment hostEnvironment) : IPlayIdService
|
||||
}
|
||||
}
|
||||
|
||||
public class NeverPlayIdServices : IPlayIdService
|
||||
{
|
||||
public string? PlayId
|
||||
{
|
||||
get => null;
|
||||
set { }
|
||||
}
|
||||
|
||||
public bool InPlay(out string playId)
|
||||
{
|
||||
playId = string.Empty;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public class PlayIdSingletonService(IHttpContextAccessor httpContextAccessor, IHostEnvironment hostEnvironment) : IPlayIdService
|
||||
{
|
||||
private PlayIdService Current
|
||||
private IPlayIdService Current
|
||||
{
|
||||
get
|
||||
{
|
||||
var httpContext = httpContextAccessor.HttpContext;
|
||||
if (httpContext == null)
|
||||
{
|
||||
throw new InvalidOperationException("HttpContext is not available");
|
||||
return new NeverPlayIdServices();
|
||||
}
|
||||
return httpContext.RequestServices.GetRequiredService<PlayIdService>();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Reflection;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Services;
|
||||
using Bit.Core.Settings;
|
||||
using Bit.Infrastructure.Dapper;
|
||||
using Bit.Infrastructure.EntityFramework;
|
||||
@@ -124,6 +125,13 @@ public class DatabaseDataAttribute : DataAttribute
|
||||
{
|
||||
services.AddSingleton<TimeProvider, FakeTimeProvider>();
|
||||
}
|
||||
|
||||
// Include PlayIdService for tracking Play Ids in repositories
|
||||
// We need the http context accessor to use the Singleton version, which pulls from the scoped version
|
||||
services.AddHttpContextAccessor();
|
||||
|
||||
services.AddSingleton<IPlayIdService, PlayIdSingletonService>();
|
||||
services.AddScoped<PlayIdService>();
|
||||
}
|
||||
|
||||
private void AddDapperServices(IServiceCollection services, Database database)
|
||||
|
||||
Reference in New Issue
Block a user