From 003203c45d853cea8896229703b1fadd20456060 Mon Sep 17 00:00:00 2001
From: Georg Krause <mail@georg-krause.net>
Date: Sat, 24 Jul 2021 11:48:52 +0000
Subject: [PATCH] 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.
---
 api/config/routing.py                |  4 +++-
 api/requirements/base.txt            |  2 +-
 api/requirements/test.txt            |  1 +
 api/tests/channels/test_consumers.py | 36 +++++++++++-----------------
 4 files changed, 19 insertions(+), 24 deletions(-)

diff --git a/api/config/routing.py b/api/config/routing.py
index ce293ff768..4d2f482078 100644
--- a/api/config/routing.py
+++ b/api/config/routing.py
@@ -8,7 +8,9 @@ application = ProtocolTypeRouter(
     {
         # Empty for now (http->django views is added by default)
         "websocket": AuthMiddlewareStack(
-            URLRouter([url("^api/v1/activity$", consumers.InstanceActivityConsumer)])
+            URLRouter(
+                [url("^api/v1/activity$", consumers.InstanceActivityConsumer.as_asgi())]
+            )
         )
     }
 )
diff --git a/api/requirements/base.txt b/api/requirements/base.txt
index 704f57f6ed..be7b98a48e 100644
--- a/api/requirements/base.txt
+++ b/api/requirements/base.txt
@@ -36,7 +36,7 @@ pymemoize~=1.0.0
 
 django-dynamic-preferences~=1.10
 python-magic~=0.4.0
-channels~=2.4.0
+channels~=3.0.3
 channels_redis~=3.3.0
 uvicorn[standard]~=0.14.0
 gunicorn~=20.1.0
diff --git a/api/requirements/test.txt b/api/requirements/test.txt
index 0821198f06..8307ce2676 100644
--- a/api/requirements/test.txt
+++ b/api/requirements/test.txt
@@ -8,5 +8,6 @@ pytest-env~=0.6.0
 pytest-mock~=3.6.0
 pytest-randomly~=3.8.0
 pytest-sugar~=0.9.0
+pytest-asyncio~=0.15.1
 requests-mock~=1.9.0
 faker~=8.9.1
diff --git a/api/tests/channels/test_consumers.py b/api/tests/channels/test_consumers.py
index f4f5f3ad6f..985e3cdd6f 100644
--- a/api/tests/channels/test_consumers.py
+++ b/api/tests/channels/test_consumers.py
@@ -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):
-    m = mocker.patch("funkwhale_api.common.consumers.JsonAuthConsumer.close")
-    scope = {"user": None}
-    consumer = consumers.JsonAuthConsumer(scope=scope)
-    consumer.connect()
-    m.assert_called_once_with()
+@pytest.mark.asyncio
+async def test_auth_consumer_requires_valid_user():
+    communicator = WebsocketCommunicator(JsonAuthConsumer.as_asgi(), "api/v1/activity")
+    communicator.scope["user"] = None
+    connected, subprotocol = await communicator.connect()
+    assert not connected
 
 
-def test_auth_consumer_requires_user_in_scope(mocker):
-    m = mocker.patch("funkwhale_api.common.consumers.JsonAuthConsumer.close")
-    scope = {}
-    consumer = consumers.JsonAuthConsumer(scope=scope)
-    consumer.connect()
-    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()
+@pytest.mark.asyncio
+async def test_auth_consumer_requires_user_in_scope():
+    communicator = WebsocketCommunicator(JsonAuthConsumer.as_asgi(), "api/v1/activity")
+    connected, subprotocol = await communicator.connect()
+    assert not connected
-- 
GitLab