-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
257 lines (198 loc) · 10.1 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import pytest
import jwt
from flask import Flask, jsonify
from flask_jwt_group import jwt_identity, jwt_group
from flask_jwt_group.jwt_manager import JWTManager
from flask_jwt_group.util import (create_access_token, create_refresh_token,
get_jwt_identity, get_jwt_group, _get_jwt_manager, add_current_token_to_blacklist)
from flask_jwt_group.view_decorator import jwt_required, jwt_optional, jwt_refresh_token_required
@pytest.fixture(scope="function")
def flask_app():
app = Flask(__name__)
app.config['JWT_SECRET_KEY'] = 'iwanttogoodatprogramming'
app.config['JWT_BLACKLIST_ENABLED'] = True
JWTManager(app)
@app.route('/required', methods=['GET'])
@jwt_required('student', 'admin')
def required():
identity, group = str(jwt_identity), str(jwt_group)
add_current_token_to_blacklist()
return jsonify({
'identity': identity,
'group': group,
'blacklist': _get_jwt_manager().blacklisted_access_tokens
}), 200
@app.route('/optional', methods=['GET'])
@jwt_optional('student', 'admin')
def optional():
identity, group = str(jwt_identity), str(jwt_group)
if jwt_identity:
add_current_token_to_blacklist()
return jsonify({
'identity': identity,
'group': group,
'identity_from_func': get_jwt_identity(),
'group_from_func': get_jwt_group()
})
@app.route('/refresh-required', methods=['GET'])
@jwt_refresh_token_required('student', 'admin')
def refresh_required():
identity, group = str(jwt_identity), str(jwt_group)
add_current_token_to_blacklist()
return jsonify({
'identity': identity,
'group': group,
'blacklist': _get_jwt_manager().blacklisted_refresh_tokens
}), 200
return app
def test_creation_success(flask_app):
secret_key = flask_app.config['JWT_SECRET_KEY']
algorithm = flask_app.config['JWT_ALGORITHM']
with flask_app.test_request_context():
token = create_access_token('flouie74', 'student')
decoded = jwt.decode(token, key=secret_key, algorithms=algorithm)
assert 'iat' in decoded
assert 'nbf' in decoded
assert 'exp' in decoded
assert 'jti' in decoded
assert 'identity' in decoded
assert decoded['identity'] == 'flouie74'
assert 'type' in decoded
assert decoded['type'] == 'access'
assert decoded['group'] == 'student'
def test_get_jwt_identity(flask_app):
prefix = flask_app.config['JWT_HEADER_PREFIX']
test_client = flask_app.test_client()
with flask_app.test_request_context():
token = create_access_token('flouie74', 'student')
resp = test_client.get('/optional')
assert resp.json['identity_from_func'] == 'None'
resp = test_client.get('/optional', headers={'Authorization': '{0} {1}'.format(prefix, token)})
assert resp.json['identity_from_func'] == 'flouie74'
def test_get_jwt_group(flask_app):
prefix = flask_app.config['JWT_HEADER_PREFIX']
test_client = flask_app.test_client()
with flask_app.test_request_context():
token = create_access_token('flouie74', 'student')
resp = test_client.get('/optional')
assert resp.json['group_from_func'] == 'None'
resp = test_client.get('/optional', headers={'Authorization': '{0} {1}'.format(prefix, token)})
assert resp.json['group_from_func'] == 'student'
def test_jwt_required(flask_app):
prefix = flask_app.config['JWT_HEADER_PREFIX']
test_client = flask_app.test_client()
with flask_app.test_request_context():
token = create_access_token('flouie74', 'student')
different_groups_token = create_access_token('flouie74', 'teacher')
# has valid token
resp = test_client.get('/required', headers={'Authorization': '{0} {1}'.format(prefix, token)})
assert resp.status_code == 200
assert resp.json['identity'] == 'flouie74'
assert resp.json['group'] == 'student'
# non exist token in header
resp = test_client.get('/required', headers=None)
assert resp.status_code == 400
# has incorrect type token
with flask_app.test_request_context():
refresh_token = create_refresh_token('flouie74', 'teacher')
resp = test_client.get('/required', headers={'Authorization': '{0} {1}'.format(prefix, refresh_token)})
assert resp.status_code == 422
# has different groups token
resp = test_client.get('/required', headers={'Authorization': '{0} {1}'.format(prefix, different_groups_token)})
assert resp.status_code == 422
# token is in blacklist
test_client.get('/required', headers={'Authorization': '{0} {1}'.format(prefix, token)})
resp = test_client.get('/required', headers={'Authorization': '{0} {1}'.format(prefix, token)})
assert resp.status_code == 403
def test_jwt_optional(flask_app):
prefix = flask_app.config['JWT_HEADER_PREFIX']
test_client = flask_app.test_client()
with flask_app.test_request_context():
token = create_access_token('flouie74', 'student')
different_groups_token = create_access_token('flouie74', 'teacher')
# no authorization header
resp = test_client.get('/optional')
assert resp.status_code == 200
assert resp.json['identity'] == 'None'
assert resp.json['group'] == 'None'
# has valid token
resp = test_client.get('/optional', headers={'Authorization': '{0} {1}'.format(prefix, token)})
assert resp.status_code == 200
assert resp.json['identity'] == 'flouie74'
assert resp.json['group'] == 'student'
# non exist token in header
resp = test_client.get('/required', headers=None)
assert resp.status_code == 400
# has incorrect type token
with flask_app.test_request_context():
refresh_token = create_refresh_token('flouie74', 'teacher')
resp = test_client.get('/optional', headers={'Authorization': '{0} {1}'.format(prefix, refresh_token)})
assert resp.status_code == 422
# has different groups token
resp = test_client.get('/optional', headers={'Authorization': '{0} {1}'.format(prefix, different_groups_token)})
assert resp.status_code == 422
# token is in blacklist
test_client.get('/optional', headers={'Authorization': '{0} {1}'.format(prefix, token)})
resp = test_client.get('/optional', headers={'Authorization': '{0} {1}'.format(prefix, token)})
assert resp.status_code == 403
def test_jwt_refresh_required(flask_app):
prefix = flask_app.config['JWT_HEADER_PREFIX']
test_client = flask_app.test_client()
with flask_app.test_request_context():
token = create_refresh_token('flouie74', 'student')
different_groups_token = create_refresh_token('flouie74', 'teacher')
# has valid token
resp = test_client.get('/refresh-required', headers={'Authorization': '{0} {1}'.format(prefix, token)})
assert resp.status_code == 200
assert resp.json['identity'] == 'flouie74'
assert resp.json['group'] == 'student'
# non exist token in header
resp = test_client.get('/refresh-required', headers=None)
assert resp.status_code == 400
# has incorrect type token
with flask_app.test_request_context():
access_token = create_access_token('flouie74', 'teacher')
resp = test_client.get('/refresh-required', headers={'Authorization': '{0} {1}'.format(prefix, access_token)})
assert resp.status_code == 422
# has different groups token
resp = test_client.get('/refresh-required',
headers={'Authorization': '{0} {1}'.format(prefix, different_groups_token)})
assert resp.status_code == 422
# token is in blacklist
test_client.get('/refresh-required', headers={'Authorization': '{0} {1}'.format(prefix, token)})
resp = test_client.get('/refresh-required', headers={'Authorization': '{0} {1}'.format(prefix, token)})
assert resp.status_code == 403
def test_add_token_to_blacklist(flask_app):
test_client = flask_app.test_client()
with flask_app.test_request_context():
access_token = create_access_token('flouie74', 'student')
refresh_token = create_refresh_token('flouie74', 'student')
another_access_token = create_access_token('geni429', 'admin')
another_refresh_token = create_refresh_token('geni429', 'admin')
secret_key = flask_app.config['JWT_SECRET_KEY']
algorithm = flask_app.config['JWT_ALGORITHM']
prefix = flask_app.config['JWT_HEADER_PREFIX']
decoded_access_token = jwt.decode(access_token, key=secret_key, algorithms=algorithm)
decoded_another_access_token = jwt.decode(another_access_token, key=secret_key, algorithms=algorithm)
decoded_refresh_token = jwt.decode(refresh_token, key=secret_key, algorithms=algorithm)
decoded_another_refresh_token = jwt.decode(another_refresh_token, key=secret_key, algorithms=algorithm)
# test blacklisted_access_tokens
resp = test_client.get('/required', headers={'Authorization': '{0} {1}'.format(prefix, access_token)})
assert resp.status_code == 200
assert resp.json['blacklist'] == {decoded_access_token['jti']: decoded_access_token['exp']}
resp2 = test_client.get('/required', headers={'Authorization': '{0} {1}'.format(prefix, another_access_token)})
assert resp.status_code == 200
assert resp2.json['blacklist'] == {decoded_access_token['jti']: decoded_access_token['exp'],
decoded_another_access_token['jti']: decoded_another_access_token['exp']}
# test blacklisted_refresh_tokens
refresh_resp = test_client.get('/refresh-required',
headers={'Authorization': '{0} {1}'.format(prefix, refresh_token)})
assert refresh_resp.status_code == 200
assert refresh_resp.json['blacklist'] == {decoded_refresh_token['jti']: decoded_refresh_token['exp']}
refresh_resp2 = test_client.get('/refresh-required',
headers={'Authorization': '{0} {1}'.format(prefix, another_refresh_token)})
assert refresh_resp2.status_code == 200
assert refresh_resp2.json['blacklist'] == {
decoded_refresh_token['jti']: decoded_refresh_token['exp'],
decoded_another_refresh_token['jti']: decoded_another_refresh_token['exp']
}