V14: Move towards get guid (#15889)

* Implement using keymap for member

* Remove current usages of GetUserById

* User userId resolver to resolve user key

* Refactor user repository to use GUID not int

* Add happy path test

* Remove user in cache when user gets updated

* Use await in async method

* Fix up according to review

* Update IMetricsConsentService.cs to have async method

* Fix according to review

* Fix more according to comments

* Revert "Fix up according to review"

This reverts commit a75acaaa

* Get current backoffice user from method

* Update user repository delete functionality

* Fix up more test

* Try to get user by id if key fails

* Add user key as required claim

* Fix tests

* Don't set claim in BackofficeController

* Create constant for the Sub claim

---------

Co-authored-by: kjac <kja@umbraco.dk>
This commit is contained in:
Nikolaj Geisle
2024-04-11 13:53:34 +02:00
committed by GitHub
parent 0b62df2bb4
commit d5809da665
31 changed files with 244 additions and 169 deletions

View File

@@ -18,9 +18,9 @@ public class MetricsConsentServiceTest : UmbracoIntegrationTest
[TestCase(TelemetryLevel.Minimal)]
[TestCase(TelemetryLevel.Basic)]
[TestCase(TelemetryLevel.Detailed)]
public void Can_Store_Consent(TelemetryLevel level)
public async Task Can_Store_Consent(TelemetryLevel level)
{
MetricsConsentService.SetConsentLevel(level);
await MetricsConsentService.SetConsentLevelAsync(level);
var actual = MetricsConsentService.GetConsentLevel();
Assert.IsNotNull(actual);
@@ -28,9 +28,9 @@ public class MetricsConsentServiceTest : UmbracoIntegrationTest
}
[Test]
public void Enum_Stored_as_string()
public async Task Enum_Stored_as_string()
{
MetricsConsentService.SetConsentLevel(TelemetryLevel.Detailed);
await MetricsConsentService.SetConsentLevelAsync(TelemetryLevel.Detailed);
var stringValue = KeyValueService.GetValue(Cms.Core.Services.MetricsConsentService.Key);

View File

@@ -126,6 +126,34 @@ public partial class UserServiceCrudTests
Assert.AreEqual(email, updatedUser.Email);
}
[Test]
public async Task Can_Update_User_Name()
{
const string userName = "UpdateUserName";
const string name = "UpdatedName";
const string email = "update@email.com";
var userService = CreateUserService(securitySettings: new SecuritySettings { UsernameIsEmail = false });
var (updateModel, createdUser) = await CreateUserForUpdate(userService);
updateModel.UserName = userName;
updateModel.Email = email;
updateModel.Name = name;
var result = await userService.UpdateAsync(Constants.Security.SuperUserKey, updateModel);
Assert.IsTrue(result.Success);
var updatedUser = await userService.GetAsync(createdUser.Key);
Assert.Multiple(() =>
{
Assert.IsNotNull(updatedUser);
Assert.AreEqual(userName, updatedUser.Username);
Assert.AreEqual(email, updatedUser.Email);
Assert.AreEqual(name, updatedUser.Name);
});
}
[Test]
public async Task Cannot_Change_Email_To_Duplicate_Email_On_Update()
{

View File

@@ -23,7 +23,7 @@ public class TelemetryServiceTests : UmbracoIntegrationTest
private IMetricsConsentService MetricsConsentService => GetRequiredService<IMetricsConsentService>();
[Test]
public void Expected_Detailed_Telemetry_Exists()
public async Task Expected_Detailed_Telemetry_Exists()
{
var expectedData = new[]
{
@@ -54,7 +54,7 @@ public class TelemetryServiceTests : UmbracoIntegrationTest
Constants.Telemetry.DeliveryApiPublicAccess
};
MetricsConsentService.SetConsentLevel(TelemetryLevel.Detailed);
await MetricsConsentService.SetConsentLevelAsync(TelemetryLevel.Detailed);
var success = TelemetryService.TryGetTelemetryReportData(out var telemetryReportData);
var detailed = telemetryReportData.Detailed.ToArray();

View File

@@ -85,8 +85,7 @@ public class BackOfficeExamineSearcherTests : ExamineBaseTest
private async Task SetupUserIdentity(string userId)
{
var identity =
await BackOfficeUserStore.FindByIdAsync(userId, CancellationToken.None);
var identity = await BackOfficeUserStore.FindByIdAsync(userId, CancellationToken.None);
await BackOfficeSignInManager.SignInAsync(identity, false);
var principal = await BackOfficeSignInManager.CreateUserPrincipalAsync(identity);
HttpContextAccessor.HttpContext.SetPrincipalForRequest(principal);
@@ -595,7 +594,7 @@ public class BackOfficeExamineSearcherTests : ExamineBaseTest
public async Task Check_All_Indexed_Values_For_Published_Content_With_No_Properties()
{
// Arrange
await SetupUserIdentity(Constants.Security.SuperUserIdAsString);
await SetupUserIdentity(Constants.Security.SuperUserKey.ToString());
const string contentName = "TestContent";

View File

@@ -126,7 +126,7 @@ public class UserRepositoryTest : UmbracoIntegrationTest
repository.Save(user);
// Act
var resolved = repository.Get(user.Id);
var resolved = repository.Get(user.Key);
var dirty = ((User)resolved).IsDirty();
// Assert
@@ -148,7 +148,7 @@ public class UserRepositoryTest : UmbracoIntegrationTest
// Act
repository.Save(user);
var id = user.Id;
var id = user.Key;
var mockRuntimeState = CreateMockRuntimeState(RuntimeLevel.Run);
@@ -185,7 +185,7 @@ public class UserRepositoryTest : UmbracoIntegrationTest
var user = CreateAndCommitUserWithGroup(repository, userGroupRepository);
// Act
var updatedItem = repository.Get(user.Id);
var updatedItem = repository.Get(user.Key);
// TODO: this test cannot work, user has 2 sections but the way it's created,
// they don't show, so the comparison with updatedItem fails - fix!
@@ -227,7 +227,7 @@ public class UserRepositoryTest : UmbracoIntegrationTest
var users = CreateAndCommitMultipleUsers(repository);
// Act
var result = repository.GetMany(users[0].Id, users[1].Id).ToArray();
var result = repository.GetMany(users[0].Key, users[1].Key).ToArray();
// Assert
Assert.That(result, Is.Not.Null);
@@ -269,7 +269,7 @@ public class UserRepositoryTest : UmbracoIntegrationTest
var users = CreateAndCommitMultipleUsers(repository);
// Act
var exists = repository.Exists(users[0].Id);
var exists = repository.Exists(users[0].Key);
// Assert
Assert.That(exists, Is.True);
@@ -396,7 +396,7 @@ public class UserRepositoryTest : UmbracoIntegrationTest
repository.Save(user);
// Get the user
var updatedUser = repository.Get(user.Id);
var updatedUser = repository.Get(user.Key);
// Ensure the Security Stamp is invalidated & no longer the same
Assert.AreNotEqual(originalSecurityStamp, updatedUser.SecurityStamp);
@@ -460,7 +460,7 @@ public class UserRepositoryTest : UmbracoIntegrationTest
var user = CreateAndCommitUserWithGroup(userRepository, userGroupRepository);
// Act
var resolved = (User)userRepository.Get(user.Id);
var resolved = (User)userRepository.Get(user.Key);
resolved.Name = "New Name";
@@ -478,7 +478,7 @@ public class UserRepositoryTest : UmbracoIntegrationTest
userRepository.Save(resolved);
var updatedItem = (User)userRepository.Get(user.Id);
var updatedItem = (User)userRepository.Get(user.Key);
// Assert
Assert.That(updatedItem.Id, Is.EqualTo(resolved.Id));

View File

@@ -63,7 +63,7 @@ public class ScopedRepositoryTests : UmbracoIntegrationTest
[TestCase(true)]
[TestCase(false)]
public void DefaultRepositoryCachePolicy(bool complete)
public async Task DefaultRepositoryCachePolicy(bool complete)
{
var scopeProvider = (ScopeProvider)ScopeProvider;
var service = (UserService)UserService;
@@ -72,13 +72,13 @@ public class ScopedRepositoryTests : UmbracoIntegrationTest
service.Save(user);
// User has been saved so the cache has been cleared of it
var globalCached = (IUser)globalCache.Get(GetCacheIdKey<IUser>(user.Id), () => null);
var globalCached = (IUser)globalCache.Get(GetCacheIdKey<IUser>(user.Key), () => null);
Assert.IsNull(globalCached);
// Get user again to load it into the cache again, this also ensure we don't modify the one that's in the cache.
user = service.GetUserById(user.Id);
user = await service.GetAsync(user.Key);
// global cache contains the entity
globalCached = (IUser)globalCache.Get(GetCacheIdKey<IUser>(user.Id), () => null);
globalCached = (IUser)globalCache.Get(GetCacheIdKey<IUser>(user.Key), () => null);
Assert.IsNotNull(globalCached);
Assert.AreEqual(user.Id, globalCached.Id);
Assert.AreEqual("name", globalCached.Name);
@@ -104,7 +104,7 @@ public class ScopedRepositoryTests : UmbracoIntegrationTest
Assert.AreEqual("changed", scopeCached.Name);
// global cache is unchanged
globalCached = (IUser)globalCache.Get(GetCacheIdKey<IUser>(user.Id), () => null);
globalCached = (IUser)globalCache.Get(GetCacheIdKey<IUser>(user.Key), () => null);
Assert.IsNotNull(globalCached);
Assert.AreEqual(user.Id, globalCached.Id);
Assert.AreEqual("name", globalCached.Name);
@@ -117,7 +117,7 @@ public class ScopedRepositoryTests : UmbracoIntegrationTest
Assert.IsNull(scopeProvider.AmbientScope);
globalCached = (IUser)globalCache.Get(GetCacheIdKey<IUser>(user.Id), () => null);
globalCached = (IUser)globalCache.Get(GetCacheIdKey<IUser>(user.Key), () => null);
if (complete)
{
// global cache has been cleared
@@ -130,11 +130,11 @@ public class ScopedRepositoryTests : UmbracoIntegrationTest
}
// get again, updated if completed
user = service.GetUserById(user.Id);
user = await service.GetAsync(user.Key);
Assert.AreEqual(complete ? "changed" : "name", user.Name);
// global cache contains the entity again
globalCached = (IUser)globalCache.Get(GetCacheIdKey<IUser>(user.Id), () => null);
globalCached = (IUser)globalCache.Get(GetCacheIdKey<IUser>(user.Key), () => null);
Assert.IsNotNull(globalCached);
Assert.AreEqual(user.Id, globalCached.Id);
Assert.AreEqual(complete ? "changed" : "name", globalCached.Name);

View File

@@ -111,6 +111,7 @@ public class UmbracoBackOfficeIdentityTests
claimsIdentity.AddRequiredClaims(
"1234",
Guid.NewGuid(),
"testing",
"hello world",
new[] { 654 },
@@ -120,7 +121,7 @@ public class UmbracoBackOfficeIdentityTests
new[] { "content", "media" },
new[] { "admin" });
Assert.AreEqual(12, claimsIdentity.Claims.Count());
Assert.AreEqual(13, claimsIdentity.Claims.Count());
Assert.IsNull(claimsIdentity.Actor);
}
}

View File

@@ -18,6 +18,7 @@ public class ClaimsPrincipalExtensionsTests
var backOfficeIdentity = new ClaimsIdentity();
backOfficeIdentity.AddRequiredClaims(
Constants.Security.SuperUserIdAsString,
Constants.Security.SuperUserKey,
"test",
"test",
Enumerable.Empty<int>(),
@@ -55,6 +56,7 @@ public class ClaimsPrincipalExtensionsTests
var backOfficeIdentity = new ClaimsIdentity();
backOfficeIdentity.AddRequiredClaims(
Constants.Security.SuperUserIdAsString,
Constants.Security.SuperUserKey,
"test",
"test",
Enumerable.Empty<int>(),