From 80441aaf3272b0f174a7b3cdda2f9d361e2c6014 Mon Sep 17 00:00:00 2001 From: Bono Date: Fri, 18 Nov 2016 18:08:37 +0100 Subject: [PATCH] Log create_uri_response exceptions to logger.exception --- oidc_provider/lib/endpoints/authorize.py | 2 +- .../tests/test_authorize_endpoint.py | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/oidc_provider/lib/endpoints/authorize.py b/oidc_provider/lib/endpoints/authorize.py index 4be87c0..d2a805c 100644 --- a/oidc_provider/lib/endpoints/authorize.py +++ b/oidc_provider/lib/endpoints/authorize.py @@ -207,7 +207,7 @@ class AuthorizeEndpoint(object): query_fragment['session_state'] = session_state except Exception as error: - logger.debug('[Authorize] Error when trying to create response uri: %s', error) + logger.exception('[Authorize] Error when trying to create response uri: %s', error) raise AuthorizeError(self.params['redirect_uri'], 'server_error', self.grant_type) uri = uri._replace(query=urlencode(query_params, doseq=True)) diff --git a/oidc_provider/tests/test_authorize_endpoint.py b/oidc_provider/tests/test_authorize_endpoint.py index 030d50b..80aab38 100644 --- a/oidc_provider/tests/test_authorize_endpoint.py +++ b/oidc_provider/tests/test_authorize_endpoint.py @@ -7,6 +7,7 @@ try: except ImportError: from urlparse import parse_qs, urlsplit import uuid +from mock import patch from django.contrib.auth.models import AnonymousUser from django.core.management import call_command @@ -26,6 +27,7 @@ from oidc_provider.tests.app.utils import ( is_code_valid, ) from oidc_provider.views import AuthorizeView +from oidc_provider.lib.endpoints.authorize import AuthorizeEndpoint class AuthorizeEndpointMixin(object): @@ -498,3 +500,40 @@ class AuthorizationHybridFlowTestCase(TestCase, AuthorizeEndpointMixin): response = self._auth_request('post', self.data, is_user_authenticated=True) self.assertIn('expires_in=36000', response['Location']) + + +class TestCreateResponseURI(TestCase): + def setUp(self): + url = reverse('oidc_provider:authorize') + user = create_fake_user() + client = create_fake_client(response_type='code', is_public=True) + + # Base data to create a uri response + data = { + 'client_id': client.client_id, + 'redirect_uri': client.default_redirect_uri, + 'response_type': client.response_type, + } + + factory = RequestFactory() + self.request = factory.post(url, data=data) + self.request.user = user + + @patch('oidc_provider.lib.endpoints.authorize.create_code') + @patch('oidc_provider.lib.endpoints.authorize.logger.exception') + def test_create_response_uri_logs_to_error(self, log_exception, create_code): + """ + A lot can go wrong when creating a response uri and this is caught with a general Exception error. The + information contained within this error should show up in the error log so production servers have something + to work with when things don't work as expected. + """ + exception = Exception("Something went wrong!") + create_code.side_effect = exception + + authorization_endpoint = AuthorizeEndpoint(self.request) + authorization_endpoint.validate_params() + + with self.assertRaises(Exception): + authorization_endpoint.create_response_uri() + + log_exception.assert_called_once_with('[Authorize] Error when trying to create response uri: %s', exception)