-
Notifications
You must be signed in to change notification settings - Fork 15
settings: catch IntegrityError and render error in output (Bug 2037991) #1159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cgsheeh
wants to merge
4
commits into
mozilla-conduit:main
Choose a base branch
from
cgsheeh:api-key-error-message
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5b6c617
settings: catch IntegrityError and render error in output (Bug 2037991)
cgsheeh b13abc8
use `info` level for log
cgsheeh a050b2b
check for error state earlier
cgsheeh 1026915
Merge remote-tracking branch 'origin/main' into api-key-error-message
cgsheeh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import pytest | ||
| from django.contrib.auth.models import User | ||
| from django.test import Client | ||
|
|
||
| from lando.main.models.profile import Profile | ||
|
|
||
| VALID_API_KEY = "api-aaaaaaaaaaaaaaaaaaaaaaaaaaaa" | ||
|
|
||
|
|
||
| def post_api_key(client: Client, api_key: str = VALID_API_KEY): | ||
| """Submit an API key to the `manage_api_key` view.""" | ||
| return client.post( | ||
| "/manage_api_key/", | ||
| data={"phabricator_api_key": api_key, "reset_key": False}, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.django_db(transaction=True) | ||
| def test_manage_api_key_happy_path( | ||
| client: Client, | ||
| user, | ||
| phabdouble, | ||
| ): | ||
| """A successful API key update stores the new key and PHID on the profile.""" | ||
| phab_user = phabdouble.user(username="phab_user", api_key=VALID_API_KEY) | ||
|
|
||
| client.force_login(user) | ||
| response = post_api_key(client) | ||
|
|
||
| assert response.status_code == 200, ( | ||
| "A valid API key with a unique PHID should be accepted." | ||
| ) | ||
| assert response.json() == {"success": True}, "Response body should report success." | ||
|
|
||
| user.profile.refresh_from_db() | ||
| assert user.profile.phabricator_phid == phab_user["phid"], ( | ||
| "Profile should store the PHID reported by `user.whoami`." | ||
| ) | ||
| assert user.profile.phabricator_api_key == VALID_API_KEY, ( | ||
| "Profile should store the submitted API key." | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.django_db(transaction=True) | ||
| def test_manage_api_key_phid_conflict_returns_error( | ||
| client: Client, | ||
| user, | ||
| phabdouble, | ||
| ): | ||
| """Linking an API key whose PHID already belongs to another user returns a 400.""" | ||
| phab_user = phabdouble.user(username="phab_user", api_key=VALID_API_KEY) | ||
|
|
||
| # Another Lando user already owns this Phabricator PHID. | ||
| other_user = User.objects.create_user( | ||
| username="other_user", email="other@example.org" | ||
| ) | ||
| Profile.objects.create(user=other_user, phabricator_phid=phab_user["phid"]) | ||
|
|
||
| client.force_login(user) | ||
| response = post_api_key(client) | ||
|
|
||
| assert response.status_code == 400, ( | ||
| "Linking an API key whose PHID is owned by another user should return 400." | ||
| ) | ||
| assert response.json() == { | ||
| "errors": { | ||
| "phabricator_api_key": [ | ||
| "This Phabricator account is already linked to another Lando user." | ||
| ] | ||
| } | ||
| }, "Response body should describe the PHID conflict." |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we not check for existing PHIDs here, avoiding the integrity error altogether?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could, but it would add another DB query on every API key update, and we'd still want to keep the
IntegrityErrorhandler to cover the potential race condition (unlikely but technically possible). Happy to change it, this just follows EAFP and is more efficient for the happy path. WDYT?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This endpoint is not used frequently, the extra DB query would be negligible.
The code flow would be more logical if we're explicit about detecting the error state, rather than working around the issue. The integrity error could also (in theory, though very unlikely) occur in a different situation, so here we'd be handling the expected error case that we already discovered.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a check for a duplicate PHID. Personally I don't think it's a meaningful improvement - we're still catching the
IntegrityErrorto catch the race condition, so it's more code for the same functionality - but happy to move forward with it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm what's the point of catching the
IntegrityError? We don't need to do that as long as we know there is no duplicate value. Otherwise it would be a misleading error since if triggered, it is some other issue that is causing that and not the PHID.I would suggest removing the try/except altogether.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
IntegrityErrorhandler covers the race between our.exists()and.save()— without it, two concurrent requests linking the same PHID would 500 instead of returning a clean 400. And sincephabricator_phidis the only unique field we touch here, anIntegrityErrorcan't really come from anywhere else.Yes, the race is unlikely — but the bug is specifically about an unhandled server error, and switching to just an
exists()check doesn't actually resolve that; it just makes the window smaller.This is why I preferred just the
try/exceptversion — it was less code, handled both cases, and required fewer DB lookups on the happy path.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To handle the (very unlikely) race condition we could do the check and the save with a lock, but I think that's overkill. This endpoint is infrequently used, I think the basic issue is to present a meaningful error to the user and avoid having a server issue.
I won't split more hairs here but I think preventing an integrity error is better than handling it when it happens.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TL;DR this should be handled as a validation problem, and not a database integrity problem.