Files
Umbraco-CMS/tests/Umbraco.Tests.Common/Builders/UserGroupBuilder.cs
Bjarke Berg 2494d8c5aa Granular permissions in Management API (#15734)
* It builds..

* Added granular permissions

* Added granular permissions

* Rename content to document

* Added migration

* Fixed issues causing the migration from v13 was not possible.

* Merged Permissions and Granular Permissions in viewmodel

* Prepared the viewmodel to a future where permissions can be more types.

* OpenApi

* Allow to translate a single char to many strings

* Use frontend friendly values for known permissions

* Validate the documents exist

* Allow setting non-document settings

* Add "$type" when required

* Rename to presentation model and update OpenApi.json

* OpenApi.json

* Fix tests

* OpenAPI

* Fixed issues with upgrades

* Add the discriminator name

* Fixed issues that only happended on SqlServer

* Fixed queries for SqlServer

* Clean up

* More cleanup

* Fix issue when migrating sqlserver

* Split fallback permissions into own concept in view model

* Also split on current user

* Added a extenable pattern for mappers between DTO => Granular Permission => ViewModel and ViewModel => Granular Permission

* Fixed issue with new exists method, that did not take duplicate keys into account.

* Added sections to current user response model

* Formatting fixes

* Move class to its own file

* xml comment

---------

Co-authored-by: Zeegaan <skrivdetud@gmail.com>
2024-02-27 21:57:02 +01:00

164 lines
4.4 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System.Collections.Generic;
using System.Linq;
using Moq;
using Umbraco.Cms.Core.Models.Membership;
using Umbraco.Cms.Core.Strings;
using Umbraco.Cms.Tests.Common.Builders.Extensions;
using Umbraco.Cms.Tests.Common.Builders.Interfaces;
namespace Umbraco.Cms.Tests.Common.Builders;
public class UserGroupBuilder : UserGroupBuilder<object>
{
public UserGroupBuilder()
: base(null)
{
}
}
public class UserGroupBuilder<TParent>
: ChildBuilderBase<TParent, IUserGroup>,
IWithIdBuilder,
IWithIconBuilder,
IWithAliasBuilder,
IWithNameBuilder
{
private string _alias;
private IEnumerable<string> _allowedSections = Enumerable.Empty<string>();
private string _icon;
private int? _id;
private string _name;
private ISet<string> _permissions = new HashSet<string>();
private int? _startContentId;
private int? _startMediaId;
private string _suffix;
private int? _userCount;
public UserGroupBuilder(TParent parentBuilder)
: base(parentBuilder)
{
}
string IWithAliasBuilder.Alias
{
get => _alias;
set => _alias = value;
}
string IWithIconBuilder.Icon
{
get => _icon;
set => _icon = value;
}
int? IWithIdBuilder.Id
{
get => _id;
set => _id = value;
}
string IWithNameBuilder.Name
{
get => _name;
set => _name = value;
}
/// <summary>
/// Will suffix the name, email and username for testing.
/// </summary>
/// <param name="suffix">Suffix to add to user group properties.</param>
/// <returns>Current builder instance.</returns>
public UserGroupBuilder<TParent> WithSuffix(string suffix)
{
_suffix = suffix;
return this;
}
public UserGroupBuilder<TParent> WithUserCount(int userCount)
{
_userCount = userCount;
return this;
}
public UserGroupBuilder<TParent> WithPermissions(ISet<string> permissions)
{
_permissions = permissions;
return this;
}
public UserGroupBuilder<TParent> WithAllowedSections(IList<string> allowedSections)
{
_allowedSections = allowedSections;
return this;
}
public UserGroupBuilder<TParent> WithStartContentId(int startContentId)
{
_startContentId = startContentId;
return this;
}
public UserGroupBuilder<TParent> WithStartMediaId(int startMediaId)
{
_startMediaId = startMediaId;
return this;
}
public IReadOnlyUserGroup BuildReadOnly(IUserGroup userGroup) =>
Mock.Of<IReadOnlyUserGroup>(x =>
x.Permissions == userGroup.Permissions &&
x.Alias == userGroup.Alias &&
x.Icon == userGroup.Icon &&
x.Name == userGroup.Name &&
x.StartContentId == userGroup.StartContentId &&
x.StartMediaId == userGroup.StartMediaId &&
x.AllowedSections == userGroup.AllowedSections &&
x.Id == userGroup.Id);
public override IUserGroup Build()
{
var id = _id ?? 0;
var name = _name ?? "TestUserGroup" + _suffix;
var alias = _alias ?? "testUserGroup" + _suffix;
var userCount = _userCount ?? 0;
var startContentId = _startContentId ?? -1;
var startMediaId = _startMediaId ?? -1;
var icon = _icon ?? "icon-group";
var shortStringHelper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig());
var userGroup = new UserGroup(shortStringHelper, userCount, alias, name, icon)
{
Id = id,
StartContentId = startContentId,
StartMediaId = startMediaId
};
userGroup.Permissions = _permissions;
foreach (var section in _allowedSections)
{
userGroup.AddAllowedSection(section);
}
return userGroup;
}
public static UserGroup CreateUserGroup(
string alias = "testGroup",
string name = "Test Group",
string suffix = "",
ISet<string>? permissions = null,
string[] allowedSections = null) =>
(UserGroup)new UserGroupBuilder()
.WithAlias(alias + suffix)
.WithName(name + suffix)
.WithPermissions(permissions ?? new[] { "A", "B", "C" }.ToHashSet())
.WithAllowedSections(allowedSections ?? new[] { "content", "media" })
.Build();
}