From ba4faee6ef14b29622ad1265c8ab563ce16ee66d Mon Sep 17 00:00:00 2001 From: Graham Ullrich Date: Thu, 11 Aug 2016 16:05:13 -0600 Subject: [PATCH 1/2] Fix global imports Global imports ("from X import *") are discouraged in Python. --- oidc_provider/lib/endpoints/authorize.py | 21 +++++++++-- oidc_provider/lib/endpoints/token.py | 18 +++++++-- oidc_provider/lib/utils/token.py | 6 ++- oidc_provider/tests/app/utils.py | 6 ++- .../tests/test_authorize_endpoint.py | 10 +++-- oidc_provider/tests/test_logout_endpoint.py | 3 +- .../tests/test_provider_info_endpoint.py | 4 +- oidc_provider/tests/test_token_endpoint.py | 37 ++++++++++++++----- oidc_provider/tests/test_userinfo_endpoint.py | 14 +++++-- oidc_provider/views.py | 13 +++++-- 10 files changed, 99 insertions(+), 33 deletions(-) diff --git a/oidc_provider/lib/endpoints/authorize.py b/oidc_provider/lib/endpoints/authorize.py index b3ea536..ca4a584 100644 --- a/oidc_provider/lib/endpoints/authorize.py +++ b/oidc_provider/lib/endpoints/authorize.py @@ -1,3 +1,4 @@ +from datetime import timedelta import logging try: from urllib import urlencode @@ -8,10 +9,22 @@ except ImportError: from django.utils import timezone from oidc_provider.lib.claims import StandardScopeClaims -from oidc_provider.lib.errors import * -from oidc_provider.lib.utils.params import * -from oidc_provider.lib.utils.token import * -from oidc_provider.models import * +from oidc_provider.lib.errors import ( + AuthorizeError, + ClientIdError, + RedirectUriError, +) +from oidc_provider.lib.utils.params import Params +from oidc_provider.lib.utils.token import ( + create_code, + create_id_token, + create_token, + encode_id_token, +) +from oidc_provider.models import ( + Client, + UserConsent, +) from oidc_provider import settings diff --git a/oidc_provider/lib/endpoints/token.py b/oidc_provider/lib/endpoints/token.py index 8a7832d..a10c508 100644 --- a/oidc_provider/lib/endpoints/token.py +++ b/oidc_provider/lib/endpoints/token.py @@ -9,10 +9,20 @@ except ImportError: from django.http import JsonResponse -from oidc_provider.lib.errors import * -from oidc_provider.lib.utils.params import * -from oidc_provider.lib.utils.token import * -from oidc_provider.models import * +from oidc_provider.lib.errors import ( + TokenError, +) +from oidc_provider.lib.utils.params import Params +from oidc_provider.lib.utils.token import ( + create_id_token, + create_token, + encode_id_token, +) +from oidc_provider.models import ( + Client, + Code, + Token, +) from oidc_provider import settings diff --git a/oidc_provider/lib/utils/token.py b/oidc_provider/lib/utils/token.py index 83291ec..680ee64 100644 --- a/oidc_provider/lib/utils/token.py +++ b/oidc_provider/lib/utils/token.py @@ -9,7 +9,11 @@ from jwkest.jwk import SYMKey from jwkest.jws import JWS from oidc_provider.lib.utils.common import get_issuer -from oidc_provider.models import * +from oidc_provider.models import ( + Code, + RSAKey, + Token, +) from oidc_provider import settings diff --git a/oidc_provider/tests/app/utils.py b/oidc_provider/tests/app/utils.py index 616b130..4c58071 100644 --- a/oidc_provider/tests/app/utils.py +++ b/oidc_provider/tests/app/utils.py @@ -1,4 +1,3 @@ -import os import random import string try: @@ -8,7 +7,10 @@ except ImportError: from django.contrib.auth.models import User -from oidc_provider.models import * +from oidc_provider.models import ( + Client, + Code, +) FAKE_NONCE = 'cb584e44c43ed6bd0bc2d9c7e242837d' diff --git a/oidc_provider/tests/test_authorize_endpoint.py b/oidc_provider/tests/test_authorize_endpoint.py index f892ae2..f323d36 100644 --- a/oidc_provider/tests/test_authorize_endpoint.py +++ b/oidc_provider/tests/test_authorize_endpoint.py @@ -16,9 +16,13 @@ from django.test import TestCase from jwkest.jwt import JWT from oidc_provider import settings -from oidc_provider.models import * -from oidc_provider.tests.app.utils import * -from oidc_provider.views import * +from oidc_provider.tests.app.utils import ( + create_fake_user, + create_fake_client, + FAKE_CODE_CHALLENGE, + is_code_valid, +) +from oidc_provider.views import AuthorizeView class AuthorizationCodeFlowTestCase(TestCase): diff --git a/oidc_provider/tests/test_logout_endpoint.py b/oidc_provider/tests/test_logout_endpoint.py index 40f4200..b9d1684 100644 --- a/oidc_provider/tests/test_logout_endpoint.py +++ b/oidc_provider/tests/test_logout_endpoint.py @@ -1,8 +1,7 @@ from django.core.urlresolvers import reverse from django.test import TestCase -from oidc_provider.views import * -from oidc_provider.tests.app.utils import * +from oidc_provider.tests.app.utils import create_fake_user class UserInfoTestCase(TestCase): diff --git a/oidc_provider/tests/test_provider_info_endpoint.py b/oidc_provider/tests/test_provider_info_endpoint.py index 1b205bc..0c01bc5 100644 --- a/oidc_provider/tests/test_provider_info_endpoint.py +++ b/oidc_provider/tests/test_provider_info_endpoint.py @@ -2,7 +2,7 @@ from django.core.urlresolvers import reverse from django.test import RequestFactory from django.test import TestCase -from oidc_provider.views import * +from oidc_provider.views import ProviderInfoView class ProviderInfoTestCase(TestCase): @@ -23,4 +23,4 @@ class ProviderInfoTestCase(TestCase): self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-Type'] == 'application/json', True) - self.assertEqual(bool(response.content), True) \ No newline at end of file + self.assertEqual(bool(response.content), True) diff --git a/oidc_provider/tests/test_token_endpoint.py b/oidc_provider/tests/test_token_endpoint.py index b993a2a..7c43122 100644 --- a/oidc_provider/tests/test_token_endpoint.py +++ b/oidc_provider/tests/test_token_endpoint.py @@ -1,3 +1,7 @@ +from datetime import timedelta +import json +import uuid + from base64 import b64encode try: from urllib.parse import urlencode @@ -5,15 +9,30 @@ except ImportError: from urllib import urlencode from django.core.management import call_command +from django.core.urlresolvers import reverse from django.test import RequestFactory, override_settings from django.test import TestCase +from django.utils import timezone from jwkest.jwk import KEYS +from jwkest.jws import JWS from jwkest.jwt import JWT from mock import patch -from oidc_provider.lib.utils.token import * -from oidc_provider.tests.app.utils import * -from oidc_provider.views import * +from oidc_provider.lib.utils.token import create_code +from oidc_provider.models import Token +from oidc_provider.tests.app.utils import ( + create_fake_user, + create_fake_client, + FAKE_CODE_CHALLENGE, + FAKE_CODE_VERIFIER, + FAKE_NONCE, + FAKE_RANDOM_STRING, +) +from oidc_provider.views import ( + JwksView, + TokenView, + userinfo, +) class TokenTestCase(TestCase): @@ -208,14 +227,14 @@ class TokenTestCase(TestCase): response = TokenView.as_view()(request) self.assertEqual(response.status_code == 405, True, - msg=request.method+' request does not return a 405 status.') + msg=request.method + ' request does not return a 405 status.') request = self.factory.post(url) response = TokenView.as_view()(request) self.assertEqual(response.status_code == 400, True, - msg=request.method+' request does not return a 400 status.') + msg=request.method + ' request does not return a 400 status.') def test_client_authentication(self): """ @@ -238,7 +257,7 @@ class TokenTestCase(TestCase): # Now, test with an invalid client_id. invalid_data = post_data.copy() - invalid_data['client_id'] = self.client.client_id * 2 # Fake id. + invalid_data['client_id'] = self.client.client_id * 2 # Fake id. # Create another grant code. code = self._create_code() @@ -264,8 +283,8 @@ class TokenTestCase(TestCase): user_pass = self.client.client_id + ':' + self.client.client_secret auth_header = b'Basic ' + b64encode(user_pass.encode('utf-8')) response = self._post_request(basicauth_data, { - 'HTTP_AUTHORIZATION': auth_header.decode('utf-8'), - }) + 'HTTP_AUTHORIZATION': auth_header.decode('utf-8'), + }) response.content.decode('utf-8') self.assertEqual('invalid_client' in response.content.decode('utf-8'), @@ -326,7 +345,7 @@ class TokenTestCase(TestCase): the JOSE Header. """ SIGKEYS = self._get_keys() - RSAKEYS = [ k for k in SIGKEYS if k.kty == 'RSA' ] + RSAKEYS = [k for k in SIGKEYS if k.kty == 'RSA'] code = self._create_code() diff --git a/oidc_provider/tests/test_userinfo_endpoint.py b/oidc_provider/tests/test_userinfo_endpoint.py index a68e011..78dc8d4 100644 --- a/oidc_provider/tests/test_userinfo_endpoint.py +++ b/oidc_provider/tests/test_userinfo_endpoint.py @@ -1,3 +1,5 @@ +import json + from datetime import timedelta try: from urllib.parse import urlencode @@ -9,9 +11,15 @@ from django.test import RequestFactory from django.test import TestCase from django.utils import timezone -from oidc_provider.lib.utils.token import * -from oidc_provider.models import * -from oidc_provider.tests.app.utils import * +from oidc_provider.lib.utils.token import ( + create_id_token, + create_token, +) +from oidc_provider.tests.app.utils import ( + create_fake_user, + create_fake_client, + FAKE_NONCE, +) from oidc_provider.views import userinfo diff --git a/oidc_provider/views.py b/oidc_provider/views.py index ae4bf69..e2a2b31 100644 --- a/oidc_provider/views.py +++ b/oidc_provider/views.py @@ -1,3 +1,5 @@ +import logging + from Crypto.PublicKey import RSA from django.contrib.auth.views import redirect_to_login, logout from django.core.urlresolvers import reverse @@ -9,9 +11,14 @@ from django.views.generic import View from jwkest import long_to_base64 from oidc_provider.lib.claims import StandardScopeClaims -from oidc_provider.lib.endpoints.authorize import * -from oidc_provider.lib.endpoints.token import * -from oidc_provider.lib.errors import * +from oidc_provider.lib.endpoints.authorize import AuthorizeEndpoint +from oidc_provider.lib.endpoints.token import TokenEndpoint +from oidc_provider.lib.errors import ( + AuthorizeError, + ClientIdError, + RedirectUriError, + TokenError, +) from oidc_provider.lib.utils.common import redirect, get_site_url, get_issuer from oidc_provider.lib.utils.oauth2 import protected_resource_view from oidc_provider.models import RESPONSE_TYPE_CHOICES, RSAKey From 84e80df3a8c85e57cfef2d00f5391d1fef1f3dd5 Mon Sep 17 00:00:00 2001 From: Graham Ullrich Date: Thu, 11 Aug 2016 16:19:10 -0600 Subject: [PATCH 2/2] Allow test suite specification --- runtests.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/runtests.py b/runtests.py index 388ce99..1557853 100644 --- a/runtests.py +++ b/runtests.py @@ -98,11 +98,13 @@ def runtests(*test_args): try: from django.test.runner import DiscoverRunner runner_class = DiscoverRunner - test_args = ["oidc_provider.tests"] + if not test_args: + test_args = ["oidc_provider.tests"] except ImportError: from django.test.simple import DjangoTestSuiteRunner runner_class = DjangoTestSuiteRunner - test_args = ["tests"] + if not test_args: + test_args = ["tests"] failures = runner_class(verbosity=1, interactive=True, failfast=False).run_tests(test_args) sys.exit(failures)