Update Channels to version 3

This reduces coverage since one test case needed to be removed. Its not that easy anymore to pass a custom scope into a tested application. It gets verified that no invalid authentication is possible though. Proper testing should be done with another issue.
This commit is contained in:
Georg Krause 2021-07-24 11:48:52 +00:00
parent c29f6778b5
commit 003203c45d
4 changed files with 19 additions and 24 deletions

View File

@ -8,7 +8,9 @@ application = ProtocolTypeRouter(
{ {
# Empty for now (http->django views is added by default) # Empty for now (http->django views is added by default)
"websocket": AuthMiddlewareStack( "websocket": AuthMiddlewareStack(
URLRouter([url("^api/v1/activity$", consumers.InstanceActivityConsumer)]) URLRouter(
[url("^api/v1/activity$", consumers.InstanceActivityConsumer.as_asgi())]
)
) )
} }
) )

View File

@ -36,7 +36,7 @@ pymemoize~=1.0.0
django-dynamic-preferences~=1.10 django-dynamic-preferences~=1.10
python-magic~=0.4.0 python-magic~=0.4.0
channels~=2.4.0 channels~=3.0.3
channels_redis~=3.3.0 channels_redis~=3.3.0
uvicorn[standard]~=0.14.0 uvicorn[standard]~=0.14.0
gunicorn~=20.1.0 gunicorn~=20.1.0

View File

@ -8,5 +8,6 @@ pytest-env~=0.6.0
pytest-mock~=3.6.0 pytest-mock~=3.6.0
pytest-randomly~=3.8.0 pytest-randomly~=3.8.0
pytest-sugar~=0.9.0 pytest-sugar~=0.9.0
pytest-asyncio~=0.15.1
requests-mock~=1.9.0 requests-mock~=1.9.0
faker~=8.9.1 faker~=8.9.1

View File

@ -1,26 +1,18 @@
from funkwhale_api.common import consumers import pytest
from channels.testing import WebsocketCommunicator
from funkwhale_api.common.consumers import JsonAuthConsumer
def test_auth_consumer_requires_valid_user(mocker): @pytest.mark.asyncio
m = mocker.patch("funkwhale_api.common.consumers.JsonAuthConsumer.close") async def test_auth_consumer_requires_valid_user():
scope = {"user": None} communicator = WebsocketCommunicator(JsonAuthConsumer.as_asgi(), "api/v1/activity")
consumer = consumers.JsonAuthConsumer(scope=scope) communicator.scope["user"] = None
consumer.connect() connected, subprotocol = await communicator.connect()
m.assert_called_once_with() assert not connected
def test_auth_consumer_requires_user_in_scope(mocker): @pytest.mark.asyncio
m = mocker.patch("funkwhale_api.common.consumers.JsonAuthConsumer.close") async def test_auth_consumer_requires_user_in_scope():
scope = {} communicator = WebsocketCommunicator(JsonAuthConsumer.as_asgi(), "api/v1/activity")
consumer = consumers.JsonAuthConsumer(scope=scope) connected, subprotocol = await communicator.connect()
consumer.connect() assert not connected
m.assert_called_once_with()
def test_auth_consumer_accepts_connection(mocker, factories):
user = factories["users.User"]()
m = mocker.patch("funkwhale_api.common.consumers.JsonAuthConsumer.accept")
scope = {"user": user}
consumer = consumers.JsonAuthConsumer(scope=scope)
consumer.connect()
m.assert_called_once_with()