-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
70 lines (51 loc) · 1.77 KB
/
tests.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
from flask import json, url_for
import views
from models import APIKey
def test_sanity_check(client):
response = client.get(url_for('api.sanity_check'))
assert response.status_code == 200 and response.data.decode('utf-8') == ''
def test_api_view(request_context):
status_code = 200
body = 'bar'
@views.api_view
def v():
return status_code, body
result = v()
assert result.status_code == status_code
assert json.loads(result.data.decode('utf-8')) == body
def test_require_perms(app, db):
jury_key = APIKey(perm_jury=True)
master_key = APIKey(perm_master=True)
db.session.add(jury_key)
db.session.add(master_key)
db.session.commit()
@views.require_perms('master')
def v():
return 'foo'
with app.test_request_context(headers=dict(api_key=jury_key.key)):
result = v()
assert result == (403, None)
with app.test_request_context(headers=dict(api_key=master_key.key)):
result = v()
assert result == 'foo'
with app.test_request_context(headers=dict()):
result = v()
assert result == (403, None)
def test_require_multiple_perms(app, db):
jury_key = APIKey(perm_jury=True)
master_key = APIKey(perm_master=True)
db.session.add(jury_key)
db.session.add(master_key)
db.session.commit()
@views.require_perms(('master', 'jury'))
def v():
return 200, 'foo'
with app.test_request_context(headers=dict(api_key=jury_key.key)):
result = v()
assert result == (200, 'foo')
with app.test_request_context(headers=dict(api_key=master_key.key)):
result = v()
assert result == (200, 'foo')
with app.test_request_context(headers=dict()):
result = v()
assert result == (403, None)