|
| 1 | +from asgiref.sync import async_to_sync |
| 2 | +from channels.routing import URLRouter |
| 3 | +from channels.testing import WebsocketCommunicator |
| 4 | +from django.contrib.auth import get_user_model |
| 5 | +from django.test import TransactionTestCase |
| 6 | +from django.urls import re_path |
| 7 | + |
| 8 | +from openwisp_users.tests.utils import TestOrganizationMixin |
| 9 | + |
| 10 | +from ..consumers import RadiusBatchConsumer |
| 11 | +from ..utils import load_model |
| 12 | +from . import CreateRadiusObjectsMixin |
| 13 | + |
| 14 | +User = get_user_model() |
| 15 | +RadiusBatch = load_model("RadiusBatch") |
| 16 | + |
| 17 | +application = URLRouter( |
| 18 | + [ |
| 19 | + re_path( |
| 20 | + r"^ws/radius/batch/(?P<batch_id>[^/]+)/$", |
| 21 | + RadiusBatchConsumer.as_asgi(), |
| 22 | + ), |
| 23 | + ] |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +class TestRadiusBatchConsumer( |
| 28 | + CreateRadiusObjectsMixin, TestOrganizationMixin, TransactionTestCase |
| 29 | +): |
| 30 | + |
| 31 | + TEST_PASSWORD = "test_password" # noqa: S105 |
| 32 | + |
| 33 | + def _create_test_data(self): |
| 34 | + org = self._create_org() |
| 35 | + user = self._create_admin(password=self.TEST_PASSWORD) |
| 36 | + batch = self._create_radius_batch( |
| 37 | + name="test-batch", |
| 38 | + strategy="prefix", |
| 39 | + prefix="test-", |
| 40 | + organization=org, |
| 41 | + ) |
| 42 | + return org, user, batch |
| 43 | + |
| 44 | + def test_websocket_connect_superuser(self): |
| 45 | + _, user, batch = self._create_test_data() |
| 46 | + |
| 47 | + async def test(): |
| 48 | + communicator = WebsocketCommunicator( |
| 49 | + application, |
| 50 | + f"/ws/radius/batch/{batch.pk}/", |
| 51 | + ) |
| 52 | + communicator.scope["user"] = user |
| 53 | + communicator.scope["url_route"] = {"kwargs": {"batch_id": str(batch.pk)}} |
| 54 | + |
| 55 | + connected, _ = await communicator.connect() |
| 56 | + assert connected is True |
| 57 | + await communicator.disconnect() |
| 58 | + |
| 59 | + async_to_sync(test)() |
| 60 | + |
| 61 | + def test_websocket_connect_staff_with_permission(self): |
| 62 | + org, _, batch = self._create_test_data() |
| 63 | + staff_user = self._create_administrator( |
| 64 | + organizations=[org], password=self.TEST_PASSWORD |
| 65 | + ) |
| 66 | + |
| 67 | + async def test(): |
| 68 | + communicator = WebsocketCommunicator( |
| 69 | + application, |
| 70 | + f"/ws/radius/batch/{batch.pk}/", |
| 71 | + ) |
| 72 | + communicator.scope["user"] = staff_user |
| 73 | + communicator.scope["url_route"] = {"kwargs": {"batch_id": str(batch.pk)}} |
| 74 | + |
| 75 | + connected, _ = await communicator.connect() |
| 76 | + assert connected is True |
| 77 | + await communicator.disconnect() |
| 78 | + |
| 79 | + async_to_sync(test)() |
| 80 | + |
| 81 | + def test_websocket_reject_unauthenticated(self): |
| 82 | + _, _, batch = self._create_test_data() |
| 83 | + |
| 84 | + async def test(): |
| 85 | + communicator = WebsocketCommunicator( |
| 86 | + application, |
| 87 | + f"/ws/radius/batch/{batch.pk}/", |
| 88 | + ) |
| 89 | + from django.contrib.auth.models import AnonymousUser |
| 90 | + |
| 91 | + communicator.scope["user"] = AnonymousUser() |
| 92 | + communicator.scope["url_route"] = {"kwargs": {"batch_id": str(batch.pk)}} |
| 93 | + |
| 94 | + connected, _ = await communicator.connect() |
| 95 | + assert connected is False |
| 96 | + |
| 97 | + async_to_sync(test)() |
| 98 | + |
| 99 | + def test_websocket_reject_non_staff(self): |
| 100 | + _, _, batch = self._create_test_data() |
| 101 | + regular_user = self._create_user(is_staff=False, password=self.TEST_PASSWORD) |
| 102 | + |
| 103 | + async def test(): |
| 104 | + communicator = WebsocketCommunicator( |
| 105 | + application, |
| 106 | + f"/ws/radius/batch/{batch.pk}/", |
| 107 | + ) |
| 108 | + communicator.scope["user"] = regular_user |
| 109 | + communicator.scope["url_route"] = {"kwargs": {"batch_id": str(batch.pk)}} |
| 110 | + |
| 111 | + connected, _ = await communicator.connect() |
| 112 | + assert connected is False |
| 113 | + |
| 114 | + async_to_sync(test)() |
| 115 | + |
| 116 | + def test_websocket_reject_no_permission(self): |
| 117 | + _, _, batch = self._create_test_data() |
| 118 | + |
| 119 | + staff_user = self._create_user(is_staff=True, password=self.TEST_PASSWORD) |
| 120 | + |
| 121 | + async def test(): |
| 122 | + communicator = WebsocketCommunicator( |
| 123 | + application, |
| 124 | + f"/ws/radius/batch/{batch.pk}/", |
| 125 | + ) |
| 126 | + communicator.scope["user"] = staff_user |
| 127 | + communicator.scope["url_route"] = {"kwargs": {"batch_id": str(batch.pk)}} |
| 128 | + |
| 129 | + connected, _ = await communicator.connect() |
| 130 | + assert connected is False |
| 131 | + |
| 132 | + async_to_sync(test)() |
| 133 | + |
| 134 | + def test_websocket_group_connection(self): |
| 135 | + _, user, batch = self._create_test_data() |
| 136 | + |
| 137 | + async def test(): |
| 138 | + communicator = WebsocketCommunicator( |
| 139 | + application, |
| 140 | + f"/ws/radius/batch/{batch.pk}/", |
| 141 | + ) |
| 142 | + communicator.scope["user"] = user |
| 143 | + communicator.scope["url_route"] = {"kwargs": {"batch_id": str(batch.pk)}} |
| 144 | + |
| 145 | + connected, _ = await communicator.connect() |
| 146 | + assert connected is True |
| 147 | + await communicator.disconnect() |
| 148 | + |
| 149 | + async_to_sync(test)() |
0 commit comments