2020-06-24 22:00:27 +02:00
|
|
|
|
using System;
|
2020-09-11 21:13:18 +02:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Reflection;
|
2020-06-24 22:00:27 +02:00
|
|
|
|
using AutoFixture;
|
|
|
|
|
|
using AutoFixture.AutoMoq;
|
2020-06-25 11:22:59 +02:00
|
|
|
|
using AutoFixture.Kernel;
|
2020-06-24 22:00:27 +02:00
|
|
|
|
using AutoFixture.NUnit3;
|
2020-06-25 11:22:59 +02:00
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
2020-09-11 21:13:18 +02:00
|
|
|
|
using Microsoft.Extensions.Options;
|
2020-06-24 22:00:27 +02:00
|
|
|
|
using Moq;
|
2020-09-03 13:02:29 +02:00
|
|
|
|
using Umbraco.Core;
|
2020-06-24 22:00:27 +02:00
|
|
|
|
using Umbraco.Core.BackOffice;
|
2020-09-03 13:02:29 +02:00
|
|
|
|
using Umbraco.Core.Configuration;
|
2020-09-11 21:13:18 +02:00
|
|
|
|
using Umbraco.Core.Configuration.Models;
|
2020-08-24 10:52:48 +02:00
|
|
|
|
using Umbraco.Tests.Common.Builders;
|
2020-06-25 11:22:59 +02:00
|
|
|
|
using Umbraco.Web.BackOffice.Controllers;
|
2020-08-21 13:53:27 +02:00
|
|
|
|
using Umbraco.Web.Common.Install;
|
2020-06-24 22:00:27 +02:00
|
|
|
|
|
2020-07-01 17:42:39 +02:00
|
|
|
|
namespace Umbraco.Tests.UnitTests.AutoFixture
|
2020-06-24 22:00:27 +02:00
|
|
|
|
{
|
2020-09-28 12:51:12 +02:00
|
|
|
|
|
|
|
|
|
|
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor, AllowMultiple = true)]
|
|
|
|
|
|
public class InlineAutoMoqDataAttribute : InlineAutoDataAttribute
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Uses AutoFixture to automatically mock (using Moq) the injected types. E.g when injecting interfaces.
|
|
|
|
|
|
/// AutoFixture is used to generate concrete types. If the concrete type required some types injected, the
|
|
|
|
|
|
/// [Frozen] can be used to ensure the same variable is injected and available as parameter for the test
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public InlineAutoMoqDataAttribute(params object[] arguments) : base(() => AutoMoqDataAttribute.AutoMockCustomizations.Default, arguments)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-24 22:00:27 +02:00
|
|
|
|
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor)]
|
|
|
|
|
|
public class AutoMoqDataAttribute : AutoDataAttribute
|
|
|
|
|
|
{
|
2020-07-09 11:00:29 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Uses AutoFixture to automatically mock (using Moq) the injected types. E.g when injecting interfaces.
|
|
|
|
|
|
/// AutoFixture is used to generate concrete types. If the concrete type required some types injected, the
|
|
|
|
|
|
/// [Frozen] can be used to ensure the same variable is injected and available as parameter for the test
|
|
|
|
|
|
/// </summary>
|
2020-06-24 22:00:27 +02:00
|
|
|
|
public AutoMoqDataAttribute() : base(() => AutoMockCustomizations.Default)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-28 12:51:12 +02:00
|
|
|
|
internal static class AutoMockCustomizations
|
2020-06-24 22:00:27 +02:00
|
|
|
|
{
|
|
|
|
|
|
public static IFixture Default => new Fixture().Customize(new UmbracoCustomization());
|
|
|
|
|
|
|
|
|
|
|
|
private class UmbracoCustomization : ICustomization
|
|
|
|
|
|
{
|
|
|
|
|
|
public void Customize(IFixture fixture)
|
|
|
|
|
|
{
|
|
|
|
|
|
fixture.Customize<BackOfficeIdentityUser>(
|
|
|
|
|
|
u => u.FromFactory<string ,string, string>(
|
2020-09-21 21:06:24 +02:00
|
|
|
|
(a,b,c) => BackOfficeIdentityUser.CreateNew(new GlobalSettings(),a,b,c)));
|
2020-06-25 11:22:59 +02:00
|
|
|
|
fixture
|
|
|
|
|
|
.Customize(new ConstructorCustomization(typeof(UsersController), new GreedyConstructorQuery()))
|
2020-08-21 13:53:27 +02:00
|
|
|
|
.Customize(new ConstructorCustomization(typeof(InstallController), new GreedyConstructorQuery()))
|
2020-08-24 08:45:41 +02:00
|
|
|
|
.Customize(new ConstructorCustomization(typeof(PreviewController), new GreedyConstructorQuery()))
|
2020-08-26 09:55:14 +02:00
|
|
|
|
.Customize(new ConstructorCustomization(typeof(BackOfficeController), new GreedyConstructorQuery()))
|
2020-09-11 21:13:18 +02:00
|
|
|
|
.Customize(new ConstructorCustomization(typeof(BackOfficeUserManager), new GreedyConstructorQuery()));
|
2020-06-25 11:22:59 +02:00
|
|
|
|
|
2020-09-11 21:13:18 +02:00
|
|
|
|
fixture.Customize(new AutoMoqCustomization());
|
2020-07-09 11:00:29 +02:00
|
|
|
|
// When requesting an IUserStore ensure we actually uses a IUserLockoutStore
|
2020-06-25 11:22:59 +02:00
|
|
|
|
fixture.Customize<IUserStore<BackOfficeIdentityUser>>(cc => cc.FromFactory(() => Mock.Of<IUserLockoutStore<BackOfficeIdentityUser>>()));
|
|
|
|
|
|
|
2020-08-21 13:53:27 +02:00
|
|
|
|
fixture.Customize<ConfigConnectionString>(
|
|
|
|
|
|
u => u.FromFactory<string, string, string>(
|
|
|
|
|
|
(a, b, c) => new ConfigConnectionString(a, b, c)));
|
|
|
|
|
|
|
|
|
|
|
|
fixture.Customize<IUmbracoVersion>(
|
|
|
|
|
|
u => u.FromFactory(
|
|
|
|
|
|
() => new UmbracoVersion()));
|
|
|
|
|
|
|
2020-09-11 21:13:18 +02:00
|
|
|
|
var connectionStrings = new ConnectionStrings();
|
|
|
|
|
|
fixture.Customize<ConnectionStrings>(x => x.FromFactory(() => connectionStrings ));
|
|
|
|
|
|
|
2020-06-25 11:22:59 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-06-24 22:00:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|