Merge main into PR #291 and resolve scoreboard phase conflicts
This commit is contained in:
@@ -1,10 +1,21 @@
|
||||
import unittest
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from channels.exceptions import InvalidChannelLayerError
|
||||
from django.test import SimpleTestCase
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.test import SimpleTestCase, TestCase
|
||||
|
||||
try:
|
||||
from channels.testing import WebsocketCommunicator
|
||||
except Exception: # pragma: no cover - optional test dependency
|
||||
WebsocketCommunicator = None
|
||||
|
||||
from fupogfakta.models import GameSession, Player
|
||||
from partyhub.asgi import application
|
||||
from realtime.broadcast import broadcast_phase_event, sync_broadcast_phase_event
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
class BroadcastPhaseEventTests(SimpleTestCase):
|
||||
@patch("realtime.broadcast.get_channel_layer", return_value=None)
|
||||
@@ -25,3 +36,67 @@ class BroadcastPhaseEventTests(SimpleTestCase):
|
||||
sync_broadcast_phase_event("ABCD", "phase.scoreboard", {"phase": "scoreboard"})
|
||||
|
||||
sender.assert_called_once_with("ABCD", "phase.scoreboard", {"phase": "scoreboard"})
|
||||
|
||||
|
||||
@unittest.skipIf(WebsocketCommunicator is None, "channels.testing dependencies unavailable")
|
||||
class GameConsumerConnectTest(TestCase):
|
||||
def setUp(self):
|
||||
self.user = User.objects.create_user(username="host", password="pw")
|
||||
self.session = GameSession.objects.create(host=self.user, code="AABBCC")
|
||||
self.player = Player.objects.create(session=self.session, nickname="Tester")
|
||||
|
||||
async def test_player_connect_and_ping(self):
|
||||
token = self.player.session_token
|
||||
communicator = WebsocketCommunicator(
|
||||
application,
|
||||
f"/ws/game/AABBCC/?session_token={token}",
|
||||
)
|
||||
connected, _ = await communicator.connect()
|
||||
self.assertTrue(connected)
|
||||
|
||||
await communicator.send_json_to({"type": "ping"})
|
||||
response = await communicator.receive_json_from()
|
||||
self.assertEqual(response["type"], "pong")
|
||||
|
||||
await communicator.disconnect()
|
||||
|
||||
async def test_connect_without_token_rejected(self):
|
||||
communicator = WebsocketCommunicator(application, "/ws/game/AABBCC/")
|
||||
connected, code = await communicator.connect()
|
||||
self.assertFalse(connected)
|
||||
self.assertEqual(code, 4001)
|
||||
|
||||
async def test_connect_invalid_token_rejected(self):
|
||||
communicator = WebsocketCommunicator(
|
||||
application,
|
||||
"/ws/game/AABBCC/?session_token=invalid-token",
|
||||
)
|
||||
connected, code = await communicator.connect()
|
||||
self.assertFalse(connected)
|
||||
self.assertEqual(code, 4003)
|
||||
|
||||
async def test_host_connect_without_token(self):
|
||||
communicator = WebsocketCommunicator(
|
||||
application,
|
||||
"/ws/game/AABBCC/?role=host",
|
||||
)
|
||||
connected, _ = await communicator.connect()
|
||||
self.assertTrue(connected)
|
||||
await communicator.disconnect()
|
||||
|
||||
async def test_broadcast_reaches_connected_client(self):
|
||||
token = self.player.session_token
|
||||
communicator = WebsocketCommunicator(
|
||||
application,
|
||||
f"/ws/game/AABBCC/?session_token={token}",
|
||||
)
|
||||
connected, _ = await communicator.connect()
|
||||
self.assertTrue(connected)
|
||||
|
||||
await broadcast_phase_event("AABBCC", "phase.test_event", {"hello": "world"})
|
||||
|
||||
message = await communicator.receive_json_from(timeout=2)
|
||||
self.assertEqual(message["type"], "phase.test_event")
|
||||
self.assertEqual(message["hello"], "world")
|
||||
|
||||
await communicator.disconnect()
|
||||
|
||||
Reference in New Issue
Block a user