1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-24 12:35:25 +01:00
bitwarden-server/test/Core.Test/AutoFixture/GlobalSettingsFixtures.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

57 lines
1.7 KiB
C#
Raw Normal View History

using System.Reflection;
using System.Text;
using AutoFixture;
using AutoFixture.Kernel;
using AutoFixture.Xunit2;
using Bit.Core;
using Bit.Core.Test.Helpers.Factories;
using Microsoft.AspNetCore.DataProtection;
2023-08-10 17:03:42 +02:00
using NSubstitute;
namespace Bit.Test.Common.AutoFixture;
2022-08-29 22:06:55 +02:00
public class GlobalSettingsBuilder : ISpecimenBuilder
{
public object Create(object request, ISpecimenContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
var fixture = new Fixture();
if (request is not ParameterInfo pi)
{
return new NoSpecimen();
}
if (pi.ParameterType == typeof(Bit.Core.Settings.GlobalSettings))
{
return GlobalSettingsFactory.GlobalSettings;
}
if (pi.ParameterType == typeof(IDataProtectionProvider))
{
2023-08-10 17:03:42 +02:00
var dataProtector = Substitute.For<IDataProtector>();
dataProtector.Unprotect(Arg.Any<byte[]>())
.Returns(data =>
Encoding.UTF8.GetBytes(Constants.DatabaseFieldProtectedPrefix +
Encoding.UTF8.GetString((byte[])data[0])));
2023-08-10 17:03:42 +02:00
var dataProtectionProvider = Substitute.For<IDataProtectionProvider>();
dataProtectionProvider.CreateProtector(Constants.DatabaseFieldProtectorPurpose)
.Returns(dataProtector);
2023-08-10 17:03:42 +02:00
return dataProtectionProvider;
}
return new NoSpecimen();
}
2022-08-29 22:06:55 +02:00
}
public class GlobalSettingsCustomizeAttribute : CustomizeAttribute
{
public override ICustomization GetCustomization(ParameterInfo parameter) => new GlobalSettings();
}