Updates to unit store tests and managing null username in members user store

This commit is contained in:
Emma Garland
2021-03-08 17:12:30 +00:00
parent a53688c503
commit 1df78c1321
4 changed files with 38 additions and 10 deletions

View File

@@ -19,6 +19,7 @@ namespace Umbraco.Cms.Core.Security
//TODO: How revealing can the error messages be?
private readonly IdentityError _intParseError = new IdentityError { Code = "IdentityIdParseError", Description = "Cannot parse ID to int" };
private readonly IdentityError _memberGroupNotFoundError = new IdentityError { Code = "IdentityMemberGroupNotFound", Description = "Member group not found" };
private const string genericIdentityErrorCode = "IdentityErrorUserStore";
public MemberRoleStore(IMemberGroupService memberGroupService, IdentityErrorDescriber errorDescriber)
{
@@ -57,7 +58,7 @@ namespace Umbraco.Cms.Core.Security
}
catch (Exception ex)
{
return Task.FromResult(IdentityResult.Failed(new IdentityError { Code = ex.Message, Description = ex.Message }));
return Task.FromResult(IdentityResult.Failed(new IdentityError { Code = genericIdentityErrorCode, Description = ex.Message }));
}
}

View File

@@ -53,7 +53,30 @@ namespace Umbraco.Cms.Core.Security
public override Task<string> GetNormalizedUserNameAsync(MemberIdentityUser user, CancellationToken cancellationToken = default) => GetUserNameAsync(user, cancellationToken);
/// <inheritdoc />
public override Task SetNormalizedUserNameAsync(MemberIdentityUser user, string normalizedName, CancellationToken cancellationToken = default) => SetUserNameAsync(user, normalizedName, cancellationToken);
public override Task SetNormalizedUserNameAsync(MemberIdentityUser user, string normalizedName,
CancellationToken cancellationToken = default)
{
try
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
if (normalizedName == null)
{
throw new ArgumentNullException(nameof(normalizedName));
}
return SetUserNameAsync(user, normalizedName, cancellationToken);
}
catch (Exception ex)
{
return Task.FromResult(IdentityResult.Failed(new IdentityError { Code = genericIdentityErrorCode, Description = ex.Message }));
}
}
/// <inheritdoc />
public override Task<IdentityResult> CreateAsync(MemberIdentityUser user, CancellationToken cancellationToken = default)

View File

@@ -67,13 +67,15 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.Security
{
// arrange
MemberUserStore sut = CreateSut();
CancellationToken fakeCancellationToken = new CancellationToken() { };
var fakeCancellationToken = new CancellationToken() { };
// act
Action actual = () => sut.SetNormalizedUserNameAsync(null, "username", fakeCancellationToken);
var actual = (Task<IdentityResult>)sut.SetNormalizedUserNameAsync(null, "username", fakeCancellationToken);
// assert
Assert.That(actual, Throws.ArgumentNullException);
Assert.IsFalse(actual.Result.Succeeded);
Assert.IsTrue(actual.Result.Errors.Any(x => x.Code == "IdentityErrorUserStore" && x.Description == "Value cannot be null. (Parameter 'user')"));
_mockMemberService.VerifyNoOtherCalls();
}
@@ -86,10 +88,12 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.Security
var fakeUser = new MemberIdentityUser() { };
// act
Task actual = sut.SetNormalizedUserNameAsync(fakeUser, null, fakeCancellationToken);
var actual = (Task<IdentityResult>)sut.SetNormalizedUserNameAsync(fakeUser, null, fakeCancellationToken);
// assert
Assert.AreEqual(null, actual);
Assert.IsFalse(actual.Result.Succeeded);
Assert.IsTrue(actual.Result.Errors.Any(x => x.Code == "IdentityErrorUserStore" && x.Description == "Value cannot be null. (Parameter 'normalizedName')"));
_mockMemberService.VerifyNoOtherCalls();
}
[Test]

View File

@@ -30,14 +30,14 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.Security
{
// arrange
MemberRoleStore<IdentityRole> sut = CreateSut();
CancellationToken fakeCancellationToken = new CancellationToken() { };
var fakeCancellationToken = new CancellationToken();
// act
Task<IdentityResult> actual = sut.CreateAsync(null, fakeCancellationToken);
// assert
Assert.IsTrue(actual.Result.Succeeded == false);
Assert.IsTrue(actual.Result.Errors.Any(x => x.Code == "IdentityMemberGroupNotFound" && x.Description == "Member group not found"));
Assert.IsFalse(actual.Result.Succeeded);
Assert.IsTrue(actual.Result.Errors.Any(x => x.Code == "IdentityErrorUserStore" && x.Description == "Value cannot be null. (Parameter 'role')"));
}