SaveRolesForUserEndpoint ignores Identity results and documents the wrong error status

Open
#321 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
Half a day
Newbie friendliness
62/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
csharp, rails

Research direction

Start in src/PublicApi/UserManagementEndpoints/SaveRolesForUserEndpoint.cs, where AddToRolesAsync and RemoveFromRolesAsync are called and their IdentityResult values discarded, and where the Produces declarations are set. Then read the existing tests in tests/PublicApiIntegrationTests/UserManagementEndpoints/SaveRolesForUserEndpointTest.cs. Done when an unknown role returns 400 or 404, failed IdentityResults return a non-success status, the Produces declarations match, and the new unknown-role and already-assigned tests pass.

Written by the indexing model from the issue text.

Description

bug

Problem

PUT api/users/{userId}/roles (src/PublicApi/UserManagementEndpoints/SaveRolesForUserEndpoint.cs):

  1. Unknown role causes a 500. If RolesToAdd contains a role that doesn't exist, UserManager.AddToRolesAsync throws InvalidOperationException ("Role X does not exist"), and ExceptionMiddleware turns that into 500 Internal Server Error.
  2. Failed results are ignored. The IdentityResult values from AddToRolesAsync and RemoveFromRolesAsync are thrown away. Adding a role the user already has, or removing one they don't have, fails in Identity, but the endpoint returns 200 OK. If the add succeeds and the remove fails, the user is left partly updated.
  3. Wrong status in the OpenAPI description. The description declares 409 Conflict, but the endpoint never returns it. When the target is the default admin user it returns 400 Bad Request, which isn't declared at all.
  4. Generic exception. When a user has no UserName, the endpoint throws a plain System.Exception("Unknown user to update"), which becomes a 500.

Expected

  • Return 400, or 404 for an unknown role, with an error message instead of a 500.
  • Check each IdentityResult and return a non-success status when it fails.
  • Make the Produces(...) declarations match the statuses the endpoint actually returns (200/400/404).

Notes

Found while adding integration tests in #319. Once this is fixed, add tests to tests/PublicApiIntegrationTests/UserManagementEndpoints/SaveRolesForUserEndpointTest.cs for the unknown-role and already-assigned cases.

Dominant language
C#
Stars
642
Forks
320
Avg merge
3h 9m
Merged PRs (30d)
11

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from NimblePros/eShopOnWeb

All issues in NimblePros/eShopOnWeb

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.