Added test
This commit is contained in:
@@ -1,13 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<TargetFramework>netstandard2.0</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" />
|
||||
|
||||
@@ -22,7 +22,7 @@ using Umbraco.Web.Models.ContentEditing;
|
||||
|
||||
namespace Umbraco.Tests.Integration.TestServerTest.Controllers
|
||||
{
|
||||
// [Explicit("We need to fix the tests on buildserver and when running multiple tests in one run")]
|
||||
[Explicit("We need to fix the tests on buildserver and when running multiple tests in one run")]
|
||||
[TestFixture]
|
||||
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
|
||||
public class UsersControllerTests : UmbracoTestServerTestBase
|
||||
@@ -175,40 +175,6 @@ namespace Umbraco.Tests.Integration.TestServerTest.Controllers
|
||||
});
|
||||
}
|
||||
|
||||
// [Test]
|
||||
// public async Task PostUnlockUsers_When_User_Lockout_Update_Fails_Expect_Failure_Response()
|
||||
// {
|
||||
// //
|
||||
// // var mockUserManager = CreateMockUserManager();
|
||||
// // var usersController = CreateSut(mockUserManager);
|
||||
// //
|
||||
// // const string expectedMessage = "identity error!";
|
||||
// // var user = new BackOfficeIdentityUser(
|
||||
// // new Mock<IGlobalSettings>().Object,
|
||||
// // 1,
|
||||
// // new List<IReadOnlyUserGroup>())
|
||||
// // {
|
||||
// // Name = "bob"
|
||||
// // };
|
||||
// //
|
||||
// // mockUserManager.Setup(x => x.FindByIdAsync(It.IsAny<string>()))
|
||||
// // .ReturnsAsync(user);
|
||||
// // mockUserManager.Setup(x => x.SetLockoutEndDateAsync(user, It.IsAny<DateTimeOffset?>()))
|
||||
// // .ReturnsAsync(IdentityResult.Failed(new IdentityError {Description = expectedMessage}));
|
||||
// //
|
||||
// // var response = await usersController.PostUnlockUsers(new[] { 1 });
|
||||
// //
|
||||
// // Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
|
||||
// // Assert.True(response.Headers.TryGetValues("X-Status-Reason", out var values));
|
||||
// // Assert.True(values.Contains("Validation failed"));
|
||||
// //
|
||||
// // var responseContent = response.Content as ObjectContent<HttpError>;
|
||||
// // var responseValue = responseContent?.Value as HttpError;
|
||||
// // Assert.NotNull(responseValue);
|
||||
// // Assert.True(responseValue.Message.Contains(expectedMessage));
|
||||
// // Assert.True(responseValue.Message.Contains(user.Id.ToString()));
|
||||
// }
|
||||
//
|
||||
[Test]
|
||||
public async Task PostUnlockUsers_When_One_UserId_Supplied_Expect_User_Locked_Out_With_Correct_Response_Message()
|
||||
{
|
||||
@@ -286,42 +252,5 @@ namespace Umbraco.Tests.Integration.TestServerTest.Controllers
|
||||
Assert.AreEqual($"Unlocked {users.Count()} users", actual.Message);
|
||||
});
|
||||
}
|
||||
//
|
||||
// [Test]
|
||||
// public async Task GetPagedUsers_Fips()
|
||||
// {
|
||||
// await RunFipsTest("GetPagedUsers", mock =>
|
||||
// {
|
||||
// var users = MockedUser.CreateMulipleUsers(10);
|
||||
// long outVal = 10;
|
||||
// mock.Setup(service => service.GetAll(
|
||||
// It.IsAny<long>(), It.IsAny<int>(), out outVal, It.IsAny<string>(), It.IsAny<Direction>(),
|
||||
// It.IsAny<UserState[]>(), It.IsAny<string[]>(), It.IsAny<string[]>(), It.IsAny<IQuery<IUser>>()))
|
||||
// .Returns(() => users);
|
||||
// }, response =>
|
||||
// {
|
||||
// var obj = JsonConvert.DeserializeObject<PagedResult<UserBasic>>(response.Item2);
|
||||
// Assert.AreEqual(10, obj.TotalItems);
|
||||
// Assert.AreEqual(10, obj.Items.Count());
|
||||
// });
|
||||
// }
|
||||
//
|
||||
// [Test]
|
||||
// public async Task GetById_Fips()
|
||||
// {
|
||||
// const int mockUserId = 1234;
|
||||
// var user = MockedUser.CreateUser();
|
||||
//
|
||||
// await RunFipsTest("GetById", mock =>
|
||||
// {
|
||||
// mock.Setup(service => service.GetUserById(1234))
|
||||
// .Returns((int i) => i == mockUserId ? user : null);
|
||||
// }, response =>
|
||||
// {
|
||||
// var obj = JsonConvert.DeserializeObject<UserDisplay>(response.Item2);
|
||||
// Assert.AreEqual(user.Username, obj.Username);
|
||||
// Assert.AreEqual(user.Email, obj.Email);
|
||||
// }, new { controller = "Users", action = "GetById" }, $"Users/GetById/{mockUserId}");
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
using System;
|
||||
using AutoFixture;
|
||||
using AutoFixture.AutoMoq;
|
||||
using AutoFixture.Kernel;
|
||||
using AutoFixture.NUnit3;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Moq;
|
||||
using Umbraco.Core.BackOffice;
|
||||
using Umbraco.Core.Configuration;
|
||||
using Umbraco.Web.BackOffice.Controllers;
|
||||
|
||||
namespace Umbraco.Tests.Common.AutoFixture
|
||||
{
|
||||
@@ -24,11 +27,19 @@ namespace Umbraco.Tests.Common.AutoFixture
|
||||
{
|
||||
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)));
|
||||
fixture
|
||||
.Customize(new ConstructorCustomization(typeof(UsersController), new GreedyConstructorQuery()))
|
||||
.Customize(new ConstructorCustomization(typeof(BackOfficeUserManager), new GreedyConstructorQuery()))
|
||||
.Customize(new AutoMoqCustomization());
|
||||
|
||||
fixture.Customize<IUserStore<BackOfficeIdentityUser>>(cc => cc.FromFactory(() => Mock.Of<IUserLockoutStore<BackOfficeIdentityUser>>()));
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,46 +1,33 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
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;
|
||||
using Umbraco.Web.Common.Exceptions;
|
||||
|
||||
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,
|
||||
[Test,AutoMoqData]
|
||||
public async Task PostUnlockUsers_When_User_Lockout_Update_Fails_Expect_Failure_Response(
|
||||
[Frozen] IUserStore<BackOfficeIdentityUser> userStore,
|
||||
UsersController sut,
|
||||
BackOfficeIdentityUser[] users,
|
||||
BackOfficeIdentityUser user,
|
||||
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 }));
|
||||
}
|
||||
Mock.Get(userStore)
|
||||
.Setup(x => x.FindByIdAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(user);
|
||||
|
||||
var actual = sut.PostUnlockUsers(userIds);
|
||||
|
||||
//
|
||||
//
|
||||
// Assert.Multiple(() =>
|
||||
// {
|
||||
//
|
||||
// });
|
||||
Assert.ThrowsAsync<HttpResponseException>(() => sut.PostUnlockUsers(userIds));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,7 +92,7 @@ namespace Umbraco.Tests.Persistence.Repositories
|
||||
Assert.That(repository.Get("test"), Is.Not.Null);
|
||||
Assert.That(_fileSystems.MvcViewsFileSystem.FileExists("test.cshtml"), Is.True);
|
||||
Assert.AreEqual(
|
||||
@"@inherits Umbraco.Web.Mvc.UmbracoViewPage @{ Layout = null;}".StripWhitespace(),
|
||||
@"@inherits Umbraco.Web.Common.AspNetCore.UmbracoViewPage @{ Layout = null;}".StripWhitespace(),
|
||||
template.Content.StripWhitespace());
|
||||
}
|
||||
}
|
||||
@@ -118,7 +118,7 @@ namespace Umbraco.Tests.Persistence.Repositories
|
||||
Assert.That(repository.Get("test2"), Is.Not.Null);
|
||||
Assert.That(_fileSystems.MvcViewsFileSystem.FileExists("test2.cshtml"), Is.True);
|
||||
Assert.AreEqual(
|
||||
"@inherits Umbraco.Web.Mvc.UmbracoViewPage @{ Layout = \"test.cshtml\";}".StripWhitespace(),
|
||||
"@inherits Umbraco.Web.Common.AspNetCore.UmbracoViewPage @{ Layout = \"test.cshtml\";}".StripWhitespace(),
|
||||
template2.Content.StripWhitespace());
|
||||
}
|
||||
}
|
||||
|
||||
53
src/Umbraco.Web/HttpContextExtensions.cs
Normal file
53
src/Umbraco.Web/HttpContextExtensions.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -147,6 +147,7 @@
|
||||
<Compile Include="Compose\NestedContentPropertyComponent.cs" />
|
||||
<Compile Include="Composing\LightInject\LightInjectContainer.cs" />
|
||||
<Compile Include="Editors\PasswordChanger.cs" />
|
||||
<Compile Include="HttpContextExtensions.cs" />
|
||||
<Compile Include="Macros\MacroRenderer.cs" />
|
||||
<Compile Include="Macros\MemberUserKeyProvider.cs" />
|
||||
<Compile Include="Macros\PartialViewMacroController.cs" />
|
||||
|
||||
Reference in New Issue
Block a user