Throw better exception when security role isn't found on create#315
Merged
Throw better exception when security role isn't found on create#315
Conversation
Contributor
There was a problem hiding this comment.
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
AddPrinciplePrivlieges→AddPrinciplePrivileges. - Validate role IDs during privilege aggregation using
TryGetValueand throw a descriptiveMockupExceptionwhen 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, thesecRoles.Any(s => !SecurityRoles.ContainsKey(s))check is effectively redundant becauseAddPrinciplePrivileges(entRef.Id, secRoles)now validates roles viaTryGetValueand throws aMockupExceptionwith 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
AddPrinciplePrivilegesto throw aMockupExceptionif a security role ID does not exist, with a clear error message that includes the missing role ID.TestCreateUserWithNonExistentSecurityRoleThrowsMockupExceptionto verify that attempting to create a user with a non-existent security role throws the expected exception and error message.Code clarity and consistency:
AddPrinciplePrivliegestoAddPrinciplePrivilegesand updated all relevant references. [1] [2]