1
+ from datetime import timedelta
2
+
3
+ import pytest
4
+
5
+ from django .conf import settings
6
+ from django .test .utils import override_settings
7
+ from django .urls import reverse
8
+ from django .contrib .auth import get_user_model
9
+ from oauth2_provider .models import Application , AccessToken
10
+ from django .utils import timezone
11
+ from rest_framework .test import APIClient
12
+ import json
13
+
14
+ from oioioi .base .tests import TestCase
15
+
16
+ User = get_user_model ()
17
+
18
+ @override_settings (
19
+ OAUTH2_PROVIDER = {
20
+ 'SCOPES' : {'read' : 'Read access' , 'write' : 'Write access' },
21
+ 'ALLOWED_GRANT_TYPES' : ['password' , 'authorization_code' , 'client_credentials' , 'refresh_token' ],
22
+ }
23
+ )
24
+ class PasswordGrantTestCase (TestCase ):
25
+ fixtures = ['test_users' ]
26
+
27
+ def setUp (self ):
28
+ # For this test, we need to explicitly create a password-grant-based app
29
+ # Since secrets are hashed we will store their plaintext as members
30
+ self .user_pwd = 'password'
31
+ self .
user = User .
objects .
create_user (
'oauth_test_user' ,
'[email protected] ' ,
self .
user_pwd )
32
+
33
+ self .app_secret = 'test-client-secret'
34
+ self .application = Application .objects .create (
35
+ name = 'Password Grant Test App' ,
36
+ user = self .user ,
37
+ client_type = Application .CLIENT_CONFIDENTIAL ,
38
+ authorization_grant_type = Application .GRANT_PASSWORD ,
39
+ client_id = 'test-client-id' ,
40
+ client_secret = self .app_secret ,
41
+ redirect_uris = ''
42
+ )
43
+
44
+ # Create an access token for reference
45
+ self .access_token = AccessToken .objects .create (
46
+ user = self .user ,
47
+ application = self .application ,
48
+ token = 'test_token' ,
49
+ expires = timezone .now () + timedelta (days = 1 ),
50
+ scope = 'read write'
51
+ )
52
+
53
+ self .api_client = APIClient ()
54
+
55
+ # Test a token created without a grant for reference
56
+ def test_protected_endpoint_with_token (self ):
57
+ self .api_client .credentials (HTTP_AUTHORIZATION = f'Bearer { self .access_token .token } ' )
58
+ response = self .api_client .get ('/api/auth_ping' )
59
+ self .assertEqual (response .status_code , 200 )
60
+
61
+ self .api_client .credentials ()
62
+ response = self .api_client .get ('/api/auth_ping' )
63
+ self .assertEqual (response .status_code , 403 )
64
+
65
+ def test_token_flow (self ):
66
+ token_url = reverse ('oauth2_provider:token' )
67
+
68
+ response = self .client .post (token_url , {
69
+ 'grant_type' : Application .GRANT_PASSWORD ,
70
+ 'username' : self .user .username ,
71
+ 'password' : self .user_pwd ,
72
+ 'client_id' : self .application .client_id ,
73
+ 'client_secret' : self .app_secret ,
74
+ })
75
+
76
+ self .assertEqual (response .status_code , 200 )
77
+ content = json .loads (response .content .decode ('utf-8' ))
78
+ self .assertIn ('access_token' , content )
79
+ self .assertIn ('token_type' , content )
80
+ self .assertEqual (content ['token_type' ], 'Bearer' )
81
+
82
+ self .api_client .credentials (HTTP_AUTHORIZATION = f'Bearer { content ["access_token" ]} ' )
83
+ response = self .api_client .get ('/api/auth_ping' )
84
+ self .assertEqual (response .status_code , 200 )
85
+
86
+ self .api_client .credentials ()
87
+ response = self .api_client .get ('/api/auth_ping' )
88
+ self .assertEqual (response .status_code , 403 )
0 commit comments