Spaces:
Running
Running
Download api/tests/chat_test_support.py from Dayozoe/harvestconnect-api: direct link, hf CLI and curl.
- Browser
- Download file 1.96 kB
-
https://huggingface.co/spaces/Dayozoe/harvestconnect-api/resolve/main/api/tests/chat_test_support.py
- Command line
-
hf download hf://spaces/Dayozoe/harvestconnect-api/api/tests/chat_test_support.py
-
curl -L -o chat_test_support.py https://huggingface.co/spaces/Dayozoe/harvestconnect-api/resolve/main/api/tests/chat_test_support.py
1.96 kB
| import asyncio | |
| from unittest.mock import patch | |
| from asgiref.sync import async_to_sync | |
| from channels.routing import URLRouter | |
| from channels.layers import channel_layers | |
| from channels.testing import WebsocketCommunicator | |
| from channels.security.websocket import OriginValidator | |
| from django.contrib.auth import get_user_model | |
| from django.test import TransactionTestCase, override_settings | |
| from django.urls import re_path | |
| from rest_framework_simplejwt.tokens import AccessToken | |
| from ..consumers import ChatConsumer | |
| from ..middleware import JwtAuthMiddleware | |
| from ..models import ChatRoom | |
| class ChatWebSocketTestCase(TransactionTestCase): | |
| reset_sequences = True | |
| def setUp(self): | |
| # Reset Channels' cached backend so this isolated suite always uses its | |
| # deterministic in-memory layer regardless of the runtime Redis config. | |
| channel_layers.backends.clear() | |
| User = get_user_model() | |
| self.member = User.objects.create_user(username="chat-member", email="member@example.test", password="test-pass") | |
| self.other = User.objects.create_user(username="chat-other", email="other@example.test", password="test-pass") | |
| self.room = ChatRoom.objects.create(name="private-room", room_type="group") | |
| self.room.participants.add(self.member) | |
| self.application = OriginValidator( | |
| JwtAuthMiddleware(URLRouter([ | |
| re_path(r"^ws/chat/(?P<room_id>[^/]+)/?$", ChatConsumer.as_asgi()), | |
| ])), | |
| ["http://localhost:3001"], | |
| ) | |
| def _communicator(self, token=None, room_id=None): | |
| protocols = ["jwt", token] if token else [] | |
| return WebsocketCommunicator( | |
| self.application, | |
| f"/ws/chat/{room_id or self.room.pk}/", | |
| subprotocols=protocols, | |
| headers=[(b"origin", b"http://localhost:3001")], | |
| ) | |