Started on AutoMoqDataAttribute
Signed-off-by: Bjarke Berg <mail@bergmania.dk>
This commit is contained in:
36
src/Umbraco.Tests.Common/AutoFixture/AutoMoqDataAttribute.cs
Normal file
36
src/Umbraco.Tests.Common/AutoFixture/AutoMoqDataAttribute.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using AutoFixture;
|
||||
using AutoFixture.AutoMoq;
|
||||
using AutoFixture.NUnit3;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using Moq;
|
||||
using Umbraco.Core.BackOffice;
|
||||
using Umbraco.Core.Configuration;
|
||||
|
||||
namespace Umbraco.Tests.Common.AutoFixture
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor)]
|
||||
public class AutoMoqDataAttribute : AutoDataAttribute
|
||||
{
|
||||
public AutoMoqDataAttribute() : base(() => AutoMockCustomizations.Default)
|
||||
{
|
||||
}
|
||||
|
||||
private static class AutoMockCustomizations
|
||||
{
|
||||
public static IFixture Default => new Fixture().Customize(new UmbracoCustomization());
|
||||
|
||||
private class UmbracoCustomization : ICustomization
|
||||
{
|
||||
public void Customize(IFixture fixture)
|
||||
{
|
||||
fixture.Customize(new AutoMoqCustomization());
|
||||
fixture.Customize<BindingInfo>(c => c.OmitAutoProperties());
|
||||
fixture.Customize<BackOfficeIdentityUser>(
|
||||
u => u.FromFactory<string ,string, string>(
|
||||
(a,b,c) => BackOfficeIdentityUser.CreateNew(Mock.Of<IGlobalSettings>(),a,b,c)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,17 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<FrameworkReference Include="Microsoft.AspNetCore.App" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoFixture.AutoMoq" Version="4.11.0" />
|
||||
<PackageReference Include="AutoFixture.NUnit3" Version="4.11.0" />
|
||||
<PackageReference Include="Microsoft.AspNet.Mvc" Version="5.2.7" />
|
||||
<PackageReference Include="MiniProfiler" Version="4.1.0" />
|
||||
<PackageReference Include="Moq" Version="4.13.1" />
|
||||
<PackageReference Include="nunit" Version="3.12.0" />
|
||||
|
||||
@@ -101,7 +101,7 @@ namespace Umbraco.Tests.Integration.TestServerTest.Controllers
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetPagedUsers_11()
|
||||
public async Task GetPagedUsers_multiple_pages()
|
||||
{
|
||||
var totalNumberOfUsers = 11;
|
||||
var pageSize = totalNumberOfUsers - 1;
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Lucene.Net.Contrib" Version="3.0.3" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
|
||||
<PackageReference Include="NUnit" Version="3.12.0" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using AutoFixture.NUnit3;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Core.BackOffice;
|
||||
using Umbraco.Core.Models.Membership;
|
||||
using Umbraco.Tests.Common.AutoFixture;
|
||||
using Umbraco.Web.BackOffice.Controllers;
|
||||
|
||||
namespace Umbraco.Tests.Web.Controllers
|
||||
{
|
||||
[TestFixture]
|
||||
public class UsersControllerUnitTests
|
||||
{
|
||||
[Test]
|
||||
[AutoMoqData]
|
||||
public void PostUnlockUsers_When_User_Lockout_Update_Fails_Expect_Failure_Response(
|
||||
[Frozen(Matching.ParameterName)] Mock<BackOfficeUserManager> backOfficeUserManager,
|
||||
UsersController sut,
|
||||
BackOfficeIdentityUser[] users,
|
||||
int[] userIds,
|
||||
string expectedMessage)
|
||||
{
|
||||
for (var i = 0; i < userIds.Length; i++)
|
||||
{
|
||||
var userId = userIds[i];
|
||||
var user = users[i];
|
||||
backOfficeUserManager.Setup(x => x.FindByIdAsync(userId.ToString()))
|
||||
.ReturnsAsync(user);
|
||||
backOfficeUserManager.Setup(x => x.SetLockoutEndDateAsync(user, It.IsAny<DateTimeOffset?>()))
|
||||
.ReturnsAsync(IdentityResult.Failed(new IdentityError { Description = expectedMessage }));
|
||||
}
|
||||
|
||||
var actual = sut.PostUnlockUsers(userIds);
|
||||
|
||||
//
|
||||
//
|
||||
// Assert.Multiple(() =>
|
||||
// {
|
||||
//
|
||||
// });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
using System.Web;
|
||||
|
||||
namespace Umbraco.Core
|
||||
{
|
||||
public static class HttpContextExtensions
|
||||
{
|
||||
public static string GetCurrentRequestIpAddress(this HttpContextBase httpContext)
|
||||
{
|
||||
if (httpContext == null)
|
||||
{
|
||||
return "Unknown, httpContext is null";
|
||||
}
|
||||
|
||||
HttpRequestBase request;
|
||||
try
|
||||
{
|
||||
// is not null - throws
|
||||
request = httpContext.Request;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return "Unknown, httpContext.Request is null";
|
||||
}
|
||||
|
||||
if (request.ServerVariables == null)
|
||||
{
|
||||
return "Unknown, httpContext.Request.ServerVariables is null";
|
||||
}
|
||||
|
||||
// From: http://stackoverflow.com/a/740431/5018
|
||||
|
||||
try
|
||||
{
|
||||
var ipAddress = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
|
||||
|
||||
if (string.IsNullOrEmpty(ipAddress))
|
||||
return request.UserHostAddress;
|
||||
|
||||
var addresses = ipAddress.Split(',');
|
||||
if (addresses.Length != 0)
|
||||
return addresses[0];
|
||||
|
||||
return request.UserHostAddress;
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
//This try catch is to just always ensure that no matter what we're not getting any exceptions caused since
|
||||
// that would cause people to not be able to login
|
||||
return string.Format("Unknown, exception occurred trying to resolve IP {0}", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -159,7 +159,6 @@
|
||||
<Compile Include="Models\NoNodesViewModel.cs" />
|
||||
<Compile Include="Mvc\RenderNoContentController.cs" />
|
||||
<Compile Include="HttpContextAccessorExtensions.cs" />
|
||||
<Compile Include="HttpContextExtensions.cs" />
|
||||
<Compile Include="ImageCropperTemplateCoreExtensions.cs" />
|
||||
<Compile Include="Logging\OwinLogger.cs" />
|
||||
<Compile Include="Logging\OwinLoggerFactory.cs" />
|
||||
|
||||
Reference in New Issue
Block a user