-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_consumers.py
More file actions
932 lines (746 loc) · 35.3 KB
/
test_consumers.py
File metadata and controls
932 lines (746 loc) · 35.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
"""
Unit tests for the Drycc api consumers.
Run the tests with "./manage.py test api.tests.test_consumers"
"""
import json
import asyncio
import aiohttp
from unittest import mock
from unittest.mock import MagicMock, patch
from django.contrib.auth import get_user_model
from django.core.cache import cache
from channels.exceptions import DenyConnection
from api.models.app import App
from api.models.volume import Volume
from api.consumers import (
AppPermChecker,
BaseAppConsumer,
BaseK8sConsumer,
AppPodLogsConsumer,
AppPodExecConsumer,
FilerProxyConsumer
)
from api.tests import adapter, DryccTransactionTestCase
import requests_mock
User = get_user_model()
@requests_mock.Mocker(real_http=True, adapter=adapter)
class ConsumerTestCase(DryccTransactionTestCase):
"""Base test case for consumer tests"""
fixtures = ['tests.json']
def setUp(self):
self.user = User.objects.get(username='autotest')
self.token = self.get_or_create_token(self.user)
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
self.app_id = self.create_app()
self.app = App.objects.get(id=self.app_id)
def tearDown(self):
# make sure every test has a clean slate for k8s mocking
cache.clear()
class AppPermCheckerTest(ConsumerTestCase):
"""Tests for AppPermChecker"""
def test_has_perm_with_logged_in_user(self):
"""Test permission check with valid user and app"""
checker = AppPermChecker({
'user': self.user,
'url_route': {'kwargs': {'id': self.app_id}}
})
# Mock the async method
async def test_async():
result = await checker.has_perm()
self.assertTrue(result[0]) # Should have permission
# When permission is granted, message is None
asyncio.run(test_async())
def test_has_perm_with_anonymous_user(self):
"""Test permission check with anonymous user"""
checker = AppPermChecker({
'user': None,
'url_route': {'kwargs': {'id': self.app_id}}
})
async def test_async():
result = await checker.has_perm()
self.assertFalse(result[0]) # Should not have permission
self.assertEqual(result[1], "user not login")
asyncio.run(test_async())
def test_has_perm_with_nonexistent_app(self):
"""Test permission check with non-existent app"""
checker = AppPermChecker({
'user': self.user,
'url_route': {'kwargs': {'id': 'nonexistent-app'}}
})
async def test_async():
result = await checker.has_perm()
self.assertFalse(result[0]) # Should not have permission
self.assertEqual(result[1], "app not exists")
asyncio.run(test_async())
def test_has_perm_with_cache(self):
"""Test permission check with cached permissions"""
checker = AppPermChecker({
'user': self.user,
'url_route': {'kwargs': {'id': self.app_id}}
})
# Set cache
cache_key = f"permission:user:{self.user.id}:app:{self.app_id}"
cache.set(cache_key, (True, "cached permission"))
async def test_async():
result = await checker.has_perm()
self.assertTrue(result[0])
self.assertEqual(result[1], "cached permission")
asyncio.run(test_async())
@requests_mock.Mocker(real_http=True, adapter=adapter)
class BaseAppConsumerTest(ConsumerTestCase):
"""Tests for BaseAppConsumer"""
def test_connect_with_permission(self, mock_requests):
"""Test WebSocket connection with valid permissions"""
async def test_async():
# Mock the has_perm method to return True
async def mock_has_perm():
return (True, "permission granted")
with patch.object(AppPermChecker, 'has_perm', side_effect=mock_has_perm):
with patch.object(BaseAppConsumer, 'accept') as mock_accept:
consumer = BaseAppConsumer()
consumer.scope = {
'user': self.user,
'url_route': {'kwargs': {'id': self.app_id}}
}
await consumer.connect()
mock_accept.assert_called_once()
asyncio.run(test_async())
def test_connect_without_permission(self, mock_requests):
"""Test WebSocket connection without valid permissions"""
async def test_async():
# Mock the has_perm method to return False
async def mock_has_perm():
return (False, "permission denied")
with patch.object(AppPermChecker, 'has_perm', side_effect=mock_has_perm):
consumer = BaseAppConsumer()
consumer.scope = {
'user': self.user,
'url_route': {'kwargs': {'id': self.app_id}}
}
with self.assertRaises(DenyConnection):
await consumer.connect()
asyncio.run(test_async())
@requests_mock.Mocker(real_http=True, adapter=adapter)
class BaseK8sConsumerTest(ConsumerTestCase):
"""Tests for BaseK8sConsumer"""
def test_kubernetes_property(self, mock_requests):
"""Test kubernetes property initialization"""
consumer = BaseK8sConsumer()
# Mock the token file reading and Kubernetes API client
with patch('builtins.open', mock.mock_open(read_data='mock-token')):
with patch('api.consumers.Configuration') as mock_config:
with patch('api.consumers.core_v1_api.CoreV1Api') as mock_api:
mock_config_instance = MagicMock()
mock_config.return_value = mock_config_instance
consumer.kubernetes
# Verify configuration was called
mock_config.assert_called_once()
mock_api.assert_called_once()
@requests_mock.Mocker(real_http=True, adapter=adapter)
class AppPodLogsConsumerTest(ConsumerTestCase):
"""Tests for AppPodLogsConsumer"""
def test_connect(self, mock_requests):
"""Test WebSocket connection for pod logs"""
async def test_async():
with patch.object(BaseAppConsumer, 'connect') as mock_super_connect:
consumer = AppPodLogsConsumer()
consumer.scope = {
'user': self.user,
'url_route': {'kwargs': {'name': 'test-pod', 'id': self.app_id}}
}
await consumer.connect()
mock_super_connect.assert_called_once()
self.assertFalse(consumer.running)
self.assertIsNone(consumer.response)
self.assertTrue(consumer.conneted)
self.assertEqual(consumer.buffer, b'')
self.assertEqual(consumer.pod_name, 'test-pod')
asyncio.run(test_async())
def test_receive_log_request(self, mock_requests):
"""Test receiving log request message"""
async def test_async():
consumer = AppPodLogsConsumer()
consumer.scope = {
'user': self.user,
'url_route': {'kwargs': {'name': 'test-pod', 'id': self.app_id}}
}
consumer.running = False
consumer.id = self.app_id
consumer.pod_name = 'test-pod'
# Mock kubernetes client
mock_k8s = MagicMock()
mock_response = MagicMock()
mock_k8s.read_namespaced_pod_log.return_value = mock_response
with patch.object(type(consumer), 'kubernetes', new_callable=lambda: mock_k8s):
# Create a proper async function instead of using AsyncMock
async def mock_sync_to_async_func(*args, **kwargs):
return mock_response
with patch('api.consumers.sync_to_async') as mock_sync_to_async:
mock_sync_to_async.return_value = mock_sync_to_async_func
with patch.object(consumer, 'send'):
with patch.object(consumer, 'close'):
test_data = {
"lines": 100,
"follow": False,
"previous": False,
"container": "web"
}
await consumer.receive(text_data=json.dumps(test_data))
self.assertTrue(consumer.running)
asyncio.run(test_async())
def test_disconnect(self, mock_requests):
"""Test WebSocket disconnection"""
async def test_async():
consumer = AppPodLogsConsumer()
consumer.conneted = True
consumer.response = MagicMock()
consumer.response.connection.sock = MagicMock()
with patch('asyncio.get_event_loop') as mock_loop:
mock_event_loop = MagicMock()
mock_loop.return_value = mock_event_loop
with patch('api.consumers.sync_to_async') as mock_sync_to_async:
# Create a proper async function instead of AsyncMock
async def mock_sync_close():
pass
mock_sync_to_async.return_value = mock_sync_close
await consumer.disconnect(1000)
self.assertFalse(consumer.conneted)
asyncio.run(test_async())
@requests_mock.Mocker(real_http=True, adapter=adapter)
class AppPodExecConsumerTest(ConsumerTestCase):
"""Tests for AppPodExecConsumer"""
def test_connect(self, mock_requests):
"""Test WebSocket connection for pod exec"""
async def test_async():
with patch.object(BaseAppConsumer, 'connect') as mock_super_connect:
consumer = AppPodExecConsumer()
consumer.scope = {
'user': self.user,
'url_route': {'kwargs': {'name': 'test-pod', 'id': self.app_id}}
}
await consumer.connect()
mock_super_connect.assert_called_once()
self.assertIsNone(consumer.stream)
self.assertTrue(consumer.conneted)
self.assertEqual(consumer.pod_name, 'test-pod')
asyncio.run(test_async())
def test_send_stdout(self, mock_requests):
"""Test sending stdout data"""
async def test_async():
consumer = AppPodExecConsumer()
with patch.object(consumer.__class__.__bases__[0], 'send') as mock_super_send:
# Test sending bytes data
await consumer.send(b'test output', channel=1) # STDOUT_CHANNEL
mock_super_send.assert_called_with(bytes_data=b'\x01test output')
# Test sending string data
await consumer.send('test string', channel=1)
mock_super_send.assert_called_with(text_data='\x01test string')
asyncio.run(test_async())
def test_send_none_data(self, mock_requests):
"""Test sending None data"""
async def test_async():
consumer = AppPodExecConsumer()
with patch.object(consumer.__class__.__bases__[0], 'send') as mock_super_send:
await consumer.send(None)
mock_super_send.assert_not_called()
asyncio.run(test_async())
def test_receive_exec_request(self, mock_requests):
"""Test receiving exec request"""
async def test_async():
consumer = AppPodExecConsumer()
consumer.scope = {
'user': self.user,
'url_route': {'kwargs': {'name': 'test-pod', 'id': self.app_id}}
}
consumer.stream = None
consumer.pod_name = 'test-pod'
consumer.id = self.app_id
exec_request = {
"command": ["/bin/bash"],
"stdin": True,
"tty": True
}
# Mock the kubernetes property
mock_k8s = MagicMock()
mock_k8s.connect_get_namespaced_pod_exec = MagicMock()
with patch.object(type(consumer), 'kubernetes', new_callable=lambda: mock_k8s):
with patch('api.consumers.sync_to_async') as mock_sync_to_async:
with patch('api.consumers.stream'):
mock_stream_obj = MagicMock()
# Create a proper async function instead of AsyncMock
async def mock_sync_to_async_func(*args, **kwargs):
return mock_stream_obj
mock_sync_to_async.return_value = mock_sync_to_async_func
# Mock the task method to avoid creating unawaited coroutine
async def mock_task():
pass
with patch.object(
consumer, 'task', side_effect=mock_task
) as mock_task_method:
await consumer.receive(text_data=json.dumps(exec_request))
self.assertEqual(consumer.stream, mock_stream_obj)
mock_task_method.assert_called_once()
asyncio.run(test_async())
def test_receive_command_data(self, mock_requests):
"""Test receiving command input data"""
async def test_async():
consumer = AppPodExecConsumer()
mock_stream = MagicMock()
consumer.stream = mock_stream
with patch('api.consumers.sync_to_async') as mock_sync_to_async:
# Create a proper async function instead of AsyncMock
async def mock_write_stdin(channel, data):
pass
mock_sync_to_async.return_value = mock_write_stdin
# Test text data
await consumer.receive(text_data='\x00test input')
mock_sync_to_async.assert_called()
asyncio.run(test_async())
def test_receive_invalid_operation(self, mock_requests):
"""Test receiving data when stream is None and no valid text data"""
async def test_async():
consumer = AppPodExecConsumer()
consumer.stream = None
with self.assertRaises(ValueError) as context:
await consumer.receive(bytes_data=b'invalid')
self.assertEqual(str(context.exception), "This operation is not supported!")
asyncio.run(test_async())
def test_disconnect(self, mock_requests):
"""Test WebSocket disconnection"""
async def test_async():
consumer = AppPodExecConsumer()
consumer.stream = MagicMock()
consumer.conneted = True
with patch('api.consumers.sync_to_async') as mock_sync_to_async:
# Create a proper async function instead of AsyncMock
async def mock_close_stream():
pass
mock_sync_to_async.return_value = mock_close_stream
await consumer.disconnect(1000)
self.assertFalse(consumer.conneted)
asyncio.run(test_async())
@requests_mock.Mocker(real_http=True, adapter=adapter)
class FilerProxyConsumerTest(ConsumerTestCase):
"""Tests for FilerProxyConsumer"""
def setUp(self):
super().setUp()
# Create a test volume using Volume.objects.create to avoid k8s calls
# We'll patch the save method to prevent k8s interactions
with patch.object(Volume, 'save_to_k8s'), patch.object(Volume, 'delete_from_k8s'):
self.test_volume = Volume.objects.create(
owner=self.user,
app=self.app,
name='test-volume',
size='1G',
path={'web': '/data'},
type='csi',
parameters={'csi': {'driver': 'test-driver'}}
)
def test_handle_without_permission(self, mock_requests):
"""Test handling request without permission"""
async def test_async():
consumer = FilerProxyConsumer()
consumer.scope = {
'user': self.user,
'path': f'/v2/apps/{self.app_id}/volumes/test-volume/filer/_/ping',
'url_route': {
'kwargs': {'path': '_/ping', 'name': 'test-volume', 'id': self.app_id}
},
'method': 'GET',
'headers': []
}
# Set the id attribute that FilerProxyConsumer expects
consumer.id = self.app_id
async def mock_has_perm():
return (False, "permission denied")
async def mock_bind_func():
return {'endpoint': '', 'username': 'test', 'password': 'test'}
async def mock_send_response(*args, **kwargs):
pass
with patch.object(AppPermChecker, 'has_perm', side_effect=mock_has_perm):
with patch.object(
consumer, 'send_response', side_effect=mock_send_response
) as mock_send_response_obj:
with patch('api.filer.FilerClient') as mock_filer_client:
mock_client = MagicMock()
mock_client.bind = mock_bind_func
mock_filer_client.return_value = mock_client
await consumer.handle(b'')
mock_send_response_obj.assert_awaited_once_with(403, b'permission denied')
asyncio.run(test_async())
def test_handle_ping_endpoint(self, mock_requests):
"""Test handling ping endpoint"""
async def test_async():
consumer = FilerProxyConsumer()
consumer.scope = {
'user': self.user,
'path': f'/v2/apps/{self.app_id}/volumes/test-volume/filer/_/ping',
'url_route': {
'kwargs': {'path': '_/ping', 'name': 'test-volume', 'id': self.app_id}
},
'method': 'GET',
'headers': []
}
# Set the id attribute that FilerProxyConsumer expects
consumer.id = self.app_id
async def mock_has_perm():
return (True, "permission granted")
async def mock_send_response(*args, **kwargs):
pass
with patch.object(AppPermChecker, 'has_perm', side_effect=mock_has_perm):
with patch.object(
consumer, 'send_response', side_effect=mock_send_response
) as mock_send_response_obj:
# Create a proper async function instead of AsyncMock
async def mock_bind_func():
return {'username': 'test', 'password': 'test'}
with patch('api.filer.FilerClient') as mock_filer_client:
mock_client = MagicMock()
mock_client.info = mock_client.bind = mock_bind_func
mock_filer_client.return_value = mock_client
await consumer.handle(b'')
mock_send_response_obj.assert_called_once_with(200, b"pong")
asyncio.run(test_async())
def test_handle_bind_endpoint(self, mock_requests):
"""Test handling bind endpoint"""
async def test_async():
consumer = FilerProxyConsumer()
consumer.scope = {
'user': self.user,
'path': f'/v2/apps/{self.app_id}/volumes/test-volume/filer/',
'url_route': {
'kwargs': {'path': '_/bind', 'name': 'test-volume', 'id': self.app_id}
},
'method': 'GET',
'headers': []
}
# Set the id attribute that FilerProxyConsumer expects
consumer.id = self.app_id
filer_data = {
'username': 'test-username',
'password': 'test-password'
}
async def mock_has_perm():
return (True, "permission granted")
async def mock_send_response(*args, **kwargs):
pass
with patch.object(AppPermChecker, 'has_perm', side_effect=mock_has_perm):
with patch.object(
consumer, 'send_response', side_effect=mock_send_response
) as mock_send_response_obj:
# Create a proper async function instead of AsyncMock
async def mock_bind_func():
return filer_data
with patch('api.filer.FilerClient') as mock_filer_client:
mock_client = MagicMock()
mock_client.bind = mock_bind_func
mock_filer_client.return_value = mock_client
await consumer.handle(b'')
expected_response = json.dumps({
"username": "test-username",
"password": "test-password"
}).encode('utf-8')
mock_send_response_obj.assert_called_once_with(200, expected_response)
asyncio.run(test_async())
def test_handle_proxy_request(self, mock_requests):
"""Test handling proxy request to filer"""
path = 'webdav/files/test.txt'
async def test_async():
consumer = FilerProxyConsumer()
consumer.scope = {
'user': self.user,
'path': f'/v2/apps/{self.app_id}/volumes/test-volume/filer/{path}',
'url_route': {
'kwargs': {'path': path, 'name': 'test-volume', 'id': self.app_id}
},
'method': 'GET',
'headers': [(b'host', b'example.com'), (b'user-agent', b'test-agent')],
'query_string': 'param=value'
}
# Set the id attribute that FilerProxyConsumer expects
consumer.id = self.app_id
filer_data = {
'endpoint': 'http://filer.example.com',
'username': 'test-username',
'password': 'test-password'
}
async def mock_has_perm():
return (True, "permission granted")
with patch.object(AppPermChecker, 'has_perm', side_effect=mock_has_perm):
with patch.object(consumer, '_handle_proxy_request') as mock_handle_proxy:
# Create a proper async function instead of AsyncMock
async def mock_bind_func():
return filer_data
with patch('api.filer.FilerClient') as mock_filer_client:
mock_client = MagicMock()
mock_client.info = mock_client.bind = mock_bind_func
mock_filer_client.return_value = mock_client
await consumer.handle(b'test body')
expected_url = "http://filer.example.com{}?param=value".format(
consumer.scope['path']
)
expected_headers = {'user-agent': 'test-agent'}
mock_handle_proxy.assert_called_once_with(
expected_url,
'GET',
expected_headers,
b'test body'
)
asyncio.run(test_async())
def test_forward_response(self, mock_requests):
"""Test forwarding response from filer"""
async def test_async():
consumer = FilerProxyConsumer()
consumer.config = MagicMock()
consumer.config.chunk_size = 1024
# Mock aiohttp response
mock_response = MagicMock()
mock_response.status = 200
mock_response.headers = {'content-type': 'application/json', 'content-length': '100'}
# Create an async iterator for iter_chunked
class AsyncIterator:
def __init__(self, items):
self.items = iter(items)
def __aiter__(self):
return self
async def __anext__(self):
try:
return next(self.items)
except StopIteration:
raise StopAsyncIteration
# Mock the iter_chunked method to return our async iterator
mock_response.content.iter_chunked = MagicMock(
return_value=AsyncIterator([b'chunk1', b'chunk2', b''])
)
# Mock async methods properly
async def mock_send_response(*args, **kwargs):
pass
async def mock_send_body(*args, **kwargs):
pass
async def mock_send_headers(*args, **kwargs):
pass
with patch.object(
consumer, 'send_body', side_effect=mock_send_body
) as mock_send_body_obj:
with patch.object(
consumer, 'send_headers', side_effect=mock_send_headers
) as mock_send_headers_obj:
await consumer._forward_response(mock_response)
# Check that response headers were sent
expected_headers = [
[b'content-type', b'application/json'],
[b'content-length', b'100']
]
mock_send_headers_obj.assert_called_once_with(
status=200,
headers=expected_headers
)
# Check that body chunks were sent
self.assertEqual(mock_send_body_obj.call_count, 3)
asyncio.run(test_async())
def test_handle_proxy_request_connection_error(self, mock_requests):
"""Test handling connection error during proxy request"""
async def test_async():
consumer = FilerProxyConsumer()
consumer.config = MagicMock()
consumer.config.timeout = aiohttp.ClientTimeout(total=30)
# Mock the entire session context manager flow
mock_session_instance = MagicMock()
async def mock_aenter(self):
return mock_session_instance
async def mock_aexit(self, *args):
return None
mock_session_instance.__aenter__ = mock_aenter
mock_session_instance.__aexit__ = mock_aexit
# Mock the request context manager to raise an exception
mock_request_cm = MagicMock()
async def mock_request_aenter(self):
raise aiohttp.ClientError("Connection failed")
async def mock_request_aexit(self, *args):
return None
mock_request_cm.__aenter__ = mock_request_aenter
mock_request_cm.__aexit__ = mock_request_aexit
mock_session_instance.request = MagicMock(return_value=mock_request_cm)
async def mock_send_response(*args, **kwargs):
pass
with patch('aiohttp.ClientSession', return_value=mock_session_instance):
with patch.object(
consumer, 'send_response', side_effect=mock_send_response
):
await consumer._handle_proxy_request(
'http://example.com/test',
'GET',
{},
b'proxy service unavailable: Connection failed'
)
asyncio.run(test_async())
def test_handle_proxy_request_timeout(self, mock_requests):
"""Test handling timeout during proxy request"""
async def test_async():
consumer = FilerProxyConsumer()
consumer.config = MagicMock()
consumer.config.timeout = aiohttp.ClientTimeout(total=30)
# Mock the entire session context manager flow
mock_session_instance = MagicMock()
async def mock_aenter(self):
return mock_session_instance
async def mock_aexit(self, *args):
return None
mock_session_instance.__aenter__ = mock_aenter
mock_session_instance.__aexit__ = mock_aexit
# Mock the request context manager to raise a timeout
mock_request_cm = MagicMock()
async def mock_request_aenter(self):
raise asyncio.TimeoutError()
async def mock_request_aexit(self, *args):
return None
mock_request_cm.__aenter__ = mock_request_aenter
mock_request_cm.__aexit__ = mock_request_aexit
mock_session_instance.request = MagicMock(return_value=mock_request_cm)
with patch('aiohttp.ClientSession', return_value=mock_session_instance):
with patch.object(consumer, 'send_response') as mock_send_response:
await consumer._handle_proxy_request(
'http://example.com/test',
'GET',
{},
b''
)
mock_send_response.assert_called_once_with(
504,
b'proxy request to backend filer timeout'
)
asyncio.run(test_async())
def test_skip_request_headers(self, mock_requests):
"""Test that certain headers are skipped in proxy requests"""
consumer = FilerProxyConsumer()
# Verify that SKIP_REQUEST_HEADERS contains expected headers
expected_skip_headers = {
'host', 'connection', 'keep-alive', 'proxy-connection',
'te', 'trailers', 'upgrade'
}
self.assertEqual(consumer.SKIP_REQUEST_HEADERS, expected_skip_headers)
def test_skip_response_headers(self, mock_requests):
"""Test that certain headers are skipped in proxy responses"""
consumer = FilerProxyConsumer()
# Verify that SKIP_RESPONSE_HEADERS contains expected headers
expected_skip_headers = {
'connection', 'keep-alive', 'proxy-authenticate',
'proxy-authorization', 'te', 'trailers',
'transfer-encoding', 'upgrade'
}
self.assertEqual(consumer.SKIP_RESPONSE_HEADERS, expected_skip_headers)
def test_get_client_with_existing_volume(self, mock_requests):
"""Test _get_client method with existing volume"""
path = 'test/file.txt'
async def test_async():
consumer = FilerProxyConsumer()
consumer.scope = {
'user': self.user,
'url_route': {
'kwargs': {'path': path, 'name': 'test-volume', 'id': self.app_id}
}
}
consumer.id = self.app_id
with patch('api.filer.FilerClient') as mock_filer_client:
mock_client = MagicMock()
mock_filer_client.return_value = mock_client
# Test the _get_client method
client = await consumer._get_client(path)
# Verify that FilerClient was called with correct parameters
mock_filer_client.assert_called_once_with(self.app_id, self.test_volume, path)
self.assertEqual(client, mock_client)
asyncio.run(test_async())
def test_get_client_with_nonexistent_volume(self, mock_requests):
"""Test _get_client method with non-existent volume"""
path = 'test/file.txt'
async def test_async():
consumer = FilerProxyConsumer()
consumer.scope = {
'user': self.user,
'url_route': {
'kwargs': {
'path': path, 'name': 'nonexistent-volume', 'id': self.app_id,
}
}
}
consumer.id = self.app_id
# Test the _get_client method
client = await consumer._get_client(path)
# Should return None when volume doesn't exist
self.assertIsNone(client)
asyncio.run(test_async())
def test_handle_with_volume_not_found(self, mock_requests):
"""Test handling request when volume is not found"""
async def test_async():
consumer = FilerProxyConsumer()
path = 'test/file.txt'
consumer.scope = {
'user': self.user,
'path': f'/v2/apps/{self.app_id}/volumes/nonexistent-volume/filer/{path}',
'url_route': {
'kwargs': {
'path': path, 'name': 'nonexistent-volume', 'id': self.app_id,
}
},
'method': 'GET',
'headers': []
}
consumer.id = self.app_id
async def mock_has_perm():
return (True, "permission granted")
async def mock_send_response(*args, **kwargs):
pass
with patch.object(AppPermChecker, 'has_perm', side_effect=mock_has_perm):
with patch.object(
consumer, 'send_response', side_effect=mock_send_response
) as mock_send_response_obj:
await consumer.handle(b'test body')
mock_send_response_obj.assert_called_once_with(
status=404,
body=b'app or volume not found'
)
asyncio.run(test_async())
def test_handle_with_existing_volume(self, mock_requests):
"""Test handling request with existing volume"""
path = 'webdav/test/file.txt'
async def test_async():
consumer = FilerProxyConsumer()
consumer.scope = {
'user': self.user,
'path': f'/v2/apps/{self.app_id}/volumes/nonexistent-volume/filer/{path}',
'url_route': {
'kwargs': {'path': path, 'name': 'test-volume', 'id': self.app_id}
},
'method': 'GET',
'headers': [(b'host', b'example.com'), (b'user-agent', b'test-agent')],
'query_string': 'param=value'
}
consumer.id = self.app_id
filer_data = {
'endpoint': 'http://filer.example.com',
'username': 'test-username',
'password': 'test-password'
}
async def mock_has_perm():
return (True, "permission granted")
with patch.object(AppPermChecker, 'has_perm', side_effect=mock_has_perm):
with patch.object(consumer, '_handle_proxy_request') as mock_handle_proxy:
# Create a proper async function instead of AsyncMock
async def mock_bind_func():
return filer_data
with patch('api.filer.FilerClient') as mock_filer_client:
mock_client = MagicMock()
mock_client.bind = mock_client.info = mock_bind_func
mock_filer_client.return_value = mock_client
await consumer.handle(b'test body')
expected_url = "http://filer.example.com{}?param=value".format(
consumer.scope['path']
)
expected_headers = {'user-agent': 'test-agent'}
mock_handle_proxy.assert_called_once_with(
expected_url,
'GET',
expected_headers,
b'test body'
)
asyncio.run(test_async())