-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_sample_controller.py
More file actions
126 lines (111 loc) · 3.92 KB
/
Copy pathtest_sample_controller.py
File metadata and controls
126 lines (111 loc) · 3.92 KB
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
import base64
import pickle
import pytest
from ellar.testing import Test
from .app import AppModule, SimpleHeaderAuthHandler
class TestArticleController:
test_module = Test.create_test_module(modules=[AppModule])
test_module.create_application().add_authentication_schemes(SimpleHeaderAuthHandler)
client = test_module.get_test_client()
@pytest.mark.parametrize(
"roles, path, status",
[
(["admin", "staff"], "staff-only", 200),
(
[
"admin",
],
"staff-only",
403,
),
(
[
"staff",
],
"staff-only",
200,
),
(["admin", "staff"], "admin-only", 200),
(
[
"admin",
],
"admin-only",
200,
),
(
[
"staff",
],
"admin-only",
403,
),
],
)
def test_admin_and_staff_only_works(self, roles, path, status):
with self.client as client:
auth = base64.b64encode(pickle.dumps({"roles": roles, "id": 23}))
res = client.get(f"/article/{path}", headers={"key": auth})
assert res.status_code == status
@pytest.mark.parametrize(
"claim_value", [["create"], ["publish"], ["create", "publish"]]
)
def test_create_and_publish_works(self, claim_value):
with self.client as client:
auth = base64.b64encode(pickle.dumps({"article": claim_value, "id": 23}))
res = client.get("/article/create", headers={"key": auth})
assert res.status_code == 200
assert res.text == '"fast and furious 10 Article"'
@pytest.mark.parametrize(
"age, route, status",
[
# 'at-least-21'
(19, "at-least-21", 403),
(20, "at-least-21", 403),
(21, "at-least-21", 200),
(22, "at-least-21", 200),
# 'at-least-21-case-2'
(19, "at-least-21-case-2", 403),
(20, "at-least-21-case-2", 403),
(21, "at-least-21-case-2", 200),
(22, "at-least-21-case-2", 200),
],
)
def test_at_least_21_works(self, age, route, status):
with self.client as client:
auth = base64.b64encode(pickle.dumps({"age": age, "id": 23}))
res = client.get(f"/article/{route}", headers={"key": auth})
assert res.status_code == status
if status == 200:
assert res.text == '"Only for Age of 21 or more"'
def test_authorization_guard_without_policy_checks_for_authentication(self):
res = self.client.get("/article/list")
assert res.status_code == 401
auth = base64.b64encode(pickle.dumps({"age": 19, "id": 23}))
res = self.client.get("/article/list", headers={"key": auth})
assert res.status_code == 200
assert res.text == '"List of articles"'
class TestMovieController:
test_module = Test.create_test_module(modules=[AppModule])
test_module.create_application().add_authentication_schemes(
SimpleHeaderAuthHandler()
)
client = test_module.get_test_client()
def test_get_fast_x_fails_for_anonymous_users(self):
with self.client as client:
res = client.get("/movies/")
assert res.status_code == 401
@pytest.mark.parametrize(
"age, status",
[
(17, 403),
(16, 403),
(18, 200),
(22, 200),
],
)
def test_get_fast_x_works(self, age, status):
with self.client as client:
auth = base64.b64encode(pickle.dumps({"age": age, "id": 23}))
res = client.get("/movies/", headers={"key": auth})
assert res.status_code == status