authentik.providers.rac.tests.test_consumer_client

Tests for the browser-facing RAC WebSocket consumer.

  1"""Tests for the browser-facing RAC WebSocket consumer."""
  2
  3from unittest.mock import AsyncMock, MagicMock
  4
  5from django.test import SimpleTestCase
  6
  7from authentik.providers.rac.consumer_client import RACClientConsumer
  8from authentik.providers.rac.guacamole import (
  9    GuacamoleInstructionParser,
 10    GuacamoleProtocolError,
 11)
 12from authentik.providers.rac.models import ConnectionToken
 13
 14PING = "0.,4.ping,13.1700000000000;"
 15MOUSE = "5.mouse,1.1,1.2,1.0;"
 16KEY = "3.key,2.65,1.1;"
 17
 18
 19class TestGuacamoleInstructionParser(SimpleTestCase):
 20    """Test Guacamole framing and size handling."""
 21
 22    def test_delimiters_inside_element_contents_are_preserved(self):
 23        instruction = "4.test,5.a,b;c;"
 24
 25        parser = GuacamoleInstructionParser()
 26        responses, forwarded = parser.split_internal(parser.feed(instruction))
 27
 28        self.assertEqual([], responses)
 29        self.assertEqual(instruction, forwarded)
 30
 31    def test_non_bmp_instruction_size_uses_utf16_units(self):
 32        content = "😀" * 4092
 33        instruction = f"4.test,{len(content) * 2}.{content};"
 34        parser = GuacamoleInstructionParser()
 35
 36        with self.assertRaises(GuacamoleProtocolError):
 37            parser.feed(instruction)
 38
 39        responses, forwarded = parser.split_internal(parser.feed("3.nop;"))
 40        self.assertEqual([], responses)
 41        self.assertEqual("3.nop;", forwarded)
 42
 43
 44class TestRACClientConsumer(SimpleTestCase):
 45    """Test Guacamole tunnel instruction handling."""
 46
 47    def setUp(self):
 48        self.consumer = RACClientConsumer()
 49        self.consumer.dest_channel_id = "outpost-channel"
 50        self.consumer.token = MagicMock(spec=ConnectionToken)
 51        self.consumer.token.is_expired = False
 52        self.logger = MagicMock()
 53        self.consumer.logger = self.logger
 54        self.consumer.guacamole_parser = GuacamoleInstructionParser()
 55        self.channel_send = AsyncMock()
 56        self.consumer.channel_layer = MagicMock()
 57        self.consumer.channel_layer.send = self.channel_send
 58        self.browser_send = AsyncMock()
 59        self.consumer.send = self.browser_send
 60        self.disconnect = AsyncMock()
 61        self.consumer.event_disconnect = self.disconnect
 62
 63    async def test_internal_ping_is_echoed(self):
 64        await self.consumer.receive(text_data=PING)
 65
 66        self.browser_send.assert_awaited_once_with(text_data=PING)
 67        self.channel_send.assert_not_awaited()
 68        self.disconnect.assert_not_awaited()
 69
 70    async def test_normal_instruction_is_forwarded_unchanged(self):
 71        await self.consumer.receive(text_data="3.nop;")
 72
 73        self.browser_send.assert_not_awaited()
 74        self.channel_send.assert_awaited_once_with(
 75            "outpost-channel",
 76            {
 77                "type": "event.send",
 78                "text_data": "3.nop;",
 79            },
 80        )
 81
 82    async def test_utf16_element_length_is_forwarded_unchanged(self):
 83        instruction = "4.test,2.😀;"
 84
 85        await self.consumer.receive(text_data=instruction)
 86
 87        self.channel_send.assert_awaited_once_with(
 88            "outpost-channel",
 89            {
 90                "type": "event.send",
 91                "text_data": instruction,
 92            },
 93        )
 94
 95    async def test_multiple_instructions_filter_ping(self):
 96        await self.consumer.receive(text_data=MOUSE + PING + KEY)
 97
 98        self.browser_send.assert_awaited_once_with(text_data=PING)
 99        self.channel_send.assert_awaited_once_with(
100            "outpost-channel",
101            {
102                "type": "event.send",
103                "text_data": MOUSE + KEY,
104            },
105        )
106
107    async def test_binary_frames_are_ignored(self):
108        await self.consumer.receive(bytes_data=(MOUSE + PING + KEY).encode())
109
110        self.browser_send.assert_not_awaited()
111        self.channel_send.assert_not_awaited()
112
113    async def test_unknown_internal_instruction_is_filtered(self):
114        await self.consumer.receive(text_data="0.,7.unknown,5.value;")
115
116        self.browser_send.assert_not_awaited()
117        self.channel_send.assert_not_awaited()
118
119    async def test_malformed_instruction_is_ignored_and_parser_recovers(self):
120        await self.consumer.receive(text_data="x.mouse,1.1;")
121        await self.consumer.receive(text_data=KEY)
122
123        self.browser_send.assert_not_awaited()
124        self.channel_send.assert_awaited_once_with(
125            "outpost-channel",
126            {
127                "type": "event.send",
128                "text_data": KEY,
129            },
130        )
131        self.logger.debug.assert_called_once()
132
133    async def test_fragmented_ping_is_echoed_once_complete(self):
134        await self.consumer.receive(text_data=PING[:10])
135
136        self.browser_send.assert_not_awaited()
137        self.channel_send.assert_not_awaited()
138
139        await self.consumer.receive(text_data=PING[10:])
140
141        self.browser_send.assert_awaited_once_with(text_data=PING)
142        self.channel_send.assert_not_awaited()
143
144    async def test_repeated_pings_do_not_disconnect_or_relay(self):
145        await self.consumer.receive(text_data=PING)
146        await self.consumer.receive(text_data=PING)
147
148        self.assertEqual(self.browser_send.await_count, 2)
149        self.channel_send.assert_not_awaited()
150        self.disconnect.assert_not_awaited()
PING = '0.,4.ping,13.1700000000000;'
MOUSE = '5.mouse,1.1,1.2,1.0;'
KEY = '3.key,2.65,1.1;'
class TestGuacamoleInstructionParser(django.test.testcases.SimpleTestCase):
20class TestGuacamoleInstructionParser(SimpleTestCase):
21    """Test Guacamole framing and size handling."""
22
23    def test_delimiters_inside_element_contents_are_preserved(self):
24        instruction = "4.test,5.a,b;c;"
25
26        parser = GuacamoleInstructionParser()
27        responses, forwarded = parser.split_internal(parser.feed(instruction))
28
29        self.assertEqual([], responses)
30        self.assertEqual(instruction, forwarded)
31
32    def test_non_bmp_instruction_size_uses_utf16_units(self):
33        content = "😀" * 4092
34        instruction = f"4.test,{len(content) * 2}.{content};"
35        parser = GuacamoleInstructionParser()
36
37        with self.assertRaises(GuacamoleProtocolError):
38            parser.feed(instruction)
39
40        responses, forwarded = parser.split_internal(parser.feed("3.nop;"))
41        self.assertEqual([], responses)
42        self.assertEqual("3.nop;", forwarded)

Test Guacamole framing and size handling.

def test_delimiters_inside_element_contents_are_preserved(self):
23    def test_delimiters_inside_element_contents_are_preserved(self):
24        instruction = "4.test,5.a,b;c;"
25
26        parser = GuacamoleInstructionParser()
27        responses, forwarded = parser.split_internal(parser.feed(instruction))
28
29        self.assertEqual([], responses)
30        self.assertEqual(instruction, forwarded)
def test_non_bmp_instruction_size_uses_utf16_units(self):
32    def test_non_bmp_instruction_size_uses_utf16_units(self):
33        content = "😀" * 4092
34        instruction = f"4.test,{len(content) * 2}.{content};"
35        parser = GuacamoleInstructionParser()
36
37        with self.assertRaises(GuacamoleProtocolError):
38            parser.feed(instruction)
39
40        responses, forwarded = parser.split_internal(parser.feed("3.nop;"))
41        self.assertEqual([], responses)
42        self.assertEqual("3.nop;", forwarded)
class TestRACClientConsumer(django.test.testcases.SimpleTestCase):
 45class TestRACClientConsumer(SimpleTestCase):
 46    """Test Guacamole tunnel instruction handling."""
 47
 48    def setUp(self):
 49        self.consumer = RACClientConsumer()
 50        self.consumer.dest_channel_id = "outpost-channel"
 51        self.consumer.token = MagicMock(spec=ConnectionToken)
 52        self.consumer.token.is_expired = False
 53        self.logger = MagicMock()
 54        self.consumer.logger = self.logger
 55        self.consumer.guacamole_parser = GuacamoleInstructionParser()
 56        self.channel_send = AsyncMock()
 57        self.consumer.channel_layer = MagicMock()
 58        self.consumer.channel_layer.send = self.channel_send
 59        self.browser_send = AsyncMock()
 60        self.consumer.send = self.browser_send
 61        self.disconnect = AsyncMock()
 62        self.consumer.event_disconnect = self.disconnect
 63
 64    async def test_internal_ping_is_echoed(self):
 65        await self.consumer.receive(text_data=PING)
 66
 67        self.browser_send.assert_awaited_once_with(text_data=PING)
 68        self.channel_send.assert_not_awaited()
 69        self.disconnect.assert_not_awaited()
 70
 71    async def test_normal_instruction_is_forwarded_unchanged(self):
 72        await self.consumer.receive(text_data="3.nop;")
 73
 74        self.browser_send.assert_not_awaited()
 75        self.channel_send.assert_awaited_once_with(
 76            "outpost-channel",
 77            {
 78                "type": "event.send",
 79                "text_data": "3.nop;",
 80            },
 81        )
 82
 83    async def test_utf16_element_length_is_forwarded_unchanged(self):
 84        instruction = "4.test,2.😀;"
 85
 86        await self.consumer.receive(text_data=instruction)
 87
 88        self.channel_send.assert_awaited_once_with(
 89            "outpost-channel",
 90            {
 91                "type": "event.send",
 92                "text_data": instruction,
 93            },
 94        )
 95
 96    async def test_multiple_instructions_filter_ping(self):
 97        await self.consumer.receive(text_data=MOUSE + PING + KEY)
 98
 99        self.browser_send.assert_awaited_once_with(text_data=PING)
100        self.channel_send.assert_awaited_once_with(
101            "outpost-channel",
102            {
103                "type": "event.send",
104                "text_data": MOUSE + KEY,
105            },
106        )
107
108    async def test_binary_frames_are_ignored(self):
109        await self.consumer.receive(bytes_data=(MOUSE + PING + KEY).encode())
110
111        self.browser_send.assert_not_awaited()
112        self.channel_send.assert_not_awaited()
113
114    async def test_unknown_internal_instruction_is_filtered(self):
115        await self.consumer.receive(text_data="0.,7.unknown,5.value;")
116
117        self.browser_send.assert_not_awaited()
118        self.channel_send.assert_not_awaited()
119
120    async def test_malformed_instruction_is_ignored_and_parser_recovers(self):
121        await self.consumer.receive(text_data="x.mouse,1.1;")
122        await self.consumer.receive(text_data=KEY)
123
124        self.browser_send.assert_not_awaited()
125        self.channel_send.assert_awaited_once_with(
126            "outpost-channel",
127            {
128                "type": "event.send",
129                "text_data": KEY,
130            },
131        )
132        self.logger.debug.assert_called_once()
133
134    async def test_fragmented_ping_is_echoed_once_complete(self):
135        await self.consumer.receive(text_data=PING[:10])
136
137        self.browser_send.assert_not_awaited()
138        self.channel_send.assert_not_awaited()
139
140        await self.consumer.receive(text_data=PING[10:])
141
142        self.browser_send.assert_awaited_once_with(text_data=PING)
143        self.channel_send.assert_not_awaited()
144
145    async def test_repeated_pings_do_not_disconnect_or_relay(self):
146        await self.consumer.receive(text_data=PING)
147        await self.consumer.receive(text_data=PING)
148
149        self.assertEqual(self.browser_send.await_count, 2)
150        self.channel_send.assert_not_awaited()
151        self.disconnect.assert_not_awaited()

Test Guacamole tunnel instruction handling.

def setUp(self):
48    def setUp(self):
49        self.consumer = RACClientConsumer()
50        self.consumer.dest_channel_id = "outpost-channel"
51        self.consumer.token = MagicMock(spec=ConnectionToken)
52        self.consumer.token.is_expired = False
53        self.logger = MagicMock()
54        self.consumer.logger = self.logger
55        self.consumer.guacamole_parser = GuacamoleInstructionParser()
56        self.channel_send = AsyncMock()
57        self.consumer.channel_layer = MagicMock()
58        self.consumer.channel_layer.send = self.channel_send
59        self.browser_send = AsyncMock()
60        self.consumer.send = self.browser_send
61        self.disconnect = AsyncMock()
62        self.consumer.event_disconnect = self.disconnect

Hook method for setting up the test fixture before exercising it.

async def test_internal_ping_is_echoed(self):
64    async def test_internal_ping_is_echoed(self):
65        await self.consumer.receive(text_data=PING)
66
67        self.browser_send.assert_awaited_once_with(text_data=PING)
68        self.channel_send.assert_not_awaited()
69        self.disconnect.assert_not_awaited()
async def test_normal_instruction_is_forwarded_unchanged(self):
71    async def test_normal_instruction_is_forwarded_unchanged(self):
72        await self.consumer.receive(text_data="3.nop;")
73
74        self.browser_send.assert_not_awaited()
75        self.channel_send.assert_awaited_once_with(
76            "outpost-channel",
77            {
78                "type": "event.send",
79                "text_data": "3.nop;",
80            },
81        )
async def test_utf16_element_length_is_forwarded_unchanged(self):
83    async def test_utf16_element_length_is_forwarded_unchanged(self):
84        instruction = "4.test,2.😀;"
85
86        await self.consumer.receive(text_data=instruction)
87
88        self.channel_send.assert_awaited_once_with(
89            "outpost-channel",
90            {
91                "type": "event.send",
92                "text_data": instruction,
93            },
94        )
async def test_multiple_instructions_filter_ping(self):
 96    async def test_multiple_instructions_filter_ping(self):
 97        await self.consumer.receive(text_data=MOUSE + PING + KEY)
 98
 99        self.browser_send.assert_awaited_once_with(text_data=PING)
100        self.channel_send.assert_awaited_once_with(
101            "outpost-channel",
102            {
103                "type": "event.send",
104                "text_data": MOUSE + KEY,
105            },
106        )
async def test_binary_frames_are_ignored(self):
108    async def test_binary_frames_are_ignored(self):
109        await self.consumer.receive(bytes_data=(MOUSE + PING + KEY).encode())
110
111        self.browser_send.assert_not_awaited()
112        self.channel_send.assert_not_awaited()
async def test_unknown_internal_instruction_is_filtered(self):
114    async def test_unknown_internal_instruction_is_filtered(self):
115        await self.consumer.receive(text_data="0.,7.unknown,5.value;")
116
117        self.browser_send.assert_not_awaited()
118        self.channel_send.assert_not_awaited()
async def test_malformed_instruction_is_ignored_and_parser_recovers(self):
120    async def test_malformed_instruction_is_ignored_and_parser_recovers(self):
121        await self.consumer.receive(text_data="x.mouse,1.1;")
122        await self.consumer.receive(text_data=KEY)
123
124        self.browser_send.assert_not_awaited()
125        self.channel_send.assert_awaited_once_with(
126            "outpost-channel",
127            {
128                "type": "event.send",
129                "text_data": KEY,
130            },
131        )
132        self.logger.debug.assert_called_once()
async def test_fragmented_ping_is_echoed_once_complete(self):
134    async def test_fragmented_ping_is_echoed_once_complete(self):
135        await self.consumer.receive(text_data=PING[:10])
136
137        self.browser_send.assert_not_awaited()
138        self.channel_send.assert_not_awaited()
139
140        await self.consumer.receive(text_data=PING[10:])
141
142        self.browser_send.assert_awaited_once_with(text_data=PING)
143        self.channel_send.assert_not_awaited()
async def test_repeated_pings_do_not_disconnect_or_relay(self):
145    async def test_repeated_pings_do_not_disconnect_or_relay(self):
146        await self.consumer.receive(text_data=PING)
147        await self.consumer.receive(text_data=PING)
148
149        self.assertEqual(self.browser_send.await_count, 2)
150        self.channel_send.assert_not_awaited()
151        self.disconnect.assert_not_awaited()