2021-03-29 17:37:58 +11:00
|
|
|
using System;
|
2021-04-12 18:02:43 +10:00
|
|
|
using System.Linq;
|
2021-03-29 17:37:58 +11:00
|
|
|
using System.Net;
|
2020-11-30 00:45:38 +00:00
|
|
|
using System.Threading.Tasks;
|
2021-03-14 18:58:33 +00:00
|
|
|
using Microsoft.AspNetCore.Authentication;
|
2020-12-08 01:57:14 +00:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2021-03-29 17:37:58 +11:00
|
|
|
using Microsoft.AspNetCore.Http.Features;
|
2020-11-30 00:45:38 +00:00
|
|
|
using Microsoft.AspNetCore.Identity;
|
2021-03-29 17:37:58 +11:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-11-30 00:45:38 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using Moq;
|
|
|
|
|
using NUnit.Framework;
|
Implements Public Access in netcore (#10137)
* Getting new netcore PublicAccessChecker in place
* Adds full test coverage for PublicAccessChecker
* remove PublicAccessComposer
* adjust namespaces, ensure RoleManager works, separate public access controller, reduce content controller
* Implements the required methods on IMemberManager, removes old migrated code
* Updates routing to be able to re-route, Fixes middleware ordering ensuring endpoints are last, refactors pipeline options, adds public access middleware, ensures public access follows all hops
* adds note
* adds note
* Cleans up ext methods, ensures that members identity is added on both front-end and back ends. updates how UmbracoApplicationBuilder works in that it explicitly starts endpoints at the time of calling.
* Changes name to IUmbracoEndpointBuilder
* adds note
* Fixing tests, fixing error describers so there's 2x one for back office, one for members, fixes TryConvertTo, fixes login redirect
* fixing build
* Fixes keepalive, fixes PublicAccessMiddleware to not throw, updates startup code to be more clear and removes magic that registers middleware.
* adds note
* removes unused filter, fixes build
* fixes WebPath and tests
* Looks up entities in one query
* remove usings
* Fix test, remove stylesheet
* Set status code before we write to response to avoid error
* Ensures that users and members are validated when logging in. Shares more code between users and members.
* Fixes RepositoryCacheKeys to ensure the keys are normalized
* oops didn't mean to commit this
* Fix casing issues with caching, stop boxing value types for all cache operations, stop re-creating string keys in DefaultRepositoryCachePolicy
* bah, far out this keeps getting recommitted. sorry
Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2021-04-20 15:11:45 +10:00
|
|
|
using Umbraco.Cms.Core.Configuration.Models;
|
2022-01-21 13:10:34 +01:00
|
|
|
using Umbraco.Cms.Core.Events;
|
2021-02-22 20:55:12 +00:00
|
|
|
using Umbraco.Cms.Core.Net;
|
2021-02-23 09:05:06 +01:00
|
|
|
using Umbraco.Cms.Core.Security;
|
Implements Public Access in netcore (#10137)
* Getting new netcore PublicAccessChecker in place
* Adds full test coverage for PublicAccessChecker
* remove PublicAccessComposer
* adjust namespaces, ensure RoleManager works, separate public access controller, reduce content controller
* Implements the required methods on IMemberManager, removes old migrated code
* Updates routing to be able to re-route, Fixes middleware ordering ensuring endpoints are last, refactors pipeline options, adds public access middleware, ensures public access follows all hops
* adds note
* adds note
* Cleans up ext methods, ensures that members identity is added on both front-end and back ends. updates how UmbracoApplicationBuilder works in that it explicitly starts endpoints at the time of calling.
* Changes name to IUmbracoEndpointBuilder
* adds note
* Fixing tests, fixing error describers so there's 2x one for back office, one for members, fixes TryConvertTo, fixes login redirect
* fixing build
* Fixes keepalive, fixes PublicAccessMiddleware to not throw, updates startup code to be more clear and removes magic that registers middleware.
* adds note
* removes unused filter, fixes build
* fixes WebPath and tests
* Looks up entities in one query
* remove usings
* Fix test, remove stylesheet
* Set status code before we write to response to avoid error
* Ensures that users and members are validated when logging in. Shares more code between users and members.
* Fixes RepositoryCacheKeys to ensure the keys are normalized
* oops didn't mean to commit this
* Fix casing issues with caching, stop boxing value types for all cache operations, stop re-creating string keys in DefaultRepositoryCachePolicy
* bah, far out this keeps getting recommitted. sorry
Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2021-04-20 15:11:45 +10:00
|
|
|
using Umbraco.Cms.Core.Services;
|
2021-02-23 09:05:06 +01:00
|
|
|
using Umbraco.Cms.Web.Common.Security;
|
2020-11-30 00:45:38 +00:00
|
|
|
|
2021-03-14 18:58:33 +00:00
|
|
|
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common.Security
|
2020-11-30 00:45:38 +00:00
|
|
|
{
|
|
|
|
|
[TestFixture]
|
2021-03-14 18:58:33 +00:00
|
|
|
public class MemberSignInManagerTests
|
2020-11-30 00:45:38 +00:00
|
|
|
{
|
2021-03-29 17:37:58 +11:00
|
|
|
private Mock<ILogger<SignInManager<MemberIdentityUser>>> _mockLogger;
|
2021-04-12 18:02:43 +10:00
|
|
|
private readonly Mock<MemberManager> _memberManager = MockMemberManager();
|
2021-03-29 17:37:58 +11:00
|
|
|
|
2021-04-09 15:24:12 +10:00
|
|
|
public UserClaimsPrincipalFactory<MemberIdentityUser> CreateClaimsFactory(MemberManager userMgr)
|
Implements Public Access in netcore (#10137)
* Getting new netcore PublicAccessChecker in place
* Adds full test coverage for PublicAccessChecker
* remove PublicAccessComposer
* adjust namespaces, ensure RoleManager works, separate public access controller, reduce content controller
* Implements the required methods on IMemberManager, removes old migrated code
* Updates routing to be able to re-route, Fixes middleware ordering ensuring endpoints are last, refactors pipeline options, adds public access middleware, ensures public access follows all hops
* adds note
* adds note
* Cleans up ext methods, ensures that members identity is added on both front-end and back ends. updates how UmbracoApplicationBuilder works in that it explicitly starts endpoints at the time of calling.
* Changes name to IUmbracoEndpointBuilder
* adds note
* Fixing tests, fixing error describers so there's 2x one for back office, one for members, fixes TryConvertTo, fixes login redirect
* fixing build
* Fixes keepalive, fixes PublicAccessMiddleware to not throw, updates startup code to be more clear and removes magic that registers middleware.
* adds note
* removes unused filter, fixes build
* fixes WebPath and tests
* Looks up entities in one query
* remove usings
* Fix test, remove stylesheet
* Set status code before we write to response to avoid error
* Ensures that users and members are validated when logging in. Shares more code between users and members.
* Fixes RepositoryCacheKeys to ensure the keys are normalized
* oops didn't mean to commit this
* Fix casing issues with caching, stop boxing value types for all cache operations, stop re-creating string keys in DefaultRepositoryCachePolicy
* bah, far out this keeps getting recommitted. sorry
Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2021-04-20 15:11:45 +10:00
|
|
|
=> new UserClaimsPrincipalFactory<MemberIdentityUser>(userMgr, Options.Create(new IdentityOptions()));
|
2020-11-30 00:45:38 +00:00
|
|
|
|
2021-03-14 18:58:33 +00:00
|
|
|
public MemberSignInManager CreateSut()
|
2020-11-30 00:45:38 +00:00
|
|
|
{
|
2021-03-29 17:37:58 +11:00
|
|
|
// This all needs to be setup because internally aspnet resolves a bunch
|
|
|
|
|
// of services from the HttpContext.RequestServices.
|
|
|
|
|
var serviceProviderFactory = new DefaultServiceProviderFactory();
|
|
|
|
|
var serviceCollection = new ServiceCollection();
|
|
|
|
|
serviceCollection
|
|
|
|
|
.AddLogging()
|
|
|
|
|
.AddAuthentication()
|
2022-01-19 09:21:50 +01:00
|
|
|
.AddCookie(IdentityConstants.ApplicationScheme)
|
|
|
|
|
.AddCookie(IdentityConstants.ExternalScheme, o =>
|
|
|
|
|
{
|
|
|
|
|
o.Cookie.Name = IdentityConstants.ExternalScheme;
|
|
|
|
|
o.ExpireTimeSpan = TimeSpan.FromMinutes(5);
|
|
|
|
|
})
|
|
|
|
|
.AddCookie(IdentityConstants.TwoFactorUserIdScheme, o =>
|
|
|
|
|
{
|
|
|
|
|
o.Cookie.Name = IdentityConstants.TwoFactorUserIdScheme;
|
|
|
|
|
o.ExpireTimeSpan = TimeSpan.FromMinutes(5);
|
2022-01-21 13:10:34 +01:00
|
|
|
})
|
|
|
|
|
.AddCookie(IdentityConstants.TwoFactorRememberMeScheme, o =>
|
|
|
|
|
{
|
|
|
|
|
o.Cookie.Name = IdentityConstants.TwoFactorRememberMeScheme;
|
|
|
|
|
o.ExpireTimeSpan = TimeSpan.FromMinutes(5);
|
2022-01-19 09:21:50 +01:00
|
|
|
});
|
2021-03-29 17:37:58 +11:00
|
|
|
IServiceProvider serviceProvider = serviceProviderFactory.CreateServiceProvider(serviceCollection);
|
|
|
|
|
var httpContextFactory = new DefaultHttpContextFactory(serviceProvider);
|
|
|
|
|
IFeatureCollection features = new DefaultHttpContext().Features;
|
|
|
|
|
features.Set<IHttpConnectionFeature>(new HttpConnectionFeature
|
|
|
|
|
{
|
|
|
|
|
LocalIpAddress = IPAddress.Parse("127.0.0.1")
|
|
|
|
|
});
|
|
|
|
|
HttpContext httpContext = httpContextFactory.Create(features);
|
|
|
|
|
|
|
|
|
|
_mockLogger = new Mock<ILogger<SignInManager<MemberIdentityUser>>>();
|
2021-03-14 18:58:33 +00:00
|
|
|
return new MemberSignInManager(
|
2021-03-17 18:04:54 +00:00
|
|
|
_memberManager.Object,
|
2021-03-29 17:37:58 +11:00
|
|
|
Mock.Of<IHttpContextAccessor>(x => x.HttpContext == httpContext),
|
|
|
|
|
CreateClaimsFactory(_memberManager.Object),
|
2021-03-14 18:58:33 +00:00
|
|
|
Mock.Of<IOptions<IdentityOptions>>(),
|
2021-03-29 17:37:58 +11:00
|
|
|
_mockLogger.Object,
|
2021-03-14 18:58:33 +00:00
|
|
|
Mock.Of<IAuthenticationSchemeProvider>(),
|
2022-01-19 09:21:50 +01:00
|
|
|
Mock.Of<IUserConfirmation<MemberIdentityUser>>(),
|
2022-01-21 13:10:34 +01:00
|
|
|
Mock.Of<IMemberExternalLoginProviders>(),
|
|
|
|
|
Mock.Of<IEventAggregator>()
|
2022-01-19 09:21:50 +01:00
|
|
|
);
|
2020-11-30 00:45:38 +00:00
|
|
|
}
|
2021-04-12 18:02:43 +10:00
|
|
|
private static Mock<MemberManager> MockMemberManager()
|
|
|
|
|
=> new Mock<MemberManager>(
|
|
|
|
|
Mock.Of<IIpResolver>(),
|
Published members cleanup (#10159)
* Getting new netcore PublicAccessChecker in place
* Adds full test coverage for PublicAccessChecker
* remove PublicAccessComposer
* adjust namespaces, ensure RoleManager works, separate public access controller, reduce content controller
* Implements the required methods on IMemberManager, removes old migrated code
* Updates routing to be able to re-route, Fixes middleware ordering ensuring endpoints are last, refactors pipeline options, adds public access middleware, ensures public access follows all hops
* adds note
* adds note
* Cleans up ext methods, ensures that members identity is added on both front-end and back ends. updates how UmbracoApplicationBuilder works in that it explicitly starts endpoints at the time of calling.
* Changes name to IUmbracoEndpointBuilder
* adds note
* Fixing tests, fixing error describers so there's 2x one for back office, one for members, fixes TryConvertTo, fixes login redirect
* fixing build
* Updates user manager to correctly validate password hashing and injects the IBackOfficeUserPasswordChecker
* Merges PR
* Fixes up build and notes
* Implements security stamp and email confirmed for members, cleans up a bunch of repo/service level member groups stuff, shares user store code between members and users and fixes the user identity object so we arent' tracking both groups and roles.
* Security stamp for members is now working
* Fixes keepalive, fixes PublicAccessMiddleware to not throw, updates startup code to be more clear and removes magic that registers middleware.
* adds note
* removes unused filter, fixes build
* fixes WebPath and tests
* Looks up entities in one query
* remove usings
* Fix test, remove stylesheet
* Set status code before we write to response to avoid error
* Ensures that users and members are validated when logging in. Shares more code between users and members.
* merge changes
* oops
* Reducing and removing published member cache
* Fixes RepositoryCacheKeys to ensure the keys are normalized
* oops didn't mean to commit this
* Fix casing issues with caching, stop boxing value types for all cache operations, stop re-creating string keys in DefaultRepositoryCachePolicy
* oops didn't mean to comit this
* bah, far out this keeps getting recommitted. sorry
* cannot inject IPublishedMemberCache and cannot have IPublishedMember
* splits out files, fixes build
* fix tests
* removes membership provider classes
* removes membership provider classes
* updates the identity map definition
* reverts commented out lines
* reverts commented out lines
Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2021-04-22 21:21:43 +10:00
|
|
|
Mock.Of<IMemberUserStore>(),
|
Implements Public Access in netcore (#10137)
* Getting new netcore PublicAccessChecker in place
* Adds full test coverage for PublicAccessChecker
* remove PublicAccessComposer
* adjust namespaces, ensure RoleManager works, separate public access controller, reduce content controller
* Implements the required methods on IMemberManager, removes old migrated code
* Updates routing to be able to re-route, Fixes middleware ordering ensuring endpoints are last, refactors pipeline options, adds public access middleware, ensures public access follows all hops
* adds note
* adds note
* Cleans up ext methods, ensures that members identity is added on both front-end and back ends. updates how UmbracoApplicationBuilder works in that it explicitly starts endpoints at the time of calling.
* Changes name to IUmbracoEndpointBuilder
* adds note
* Fixing tests, fixing error describers so there's 2x one for back office, one for members, fixes TryConvertTo, fixes login redirect
* fixing build
* Fixes keepalive, fixes PublicAccessMiddleware to not throw, updates startup code to be more clear and removes magic that registers middleware.
* adds note
* removes unused filter, fixes build
* fixes WebPath and tests
* Looks up entities in one query
* remove usings
* Fix test, remove stylesheet
* Set status code before we write to response to avoid error
* Ensures that users and members are validated when logging in. Shares more code between users and members.
* Fixes RepositoryCacheKeys to ensure the keys are normalized
* oops didn't mean to commit this
* Fix casing issues with caching, stop boxing value types for all cache operations, stop re-creating string keys in DefaultRepositoryCachePolicy
* bah, far out this keeps getting recommitted. sorry
Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2021-04-20 15:11:45 +10:00
|
|
|
Options.Create(new IdentityOptions()),
|
2021-04-12 18:02:43 +10:00
|
|
|
Mock.Of<IPasswordHasher<MemberIdentityUser>>(),
|
|
|
|
|
Enumerable.Empty<IUserValidator<MemberIdentityUser>>(),
|
|
|
|
|
Enumerable.Empty<IPasswordValidator<MemberIdentityUser>>(),
|
2021-05-03 14:05:17 +02:00
|
|
|
new MembersErrorDescriber(Mock.Of<ILocalizedTextService>()),
|
2021-04-12 18:02:43 +10:00
|
|
|
Mock.Of<IServiceProvider>(),
|
|
|
|
|
Mock.Of<ILogger<UserManager<MemberIdentityUser>>>(),
|
Implements Public Access in netcore (#10137)
* Getting new netcore PublicAccessChecker in place
* Adds full test coverage for PublicAccessChecker
* remove PublicAccessComposer
* adjust namespaces, ensure RoleManager works, separate public access controller, reduce content controller
* Implements the required methods on IMemberManager, removes old migrated code
* Updates routing to be able to re-route, Fixes middleware ordering ensuring endpoints are last, refactors pipeline options, adds public access middleware, ensures public access follows all hops
* adds note
* adds note
* Cleans up ext methods, ensures that members identity is added on both front-end and back ends. updates how UmbracoApplicationBuilder works in that it explicitly starts endpoints at the time of calling.
* Changes name to IUmbracoEndpointBuilder
* adds note
* Fixing tests, fixing error describers so there's 2x one for back office, one for members, fixes TryConvertTo, fixes login redirect
* fixing build
* Fixes keepalive, fixes PublicAccessMiddleware to not throw, updates startup code to be more clear and removes magic that registers middleware.
* adds note
* removes unused filter, fixes build
* fixes WebPath and tests
* Looks up entities in one query
* remove usings
* Fix test, remove stylesheet
* Set status code before we write to response to avoid error
* Ensures that users and members are validated when logging in. Shares more code between users and members.
* Fixes RepositoryCacheKeys to ensure the keys are normalized
* oops didn't mean to commit this
* Fix casing issues with caching, stop boxing value types for all cache operations, stop re-creating string keys in DefaultRepositoryCachePolicy
* bah, far out this keeps getting recommitted. sorry
Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2021-04-20 15:11:45 +10:00
|
|
|
Options.Create(new MemberPasswordConfigurationSettings()),
|
|
|
|
|
Mock.Of<IPublicAccessService>(),
|
|
|
|
|
Mock.Of<IHttpContextAccessor>());
|
2020-11-30 00:45:38 +00:00
|
|
|
|
|
|
|
|
[Test]
|
2021-03-14 21:59:30 +00:00
|
|
|
public async Task WhenPasswordSignInAsyncIsCalled_AndEverythingIsSetup_ThenASignInResultSucceededShouldBeReturnedAsync()
|
2020-11-30 00:45:38 +00:00
|
|
|
{
|
|
|
|
|
//arrange
|
2021-03-29 17:37:58 +11:00
|
|
|
var userId = "bo8w3d32q9b98";
|
2021-03-14 18:58:33 +00:00
|
|
|
MemberSignInManager sut = CreateSut();
|
|
|
|
|
var fakeUser = new MemberIdentityUser(777)
|
2020-12-01 19:47:56 +00:00
|
|
|
{
|
2021-03-14 21:59:30 +00:00
|
|
|
UserName = "TestUser",
|
2020-12-01 19:47:56 +00:00
|
|
|
};
|
2021-03-17 18:04:54 +00:00
|
|
|
var password = "testPassword";
|
|
|
|
|
var lockoutOnFailure = false;
|
|
|
|
|
var isPersistent = true;
|
2021-03-29 17:37:58 +11:00
|
|
|
|
|
|
|
|
_memberManager.Setup(x => x.GetUserIdAsync(It.IsAny<MemberIdentityUser>())).ReturnsAsync(userId);
|
|
|
|
|
_memberManager.Setup(x => x.GetUserNameAsync(It.IsAny<MemberIdentityUser>())).ReturnsAsync(fakeUser.UserName);
|
2021-03-14 22:19:27 +00:00
|
|
|
_memberManager.Setup(x => x.FindByNameAsync(It.IsAny<string>())).ReturnsAsync(fakeUser);
|
2021-03-17 18:28:56 +00:00
|
|
|
_memberManager.Setup(x => x.CheckPasswordAsync(fakeUser, password)).ReturnsAsync(true);
|
|
|
|
|
_memberManager.Setup(x => x.IsEmailConfirmedAsync(fakeUser)).ReturnsAsync(true);
|
|
|
|
|
_memberManager.Setup(x => x.IsLockedOutAsync(fakeUser)).ReturnsAsync(false);
|
2020-11-30 00:45:38 +00:00
|
|
|
|
|
|
|
|
//act
|
2021-03-17 18:04:54 +00:00
|
|
|
SignInResult actual = await sut.PasswordSignInAsync(fakeUser, password, isPersistent, lockoutOnFailure);
|
2020-11-30 00:45:38 +00:00
|
|
|
|
|
|
|
|
//assert
|
2021-03-14 18:58:33 +00:00
|
|
|
Assert.IsTrue(actual.Succeeded);
|
2020-11-30 00:45:38 +00:00
|
|
|
}
|
2021-03-14 21:59:30 +00:00
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task WhenPasswordSignInAsyncIsCalled_AndTheResultFails_ThenASignInFailedResultShouldBeReturnedAsync()
|
|
|
|
|
{
|
|
|
|
|
//arrange
|
|
|
|
|
MemberSignInManager sut = CreateSut();
|
|
|
|
|
var fakeUser = new MemberIdentityUser(777)
|
|
|
|
|
{
|
|
|
|
|
UserName = "TestUser",
|
|
|
|
|
};
|
2021-03-17 18:04:54 +00:00
|
|
|
var password = "testPassword";
|
|
|
|
|
var lockoutOnFailure = false;
|
|
|
|
|
var isPersistent = true;
|
2021-03-14 21:59:30 +00:00
|
|
|
|
|
|
|
|
//act
|
2021-03-17 18:04:54 +00:00
|
|
|
SignInResult actual = await sut.PasswordSignInAsync(fakeUser, password, isPersistent, lockoutOnFailure);
|
2021-03-14 21:59:30 +00:00
|
|
|
|
|
|
|
|
//assert
|
|
|
|
|
Assert.IsFalse(actual.Succeeded);
|
|
|
|
|
}
|
2020-11-30 00:45:38 +00:00
|
|
|
}
|
|
|
|
|
}
|