From 80b5393d05da847c8ce413b4cc9b6c79bdc4db84 Mon Sep 17 00:00:00 2001 From: Alan Crosswell Date: Mon, 16 Sep 2024 16:45:00 -0400 Subject: [PATCH 1/3] deal with 404 or 405 validator error (apparently varies with version of django) --- tests/test_oauth2_validators.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/tests/test_oauth2_validators.py b/tests/test_oauth2_validators.py index 31d97f64a..83b87c0fa 100644 --- a/tests/test_oauth2_validators.py +++ b/tests/test_oauth2_validators.py @@ -501,17 +501,19 @@ def setUpTestData(cls): cls.introspection_token = "test_introspection_token" cls.validator = OAuth2Validator() - def test_response_when_auth_server_response_return_404(self): + def test_response_when_auth_server_response_return_404_405(self): + """ + Deal with either 404 or 405 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, + self.assertTrue( + any( + "Failed to get a valid response from authentication server" in message + for message in mock_log.output + ) ) From 23872907d26ddb5706556d75ac8cd0b14f86cd03 Mon Sep 17 00:00:00 2001 From: Darrel O'Pry Date: Thu, 19 Sep 2024 14:46:22 -0400 Subject: [PATCH 2/3] refactor: more precise test name --- tests/test_oauth2_validators.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_oauth2_validators.py b/tests/test_oauth2_validators.py index 83b87c0fa..5b6f60fb6 100644 --- a/tests/test_oauth2_validators.py +++ b/tests/test_oauth2_validators.py @@ -501,9 +501,9 @@ def setUpTestData(cls): cls.introspection_token = "test_introspection_token" cls.validator = OAuth2Validator() - def test_response_when_auth_server_response_return_404_405(self): + def test_response_when_auth_server_response_not_200(self): """ - Deal with either 404 or 405 response + Ensure we log the error when the authentication server returns a non-200 response. """ with self.assertLogs(logger="oauth2_provider") as mock_log: self.validator._get_token_from_authentication_server( From 848b8d1c00a410a267a7a7159c266a5290beaedc Mon Sep 17 00:00:00 2001 From: Alan Crosswell Date: Fri, 20 Sep 2024 11:19:55 -0400 Subject: [PATCH 3/3] mock the post request instead of POSTing to example.com --- tests/test_oauth2_validators.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/tests/test_oauth2_validators.py b/tests/test_oauth2_validators.py index 5b6f60fb6..14c74506e 100644 --- a/tests/test_oauth2_validators.py +++ b/tests/test_oauth2_validators.py @@ -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 @@ -505,16 +506,22 @@ def test_response_when_auth_server_response_not_200(self): """ Ensure we log the error when the authentication server returns a non-200 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.assertTrue( - any( - "Failed to get a valid response from authentication server" in message - for message in mock_log.output + 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)