diff --git a/src/Umbraco.Tests.Common/AutoFixture/AutoMoqDataAttribute.cs b/src/Umbraco.Tests.Common/AutoFixture/AutoMoqDataAttribute.cs new file mode 100644 index 0000000000..406d9a5a20 --- /dev/null +++ b/src/Umbraco.Tests.Common/AutoFixture/AutoMoqDataAttribute.cs @@ -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(c => c.OmitAutoProperties()); + fixture.Customize( + u => u.FromFactory( + (a,b,c) => BackOfficeIdentityUser.CreateNew(Mock.Of(),a,b,c))); + } + } + } + } +} diff --git a/src/Umbraco.Tests.Common/Umbraco.Tests.Common.csproj b/src/Umbraco.Tests.Common/Umbraco.Tests.Common.csproj index 3822f3d191..527d022be7 100644 --- a/src/Umbraco.Tests.Common/Umbraco.Tests.Common.csproj +++ b/src/Umbraco.Tests.Common/Umbraco.Tests.Common.csproj @@ -1,10 +1,17 @@ - netstandard2.0 + netcoreapp3.1 + + + + + + + diff --git a/src/Umbraco.Tests.Integration/TestServerTest/Controllers/UsersControllerTests.cs b/src/Umbraco.Tests.Integration/TestServerTest/Controllers/UsersControllerTests.cs index 024ad1873e..024239c1af 100644 --- a/src/Umbraco.Tests.Integration/TestServerTest/Controllers/UsersControllerTests.cs +++ b/src/Umbraco.Tests.Integration/TestServerTest/Controllers/UsersControllerTests.cs @@ -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; diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Tests.UnitTests.csproj b/src/Umbraco.Tests.UnitTests/Umbraco.Tests.UnitTests.csproj index c358eabf27..2cbf1549dd 100644 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Tests.UnitTests.csproj +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Tests.UnitTests.csproj @@ -16,7 +16,7 @@ - + diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Controllers/UsersControllerUnitTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Controllers/UsersControllerUnitTests.cs new file mode 100644 index 0000000000..138d4902ec --- /dev/null +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Controllers/UsersControllerUnitTests.cs @@ -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, + 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())) + .ReturnsAsync(IdentityResult.Failed(new IdentityError { Description = expectedMessage })); + } + + var actual = sut.PostUnlockUsers(userIds); + + // + // + // Assert.Multiple(() => + // { + // + // }); + } + } +} diff --git a/src/Umbraco.Web/HttpContextExtensions.cs b/src/Umbraco.Web/HttpContextExtensions.cs deleted file mode 100644 index bb080c5618..0000000000 --- a/src/Umbraco.Web/HttpContextExtensions.cs +++ /dev/null @@ -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); - } - } - } -} diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index e2af730cbb..e5d4537c85 100755 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -159,7 +159,6 @@ -