|
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
|
@@ -501,18 +502,26 @@ def setUpTestData(cls):
|
501 | 502 | cls.introspection_token = "test_introspection_token"
|
502 | 503 | cls.validator = OAuth2Validator()
|
503 | 504 |
|
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 | + ) |
516 | 525 |
|
517 | 526 |
|
518 | 527 | @pytest.mark.oauth2_settings(presets.OIDC_SETTINGS_RW)
|
|
0 commit comments