Skip to content

Commit 1499048

Browse files
adamchainzjleclanche
authored andcommitted
Use Model.objects.create where applicable
1 parent d5e6645 commit 1499048

10 files changed

+13
-27
lines changed

oauth2_provider/oauth2_validators.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ def get_code_challenge_method(self, code, request):
448448
def save_authorization_code(self, client_id, code, request, *args, **kwargs):
449449
expires = timezone.now() + timedelta(
450450
seconds=oauth2_settings.AUTHORIZATION_CODE_EXPIRE_SECONDS)
451-
g = Grant(
451+
Grant.objects.create(
452452
application=request.client,
453453
user=request.user,
454454
code=code["code"],
@@ -458,7 +458,6 @@ def save_authorization_code(self, client_id, code, request, *args, **kwargs):
458458
code_challenge=request.code_challenge or "",
459459
code_challenge_method=request.code_challenge_method or ""
460460
)
461-
g.save()
462461

463462
def rotate_refresh_token(self, request):
464463
"""
@@ -563,25 +562,22 @@ def save_bearer_token(self, token, request, *args, **kwargs):
563562
token["expires_in"] = oauth2_settings.ACCESS_TOKEN_EXPIRE_SECONDS
564563

565564
def _create_access_token(self, expires, request, token, source_refresh_token=None):
566-
access_token = AccessToken(
565+
return AccessToken.objects.create(
567566
user=request.user,
568567
scope=token["scope"],
569568
expires=expires,
570569
token=token["access_token"],
571570
application=request.client,
572571
source_refresh_token=source_refresh_token,
573572
)
574-
access_token.save()
575-
return access_token
576573

577574
def _create_refresh_token(self, request, refresh_token_code, access_token):
578-
refresh_token = RefreshToken(
575+
return RefreshToken.objects.create(
579576
user=request.user,
580577
token=refresh_token_code,
581578
application=request.client,
582579
access_token=access_token
583580
)
584-
refresh_token.save()
585581

586582
def revoke_token(self, token, token_type_hint, request, *args, **kwargs):
587583
"""

tests/test_authorization_code.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def setUp(self):
4242

4343
oauth2_settings.ALLOWED_REDIRECT_URI_SCHEMES = ["http", "custom-scheme"]
4444

45-
self.application = Application(
45+
self.application = Application.objects.create(
4646
name="Test Application",
4747
redirect_uris=(
4848
"http://localhost http://example.com http://example.org custom-scheme://example.com"
@@ -51,7 +51,6 @@ def setUp(self):
5151
client_type=Application.CLIENT_CONFIDENTIAL,
5252
authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
5353
)
54-
self.application.save()
5554

5655
oauth2_settings._SCOPES = ["read", "write"]
5756
oauth2_settings._DEFAULT_SCOPES = ["read", "write"]

tests/test_client_credential.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,12 @@ def setUp(self):
3434
self.test_user = UserModel.objects.create_user("test_user", "[email protected]", "123456")
3535
self.dev_user = UserModel.objects.create_user("dev_user", "[email protected]", "123456")
3636

37-
self.application = Application(
37+
self.application = Application.objects.create(
3838
name="test_client_credentials_app",
3939
user=self.dev_user,
4040
client_type=Application.CLIENT_PUBLIC,
4141
authorization_grant_type=Application.GRANT_CLIENT_CREDENTIALS,
4242
)
43-
self.application.save()
4443

4544
oauth2_settings._SCOPES = ["read", "write"]
4645
oauth2_settings._DEFAULT_SCOPES = ["read", "write"]
@@ -152,13 +151,12 @@ def test_client_resource_password_based(self):
152151
"""
153152

154153
self.application.delete()
155-
self.application = Application(
154+
self.application = Application.objects.create(
156155
name="test_client_credentials_app",
157156
user=self.dev_user,
158157
client_type=Application.CLIENT_CONFIDENTIAL,
159158
authorization_grant_type=Application.GRANT_PASSWORD,
160159
)
161-
self.application.save()
162160

163161
token_request_data = {
164162
"grant_type": "password",

tests/test_implicit.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@ def setUp(self):
2525
self.test_user = UserModel.objects.create_user("test_user", "[email protected]", "123456")
2626
self.dev_user = UserModel.objects.create_user("dev_user", "[email protected]", "123456")
2727

28-
self.application = Application(
28+
self.application = Application.objects.create(
2929
name="Test Implicit Application",
3030
redirect_uris="http://localhost http://example.com http://example.org",
3131
user=self.dev_user,
3232
client_type=Application.CLIENT_PUBLIC,
3333
authorization_grant_type=Application.GRANT_IMPLICIT,
3434
)
35-
self.application.save()
3635

3736
oauth2_settings._SCOPES = ["read", "write"]
3837
oauth2_settings._DEFAULT_SCOPES = ["read"]

tests/test_introspection_auth.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,13 @@ def setUp(self):
8181
"resource_server", "[email protected]", "123456"
8282
)
8383

84-
self.application = Application(
84+
self.application = Application.objects.create(
8585
name="Test Application",
8686
redirect_uris="http://localhost http://example.com http://example.org",
8787
user=self.resource_server_user,
8888
client_type=Application.CLIENT_CONFIDENTIAL,
8989
authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
9090
)
91-
self.application.save()
9291

9392
self.resource_server_token = AccessToken.objects.create(
9493
user=self.resource_server_user, token="12345678900",

tests/test_introspection_view.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@ def setUp(self):
2323
self.resource_server_user = UserModel.objects.create_user("resource_server", "[email protected]")
2424
self.test_user = UserModel.objects.create_user("bar_user", "[email protected]")
2525

26-
self.application = Application(
26+
self.application = Application.objects.create(
2727
name="Test Application",
2828
redirect_uris="http://localhost http://example.com http://example.org",
2929
user=self.test_user,
3030
client_type=Application.CLIENT_CONFIDENTIAL,
3131
authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
3232
)
33-
self.application.save()
3433

3534
self.resource_server_token = AccessToken.objects.create(
3635
user=self.resource_server_user, token="12345678900",

tests/test_password.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@ def setUp(self):
2727
self.test_user = UserModel.objects.create_user("test_user", "[email protected]", "123456")
2828
self.dev_user = UserModel.objects.create_user("dev_user", "[email protected]", "123456")
2929

30-
self.application = Application(
30+
self.application = Application.objects.create(
3131
name="Test Password Application",
3232
user=self.dev_user,
3333
client_type=Application.CLIENT_PUBLIC,
3434
authorization_grant_type=Application.GRANT_PASSWORD,
3535
)
36-
self.application.save()
3736

3837
oauth2_settings._SCOPES = ["read", "write"]
3938
oauth2_settings._DEFAULT_SCOPES = ["read", "write"]

tests/test_scopes.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,13 @@ def setUp(self):
5252
self.test_user = UserModel.objects.create_user("test_user", "[email protected]", "123456")
5353
self.dev_user = UserModel.objects.create_user("dev_user", "[email protected]", "123456")
5454

55-
self.application = Application(
55+
self.application = Application.objects.create(
5656
name="Test Application",
5757
redirect_uris="http://localhost http://example.com http://example.org",
5858
user=self.dev_user,
5959
client_type=Application.CLIENT_CONFIDENTIAL,
6060
authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
6161
)
62-
self.application.save()
6362

6463
oauth2_settings._SCOPES = ["read", "write", "scope1", "scope2", "scope3"]
6564
oauth2_settings.READ_SCOPE = "read"

tests/test_token_revocation.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@ def setUp(self):
2424
self.test_user = UserModel.objects.create_user("test_user", "[email protected]", "123456")
2525
self.dev_user = UserModel.objects.create_user("dev_user", "[email protected]", "123456")
2626

27-
self.application = Application(
27+
self.application = Application.objects.create(
2828
name="Test Application",
2929
redirect_uris="http://localhost http://example.com http://example.org",
3030
user=self.dev_user,
3131
client_type=Application.CLIENT_CONFIDENTIAL,
3232
authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
3333
)
34-
self.application.save()
3534

3635
oauth2_settings._SCOPES = ["read", "write"]
3736

tests/test_token_view.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@ def setUp(self):
2121
self.foo_user = UserModel.objects.create_user("foo_user", "[email protected]", "123456")
2222
self.bar_user = UserModel.objects.create_user("bar_user", "[email protected]", "123456")
2323

24-
self.application = Application(
24+
self.application = Application.objects.create(
2525
name="Test Application",
2626
redirect_uris="http://localhost http://example.com http://example.org",
2727
user=self.bar_user,
2828
client_type=Application.CLIENT_CONFIDENTIAL,
2929
authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
3030
)
31-
self.application.save()
3231

3332
def tearDown(self):
3433
self.foo_user.delete()

0 commit comments

Comments
 (0)