Skip to content

Commit 848b8d1

Browse files
committed
mock the post request instead of POSTing to example.com
1 parent 2387290 commit 848b8d1

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

tests/test_oauth2_validators.py

Lines changed: 16 additions & 9 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
@@ -505,16 +506,22 @@ def test_response_when_auth_server_response_not_200(self):
505506
"""
506507
Ensure we log the error when the authentication server returns a non-200 response.
507508
"""
508-
with self.assertLogs(logger="oauth2_provider") as mock_log:
509-
self.validator._get_token_from_authentication_server(
510-
self.token, self.introspection_url, self.introspection_token, None
511-
)
512-
self.assertTrue(
513-
any(
514-
"Failed to get a valid response from authentication server" in message
515-
for message in mock_log.output
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,
516524
)
517-
)
518525

519526

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

0 commit comments

Comments
 (0)