Skip to content

Commit 38c7c4c

Browse files
n2ygkauvipy
authored andcommitted
restore flake8 tests (#796)
- remove '--exit-zero' so flake8 errors will actually cause a travis failure. - restore flake8-quotes. (I may prefer single quotes but @jlelanche has decreed they shall be double. Obey!) - fix various isort and flake8 (mostly single vs. double quotes) errors that crept in since the tests were disabled.
1 parent 977414b commit 38c7c4c

File tree

13 files changed

+129
-122
lines changed

13 files changed

+129
-122
lines changed

oauth2_provider/management/commands/createapplication.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from oauth2_provider.models import get_application_model
55

6+
67
Application = get_application_model()
78

89

@@ -11,44 +12,44 @@ class Command(BaseCommand):
1112

1213
def add_arguments(self, parser):
1314
parser.add_argument(
14-
'client_type',
15+
"client_type",
1516
type=str,
16-
help='The client type, can be confidential or public',
17+
help="The client type, can be confidential or public",
1718
)
1819
parser.add_argument(
19-
'authorization_grant_type',
20+
"authorization_grant_type",
2021
type=str,
21-
help='The type of authorization grant to be used',
22+
help="The type of authorization grant to be used",
2223
)
2324
parser.add_argument(
24-
'--client-id',
25+
"--client-id",
2526
type=str,
26-
help='The ID of the new application',
27+
help="The ID of the new application",
2728
)
2829
parser.add_argument(
29-
'--user',
30+
"--user",
3031
type=str,
31-
help='The user the application belongs to',
32+
help="The user the application belongs to",
3233
)
3334
parser.add_argument(
34-
'--redirect-uris',
35+
"--redirect-uris",
3536
type=str,
36-
help='The redirect URIs, this must be a space separated string e.g "URI1 URI2',
37+
help="The redirect URIs, this must be a space separated string e.g 'URI1 URI2'",
3738
)
3839
parser.add_argument(
39-
'--client-secret',
40+
"--client-secret",
4041
type=str,
41-
help='The secret for this application',
42+
help="The secret for this application",
4243
)
4344
parser.add_argument(
44-
'--name',
45+
"--name",
4546
type=str,
46-
help='The name this application',
47+
help="The name this application",
4748
)
4849
parser.add_argument(
49-
'--skip-authorization',
50-
action='store_true',
51-
help='The ID of the new application',
50+
"--skip-authorization",
51+
action="store_true",
52+
help="The ID of the new application",
5253
)
5354

5455
def handle(self, *args, **options):
@@ -61,8 +62,8 @@ def handle(self, *args, **options):
6162
# verbosity and others. Also do not pass any None to the Application
6263
# instance so default values will be generated for those fields
6364
if key in application_fields and value:
64-
if key == 'user':
65-
application_data.update({'user_id': value})
65+
if key == "user":
66+
application_data.update({"user_id": value})
6667
else:
6768
application_data.update({key: value})
6869

@@ -71,15 +72,15 @@ def handle(self, *args, **options):
7172
try:
7273
new_application.full_clean()
7374
except ValidationError as exc:
74-
errors = "\n ".join(['- ' + err_key + ': ' + str(err_value) for err_key,
75+
errors = "\n ".join(["- " + err_key + ": " + str(err_value) for err_key,
7576
err_value in exc.message_dict.items()])
7677
self.stdout.write(
7778
self.style.ERROR(
78-
'Please correct the following errors:\n %s' % errors
79+
"Please correct the following errors:\n %s" % errors
7980
)
8081
)
8182
else:
8283
new_application.save()
8384
self.stdout.write(
84-
self.style.SUCCESS('New application created successfully')
85+
self.style.SUCCESS("New application created successfully")
8586
)

oauth2_provider/models.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import logging
12
from datetime import timedelta
23
from urllib.parse import parse_qsl, urlparse
3-
import logging
44

55
from django.apps import apps
66
from django.conf import settings
@@ -15,13 +15,14 @@
1515
from .settings import oauth2_settings
1616
from .validators import RedirectURIValidator, WildcardSet
1717

18+
1819
logger = logging.getLogger(__name__)
1920

2021

2122
class AbstractApplication(models.Model):
2223
"""
2324
An Application instance represents a Client on the Authorization server.
24-
Usually an Application is created manually by client's developers after
25+
Usually an Application is created manually by client"s developers after
2526
logging in on an Authorization Server.
2627
2728
Fields:
@@ -259,11 +260,11 @@ class Meta(AbstractGrant.Meta):
259260
class AbstractAccessToken(models.Model):
260261
"""
261262
An AccessToken instance represents the actual access token to
262-
access user's resources, as in :rfc:`5`.
263+
access user"s resources, as in :rfc:`5`.
263264
264265
Fields:
265266
266-
* :attr:`user` The Django user representing resources' owner
267+
* :attr:`user` The Django user representing resources" owner
267268
* :attr:`source_refresh_token` If from a refresh, the consumed RefeshToken
268269
* :attr:`token` Access token
269270
* :attr:`application` Application instance
@@ -323,7 +324,7 @@ def allow_scopes(self, scopes):
323324

324325
def revoke(self):
325326
"""
326-
Convenience method to uniform tokens' interface, for now
327+
Convenience method to uniform tokens" interface, for now
327328
simply remove this token from the database in order to revoke it.
328329
"""
329330
self.delete()
@@ -356,7 +357,7 @@ class AbstractRefreshToken(models.Model):
356357
357358
Fields:
358359
359-
* :attr:`user` The Django user representing resources' owner
360+
* :attr:`user` The Django user representing resources" owner
360361
* :attr:`token` Token value
361362
* :attr:`application` Application instance
362363
* :attr:`access_token` AccessToken instance this refresh token is
@@ -459,23 +460,23 @@ def clear_expired():
459460
access_token__expires__lt=refresh_expire_at,
460461
)
461462

462-
logger.info('%s Revoked refresh tokens to be deleted', revoked.count())
463-
logger.info('%s Expired refresh tokens to be deleted', expired.count())
463+
logger.info("%s Revoked refresh tokens to be deleted", revoked.count())
464+
logger.info("%s Expired refresh tokens to be deleted", expired.count())
464465

465466
revoked.delete()
466467
expired.delete()
467468
else:
468-
logger.info('refresh_expire_at is %s. No refresh tokens deleted.',
469-
refresh_expire_at)
469+
logger.info("refresh_expire_at is %s. No refresh tokens deleted.",
470+
refresh_expire_at)
470471

471472
access_tokens = access_token_model.objects.filter(
472473
refresh_token__isnull=True,
473474
expires__lt=now
474475
)
475476
grants = grant_model.objects.filter(expires__lt=now)
476477

477-
logger.info('%s Expired access tokens to be deleted', access_tokens.count())
478-
logger.info('%s Expired grant tokens to be deleted', grants.count())
478+
logger.info("%s Expired access tokens to be deleted", access_tokens.count())
479+
logger.info("%s Expired grant tokens to be deleted", grants.count())
479480

480481
access_tokens.delete()
481482
grants.delete()

oauth2_provider/oauth2_validators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ def save_bearer_token(self, token, request, *args, **kwargs):
480480
# expires_in is passed to Server on initialization
481481
# custom server class can have logic to override this
482482
expires = timezone.now() + timedelta(seconds=token.get(
483-
'expires_in', oauth2_settings.ACCESS_TOKEN_EXPIRE_SECONDS,
483+
"expires_in", oauth2_settings.ACCESS_TOKEN_EXPIRE_SECONDS,
484484
))
485485

486486
if request.grant_type == "client_credentials":

oauth2_provider/settings.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
This module is largely inspired by django-rest-framework settings.
33
44
Settings for the OAuth2 Provider are all namespaced in the OAUTH2_PROVIDER setting.
5-
For example your project's `settings.py` file might look like this:
5+
For example your project"s `settings.py` file might look like this:
66
77
OAUTH2_PROVIDER = {
88
"CLIENT_ID_GENERATOR_CLASS":
@@ -182,24 +182,25 @@ def server_kwargs(self):
182182
This is used to communicate settings to oauth server.
183183
184184
Takes relevant settings and format them accordingly.
185-
There's also EXTRA_SERVER_KWARGS that can override every value
185+
There"s also EXTRA_SERVER_KWARGS that can override every value
186186
and is more flexible regarding keys and acceptable values
187-
but doesn't have import string magic or any additional
187+
but doesn"t have import string magic or any additional
188188
processing, callables have to be assigned directly.
189189
For the likes of signed_token_generator it means something like
190190
191-
{'token_generator': signed_token_generator(privkey, **kwargs)}
191+
{"token_generator": signed_token_generator(privkey, **kwargs)}
192192
"""
193193
kwargs = {
194194
key: getattr(self, value)
195195
for key, value in [
196-
('token_expires_in', 'ACCESS_TOKEN_EXPIRE_SECONDS'),
197-
('refresh_token_expires_in', 'REFRESH_TOKEN_EXPIRE_SECONDS'),
198-
('token_generator', 'ACCESS_TOKEN_GENERATOR'),
199-
('refresh_token_generator', 'REFRESH_TOKEN_GENERATOR'),
196+
("token_expires_in", "ACCESS_TOKEN_EXPIRE_SECONDS"),
197+
("refresh_token_expires_in", "REFRESH_TOKEN_EXPIRE_SECONDS"),
198+
("token_generator", "ACCESS_TOKEN_GENERATOR"),
199+
("refresh_token_generator", "REFRESH_TOKEN_GENERATOR"),
200200
]
201201
}
202202
kwargs.update(self.EXTRA_SERVER_KWARGS)
203203
return kwargs
204204

205+
205206
oauth2_settings = OAuth2ProviderSettings(USER_SETTINGS, DEFAULTS, IMPORT_STRINGS, MANDATORY)

oauth2_provider/views/base.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
from django.contrib.auth.mixins import LoginRequiredMixin
66
from django.http import HttpResponse, JsonResponse
7+
from django.shortcuts import render
8+
from django.urls import reverse
79
from django.utils import timezone
810
from django.utils.decorators import method_decorator
911
from django.views.decorators.csrf import csrf_exempt
1012
from django.views.decorators.debug import sensitive_post_parameters
1113
from django.views.generic import FormView, View
12-
from django.shortcuts import render
13-
from django.urls import reverse
1414

1515
from ..exceptions import OAuthToolkitError
1616
from ..forms import AllowForm
@@ -21,6 +21,7 @@
2121
from ..signals import app_authorized
2222
from .mixins import OAuthLibMixin
2323

24+
2425
log = logging.getLogger("oauth2_provider")
2526

2627

@@ -61,7 +62,9 @@ def redirect(self, redirect_to, application):
6162
allowed_schemes = application.get_allowed_schemes()
6263
return OAuth2ResponseRedirect(redirect_to, allowed_schemes)
6364

64-
RFC3339 = '%Y-%m-%dT%H:%M:%SZ'
65+
66+
RFC3339 = "%Y-%m-%dT%H:%M:%SZ"
67+
6568

6669
class AuthorizationView(BaseAuthorizationView, FormView):
6770
"""
@@ -208,23 +211,22 @@ def get(self, request, *args, **kwargs):
208211

209212
return self.render_to_response(self.get_context_data(**kwargs))
210213

211-
def redirect(self, redirect_to, application,
212-
token = None):
214+
def redirect(self, redirect_to, application, token=None):
213215

214216
if not redirect_to.startswith("urn:ietf:wg:oauth:2.0:oob"):
215217
return super().redirect(redirect_to, application)
216218

217219
parsed_redirect = urllib.parse.urlparse(redirect_to)
218-
code = urllib.parse.parse_qs(parsed_redirect.query)['code'][0]
220+
code = urllib.parse.parse_qs(parsed_redirect.query)["code"][0]
219221

220-
if redirect_to.startswith('urn:ietf:wg:oauth:2.0:oob:auto'):
222+
if redirect_to.startswith("urn:ietf:wg:oauth:2.0:oob:auto"):
221223

222224
response = {
223-
'access_token': code,
224-
'token_uri': redirect_to,
225-
'client_id': application.client_id,
226-
'client_secret': application.client_secret,
227-
'revoke_uri': reverse('oauth2_provider:revoke-token'),
225+
"access_token": code,
226+
"token_uri": redirect_to,
227+
"client_id": application.client_id,
228+
"client_secret": application.client_secret,
229+
"revoke_uri": reverse("oauth2_provider:revoke-token"),
228230
}
229231

230232
return JsonResponse(response)
@@ -234,10 +236,11 @@ def redirect(self, redirect_to, application,
234236
request=self.request,
235237
template_name="oauth2_provider/authorized-oob.html",
236238
context={
237-
'code': code,
239+
"code": code,
238240
},
239241
)
240242

243+
241244
@method_decorator(csrf_exempt, name="dispatch")
242245
class TokenView(OAuthLibMixin, View):
243246
"""

tests/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from django.db import models
22

3-
from oauth2_provider.settings import oauth2_settings
43
from oauth2_provider.models import (
54
AbstractAccessToken, AbstractApplication,
65
AbstractGrant, AbstractRefreshToken
76
)
7+
from oauth2_provider.settings import oauth2_settings
88

99

1010
class BaseTestApplication(AbstractApplication):

tests/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
}
1010
}
1111

12-
AUTH_USER_MODEL = 'auth.User'
12+
AUTH_USER_MODEL = "auth.User"
1313
OAUTH2_PROVIDER_APPLICATION_MODEL = "oauth2_provider.Application"
1414
OAUTH2_PROVIDER_ACCESS_TOKEN_MODEL = "oauth2_provider.AccessToken"
1515
OAUTH2_PROVIDER_REFRESH_TOKEN_MODEL = "oauth2_provider.RefreshToken"

tests/test_application_views.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from .models import SampleApplication
1010

11+
1112
Application = get_application_model()
1213
UserModel = get_user_model()
1314

0 commit comments

Comments
 (0)