2025-03-21 18:02:31 +01:00
|
|
|
using NUnit.Framework;
|
2023-03-29 08:14:47 +02:00
|
|
|
using Umbraco.Cms.Core;
|
|
|
|
|
using Umbraco.Cms.Core.Models;
|
|
|
|
|
using Umbraco.Cms.Core.Models.Membership;
|
|
|
|
|
using Umbraco.Cms.Core.Services;
|
|
|
|
|
using Umbraco.Cms.Tests.Common.Testing;
|
|
|
|
|
using Umbraco.Cms.Tests.Integration.Testing;
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Services;
|
|
|
|
|
|
|
|
|
|
[TestFixture]
|
|
|
|
|
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
|
2025-03-21 18:02:31 +01:00
|
|
|
internal sealed class UserIdKeyResolverTests : UmbracoIntegrationTest
|
2023-03-29 08:14:47 +02:00
|
|
|
{
|
|
|
|
|
private IUserService UserService => GetRequiredService<IUserService>();
|
|
|
|
|
|
|
|
|
|
private IUserGroupService UserGroupService => GetRequiredService<IUserGroupService>();
|
|
|
|
|
|
|
|
|
|
private IUserIdKeyResolver UserIdKeyResolver => GetRequiredService<IUserIdKeyResolver>();
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Can_Resolve_Id_To_Key()
|
|
|
|
|
{
|
|
|
|
|
var userGroup = await UserGroupService.GetAsync(Constants.Security.AdminGroupAlias);
|
|
|
|
|
var userCreateModel = new UserCreateModel
|
|
|
|
|
{
|
|
|
|
|
UserName = "test@test.com",
|
|
|
|
|
Email = "test@test.com",
|
|
|
|
|
Name = "Test Mc. Gee",
|
2023-04-04 15:41:12 +02:00
|
|
|
UserGroupKeys = new HashSet<Guid> { userGroup.Key }
|
2023-03-29 08:14:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var creationResult = await UserService.CreateAsync(Constants.Security.SuperUserKey, userCreateModel);
|
|
|
|
|
Assert.IsTrue(creationResult.Success);
|
|
|
|
|
var createdUser = creationResult.Result.CreatedUser;
|
|
|
|
|
Assert.IsNotNull(createdUser);
|
|
|
|
|
|
|
|
|
|
var resolvedKey = await UserIdKeyResolver.GetAsync(createdUser.Id);
|
|
|
|
|
Assert.AreEqual(createdUser.Key, resolvedKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Can_Resolve_Key_To_Id()
|
|
|
|
|
{
|
|
|
|
|
var userGroup = await UserGroupService.GetAsync(Constants.Security.AdminGroupAlias);
|
|
|
|
|
var userCreateModel = new UserCreateModel
|
|
|
|
|
{
|
|
|
|
|
UserName = "test@test.com",
|
|
|
|
|
Email = "test@test.com",
|
|
|
|
|
Name = "Test Mc. Gee",
|
2023-04-04 15:41:12 +02:00
|
|
|
UserGroupKeys = new HashSet<Guid> { userGroup.Key }
|
2023-03-29 08:14:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var creationResult = await UserService.CreateAsync(Constants.Security.SuperUserKey, userCreateModel);
|
|
|
|
|
Assert.IsTrue(creationResult.Success);
|
|
|
|
|
var createdUser = creationResult.Result.CreatedUser;
|
|
|
|
|
Assert.IsNotNull(createdUser);
|
|
|
|
|
|
|
|
|
|
var resolvedId = await UserIdKeyResolver.GetAsync(createdUser.Key);
|
|
|
|
|
Assert.AreEqual(createdUser.Id, resolvedId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2023-04-14 12:03:25 +02:00
|
|
|
public async Task Can_Resolve_Super_User_Key_To_Id()
|
2023-03-29 08:14:47 +02:00
|
|
|
{
|
2023-04-14 12:03:25 +02:00
|
|
|
var resolvedId = await UserIdKeyResolver.GetAsync(Constants.Security.SuperUserKey);
|
|
|
|
|
Assert.AreEqual(Constants.Security.SuperUserId, resolvedId);
|
2023-03-29 08:14:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2023-04-14 12:03:25 +02:00
|
|
|
public async Task Can_Resolve_Super_User_Id_To_Key()
|
2023-03-29 08:14:47 +02:00
|
|
|
{
|
2023-04-14 12:03:25 +02:00
|
|
|
var resolvedKey = await UserIdKeyResolver.GetAsync(Constants.Security.SuperUserId);
|
|
|
|
|
Assert.AreEqual(Constants.Security.SuperUserKey, resolvedKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2025-03-03 06:47:37 +01:00
|
|
|
public Task Unknown_Key_Throws()
|
2023-04-14 12:03:25 +02:00
|
|
|
{
|
|
|
|
|
Assert.ThrowsAsync<InvalidOperationException>(async () => await UserIdKeyResolver.GetAsync(Guid.NewGuid()));
|
2025-03-03 06:47:37 +01:00
|
|
|
return Task.CompletedTask;
|
2023-04-14 12:03:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2025-03-03 06:47:37 +01:00
|
|
|
public Task Unknown_Id_Throws()
|
2023-04-14 12:03:25 +02:00
|
|
|
{
|
2025-03-03 06:47:37 +01:00
|
|
|
Assert.ThrowsAsync<InvalidOperationException>(async () => await UserIdKeyResolver.GetAsync(1234567890));
|
|
|
|
|
return Task.CompletedTask;
|
2023-03-29 08:14:47 +02:00
|
|
|
}
|
|
|
|
|
}
|