-
Notifications
You must be signed in to change notification settings - Fork 2
/
tests.py
328 lines (273 loc) · 11.6 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import unittest
try:
from unittest import mock
except ImportError:
import mock
from decimal import Decimal
import pusher as _pusher
from flask import Flask, json, render_template_string, url_for
from flask_pusher import Pusher
pusher_conf = {
"PUSHER_APP_ID": "1234",
"PUSHER_KEY": "KEY",
"PUSHER_SECRET": "SUPERSECRET",
}
SOCKET_ID = "1.42"
class CustomJSONEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, Decimal):
return str(o)
return super(CustomJSONEncoder, self).default(o)
class PusherClientTest(unittest.TestCase):
def setUp(self):
self.app = Flask(__name__)
self.app.config.update(pusher_conf)
def test_default_config(self):
# fallback to Pusher globals still works
pusher = Pusher(self.app)
with self.app.test_request_context():
self.assertIsNotNone(pusher.client)
def test_lazy_init_app(self):
pusher = Pusher()
pusher.init_app(self.app)
with self.app.test_request_context():
self.assertIsNotNone(pusher.client)
def test_create_extensions_map(self):
del self.app.extensions
pusher = Pusher(self.app)
with self.app.test_request_context():
self.assertIsNotNone(pusher.client)
def test_json_encoder(self):
self.app.json_encoder = CustomJSONEncoder
pusher = Pusher(self.app)
with self.app.test_request_context():
try:
enc = pusher.client._json_encoder
except AttributeError:
enc = pusher.client.encoder
self.assertEqual(CustomJSONEncoder, enc)
def test_configuration(self):
self.app.config["PUSHER_HOST"] = "example.com"
self.app.config["PUSHER_PORT"] = 8080
pusher = Pusher(self.app)
with self.app.test_request_context():
self.assertIsNotNone(pusher.client)
self.assertEqual("KEY", pusher.client.key)
self.assertEqual("SUPERSECRET", pusher.client.secret)
self.assertEqual("example.com", pusher.client.host)
self.assertEqual(8080, pusher.client.port)
def test_all_configurations(self):
backend = mock.Mock()
self.app.config.update({
"PUSHER_SSL": False,
"PUSHER_TIMEOUT": 3,
"PUSHER_CLUSTER": "eu",
"PUSHER_BACKEND": backend,
"PUSHER_BACKEND_OPTIONS": {"anything": True},
"PUSHER_NOTIFICATION_HOST": "example.com",
"PUSHER_NOTIFICATION_SSL": True,
"PUSHER_ENCRYPTION_MASTER_KEY": "SUPERSECRET",
})
pusher = Pusher(self.app)
with self.app.test_request_context():
self.assertIsNotNone(pusher.client)
self.assertFalse(pusher.client.ssl)
self.assertEqual(3, pusher.client.timeout)
self.assertEqual("api-eu.pusher.com", pusher.client.host)
try:
self.assertEqual(b"SUPERSECRET", pusher.client._encryption_master_key)
except AttributeError:
pass
self.assertTrue(backend.called)
self.assertTrue(backend.call_args[1]["anything"])
def test_pusher_key_in_template(self):
Pusher(self.app)
with self.app.test_request_context():
rendered = render_template_string("{{ PUSHER_KEY }}")
self.assertEqual("KEY", rendered)
class PusherAuthTest(unittest.TestCase):
def setUp(self):
self.app = Flask(__name__)
self.app.debug = True
self.app.config.update(pusher_conf)
self.pusher = Pusher(self.app)
self.client = self.app.test_client()
def test_url_for(self):
with self.app.test_request_context():
url = url_for("pusher.auth")
self.assertEqual("/pusher/auth", url)
def test_forbidden_withuot_auth_handler(self):
response = self.client.post("/pusher/auth")
self.assertEqual(403, response.status_code)
def test_auth_refused(self):
self.pusher.auth(lambda c, s: False)
response = self.client.post("/pusher/auth",
data={"channel_name": "private-a",
"socket_id": SOCKET_ID})
self.assertEqual(403, response.status_code)
def test_auth_accepted(self):
self.pusher.auth(lambda c, s: True)
response = self.client.post("/pusher/auth",
data={"channel_name": "private-a",
"socket_id": SOCKET_ID})
self.assertEqual(200, response.status_code)
data = json.loads(response.data)
self.assertIn("auth", data)
self.assertNotIn("channel_data", data)
def test_no_channel_data_in_private_channel(self):
self.pusher.auth(lambda c, s: True)
response = self.client.post("/pusher/auth",
data={"channel_name": "private-a",
"socket_id": SOCKET_ID})
self.assertEqual(200, response.status_code)
data = json.loads(response.data)
self.assertIn("auth", data)
self.assertNotIn("channel_data", data)
def test_default_channel_data_in_presence_channel(self):
self.pusher.auth(lambda c, s: True)
response = self.client.post("/pusher/auth",
data={"channel_name": "presence-a",
"socket_id": SOCKET_ID})
self.assertEqual(200, response.status_code)
data = json.loads(response.data)
self.assertIn("auth", data)
channel_data = json.loads(data["channel_data"])
self.assertEqual({"user_id": SOCKET_ID}, channel_data)
def test_channel_data_in_presence_channel(self):
self.pusher.auth(lambda c, s: True)
self.pusher.channel_data(lambda c, s: {"foo": "bar"})
response = self.client.post("/pusher/auth",
data={"channel_name": "presence-a",
"socket_id": SOCKET_ID})
self.assertEqual(200, response.status_code)
data = json.loads(response.data)
self.assertIn("auth", data)
channel_data = json.loads(data["channel_data"])
self.assertEqual(SOCKET_ID, channel_data["user_id"])
self.assertIn("bar", channel_data["foo"])
def test_invalid_channel(self):
self.pusher.auth(lambda c, s: True)
response = self.client.post("/pusher/auth",
data={"channel_name": "foo",
"socket_id": SOCKET_ID})
self.assertEqual(404, response.status_code)
def test_pretty_print_true(self):
self.app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True
self.test_no_channel_data_in_private_channel()
class PusherBatchAuthTest(unittest.TestCase):
def setUp(self):
self.app = Flask(__name__)
self.app.debug = True
self.app.config.update(pusher_conf)
self.pusher = Pusher(self.app)
self.client = self.app.test_client()
def test_one_channel(self):
self.pusher.auth(lambda c, s: True)
response = self.client.post("/pusher/auth",
data={"channel_name[0]": "private-a",
"socket_id": SOCKET_ID})
self.assertEqual(200, response.status_code)
data = json.loads(response.data)
self.assertEqual(1, len(data))
data_a = data.get("private-a")
self.assertEqual(200, data_a["status"])
self.assertTrue(data_a["data"])
def test_more_channels(self):
self.pusher.auth(lambda c, s: "b" not in c)
response = self.client.post("/pusher/auth",
data={"channel_name[0]": "private-a",
"channel_name[1]": "private-b",
"channel_name[2]": "presence-c",
"socket_id": SOCKET_ID})
self.assertEqual(200, response.status_code)
data = json.loads(response.data)
self.assertEqual(3, len(data))
a = data.get("private-a")
self.assertEqual(200, a["status"])
self.assertIn("auth", a["data"])
b = data.get("private-b")
self.assertEqual(403, b["status"])
c = data.get("presence-c")
self.assertEqual(200, c["status"])
c_data = c["data"]
self.assertIn("auth", c_data)
self.assertIn("channel_data", c_data)
def test_missing_channel(self):
self.pusher.auth(lambda c, s: True)
response = self.client.post("/pusher/auth",
data={"channel_name[1]": "private-b",
"socket_id": SOCKET_ID})
self.assertEqual(400, response.status_code)
class PusherWebhookTest(unittest.TestCase):
def setUp(self):
self.app = Flask(__name__)
self.app.debug = True
self.app.config.update(pusher_conf)
self.pusher = Pusher()
self.client = self.app.test_client()
self._called = False
@self.pusher.webhooks.client
def c():
self._called = True
self.pusher.init_app(self.app)
def test_no_webhook(self):
with self.app.test_request_context():
url = url_for("pusher.presence_event")
response = self.client.post(url)
self.assertEqual(404, response.status_code)
self.assertFalse(self._called)
def test_without_key_forbidden(self):
with self.app.test_request_context():
url = url_for("pusher.client_event")
response = self.client.post(url)
self.assertEqual(403, response.status_code)
self.assertFalse(self._called)
def test_invalid_key_forbidden(self):
with self.app.test_request_context():
url = url_for("pusher.client_event")
response = self.client.post(url, headers={
"Content-Type": "application/json",
"X-Pusher-Key": "meh"
})
self.assertEqual(403, response.status_code)
self.assertFalse(self._called)
def test_valid_key_forbidden_without_signature(self):
with self.app.test_request_context():
url = url_for("pusher.client_event")
response = self.client.post(url, headers={
"Content-Type": "application/json",
"X-Pusher-Key": "KEY"
})
self.assertEqual(403, response.status_code)
self.assertFalse(self._called)
def test_invalid_signature(self):
with self.app.test_request_context():
url = url_for("pusher.client_event")
response = self.client.post(url, headers={
"Content-Type": "application/json",
"X-Pusher-Key": "KEY",
"X-Pusher-Signature": "x"
})
self.assertEqual(403, response.status_code)
self.assertFalse(self._called)
def test_valid_signature(self):
data = '{"a": "b"}'
with self.app.test_request_context():
url = url_for("pusher.client_event")
signature = self.pusher._sign(data)
response = self.client.post(url, data=data, headers={
"Content-Type": "application/json",
"X-Pusher-Key": "KEY",
"X-Pusher-Signature": signature
})
self.assertEqual(200, response.status_code)
self.assertTrue(self._called)
def test_hook_all_handlers(self):
@self.pusher.webhooks.presence
def h1():
pass
@self.pusher.webhooks.channel_existence
def h2():
pass
if __name__ == '__main__':
unittest.main()