|
3 | 3 | import json
|
4 | 4 |
|
5 | 5 | import pytest
|
| 6 | +import requests |
6 | 7 | from django.contrib.auth import get_user_model
|
7 | 8 | from django.contrib.auth.hashers import make_password
|
8 | 9 | from django.utils import timezone
|
@@ -505,16 +506,22 @@ def test_response_when_auth_server_response_not_200(self):
|
505 | 506 | """
|
506 | 507 | Ensure we log the error when the authentication server returns a non-200 response.
|
507 | 508 | """
|
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, |
516 | 524 | )
|
517 |
| - ) |
518 | 525 |
|
519 | 526 |
|
520 | 527 | @pytest.mark.oauth2_settings(presets.OIDC_SETTINGS_RW)
|
|
0 commit comments