2019-01-06 13:48:32 +01:00
|
|
|
import random
|
2017-06-22 16:36:08 +02:00
|
|
|
from datetime import timedelta
|
2017-08-08 19:03:42 +02:00
|
|
|
from unittest import mock
|
2017-06-21 07:08:28 +02:00
|
|
|
|
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
|
2017-06-21 07:08:28 +02:00
|
|
|
from django.contrib.auth.models import AnonymousUser, Permission, User
|
2022-06-29 13:34:52 +02:00
|
|
|
from django.test import Client, TestCase
|
2017-06-22 16:36:08 +02:00
|
|
|
from django.utils import timezone
|
2017-06-21 07:08:28 +02:00
|
|
|
|
2019-01-05 19:19:58 +01:00
|
|
|
from . import OpenKfet
|
2017-06-21 07:08:28 +02:00
|
|
|
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):
|
2017-06-21 07:08:28 +02:00
|
|
|
"""OpenKfet object unit-tests suite."""
|
|
|
|
|
|
|
|
def setUp(self):
|
2019-01-05 19:19:58 +01:00
|
|
|
self.kfet_open = OpenKfet(
|
2022-10-03 10:33:29 +02:00
|
|
|
cache_prefix="test_kfetopen_%s" % random.randrange(2**20)
|
2019-01-05 19:19:58 +01:00
|
|
|
)
|
|
|
|
self.addCleanup(self.kfet_open.clear_cache)
|
2017-06-21 07:08:28 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2017-06-22 16:36:08 +02:00
|
|
|
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())
|
|
|
|
|
2017-06-21 07:08:28 +02:00
|
|
|
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))
|
2017-06-21 07:08:28 +02:00
|
|
|
|
|
|
|
def test_export_team(self):
|
|
|
|
"""Export all values for a team member."""
|
2018-10-06 12:35:49 +02:00
|
|
|
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"
|
|
|
|
)
|
2019-12-21 16:26:59 +01:00
|
|
|
user.user_permissions.add(is_team)
|
2017-06-21 07:08:28 +02:00
|
|
|
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()
|
2017-06-21 07:08:28 +02:00
|
|
|
|
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)
|
2017-06-21 07:08:28 +02:00
|
|
|
|
2022-06-29 13:34:52 +02:00
|
|
|
await self.kfet_open.send_ws()
|
2017-06-21 07:08:28 +02:00
|
|
|
|
2022-06-29 13:34:52 +02:00
|
|
|
base = await channel_layer.receive(base_channel)
|
2017-06-21 07:08:28 +02:00
|
|
|
|
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)
|
|
|
|
)
|
2017-06-21 07:08:28 +02:00
|
|
|
|
|
|
|
|
2022-06-29 13:34:52 +02:00
|
|
|
class OpenKfetViewsTest(TestCase):
|
2017-06-21 07:08:28 +02:00
|
|
|
"""OpenKfet views unit-tests suite."""
|
|
|
|
|
|
|
|
def setUp(self):
|
2017-08-08 19:03:42 +02:00
|
|
|
# Need this (and here) because of '<client>.login' in setUp
|
2018-10-06 12:35:49 +02:00
|
|
|
patcher_messages = mock.patch("gestioncof.signals.messages")
|
2017-08-08 19:03:42 +02:00
|
|
|
patcher_messages.start()
|
|
|
|
self.addCleanup(patcher_messages.stop)
|
|
|
|
|
2017-06-21 07:08:28 +02:00
|
|
|
# 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-21 16:26:59 +01:00
|
|
|
),
|
2019-12-24 17:13:27 +01:00
|
|
|
"kfet.can_force_close": Permission.objects.get(
|
|
|
|
codename="can_force_close", content_type__app_label="kfet"
|
2019-12-21 16:26:59 +01:00
|
|
|
),
|
2017-06-21 07:08:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# authenticated user and its client
|
2018-10-06 12:35:49 +02:00
|
|
|
self.u = User.objects.create_user("user", "", "user")
|
2017-06-21 07:08:28 +02:00
|
|
|
self.c = Client()
|
2018-10-06 12:35:49 +02:00
|
|
|
self.c.login(username="user", password="user")
|
2017-06-21 07:08:28 +02:00
|
|
|
|
|
|
|
# team user and its clients
|
2018-10-06 12:35:49 +02:00
|
|
|
self.t = User.objects.create_user("team", "", "team")
|
|
|
|
self.t.user_permissions.add(perms["kfet.is_team"])
|
2017-06-21 07:08:28 +02:00
|
|
|
self.c_t = Client()
|
2018-10-06 12:35:49 +02:00
|
|
|
self.c_t.login(username="team", password="team")
|
2017-06-21 07:08:28 +02:00
|
|
|
|
|
|
|
# admin user and its client
|
2018-10-06 12:35:49 +02:00
|
|
|
self.a = User.objects.create_user("admin", "", "admin")
|
2017-06-21 07:08:28 +02:00
|
|
|
self.a.user_permissions.add(
|
2018-10-06 12:35:49 +02:00
|
|
|
perms["kfet.is_team"], perms["kfet.can_force_close"]
|
2017-06-21 07:08:28 +02:00
|
|
|
)
|
|
|
|
self.c_a = Client()
|
2018-10-06 12:35:49 +02:00
|
|
|
self.c_a.login(username="admin", password="admin")
|
2017-06-21 07:08:28 +02:00
|
|
|
|
2019-01-05 19:19:58 +01:00
|
|
|
self.kfet_open = OpenKfet(
|
2022-10-03 10:33:29 +02:00
|
|
|
cache_prefix="test_kfetopen_%s" % random.randrange(2**20)
|
2019-01-05 19:19:58 +01:00
|
|
|
)
|
|
|
|
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)
|
2017-06-21 07:08:28 +02:00
|
|
|
|
|
|
|
def test_door(self):
|
|
|
|
"""Edit raw_status."""
|
|
|
|
for sent, expected in [(1, True), (0, False)]:
|
2018-10-06 12:35:49 +02:00
|
|
|
resp = Client().post(
|
|
|
|
"/k-fet/open/raw_open", {"raw_open": sent, "token": "plop"}
|
|
|
|
)
|
2017-06-21 07:08:28 +02:00
|
|
|
self.assertEqual(200, resp.status_code)
|
2019-01-05 19:19:58 +01:00
|
|
|
self.assertEqual(expected, self.kfet_open.raw_open)
|
2017-06-21 07:08:28 +02:00
|
|
|
|
|
|
|
def test_force_close(self):
|
|
|
|
"""Edit force_close."""
|
|
|
|
for sent, expected in [(1, True), (0, False)]:
|
2018-10-06 12:35:49 +02:00
|
|
|
resp = self.c_a.post("/k-fet/open/force_close", {"force_close": sent})
|
2017-06-21 07:08:28 +02:00
|
|
|
self.assertEqual(200, resp.status_code)
|
2019-01-05 19:19:58 +01:00
|
|
|
self.assertEqual(expected, self.kfet_open.force_close)
|
2017-06-21 07:08:28 +02:00
|
|
|
|
|
|
|
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:
|
2018-10-06 12:35:49 +02:00
|
|
|
resp = client.post("/k-fet/open/force_close", {"force_close": 0})
|
2017-06-21 07:08:28 +02:00
|
|
|
self.assertEqual(403, resp.status_code)
|
|
|
|
|
|
|
|
|
2022-06-29 13:34:52 +02:00
|
|
|
class OpenKfetConsumerTest(TestCase):
|
2017-06-21 07:08:28 +02:00
|
|
|
"""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):
|
2017-06-21 07:08:28 +02:00
|
|
|
"""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")
|
2017-06-21 07:08:28 +02:00
|
|
|
|
2022-06-29 13:34:52 +02:00
|
|
|
connected, _ = await c.connect()
|
|
|
|
|
|
|
|
self.assertTrue(connected)
|
2017-06-21 07:08:28 +02:00
|
|
|
|
|
|
|
# initialization data is replied on connection
|
2022-06-29 13:34:52 +02:00
|
|
|
message = await c.receive_json_from()
|
|
|
|
self.assertIsNotNone(message)
|
2017-06-21 07:08:28 +02:00
|
|
|
|
|
|
|
# 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"})
|
2017-06-21 07:08:28 +02:00
|
|
|
|
|
|
|
# ...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())
|
2017-06-21 07:08:28 +02:00
|
|
|
|
2022-06-29 13:34:52 +02:00
|
|
|
async def test_team_user(self):
|
2017-06-21 07:08:28 +02:00
|
|
|
"""Team user is added to kfet.open.team group."""
|
|
|
|
|
2022-10-03 13:24:00 +02:00
|
|
|
# On simule l'appartenance de l'user à la team kfet car l'utilisation de
|
|
|
|
# tests async avec postgres fait tout planter si on modifie la db dans un
|
|
|
|
# des sous tests.
|
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")
|
2017-06-21 07:08:28 +02:00
|
|
|
|
2022-06-29 13:34:52 +02:00
|
|
|
connected, _ = await c.connect()
|
2017-06-21 07:08:28 +02:00
|
|
|
|
2022-06-29 13:34:52 +02:00
|
|
|
channel_layer = get_channel_layer()
|
2017-06-21 07:08:28 +02:00
|
|
|
|
2022-06-29 13:34:52 +02:00
|
|
|
self.assertTrue(connected)
|
2017-06-21 07:08:28 +02:00
|
|
|
|
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())
|
2017-06-21 07:08:28 +02:00
|
|
|
|
2022-06-29 13:34:52 +02:00
|
|
|
|
|
|
|
class OpenKfetScenarioTest(TestCase):
|
2017-06-21 07:08:28 +02:00
|
|
|
"""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")
|
2017-08-08 19:03:42 +02:00
|
|
|
|
2017-06-21 07:08:28 +02:00
|
|
|
# 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):
|
2022-06-30 09:39:05 +02:00
|
|
|
# Create channels to listen to messages
|
2022-06-30 09:17:29 +02:00
|
|
|
channel_layer = get_channel_layer()
|
2022-06-30 09:39:05 +02:00
|
|
|
|
2022-06-30 09:17:29 +02:00
|
|
|
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)
|
2017-06-21 07:08:28 +02:00
|
|
|
|
2019-01-05 19:19:58 +01:00
|
|
|
self.kfet_open = OpenKfet(
|
2022-10-03 10:33:29 +02:00
|
|
|
cache_prefix="test_kfetopen_%s" % random.randrange(2**20)
|
2019-01-05 19:19:58 +01:00
|
|
|
)
|
|
|
|
self.addCleanup(self.kfet_open.clear_cache)
|
2017-06-21 07:08:28 +02:00
|
|
|
|
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()
|
2017-06-21 07:08:28 +02:00
|
|
|
|
2022-06-30 09:17:29 +02:00
|
|
|
def test_scenario_1(self):
|
2017-06-21 07:08:28 +02:00
|
|
|
"""Clients connect, door opens, enable force close."""
|
2022-06-29 13:34:52 +02:00
|
|
|
|
2017-06-21 07:08:28 +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"})
|
2017-06-21 07:08:28 +02:00
|
|
|
|
|
|
|
# anonymous user agree
|
2022-06-30 09:17:29 +02:00
|
|
|
msg = self.receive_msg(self.channel)
|
2018-10-06 12:35:49 +02:00
|
|
|
self.assertEqual(OpenKfet.OPENED, msg["status"])
|
2017-06-21 07:08:28 +02:00
|
|
|
|
|
|
|
# root user too
|
2022-06-30 09:17:29 +02:00
|
|
|
msg = self.receive_msg(self.team_channel)
|
2018-10-06 12:35:49 +02:00
|
|
|
self.assertEqual(OpenKfet.OPENED, msg["status"])
|
|
|
|
self.assertEqual(OpenKfet.OPENED, msg["admin_status"])
|
2017-06-21 07:08:28 +02:00
|
|
|
|
|
|
|
# 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})
|
2017-06-21 07:08:28 +02:00
|
|
|
|
|
|
|
# so anonymous user see it's closed
|
2022-06-30 09:17:29 +02:00
|
|
|
msg = self.receive_msg(self.channel)
|
2018-10-06 12:35:49 +02:00
|
|
|
self.assertEqual(OpenKfet.CLOSED, msg["status"])
|
2017-06-21 07:08:28 +02:00
|
|
|
|
|
|
|
# root user too
|
2022-06-30 09:17:29 +02:00
|
|
|
msg = self.receive_msg(self.team_channel)
|
2018-10-06 12:35:49 +02:00
|
|
|
self.assertEqual(OpenKfet.CLOSED, msg["status"])
|
2017-06-21 07:08:28 +02:00
|
|
|
# but root knows things
|
2018-10-06 12:35:49 +02:00
|
|
|
self.assertEqual(OpenKfet.FAKE_CLOSED, msg["admin_status"])
|
|
|
|
self.assertTrue(msg["force_close"])
|
2017-06-21 07:08:28 +02:00
|
|
|
|
2022-06-30 09:17:29 +02:00
|
|
|
def test_scenario_2(self):
|
2017-06-21 07:08:28 +02:00
|
|
|
"""Starting falsely closed, clients connect, disable force close."""
|
2019-01-05 19:19:58 +01:00
|
|
|
self.kfet_open.raw_open = True
|
|
|
|
self.kfet_open.force_close = True
|
2017-06-21 07:08:28 +02:00
|
|
|
|
2022-06-30 09:17:29 +02:00
|
|
|
async_to_sync(OpenKfet().send_ws)()
|
2017-06-21 07:08:28 +02:00
|
|
|
|
2022-06-30 09:17:29 +02:00
|
|
|
msg = self.receive_msg(self.channel)
|
2018-10-06 12:35:49 +02:00
|
|
|
self.assertEqual(OpenKfet.CLOSED, msg["status"])
|
2017-06-21 07:08:28 +02:00
|
|
|
|
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})
|
2017-06-21 07:08:28 +02:00
|
|
|
|
2022-06-30 09:17:29 +02:00
|
|
|
msg = self.receive_msg(self.channel)
|
2018-10-06 12:35:49 +02:00
|
|
|
self.assertEqual(OpenKfet.OPENED, msg["status"])
|
2017-06-21 07:08:28 +02:00
|
|
|
|
2022-06-30 09:17:29 +02:00
|
|
|
msg = self.receive_msg(self.team_channel)
|
2018-10-06 12:35:49 +02:00
|
|
|
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)
|
|
|
|
)
|