示例#1
0
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)
示例#2
0
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)