Skip to content

Commit

Permalink
Remove Localizer from System Roles (#17388)
Browse files Browse the repository at this point in the history
Co-authored-by: Hisham Bin Ateya <[email protected]>
  • Loading branch information
MikeAlhayek and hishamco authored Jan 23, 2025
1 parent e0842a9 commit 9dfab0e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Frozen;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options;
using OrchardCore.Environment.Shell;
using OrchardCore.Security;
Expand All @@ -13,8 +12,7 @@ public sealed class DefaultSystemRoleProvider : ISystemRoleProvider

public DefaultSystemRoleProvider(
ShellSettings shellSettings,
IOptions<SystemRoleOptions> options,
IStringLocalizer<DefaultSystemRoleProvider> S)
IOptions<SystemRoleOptions> options)
{
var adminRoleName = shellSettings["AdminRoleName"];
if (string.IsNullOrWhiteSpace(adminRoleName))
Expand All @@ -30,7 +28,7 @@ public DefaultSystemRoleProvider(
_adminRole = new Role
{
RoleName = adminRoleName,
RoleDescription = S["A system role that grants all permissions to the assigned users."]
RoleDescription = "A system role that grants all permissions to the assigned users.",
};

_systemRoles = new Dictionary<string, Role>()
Expand All @@ -40,14 +38,14 @@ public DefaultSystemRoleProvider(
OrchardCoreConstants.Roles.Authenticated, new Role
{
RoleName = OrchardCoreConstants.Roles.Authenticated,
RoleDescription = S["A system role representing all authenticated users."]
RoleDescription = "A system role representing all authenticated users.",
}
},
{
OrchardCoreConstants.Roles.Anonymous, new Role
{
RoleName = OrchardCoreConstants.Roles.Anonymous,
RoleDescription = S["A system role representing all non-authenticated users."]
RoleDescription = "A system role representing all non-authenticated users.",
}
}
}.ToFrozenDictionary(StringComparer.OrdinalIgnoreCase);
Expand Down
51 changes: 38 additions & 13 deletions test/OrchardCore.Tests/Roles/DefaultSystemRoleProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ namespace OrchardCore.Roles.Tests;
public class DefaultSystemRoleProviderTests
{
[Fact]
public void GetSystemRoles_Have_Administrator_Authenticated_Anonymous()
public void GetSystemRoles_WhenCalledByDefault_ContainsAdministratorAuthenticatedAndAnonymousRoles()
{
// Arrange
var stringLocalizer = Mock.Of<IStringLocalizer<DefaultSystemRoleProvider>>();
var shellSettings = new ShellSettings();
var options = new Mock<IOptions<SystemRoleOptions>>();
options.Setup(o => o.Value)
.Returns(new SystemRoleOptions());

var provider = new DefaultSystemRoleProvider(shellSettings, options.Object, stringLocalizer);
var provider = new DefaultSystemRoleProvider(shellSettings, options.Object);

// Act
var roles = provider.GetSystemRoles();
Expand All @@ -27,10 +26,9 @@ public void GetSystemRoles_Have_Administrator_Authenticated_Anonymous()
}

[Fact]
public void GetAdminRole_FromConfiguredAdminRole()
public void GetAdminRole_FromOptions_ReturnAdminRoleName()
{
// Arrange
var stringLocalizer = Mock.Of<IStringLocalizer<DefaultSystemRoleProvider>>();
var shellSettings = new ShellSettings();
var configureSystemAdminRoleName = "SystemAdmin";
var options = new Mock<IOptions<SystemRoleOptions>>();
Expand All @@ -40,7 +38,7 @@ public void GetAdminRole_FromConfiguredAdminRole()
SystemAdminRoleName = configureSystemAdminRoleName,
});

var provider = new DefaultSystemRoleProvider(shellSettings, options.Object, stringLocalizer);
var provider = new DefaultSystemRoleProvider(shellSettings, options.Object);

// Act
var role = provider.GetAdminRole();
Expand All @@ -51,19 +49,18 @@ public void GetAdminRole_FromConfiguredAdminRole()
}

[Fact]
public void GetAdminRole_FromAppSettings()
public void GetAdminRole_FromTheTenantSettings_ReturnsAdminRoleName()
{
// Arrange
var adminRoleName = "Foo";
var stringLocalizer = Mock.Of<IStringLocalizer<DefaultSystemRoleProvider>>();
var shellSettings = new ShellSettings();
shellSettings["AdminRoleName"] = adminRoleName;

var options = new Mock<IOptions<SystemRoleOptions>>();
options.Setup(o => o.Value)
.Returns(new SystemRoleOptions());

var provider = new DefaultSystemRoleProvider(shellSettings, options.Object, stringLocalizer);
var provider = new DefaultSystemRoleProvider(shellSettings, options.Object);

// Act
var role = provider.GetAdminRole();
Expand All @@ -84,20 +81,48 @@ public void GetAdminRole_FromAppSettings()
[InlineData("TEST", false)]
[InlineData("TesT", false)]
[InlineData("test", false)]
public void IsSystemRole_ReturnsTrue_IfTheRoleExists(string roleName, bool expectedResult)
public void IsSystemRole_IfTheRoleExists_ReturnsTrue(string roleName, bool expectedResult)
{
// Arrange
var stringLocalizer = Mock.Of<IStringLocalizer<DefaultSystemRoleProvider>>();
var shellSettings = new ShellSettings();

var options = new Mock<IOptions<SystemRoleOptions>>();
options.Setup(o => o.Value)
.Returns(new SystemRoleOptions());

var provider = new DefaultSystemRoleProvider(shellSettings, options.Object);

// Act
var result = provider.IsSystemRole(roleName);

// Assert
Assert.Equal(expectedResult, result);
}

[Theory]
[InlineData("Administrator", true)]
[InlineData("ADMINISTRATOR", true)]
[InlineData("administrator", true)]
[InlineData("AdminiSTratoR", true)]
[InlineData("Test", false)]
[InlineData("TEST", false)]
[InlineData("TesT", false)]
[InlineData("test", false)]
public void IsAdminRole_WhenCalled_ReturnsAdministrator(string roleName, bool expectedResult)
{
// Arrange
var shellSettings = new ShellSettings();

var options = new Mock<IOptions<SystemRoleOptions>>();
options.Setup(o => o.Value)
.Returns(new SystemRoleOptions());

var provider = new DefaultSystemRoleProvider(shellSettings, options.Object);

// Act
var provider = new DefaultSystemRoleProvider(shellSettings, options.Object, stringLocalizer);
var result = provider.IsAdminRole(roleName);

// Assert
Assert.Equal(expectedResult, provider.IsSystemRole(roleName));
Assert.Equal(expectedResult, result);
}
}

This file was deleted.

0 comments on commit 9dfab0e

Please sign in to comment.