mirror of
https://github.com/bitwarden/server.git
synced 2025-07-02 08:32:50 -05:00
Create common test infrastructure project
This commit is contained in:
@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using AutoFixture;
|
||||
using AutoFixture.Xunit2;
|
||||
|
||||
namespace Bit.Test.Common.AutoFixture.Attributes
|
||||
{
|
||||
public class CustomAutoDataAttribute : AutoDataAttribute
|
||||
{
|
||||
public CustomAutoDataAttribute(params Type[] iCustomizationTypes) : this(iCustomizationTypes
|
||||
.Select(t => (ICustomization)Activator.CreateInstance(t)).ToArray())
|
||||
{ }
|
||||
|
||||
public CustomAutoDataAttribute(params ICustomization[] customizations) : base(() =>
|
||||
{
|
||||
var fixture = new Fixture();
|
||||
foreach (var customization in customizations)
|
||||
{
|
||||
fixture.Customize(customization);
|
||||
}
|
||||
return fixture;
|
||||
})
|
||||
{ }
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using Xunit;
|
||||
using Xunit.Sdk;
|
||||
using AutoFixture.Xunit2;
|
||||
using AutoFixture;
|
||||
|
||||
namespace Bit.Test.Common.AutoFixture.Attributes
|
||||
{
|
||||
public class InlineCustomAutoDataAttribute : CompositeDataAttribute
|
||||
{
|
||||
public InlineCustomAutoDataAttribute(Type[] iCustomizationTypes, params object[] values) : base(new DataAttribute[] {
|
||||
new InlineDataAttribute(values),
|
||||
new CustomAutoDataAttribute(iCustomizationTypes)
|
||||
})
|
||||
{ }
|
||||
|
||||
public InlineCustomAutoDataAttribute(ICustomization[] customizations, params object[] values) : base(new DataAttribute[] {
|
||||
new InlineDataAttribute(values),
|
||||
new CustomAutoDataAttribute(customizations)
|
||||
})
|
||||
{ }
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using AutoFixture;
|
||||
|
||||
namespace Bit.Test.Common.AutoFixture.Attributes
|
||||
{
|
||||
public class InlineSutAutoDataAttribute : InlineCustomAutoDataAttribute
|
||||
{
|
||||
public InlineSutAutoDataAttribute(params object[] values) : base(
|
||||
new Type[] { typeof(SutProviderCustomization) }, values)
|
||||
{ }
|
||||
public InlineSutAutoDataAttribute(Type[] iCustomizationTypes, params object[] values) : base(
|
||||
iCustomizationTypes.Append(typeof(SutProviderCustomization)).ToArray(), values)
|
||||
{ }
|
||||
|
||||
public InlineSutAutoDataAttribute(ICustomization[] customizations, params object[] values) : base(
|
||||
customizations.Append(new SutProviderCustomization()).ToArray(), values)
|
||||
{ }
|
||||
}
|
||||
}
|
15
test/Common/AutoFixture/Attributes/SutAutoDataAttribute.cs
Normal file
15
test/Common/AutoFixture/Attributes/SutAutoDataAttribute.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using AutoFixture;
|
||||
using AutoFixture.Xunit2;
|
||||
|
||||
namespace Bit.Test.Common.AutoFixture.Attributes
|
||||
{
|
||||
public class SutAutoDataAttribute : CustomAutoDataAttribute
|
||||
{
|
||||
public SutAutoDataAttribute(params Type[] iCustomizationTypes) : base(
|
||||
iCustomizationTypes.Append(typeof(SutProviderCustomization)).ToArray())
|
||||
{ }
|
||||
}
|
||||
}
|
11
test/Common/AutoFixture/FixtureExtensions.cs
Normal file
11
test/Common/AutoFixture/FixtureExtensions.cs
Normal file
@ -0,0 +1,11 @@
|
||||
using AutoFixture;
|
||||
using AutoFixture.AutoNSubstitute;
|
||||
|
||||
namespace Bit.Test.Common.AutoFixture
|
||||
{
|
||||
public static class FixtureExtensions
|
||||
{
|
||||
public static IFixture WithAutoNSubstitutions(this IFixture fixture)
|
||||
=> fixture.Customize(new AutoNSubstituteCustomization());
|
||||
}
|
||||
}
|
45
test/Common/AutoFixture/GlobalSettingsFixtures.cs
Normal file
45
test/Common/AutoFixture/GlobalSettingsFixtures.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using AutoFixture;
|
||||
using AutoFixture.Kernel;
|
||||
using AutoFixture.Xunit2;
|
||||
using Bit.Test.Common.Helpers.Factories;
|
||||
|
||||
namespace Bit.Test.Common.AutoFixture
|
||||
{
|
||||
public class GlobalSettingsBuilder : ISpecimenBuilder
|
||||
{
|
||||
public object Create(object request, ISpecimenContext context)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
var pi = request as ParameterInfo;
|
||||
var fixture = new Fixture();
|
||||
|
||||
if (pi == null || pi.ParameterType != typeof(Bit.Core.Settings.GlobalSettings))
|
||||
return new NoSpecimen();
|
||||
|
||||
return GlobalSettingsFactory.GlobalSettings;
|
||||
}
|
||||
}
|
||||
|
||||
public class GlobalSettings : ICustomization
|
||||
{
|
||||
public void Customize(IFixture fixture)
|
||||
{
|
||||
fixture.Customize<Bit.Core.Settings.GlobalSettings>(composer => composer
|
||||
.Without(s => s.BaseServiceUri)
|
||||
.Without(s => s.Attachment)
|
||||
.Without(s => s.Send)
|
||||
.Without(s => s.DataProtection));
|
||||
}
|
||||
}
|
||||
|
||||
public class GlobalSettingsCustomizeAttribute : CustomizeAttribute
|
||||
{
|
||||
public override ICustomization GetCustomization(ParameterInfo parameter) => new GlobalSettings();
|
||||
}
|
||||
}
|
10
test/Common/AutoFixture/ISutProvider.cs
Normal file
10
test/Common/AutoFixture/ISutProvider.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using System;
|
||||
|
||||
namespace Bit.Test.Common.AutoFixture
|
||||
{
|
||||
public interface ISutProvider
|
||||
{
|
||||
Type SutType { get; }
|
||||
ISutProvider Create();
|
||||
}
|
||||
}
|
132
test/Common/AutoFixture/SutProvider.cs
Normal file
132
test/Common/AutoFixture/SutProvider.cs
Normal file
@ -0,0 +1,132 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using AutoFixture;
|
||||
using AutoFixture.Kernel;
|
||||
using System.Reflection;
|
||||
using System.Linq;
|
||||
|
||||
namespace Bit.Test.Common.AutoFixture
|
||||
{
|
||||
public class SutProvider<TSut> : ISutProvider
|
||||
{
|
||||
private Dictionary<Type, Dictionary<string, object>> _dependencies;
|
||||
private readonly IFixture _fixture;
|
||||
private readonly ConstructorParameterRelay<TSut> _constructorParameterRelay;
|
||||
|
||||
public TSut Sut { get; private set; }
|
||||
public Type SutType => typeof(TSut);
|
||||
|
||||
public SutProvider() : this(new Fixture()) { }
|
||||
|
||||
public SutProvider(IFixture fixture)
|
||||
{
|
||||
_dependencies = new Dictionary<Type, Dictionary<string, object>>();
|
||||
_fixture = (fixture ?? new Fixture()).WithAutoNSubstitutions().Customize(new GlobalSettings());
|
||||
_constructorParameterRelay = new ConstructorParameterRelay<TSut>(this, _fixture);
|
||||
_fixture.Customizations.Add(_constructorParameterRelay);
|
||||
}
|
||||
|
||||
public SutProvider<TSut> SetDependency<T>(T dependency, string parameterName = "")
|
||||
=> SetDependency(typeof(T), dependency, parameterName);
|
||||
public SutProvider<TSut> SetDependency(Type dependencyType, object dependency, string parameterName = "")
|
||||
{
|
||||
if (_dependencies.ContainsKey(dependencyType))
|
||||
{
|
||||
_dependencies[dependencyType][parameterName] = dependency;
|
||||
}
|
||||
else
|
||||
{
|
||||
_dependencies[dependencyType] = new Dictionary<string, object> { { parameterName, dependency } };
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public T GetDependency<T>(string parameterName = "") => (T)GetDependency(typeof(T), parameterName);
|
||||
public object GetDependency(Type dependencyType, string parameterName = "")
|
||||
{
|
||||
if (DependencyIsSet(dependencyType, parameterName))
|
||||
{
|
||||
return _dependencies[dependencyType][parameterName];
|
||||
}
|
||||
else if (_dependencies.ContainsKey(dependencyType))
|
||||
{
|
||||
var knownDependencies = _dependencies[dependencyType];
|
||||
if (knownDependencies.Values.Count == 1)
|
||||
{
|
||||
return _dependencies[dependencyType].Values.Single();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException(string.Concat($"Dependency of type {dependencyType.Name} and name ",
|
||||
$"{parameterName} does not exist. Available dependency names are: ",
|
||||
string.Join(", ", knownDependencies.Keys)));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException($"Dependency of type {dependencyType.Name} and name {parameterName} has not been set.");
|
||||
}
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
_dependencies = new Dictionary<Type, Dictionary<string, object>>();
|
||||
Sut = default;
|
||||
}
|
||||
|
||||
ISutProvider ISutProvider.Create() => Create();
|
||||
public SutProvider<TSut> Create()
|
||||
{
|
||||
Sut = _fixture.Create<TSut>();
|
||||
return this;
|
||||
}
|
||||
|
||||
private bool DependencyIsSet(Type dependencyType, string parameterName = "")
|
||||
=> _dependencies.ContainsKey(dependencyType) && _dependencies[dependencyType].ContainsKey(parameterName);
|
||||
|
||||
private object GetDefault(Type type) => type.IsValueType ? Activator.CreateInstance(type) : null;
|
||||
|
||||
private class ConstructorParameterRelay<T> : ISpecimenBuilder
|
||||
{
|
||||
private readonly SutProvider<T> _sutProvider;
|
||||
private readonly IFixture _fixture;
|
||||
|
||||
public ConstructorParameterRelay(SutProvider<T> sutProvider, IFixture fixture)
|
||||
{
|
||||
_sutProvider = sutProvider;
|
||||
_fixture = fixture;
|
||||
}
|
||||
|
||||
public object Create(object request, ISpecimenContext context)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
if (!(request is ParameterInfo parameterInfo))
|
||||
{
|
||||
return new NoSpecimen();
|
||||
}
|
||||
if (parameterInfo.Member.DeclaringType != typeof(T) ||
|
||||
parameterInfo.Member.MemberType != MemberTypes.Constructor)
|
||||
{
|
||||
return new NoSpecimen();
|
||||
}
|
||||
|
||||
if (_sutProvider.DependencyIsSet(parameterInfo.ParameterType, parameterInfo.Name))
|
||||
{
|
||||
return _sutProvider.GetDependency(parameterInfo.ParameterType, parameterInfo.Name);
|
||||
}
|
||||
|
||||
|
||||
// This is the equivalent of _fixture.Create<parameterInfo.ParameterType>, but no overload for
|
||||
// Create(Type type) exists.
|
||||
var dependency = new SpecimenContext(_fixture).Resolve(new SeededRequest(parameterInfo.ParameterType,
|
||||
_sutProvider.GetDefault(parameterInfo.ParameterType)));
|
||||
_sutProvider.SetDependency(parameterInfo.ParameterType, dependency, parameterInfo.Name);
|
||||
return dependency;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
35
test/Common/AutoFixture/SutProviderCustomization.cs
Normal file
35
test/Common/AutoFixture/SutProviderCustomization.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using AutoFixture;
|
||||
using AutoFixture.Kernel;
|
||||
|
||||
namespace Bit.Test.Common.AutoFixture.Attributes
|
||||
{
|
||||
public class SutProviderCustomization : ICustomization, ISpecimenBuilder
|
||||
{
|
||||
private IFixture _fixture = null;
|
||||
|
||||
public object Create(object request, ISpecimenContext context)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
if (!(request is Type typeRequest))
|
||||
{
|
||||
return new NoSpecimen();
|
||||
}
|
||||
if (!typeof(ISutProvider).IsAssignableFrom(typeRequest))
|
||||
{
|
||||
return new NoSpecimen();
|
||||
}
|
||||
|
||||
return ((ISutProvider)Activator.CreateInstance(typeRequest, _fixture)).Create();
|
||||
}
|
||||
|
||||
public void Customize(IFixture fixture)
|
||||
{
|
||||
_fixture = fixture;
|
||||
fixture.Customizations.Add(this);
|
||||
}
|
||||
}
|
||||
}
|
25
test/Common/Common.csproj
Normal file
25
test/Common/Common.csproj
Normal file
@ -0,0 +1,25 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<IsPackable>false</IsPackable>
|
||||
<RootNamespace>Bit.Test.Common</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="NSubstitute" Version="4.2.2" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.2">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="AutoFixture.Xunit2" Version="4.14.0" />
|
||||
<PackageReference Include="AutoFixture.AutoNSubstitute" Version="4.14.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Core\Core.csproj" />
|
||||
<ProjectReference Include="..\..\src\Api\Api.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
19
test/Common/Helpers/Factories.cs
Normal file
19
test/Common/Helpers/Factories.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System.Collections.Generic;
|
||||
using Bit.Core.Repositories.EntityFramework;
|
||||
using Bit.Core.Settings;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Bit.Test.Common.Helpers.Factories
|
||||
{
|
||||
public static class GlobalSettingsFactory
|
||||
{
|
||||
public static GlobalSettings GlobalSettings { get; } = new GlobalSettings();
|
||||
static GlobalSettingsFactory()
|
||||
{
|
||||
var configBuilder = new ConfigurationBuilder().AddUserSecrets<Bit.Api.Startup>();
|
||||
var Configuration = configBuilder.Build();
|
||||
ConfigurationBinder.Bind(Configuration.GetSection("GlobalSettings"), GlobalSettings);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user