-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(chore) Add api fallback exception handler. (#430)
Signed-off-by: Joe Shimkus <[email protected]>
- Loading branch information
1 parent
a75a5a3
commit 68473e7
Showing
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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,38 @@ | ||
from unittest import mock | ||
|
||
import pytest | ||
from rest_framework import status | ||
from rest_framework.test import APIClient | ||
|
||
from tests.integration.constants import api_url_v1 | ||
|
||
|
||
class FallbackException(Exception): | ||
pass | ||
|
||
|
||
def raise_exception(self, request): | ||
raise FallbackException | ||
|
||
|
||
@pytest.mark.django_db | ||
@mock.patch( | ||
"aap_eda.api.views.project.ProjectViewSet.list", new=raise_exception | ||
) | ||
def test_debug_unexpected_exception(client: APIClient, settings): | ||
settings.DEBUG = True | ||
with pytest.raises(FallbackException): | ||
client.get(f"{api_url_v1}/projects/") | ||
|
||
|
||
@pytest.mark.django_db | ||
@mock.patch( | ||
"aap_eda.api.views.project.ProjectViewSet.list", new=raise_exception | ||
) | ||
def test_non_debug_unexpected_exception(client: APIClient, settings): | ||
settings.DEBUG = False | ||
response = client.get(f"{api_url_v1}/projects/") | ||
assert response.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR | ||
|
||
data = response.json() | ||
assert data["detail"].startswith("Unexpected server error") |