Skip to content

Commit

Permalink
deal with 404 or 405 validator error (#1499)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
n2ygk and dopry authored Sep 20, 2024
1 parent 90d7300 commit a153823
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions tests/test_oauth2_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json

import pytest
import requests
from django.contrib.auth import get_user_model
from django.contrib.auth.hashers import make_password
from django.utils import timezone
Expand Down Expand Up @@ -501,18 +502,26 @@ def setUpTestData(cls):
cls.introspection_token = "test_introspection_token"
cls.validator = OAuth2Validator()

def test_response_when_auth_server_response_return_404(self):
with self.assertLogs(logger="oauth2_provider") as mock_log:
self.validator._get_token_from_authentication_server(
self.token, self.introspection_url, self.introspection_token, None
)
self.assertIn(
"ERROR:oauth2_provider:Introspection: Failed to "
"get a valid response from authentication server. "
"Status code: 404, Reason: "
"Not Found.\nNoneType: None",
mock_log.output,
)
def test_response_when_auth_server_response_not_200(self):
"""
Ensure we log the error when the authentication server returns a non-200 response.
"""
mock_response = requests.Response()
mock_response.status_code = 404
mock_response.reason = "Not Found"
with mock.patch("requests.post") as mock_post:
mock_post.return_value = mock_response
with self.assertLogs(logger="oauth2_provider") as mock_log:
self.validator._get_token_from_authentication_server(
self.token, self.introspection_url, self.introspection_token, None
)
self.assertIn(
"ERROR:oauth2_provider:Introspection: Failed to "
"get a valid response from authentication server. "
"Status code: 404, Reason: "
"Not Found.\nNoneType: None",
mock_log.output,
)


@pytest.mark.oauth2_settings(presets.OIDC_SETTINGS_RW)
Expand Down

0 comments on commit a153823

Please sign in to comment.