I am currently working on a project using the Django channels. Our server checks for authentication on connect and disconnects unauthorized users. So we don’t check authentication on receive.
However, when using Django and the channels tests, I noticed that the receive function is executed when an unauthenticated user sends a message.
Does the send_to()
function in Django Channels assume that the user is connected?
Below is a questionable part of the test code. Is it correct that the WebsocketCommunicator()
function does not make a connection, and the send_to()
function sends the message directly without a connection?
...
self.game_id = user_response_dict["game_id"]
communicator1 = WebsocketCommunicator(
application, f"/ws/general_game/{self.game_id}/"
)
communicator2 = WebsocketCommunicator(
application, f"/ws/general_game/{self.game_id}/"
)
# without connect act
# await communicator1.connect()
# await communicator2.connect()
await communicator1.send_to(
text_data=json.dumps(
{
"message_type": "ready",
"intra_id": "test1",
"number": "player1",
}
)
)
await communicator2.send_to(
text_data=json.dumps(
{
"message_type": "ready",
"intra_id": "test2",
"number": "player2",
}
)
)
...