Replies: 2 comments 2 replies
-
|
Hi @deronnax! You're right - WebSocket authentication testing requires manual header construction. For your immediate needs: # Session auth testing pattern
user = User.objects.create_user(username='test', password='test')
self.client.login(username='test', password='test')
cookies = self.client.cookies
headers = [(b"cookie", f"sessionid={cookies['sessionid'].value}".encode())]
communicator = WebsocketCommunicator(Consumer.as_asgi(), "/ws/", headers=headers)Better Solution: ChanxI built Chanx specifically to solve this problem. It provides: Simplified Testing: from chanx.testing import WebsocketTestCase
class ChatTest(WebsocketTestCase):
async def test_auth(self):
await self.auth_communicator.connect()
await self.auth_communicator.assert_authenticated_status_ok()DRF-Style Authentication: from chanx.generic.websocket import AsyncJsonWebsocketConsumer
class ChatConsumer(AsyncJsonWebsocketConsumer[ChatMessage]):
authentication_classes = [SessionAuthentication]
permission_classes = [IsAuthenticated]Multi-User Testing: # Test multiple authenticated users easily
second_comm = self.create_communicator(headers=different_user_headers)Links:
This addresses exactly the documentation gaps you mentioned! 🚀 |
Beta Was this translation helpful? Give feedback.
-
|
@carltongibson I am sincerely sorry to ping you (I know open-source maintainers are overwhelmed with the maintenance burden and over-solicitation of open-source projects), but I did not get an answer from you, and I really would like your insights about this. To me at least those subjects are missing from the Testing section:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have been trying to create a chat app in an existing Django project, and I am surprised by the amount of channels domains not covered by the documentation. I will probably create an issue/discussion for all of them.
The one I have in front of me now is: the documentation talks about authentication, but there is no indication whatsoever on how do it in test.
Are we supposed to feed hand-made headers to the websocket communicator? If that is so, even just a line saying it is the way to go would be huge improvment over head-scratching.
I am willing to add this line, and even go some extra steps if greenlighted.
Cheers.
Beta Was this translation helpful? Give feedback.
All reactions