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 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 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)
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)