Skip to content

Commit a153823

Browse files
n2ygkdopry
andauthored
deal with 404 or 405 validator error (#1499)
* deal with 404 or 405 validator error (apparently varies with version of django) * refactor: more precise test name * mock the post request instead of POSTing to example.com --------- Co-authored-by: Darrel O'Pry <[email protected]>
1 parent 90d7300 commit a153823

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

tests/test_oauth2_validators.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44

55
import pytest
6+
import requests
67
from django.contrib.auth import get_user_model
78
from django.contrib.auth.hashers import make_password
89
from django.utils import timezone
@@ -501,18 +502,26 @@ def setUpTestData(cls):
501502
cls.introspection_token = "test_introspection_token"
502503
cls.validator = OAuth2Validator()
503504

504-
def test_response_when_auth_server_response_return_404(self):
505-
with self.assertLogs(logger="oauth2_provider") as mock_log:
506-
self.validator._get_token_from_authentication_server(
507-
self.token, self.introspection_url, self.introspection_token, None
508-
)
509-
self.assertIn(
510-
"ERROR:oauth2_provider:Introspection: Failed to "
511-
"get a valid response from authentication server. "
512-
"Status code: 404, Reason: "
513-
"Not Found.\nNoneType: None",
514-
mock_log.output,
515-
)
505+
def test_response_when_auth_server_response_not_200(self):
506+
"""
507+
Ensure we log the error when the authentication server returns a non-200 response.
508+
"""
509+
mock_response = requests.Response()
510+
mock_response.status_code = 404
511+
mock_response.reason = "Not Found"
512+
with mock.patch("requests.post") as mock_post:
513+
mock_post.return_value = mock_response
514+
with self.assertLogs(logger="oauth2_provider") as mock_log:
515+
self.validator._get_token_from_authentication_server(
516+
self.token, self.introspection_url, self.introspection_token, None
517+
)
518+
self.assertIn(
519+
"ERROR:oauth2_provider:Introspection: Failed to "
520+
"get a valid response from authentication server. "
521+
"Status code: 404, Reason: "
522+
"Not Found.\nNoneType: None",
523+
mock_log.output,
524+
)
516525

517526

518527
@pytest.mark.oauth2_settings(presets.OIDC_SETTINGS_RW)

0 commit comments

Comments
 (0)