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
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_override_config(self): pusher = Pusher() pusher.init_app(self.app, port=1337) self.assertEqual(pusher.port, 1337)
def test_options(self): pusher = Pusher() pusher.init_app(self.app, host='eu', timeout=20) self.assertEqual(pusher.host, 'eu') self.assertEqual(pusher.timeout, 20)
def test_init_app(self): pusher = Pusher() pusher.init_app(self.app) self._assert_init(pusher)