From 1df78c1321fa31cec091240d63e77f2af2e087f0 Mon Sep 17 00:00:00 2001 From: Emma Garland Date: Mon, 8 Mar 2021 17:12:30 +0000 Subject: [PATCH] Updates to unit store tests and managing null username in members user store --- .../Security/MemberRoleStore.cs | 3 ++- .../Security/MemberUserStore.cs | 25 ++++++++++++++++++- .../Security/MemberIdentityUserStoreTests.cs | 14 +++++++---- .../Security/MemberRoleStoreTests.cs | 6 ++--- 4 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/Umbraco.Infrastructure/Security/MemberRoleStore.cs b/src/Umbraco.Infrastructure/Security/MemberRoleStore.cs index 466b2435f6..9b0618e8dd 100644 --- a/src/Umbraco.Infrastructure/Security/MemberRoleStore.cs +++ b/src/Umbraco.Infrastructure/Security/MemberRoleStore.cs @@ -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 })); } } diff --git a/src/Umbraco.Infrastructure/Security/MemberUserStore.cs b/src/Umbraco.Infrastructure/Security/MemberUserStore.cs index 6be981b3d1..233f053333 100644 --- a/src/Umbraco.Infrastructure/Security/MemberUserStore.cs +++ b/src/Umbraco.Infrastructure/Security/MemberUserStore.cs @@ -53,7 +53,30 @@ namespace Umbraco.Cms.Core.Security public override Task GetNormalizedUserNameAsync(MemberIdentityUser user, CancellationToken cancellationToken = default) => GetUserNameAsync(user, cancellationToken); /// - 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 })); + } + } /// public override Task CreateAsync(MemberIdentityUser user, CancellationToken cancellationToken = default) diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Security/MemberIdentityUserStoreTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Security/MemberIdentityUserStoreTests.cs index 5d9303b908..de8ba1e36b 100644 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Security/MemberIdentityUserStoreTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Security/MemberIdentityUserStoreTests.cs @@ -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)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)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] diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Security/MemberRoleStoreTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Security/MemberRoleStoreTests.cs index d89bda494c..2c3cb901fc 100644 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Security/MemberRoleStoreTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Security/MemberRoleStoreTests.cs @@ -30,14 +30,14 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.Security { // arrange MemberRoleStore sut = CreateSut(); - CancellationToken fakeCancellationToken = new CancellationToken() { }; + var fakeCancellationToken = new CancellationToken(); // act Task 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')")); }