gestioCOF/kfet/open/tests.py

380 lines
14 KiB
Python
Raw Normal View History

import random
from datetime import timedelta
from unittest import mock
2022-06-30 09:17:29 +02:00
from asgiref.sync import async_to_sync
2022-06-29 13:34:52 +02:00
from channels.auth import AuthMiddlewareStack
from channels.consumer import get_channel_layer
from channels.testing import WebsocketCommunicator
from django.contrib.auth.models import AnonymousUser, Permission, User
2022-06-29 13:34:52 +02:00
from django.test import Client, TestCase
from django.utils import timezone
from . import OpenKfet
from .consumers import OpenKfetConsumer
2022-06-29 13:34:52 +02:00
def ws_communicator(cls, path: str, headers=[]):
return WebsocketCommunicator(AuthMiddlewareStack(cls.as_asgi()), path, headers)
class OpenKfetTest(TestCase):
"""OpenKfet object unit-tests suite."""
def setUp(self):
self.kfet_open = OpenKfet(
2022-10-03 10:33:29 +02:00
cache_prefix="test_kfetopen_%s" % random.randrange(2**20)
)
self.addCleanup(self.kfet_open.clear_cache)
def test_defaults(self):
"""Default values."""
self.assertFalse(self.kfet_open.raw_open)
self.assertIsNone(self.kfet_open.last_update)
self.assertFalse(self.kfet_open.force_close)
self.assertFalse(self.kfet_open.is_open)
def test_raw_open(self):
"""Get and set raw_open; last_update is renewed."""
for raw_open in [True, False]:
prev_update = self.kfet_open.last_update
self.kfet_open.raw_open = raw_open
self.assertEqual(raw_open, self.kfet_open.raw_open)
self.assertNotEqual(prev_update, self.kfet_open.last_update)
def test_force_close(self):
"""Get and set force_close."""
for force_close in [True, False]:
self.kfet_open.force_close = force_close
self.assertEqual(force_close, self.kfet_open.force_close)
def test_is_open(self):
"""If force_close is disabled, is_open is raw_open."""
self.kfet_open.force_close = False
for raw_open in [True, False]:
self.kfet_open.raw_open = raw_open
self.assertEqual(raw_open, self.kfet_open.is_open)
def test_is_open_force_close(self):
"""If force_close is enabled, is_open is False."""
self.kfet_open.force_close = True
for raw_open in [True, False]:
self.kfet_open.raw_open = raw_open
self.assertFalse(self.kfet_open.is_open)
def test_status(self):
# (raw_open, force_close, expected status, expected admin)
cases = [
(False, False, OpenKfet.CLOSED, OpenKfet.CLOSED),
(False, True, OpenKfet.CLOSED, OpenKfet.CLOSED),
(True, False, OpenKfet.OPENED, OpenKfet.OPENED),
(True, True, OpenKfet.CLOSED, OpenKfet.FAKE_CLOSED),
]
for raw_open, force_close, exp_stat, exp_adm_stat in cases:
self.kfet_open.raw_open = raw_open
self.kfet_open.force_close = force_close
self.assertEqual(exp_stat, self.kfet_open.status())
self.assertEqual(exp_adm_stat, self.kfet_open.admin_status())
def test_status_unknown(self):
self.kfet_open.raw_open = True
self.kfet_open._last_update = timezone.now() - timedelta(days=30)
self.assertEqual(OpenKfet.UNKNOWN, self.kfet_open.status())
def test_export_user(self):
"""Export is limited for an anonymous user."""
export = self.kfet_open.export(AnonymousUser())
2022-06-29 13:34:52 +02:00
self.assertSetEqual(set(["status", "type"]), set(export))
def test_export_team(self):
"""Export all values for a team member."""
user = User.objects.create_user("team", "", "team")
2019-12-24 17:13:27 +01:00
is_team = Permission.objects.get(
codename="is_team", content_type__app_label="kfet"
)
user.user_permissions.add(is_team)
export = self.kfet_open.export(user)
2022-06-29 13:34:52 +02:00
self.assertSetEqual(
set(["status", "admin_status", "force_close", "type"]), set(export)
)
async def test_send_ws(self):
channel_layer = get_channel_layer()
base_channel = await channel_layer.new_channel()
team_channel = await channel_layer.new_channel()
2022-06-29 13:34:52 +02:00
await channel_layer.group_add("kfet.open.base", base_channel)
await channel_layer.group_add("kfet.open.team", team_channel)
2022-06-29 13:34:52 +02:00
await self.kfet_open.send_ws()
2022-06-29 13:34:52 +02:00
base = await channel_layer.receive(base_channel)
2022-06-29 13:34:52 +02:00
self.assertSetEqual(set(["status", "type"]), set(base))
team = await channel_layer.receive(team_channel)
self.assertSetEqual(
set(["status", "admin_status", "force_close", "type"]), set(team)
)
2022-06-29 13:34:52 +02:00
class OpenKfetViewsTest(TestCase):
"""OpenKfet views unit-tests suite."""
def setUp(self):
# Need this (and here) because of '<client>.login' in setUp
patcher_messages = mock.patch("gestioncof.signals.messages")
patcher_messages.start()
self.addCleanup(patcher_messages.stop)
# get some permissions
perms = {
2019-12-24 17:13:27 +01:00
"kfet.is_team": Permission.objects.get(
codename="is_team", content_type__app_label="kfet"
),
2019-12-24 17:13:27 +01:00
"kfet.can_force_close": Permission.objects.get(
codename="can_force_close", content_type__app_label="kfet"
),
}
# authenticated user and its client
self.u = User.objects.create_user("user", "", "user")
self.c = Client()
self.c.login(username="user", password="user")
# team user and its clients
self.t = User.objects.create_user("team", "", "team")
self.t.user_permissions.add(perms["kfet.is_team"])
self.c_t = Client()
self.c_t.login(username="team", password="team")
# admin user and its client
self.a = User.objects.create_user("admin", "", "admin")
self.a.user_permissions.add(
perms["kfet.is_team"], perms["kfet.can_force_close"]
)
self.c_a = Client()
self.c_a.login(username="admin", password="admin")
self.kfet_open = OpenKfet(
2022-10-03 10:33:29 +02:00
cache_prefix="test_kfetopen_%s" % random.randrange(2**20)
)
self.addCleanup(self.kfet_open.clear_cache)
views_patcher = mock.patch("kfet.open.views.kfet_open", self.kfet_open)
views_patcher.start()
self.addCleanup(views_patcher.stop)
def test_door(self):
"""Edit raw_status."""
for sent, expected in [(1, True), (0, False)]:
resp = Client().post(
"/k-fet/open/raw_open", {"raw_open": sent, "token": "plop"}
)
self.assertEqual(200, resp.status_code)
self.assertEqual(expected, self.kfet_open.raw_open)
def test_force_close(self):
"""Edit force_close."""
for sent, expected in [(1, True), (0, False)]:
resp = self.c_a.post("/k-fet/open/force_close", {"force_close": sent})
self.assertEqual(200, resp.status_code)
self.assertEqual(expected, self.kfet_open.force_close)
def test_force_close_forbidden(self):
"""Can't edit force_close without kfet.can_force_close permission."""
clients = [Client(), self.c, self.c_t]
for client in clients:
resp = client.post("/k-fet/open/force_close", {"force_close": 0})
self.assertEqual(403, resp.status_code)
2022-06-29 13:34:52 +02:00
class OpenKfetConsumerTest(TestCase):
"""OpenKfet consumer unit-tests suite."""
2022-06-30 09:17:29 +02:00
@classmethod
def setUpTestData(cls):
2022-06-29 13:34:52 +02:00
t = User.objects.create_user("team", "", "team")
is_team = Permission.objects.get(
codename="is_team", content_type__app_label="kfet"
)
t.user_permissions.add(is_team)
2022-06-30 09:17:29 +02:00
cls.team_user = t
2022-06-29 13:34:52 +02:00
async def test_standard_user(self):
"""Lambda user is added to kfet.open.base group."""
# setup anonymous client
2022-06-29 13:34:52 +02:00
c = ws_communicator(OpenKfetConsumer, "/ws/k-fet/open")
2022-06-29 13:34:52 +02:00
connected, _ = await c.connect()
self.assertTrue(connected)
# initialization data is replied on connection
2022-06-29 13:34:52 +02:00
message = await c.receive_json_from()
self.assertIsNotNone(message)
# client belongs to the 'kfet.open' group...
2022-06-29 13:34:52 +02:00
channel_layer = get_channel_layer()
await channel_layer.group_send(
"kfet.open.base", {"test": "plop", "type": "open.status"}
)
message = await c.receive_json_from()
self.assertEqual(message, {"test": "plop"})
# ...but not to the 'kfet.open.admin' one
2022-06-29 13:34:52 +02:00
await channel_layer.group_send(
"kfet.open.team", {"test": "plop", "type": "open.status"}
)
self.assertTrue(await c.receive_nothing())
2022-06-29 13:34:52 +02:00
async def test_team_user(self):
"""Team user is added to kfet.open.team group."""
2022-06-29 13:34:52 +02:00
with mock.patch("gestioncof.signals.messages"), mock.patch(
"kfet.open.consumers.kfet_is_team", return_value=True
):
c = ws_communicator(OpenKfetConsumer, "/ws/k-fet/open")
2022-06-29 13:34:52 +02:00
connected, _ = await c.connect()
2022-06-29 13:34:52 +02:00
channel_layer = get_channel_layer()
2022-06-29 13:34:52 +02:00
self.assertTrue(connected)
2022-06-29 13:34:52 +02:00
# initialization data is replied on connection
message = await c.receive_json_from()
self.assertIsNotNone(message)
# client belongs to the 'kfet.open.team' group...
await channel_layer.group_send(
"kfet.open.team", {"test": "plop", "type": "open.status"}
)
message = await c.receive_json_from()
self.assertEqual(message, {"test": "plop"})
# ...but not to the 'kfet.open' one
await channel_layer.group_send(
"kfet.open.base", {"test": "plop", "type": "open.status"}
)
self.assertTrue(await c.receive_nothing())
2022-06-29 13:34:52 +02:00
class OpenKfetScenarioTest(TestCase):
"""OpenKfet functionnal tests suite."""
2022-06-30 09:17:29 +02:00
@classmethod
def setUpTestData(cls):
# root user
cls.r = User.objects.create_superuser("team", "", "team")
# anonymous client (for views)
2022-06-30 09:17:29 +02:00
cls.c = Client()
2022-06-29 13:34:52 +02:00
# root client
2022-06-30 09:17:29 +02:00
cls.r_c = Client()
with mock.patch("gestioncof.signals.messages"):
cls.r_c.login(username="team", password="team")
def setUp(self):
# Create a channel to listen to KPsul's messages
channel_layer = get_channel_layer()
self.channel = async_to_sync(channel_layer.new_channel)()
self.team_channel = async_to_sync(channel_layer.new_channel)()
async_to_sync(channel_layer.group_add)("kfet.open.base", self.channel)
async_to_sync(channel_layer.group_add)("kfet.open.team", self.team_channel)
self.receive_msg = lambda c: async_to_sync(channel_layer.receive)(c)
self.kfet_open = OpenKfet(
2022-10-03 10:33:29 +02:00
cache_prefix="test_kfetopen_%s" % random.randrange(2**20)
)
self.addCleanup(self.kfet_open.clear_cache)
2022-06-30 09:17:29 +02:00
async def ws_connect(self, ws_communicator):
c, _ = await ws_communicator.connect()
2022-06-29 13:34:52 +02:00
self.assertTrue(c)
2022-06-30 09:17:29 +02:00
return await ws_communicator.receive_json_from()
2022-06-30 09:17:29 +02:00
def test_scenario_1(self):
"""Clients connect, door opens, enable force close."""
2022-06-29 13:34:52 +02:00
# door sent "I'm open!"
2022-06-30 09:17:29 +02:00
self.c.post("/k-fet/open/raw_open", {"raw_open": True, "token": "plop"})
# anonymous user agree
2022-06-30 09:17:29 +02:00
msg = self.receive_msg(self.channel)
self.assertEqual(OpenKfet.OPENED, msg["status"])
# root user too
2022-06-30 09:17:29 +02:00
msg = self.receive_msg(self.team_channel)
self.assertEqual(OpenKfet.OPENED, msg["status"])
self.assertEqual(OpenKfet.OPENED, msg["admin_status"])
# admin says "no it's closed"
2022-06-30 09:17:29 +02:00
self.r_c.post("/k-fet/open/force_close", {"force_close": True})
# so anonymous user see it's closed
2022-06-30 09:17:29 +02:00
msg = self.receive_msg(self.channel)
self.assertEqual(OpenKfet.CLOSED, msg["status"])
# root user too
2022-06-30 09:17:29 +02:00
msg = self.receive_msg(self.team_channel)
self.assertEqual(OpenKfet.CLOSED, msg["status"])
# but root knows things
self.assertEqual(OpenKfet.FAKE_CLOSED, msg["admin_status"])
self.assertTrue(msg["force_close"])
2022-06-30 09:17:29 +02:00
def test_scenario_2(self):
"""Starting falsely closed, clients connect, disable force close."""
self.kfet_open.raw_open = True
self.kfet_open.force_close = True
2022-06-30 09:17:29 +02:00
async_to_sync(OpenKfet().send_ws)()
2022-06-30 09:17:29 +02:00
msg = self.receive_msg(self.channel)
self.assertEqual(OpenKfet.CLOSED, msg["status"])
2022-06-30 09:17:29 +02:00
msg = self.receive_msg(self.team_channel)
self.assertEqual(OpenKfet.CLOSED, msg["status"])
self.assertEqual(OpenKfet.FAKE_CLOSED, msg["admin_status"])
self.assertTrue(msg["force_close"])
2022-06-29 13:34:52 +02:00
2022-06-30 09:17:29 +02:00
self.r_c.post("/k-fet/open/force_close", {"force_close": False})
2022-06-30 09:17:29 +02:00
msg = self.receive_msg(self.channel)
self.assertEqual(OpenKfet.OPENED, msg["status"])
2022-06-30 09:17:29 +02:00
msg = self.receive_msg(self.team_channel)
self.assertEqual(OpenKfet.OPENED, msg["status"])
self.assertEqual(OpenKfet.OPENED, msg["admin_status"])
self.assertFalse(msg["force_close"])
2022-06-30 09:17:29 +02:00
async def test_scenario_3(self):
"""Clients connect."""
# anonymous client (for websockets)
self.c_ws = ws_communicator(OpenKfetConsumer, "/ws/k-fet/open")
# test for anonymous user
msg = await self.ws_connect(self.c_ws)
self.assertSetEqual(set(["status"]), set(msg))
# test for root user
with mock.patch(
"kfet.open.consumers.kfet_is_team", return_value=True
), mock.patch("kfet.open.open.kfet_is_team", return_value=True):
self.r_c_ws = ws_communicator(OpenKfetConsumer, "/ws/k-fet/open")
msg = await self.ws_connect(self.r_c_ws)
self.assertSetEqual(
set(["status", "admin_status", "force_close"]), set(msg)
)