Skip to content

Throw better exception when security role isn't found on create#315

Merged
mkholt merged 2 commits intomasterfrom
feature/missing-security-role
Mar 10, 2026
Merged

Throw better exception when security role isn't found on create#315
mkholt merged 2 commits intomasterfrom
feature/missing-security-role

Conversation

@mkholt
Copy link
Member

@mkholt mkholt commented Mar 10, 2026

This pull request primarily improves error handling and code clarity in the security role management logic, and adds a new test to ensure correct behavior when assigning non-existent security roles. The main changes include fixing a method name typo, adding a check for missing security roles, and introducing a test to verify that the correct exception is thrown in such cases.

Error handling and validation:

  • Added a check in AddPrinciplePrivileges to throw a MockupException if a security role ID does not exist, with a clear error message that includes the missing role ID.
  • Added a new test TestCreateUserWithNonExistentSecurityRoleThrowsMockupException to verify that attempting to create a user with a non-existent security role throws the expected exception and error message.

Code clarity and consistency:

  • Fixed a typo in the method name from AddPrinciplePrivlieges to AddPrinciplePrivileges and updated all relevant references. [1] [2]

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves security role assignment error handling so creating/assigning principals with unknown security role IDs throws a clearer MockupException, and adds regression coverage to ensure the behavior stays consistent.

Changes:

  • Rename typo’d helper method AddPrinciplePrivliegesAddPrinciplePrivileges.
  • Validate role IDs during privilege aggregation using TryGetValue and throw a descriptive MockupException when a role is missing.
  • Add a unit test asserting the exception type and message contents when creating a user with a non-existent role.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
tests/XrmMockup365Test/TestSecurityRoles.cs Adds a regression test for creating a user with an unknown security role ID.
src/XrmMockup365/Security.cs Improves missing-role handling and fixes method name typo in security role privilege aggregation.
Comments suppressed due to low confidence (1)

src/XrmMockup365/Security.cs:390

  • In SetSecurityRole, the secRoles.Any(s => !SecurityRoles.ContainsKey(s)) check is effectively redundant because AddPrinciplePrivileges(entRef.Id, secRoles) now validates roles via TryGetValue and throws a MockupException with details when a role is missing. Consider removing this follow-up check (or moving it before the call) to avoid dead/duplicate validation and a generic error message that likely won’t be hit.
            AddPrinciplePrivileges(entRef.Id, secRoles);
            if (secRoles.Any(s => !SecurityRoles.ContainsKey(s)))
            {
                throw new MockupException($"Unknown security role");
            }

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mkholt mkholt merged commit 3a43740 into master Mar 10, 2026
2 checks passed
@mkholt mkholt deleted the feature/missing-security-role branch March 10, 2026 08:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants