Why can unconneted users send message?

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",
                }
            )
        )

       ...

To the extent of my understanding, those statements are correct. Quoting from the docs at Testing — Channels 4.0.0 documentation

To help with testing, Channels provides test helpers called Communicators , which allow you to wrap up an ASGI application (like a consumer) into its own event loop and ask it questions.

If you look at the WebsocketCommunicator class in channels.testing.websocket and its parent class at asgiref.testing.ApplicationCommunicator, you can see exactly what it’s doing.

1 Like