
* core: bump django-guardian from 2.4.0 to v3.0.0 * Use GUARDIAN_MONKEY_PATCH_USER instead of deprecated GUARDIAN_MONKEY_PATCH * ??? Signed-off-by: Jens Langhammer <jens@goauthentik.io> * fix issue in outpost tests Signed-off-by: Jens Langhammer <jens@goauthentik.io> * patch all outpost tests Signed-off-by: Jens Langhammer <jens@goauthentik.io> * fixup guardian lock Signed-off-by: Jens Langhammer <jens@goauthentik.io> --------- Signed-off-by: Jens Langhammer <jens@goauthentik.io> Co-authored-by: Jens Langhammer <jens@goauthentik.io>
107 lines
3.8 KiB
Python
107 lines
3.8 KiB
Python
"""Websocket tests"""
|
|
|
|
from dataclasses import asdict
|
|
from unittest.mock import patch
|
|
|
|
from channels.routing import URLRouter
|
|
from channels.testing import WebsocketCommunicator
|
|
from django.contrib.contenttypes.models import ContentType
|
|
from django.test import TransactionTestCase
|
|
|
|
from authentik import __version__
|
|
from authentik.core.tests.utils import create_test_flow
|
|
from authentik.outposts.consumer import WebsocketMessage, WebsocketMessageInstruction
|
|
from authentik.outposts.models import Outpost, OutpostType
|
|
from authentik.providers.proxy.models import ProxyProvider
|
|
from authentik.root import websocket
|
|
|
|
|
|
def patched__get_ct_cached(app_label, codename):
|
|
"""Caches `ContentType` instances like its `QuerySet` does."""
|
|
return ContentType.objects.get(app_label=app_label, permission__codename=codename)
|
|
|
|
|
|
@patch("guardian.shortcuts._get_ct_cached", patched__get_ct_cached)
|
|
class TestOutpostWS(TransactionTestCase):
|
|
"""Websocket tests"""
|
|
|
|
def setUp(self) -> None:
|
|
self.provider: ProxyProvider = ProxyProvider.objects.create(
|
|
name="test",
|
|
internal_host="http://localhost",
|
|
external_host="http://localhost",
|
|
authorization_flow=create_test_flow(),
|
|
)
|
|
self.outpost: Outpost = Outpost.objects.create(
|
|
name="test",
|
|
type=OutpostType.PROXY,
|
|
)
|
|
self.outpost.providers.add(self.provider)
|
|
self.token = self.outpost.token.key
|
|
|
|
async def test_auth(self):
|
|
"""Test auth without token"""
|
|
communicator = WebsocketCommunicator(
|
|
URLRouter(websocket.websocket_urlpatterns), f"/ws/outpost/{self.outpost.pk}/"
|
|
)
|
|
connected, _ = await communicator.connect()
|
|
self.assertFalse(connected)
|
|
await communicator.disconnect()
|
|
|
|
async def test_auth_valid(self):
|
|
"""Test auth with token"""
|
|
communicator = WebsocketCommunicator(
|
|
URLRouter(websocket.websocket_urlpatterns),
|
|
f"/ws/outpost/{self.outpost.pk}/",
|
|
{b"authorization": f"Bearer {self.token}".encode()},
|
|
)
|
|
connected, _ = await communicator.connect()
|
|
self.assertTrue(connected)
|
|
await communicator.disconnect()
|
|
|
|
async def test_send(self):
|
|
"""Test sending of Hello"""
|
|
communicator = WebsocketCommunicator(
|
|
URLRouter(websocket.websocket_urlpatterns),
|
|
f"/ws/outpost/{self.outpost.pk}/",
|
|
{b"authorization": f"Bearer {self.token}".encode()},
|
|
)
|
|
connected, _ = await communicator.connect()
|
|
self.assertTrue(connected)
|
|
await communicator.send_json_to(
|
|
asdict(
|
|
WebsocketMessage(
|
|
instruction=WebsocketMessageInstruction.HELLO,
|
|
args={
|
|
"version": __version__,
|
|
"buildHash": "foo",
|
|
"uuid": "123",
|
|
},
|
|
)
|
|
)
|
|
)
|
|
response = await communicator.receive_json_from()
|
|
self.assertEqual(
|
|
response, asdict(WebsocketMessage(instruction=WebsocketMessageInstruction.ACK, args={}))
|
|
)
|
|
await communicator.disconnect()
|
|
|
|
async def test_send_ack(self):
|
|
"""Test sending of ACK"""
|
|
communicator = WebsocketCommunicator(
|
|
URLRouter(websocket.websocket_urlpatterns),
|
|
f"/ws/outpost/{self.outpost.pk}/",
|
|
{b"authorization": f"Bearer {self.token}".encode()},
|
|
)
|
|
connected, _ = await communicator.connect()
|
|
self.assertTrue(connected)
|
|
await communicator.send_json_to(
|
|
asdict(
|
|
WebsocketMessage(
|
|
instruction=WebsocketMessageInstruction.ACK,
|
|
args={},
|
|
)
|
|
)
|
|
)
|
|
await communicator.disconnect()
|