question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Authentication for testing Communicators

See original GitHub issue

I was wondering how it is possible to do authentication with the Communicator objects (e.g. WebsocketCommunicator while testing (similar to client.force_login() for channels 1.x). Nothing is mentioned in the documentation at the moment. Is this available (maybe I missed it somehow) now or planned for the future?

Great job on channels 2.0! 😃

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:22
  • Comments:17 (12 by maintainers)

github_iconTop GitHub Comments

12reactions
andriilahutacommented, Aug 4, 2018

Currently I’m using something like this:

from django.test import TestCase
from channels.testing import WebsocketCommunicator
from project.routing import application

class WebsocketTestCase(TestCase):
    @async_test
    async def test_auth(self):
        user = User.objects.create_user(**user_kwargs)
        self.client.login(username=user.username, password=password)

        headers = [(b'origin', b'...'), (b'cookie', self.client.cookies.output(header='', sep='; ').encode())]
        communicator = WebsocketCommunicator(application, '/endpoint/', headers)
        connected, _ = await communicator.connect()
        self.assertTrue(connected)
        self.assertEquals(communicator.instance.scope['user'], user)
        await communicator.disconnect()

Basically you take the main ASGI app instance and connect to a particular endpoint with the right headers. Then you can send/receive data. Endpoint should be wrapped with AuthMiddlewareStack of course.

10reactions
tricoder42commented, Feb 26, 2018

@mapes911 You could also set it outside constructor. I’m using this workaround to set authenticated user:

communicator = WebsocketCommunicator(Consumer, "/")
communicator.scope['user'] = user

Feels a bit fragile, but it works. Maybe the auth provider could be simple wrapper/middleware around communicator instead of replacing communicator with different class?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Understanding Authentication, Authorization, and Encryption
Usually, authentication by a server entails the use of a user name and password. Other ways to authenticate can be through cards, retina...
Read more >
Authenticate to Azure Communication Services - Microsoft Learn
Another type of authentication uses user access tokens to authenticate against services that require user participation. For example, the chat ...
Read more >
Testing RADIUS Communication - ID Agent
Testing RADIUS Communication ... execute the program and you should receive a message that either you succeeded or failed against MFA authentication.
Read more >
Testing Network Communication - OWASP MASTG
By monitoring the requests between the mobile app client and the backend, you can easily map the available server-side APIs and gain insight...
Read more >
4 API authentication methods to better protect data in transit
Protecting API communications has become a top priority for IT security teams as APIs rapidly become a popular target for hackers.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found