-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_app.py
76 lines (65 loc) · 2.28 KB
/
test_app.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
import unittest
from unittest import mock
from jose import jwt
from app import app, SECRET_HEADER
ALL_UI_EXTENSIONS = (
"/top_nav",
"/campaign",
"/content",
"/asset",
"/request",
"/task",
"/settings",
)
ALL_LIFECYCLE_EVENTS = (
"/install",
"/uninstall",
"/enable",
"/disable",
"/update",
"/upgrade",
)
class TestBiscotti(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.app = app.test_client()
cls.secret = "a" * 64
cls.secret_mock = mock.patch("app.get_app_secret", return_value=cls.secret)
cls.secret_mock.start()
cls.wrong_secret = "b" * 64
def test_ui_extensions(self):
token = jwt.encode({"banana": 42}, self.secret)
for location in ALL_UI_EXTENSIONS:
resp = self.app.get("{}?jwt={}".format(location, token))
self.assertEqual(resp.status_code, 200)
self.assertIn("banana", str(resp.data))
def test_ui_extensions_wrong_jwt_secret(self):
token = jwt.encode({"banana": 42}, self.wrong_secret)
for location in ALL_UI_EXTENSIONS:
resp = self.app.get("{}?jwt={}".format(location, token))
self.assertEqual(resp.status_code, 200)
self.assertIn("invalid jwt", str(resp.data))
@mock.patch("app.print")
def test_lifecycle_callbacks(self, mock_print):
for event in ALL_LIFECYCLE_EVENTS:
mock_print.reset_mock()
resp = self.app.post(
event, headers={SECRET_HEADER: self.secret}, json={"banana": 42}
)
self.assertEqual(resp.status_code, 200)
self.assertIn("banana", str(resp.data))
self.assertIn("banana", mock_print.mock_calls[0][1][0])
@mock.patch("app.print")
def test_lifecycle_callbacks_wrong_header_secret(self, mock_print):
for event in ALL_LIFECYCLE_EVENTS:
mock_print.reset_mock()
resp = self.app.post(
event,
headers={"X-Perc-App-Secret": self.wrong_secret},
json={"banana": 42},
)
self.assertEqual(resp.status_code, 200)
self.assertIn("banana", str(resp.data))
self.assertIn("WARNING", mock_print.mock_calls[0][1][0])
if __name__ == "__main__":
unittest.main()