summaryrefslogtreecommitdiff
path: root/tests/test_ws_interface.py
blob: 677633467d6b9ffdc2ce19e707a872bcb76d6c6b (plain)
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
import unittest

from swiftstory.interface.ws import WebsocketsInterface
from swiftstory.status import error


class TestWebsocketsInterface(unittest.TestCase):
    def setUp(self):
        # XXX Passing None as the GameManager. Only OK if we don't try to join
        # a game.
        self.interface = WebsocketsInterface(None)

    def test_receive_invalid_json(self):
        self.assertEqual(
                error("badly formatted json"),
                self.interface.message_received_handler(client=None, message="{invalid_json}")
                )

    def test_receive_json_array(self):
        self.assertEqual(
                error("invalid command"),
                self.interface.message_received_handler(client=None, message='[]')
                )

    def test_receive_json_number(self):
        self.assertEqual(
                error("invalid command"),
                self.interface.message_received_handler(client=None, message='2.3')
                )

    def test_receive_json_null(self):
        self.assertEqual(
                error("invalid command"),
                self.interface.message_received_handler(client=None, message='null')
                )

    def test_receive_unknown_command(self):
        self.assertEqual(
                error("invalid command"),
                self.interface.message_received_handler(client=None, message='{"op": "unknown"}')
                )

    def test_receive_without_command(self):
        self.assertEqual(
                error("invalid command"),
                self.interface.message_received_handler(client=None, message='{}')
                )

    def test_play_card_not_specified(self):
        payload = '{"op": "play_white_card"}'
        self.assertEqual(
                error("field `card_id' is required"),
                self.interface.message_received_handler(client=None, message=payload)
                )

    def test_join_game_not_specified(self):
        payload = '{"op": "join_game"}'
        self.assertEqual(
                error("field `game_name' is required"),
                self.interface.message_received_handler(client=None, message=payload)
                )