-
-
Notifications
You must be signed in to change notification settings - Fork 268
/
Copy pathtest_popups.py
1727 lines (1536 loc) · 58.9 KB
/
test_popups.py
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
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from collections import OrderedDict
from typing import Any, Callable, Dict, List, Optional, Tuple
import pytest
from pytest import param as case
from pytest_mock import MockerFixture
from urwid import Columns, Pile, Text, Widget
from zulipterminal.api_types import Message
from zulipterminal.config.keys import is_command_key, keys_for_command
from zulipterminal.config.ui_mappings import EDIT_MODE_CAPTIONS
from zulipterminal.helper import (
CustomProfileData,
MessageInfoPopupContent,
TidiedUserInfo,
)
from zulipterminal.ui_tools.messages import MessageBox
from zulipterminal.ui_tools.views import (
AboutView,
EditHistoryTag,
EditHistoryView,
EditModeView,
EmojiPickerView,
FullRawMsgView,
FullRenderedMsgView,
HelpView,
MarkdownHelpView,
MsgInfoView,
PopUpConfirmationView,
PopUpView,
StreamInfoView,
StreamMembersView,
UserInfoView,
)
from zulipterminal.urwid_types import urwid_Size
from zulipterminal.version import MINIMUM_SUPPORTED_SERVER_VERSION, ZT_VERSION
MODULE = "zulipterminal.ui_tools.views"
LISTWALKER = MODULE + ".urwid.SimpleFocusListWalker"
# Test classes are grouped/ordered below as:
# * an independent popup class
# * the base general popup class
# * classes derived from the base popup class, sorted alphabetically
@pytest.fixture
def message_info_content() -> MessageInfoPopupContent:
return MessageInfoPopupContent(
message=Message(),
topic_links=OrderedDict(),
message_links=OrderedDict(),
time_mentions=list(),
)
class TestPopUpConfirmationView:
@pytest.fixture
def popup_view(self, mocker: MockerFixture) -> PopUpConfirmationView:
self.controller = mocker.Mock()
self.callback = mocker.Mock()
self.list_walker = mocker.patch(LISTWALKER, return_value=[])
self.divider = mocker.patch(MODULE + ".urwid.Divider")
self.text = mocker.patch(MODULE + ".urwid.Text")
self.wrapper_w = mocker.patch(MODULE + ".urwid.WidgetWrap")
return PopUpConfirmationView(
self.controller,
self.text,
self.callback,
)
def test_init(self, popup_view: PopUpConfirmationView) -> None:
assert popup_view.controller == self.controller
assert popup_view.success_callback == self.callback
self.divider.assert_called_once_with()
self.list_walker.assert_called_once_with(
[self.text, self.divider(), self.wrapper_w()]
)
def test_exit_popup_yes(
self, mocker: MockerFixture, popup_view: PopUpConfirmationView
) -> None:
popup_view.exit_popup_yes(mocker.Mock())
self.callback.assert_called_once_with()
assert self.controller.exit_popup.called
def test_exit_popup_no(
self, mocker: MockerFixture, popup_view: PopUpConfirmationView
) -> None:
popup_view.exit_popup_no(mocker.Mock())
self.callback.assert_not_called()
assert self.controller.exit_popup.called
@pytest.mark.parametrize("key", keys_for_command("EXIT_POPUP"))
def test_exit_popup_EXIT_POPUP(
self,
popup_view: PopUpConfirmationView,
key: str,
widget_size: Callable[[Widget], urwid_Size],
) -> None:
size = widget_size(popup_view)
popup_view.keypress(size, key)
self.callback.assert_not_called()
assert self.controller.exit_popup.called
class TestPopUpView:
@pytest.fixture(autouse=True)
def pop_up_view_autouse(self, mocker: MockerFixture) -> None:
self.controller = mocker.Mock()
mocker.patch.object(
self.controller, "maximum_popup_dimensions", return_value=(64, 64)
)
self.command = "COMMAND"
self.title = "Generic title"
self.width = 16
self.body = mocker.Mock()
self.header = mocker.Mock()
self.footer = mocker.Mock()
mocker.patch.object(self.body, "rows", return_value=1)
mocker.patch.object(self.header, "rows", return_value=1)
mocker.patch.object(self.footer, "rows", return_value=1)
self.body = [self.body]
self.header = Pile([self.header])
self.footer = Pile([self.footer])
self.list_walker = mocker.patch(LISTWALKER, return_value=[])
self.super_init = mocker.patch(MODULE + ".urwid.Frame.__init__")
self.super_keypress = mocker.patch(MODULE + ".urwid.Frame.keypress")
self.pop_up_view = PopUpView(
self.controller,
self.body,
self.command,
self.width,
self.title,
self.header,
self.footer,
)
def test_init(self, mocker: MockerFixture) -> None:
assert self.pop_up_view.controller == self.controller
assert self.pop_up_view.command == self.command
assert self.pop_up_view.title == self.title
assert self.pop_up_view.width == self.width
self.list_walker.assert_called_once_with(self.body)
self.super_init.assert_called_once_with(
self.pop_up_view.body, header=mocker.ANY, footer=mocker.ANY
)
@pytest.mark.parametrize("key", keys_for_command("EXIT_POPUP"))
def test_keypress_EXIT_POPUP(
self,
key: str,
widget_size: Callable[[Widget], urwid_Size],
) -> None:
size = widget_size(self.pop_up_view)
self.pop_up_view.keypress(size, key)
assert self.controller.exit_popup.called
def test_keypress_command_key(
self,
mocker: MockerFixture,
widget_size: Callable[[Widget], urwid_Size],
) -> None:
size = widget_size(self.pop_up_view)
mocker.patch(
MODULE + ".is_command_key",
side_effect=(lambda command, key: command == self.command),
)
self.pop_up_view.keypress(size, "cmd_key")
assert self.controller.exit_popup.called
def test_keypress_navigation(
self,
mocker: MockerFixture,
navigation_key: str,
widget_size: Callable[[Widget], urwid_Size],
) -> None:
size = widget_size(self.pop_up_view)
# Patch `is_command_key` to not raise an 'Invalid Command' exception
# when its parameters are (self.command, key) as there is no
# self.command='COMMAND' command in keys.py.
mocker.patch(
MODULE + ".is_command_key",
side_effect=(
lambda command, key: False
if command == self.command
else is_command_key(command, key)
),
)
self.pop_up_view.keypress(size, navigation_key)
self.super_keypress.assert_called_once_with(size, navigation_key)
class TestAboutView:
@pytest.fixture(autouse=True)
def mock_external_classes(self, mocker: MockerFixture) -> None:
self.controller = mocker.Mock()
mocker.patch.object(
self.controller, "maximum_popup_dimensions", return_value=(64, 64)
)
server_version, server_feature_level = MINIMUM_SUPPORTED_SERVER_VERSION
# FIXME: Since we don't test on WSL explicitly, for now
# treat PLATFORM as WSL in order for it to be supported
mocker.patch(MODULE + ".PLATFORM", "WSL")
mocker.patch(MODULE + ".detected_python_in_full", lambda: "[Python version]")
self.about_view = AboutView(
self.controller,
"About",
zt_version=ZT_VERSION,
server_version=server_version,
server_feature_level=server_feature_level,
theme_name="zt_dark",
color_depth=256,
notify_enabled=False,
autohide_enabled=False,
maximum_footlinks=3,
exit_confirmation_enabled=False,
transparency_enabled=False,
)
@pytest.mark.parametrize(
"key", {*keys_for_command("EXIT_POPUP"), *keys_for_command("ABOUT")}
)
def test_keypress_exit_popup(
self, key: str, widget_size: Callable[[Widget], urwid_Size]
) -> None:
size = widget_size(self.about_view)
self.about_view.keypress(size, key)
assert self.controller.exit_popup.called
@pytest.mark.parametrize("key", {*keys_for_command("COPY_ABOUT_INFO")})
def test_keypress_copy_info(
self, key: str, widget_size: Callable[[Widget], urwid_Size]
) -> None:
size = widget_size(self.about_view)
self.about_view.keypress(size, key)
assert self.controller.copy_to_clipboard.called
def test_keypress_exit_popup_invalid_key(
self, widget_size: Callable[[Widget], urwid_Size]
) -> None:
key = "a"
size = widget_size(self.about_view)
self.about_view.keypress(size, key)
assert not self.controller.exit_popup.called
def test_feature_level_content(
self, mocker: MockerFixture, zulip_version: Tuple[str, int]
) -> None:
self.controller = mocker.Mock()
mocker.patch.object(
self.controller, "maximum_popup_dimensions", return_value=(64, 64)
)
mocker.patch(LISTWALKER, return_value=[])
server_version, server_feature_level = zulip_version
about_view = AboutView(
self.controller,
"About",
zt_version=ZT_VERSION,
server_version=server_version,
server_feature_level=server_feature_level,
theme_name="zt_dark",
color_depth=256,
notify_enabled=False,
autohide_enabled=False,
maximum_footlinks=3,
exit_confirmation_enabled=False,
transparency_enabled=False,
)
assert len(about_view.feature_level_content) == (
1 if server_feature_level else 0
)
def test_categories(self) -> None:
categories = [
widget.text
for widget in self.about_view.log
if isinstance(widget, Text)
and len(widget.attrib)
and "popup_category" in widget.attrib[0][0]
]
assert categories == [
"Application",
"Server",
"Application Configuration",
"Detected Environment",
"Copy information to clipboard [c]",
]
def test_copied_content(self) -> None:
expected_output = f"""#### Application
Zulip Terminal: {ZT_VERSION}
#### Server
Version: {MINIMUM_SUPPORTED_SERVER_VERSION[0]}
#### Application Configuration
Theme: zt_dark
Autohide: disabled
Maximum footlinks: 3
Color depth: 256
Notifications: disabled
Exit confirmation: disabled
Transparency: disabled
#### Detected Environment
Platform: WSL
Python: [Python version]"""
assert self.about_view.copy_info == expected_output
class TestUserInfoView:
@pytest.fixture(autouse=True)
def mock_external_classes(
self, mocker: MockerFixture, tidied_user_info_response: TidiedUserInfo
) -> None:
self.controller = mocker.Mock()
mocker.patch.object(
self.controller, "maximum_popup_dimensions", return_value=(64, 64)
)
mocker.patch(MODULE + ".urwid.SimpleFocusListWalker", return_value=[])
self.user_data = tidied_user_info_response
mocker.patch.object(
self.controller.model, "get_user_info", return_value=self.user_data
)
mocker.patch.object(
self.controller.model,
"formatted_local_time",
return_value="Tue Mar 13 10:55 AM",
)
mocked_user_name_from_id = {
11: "Human 1",
12: "Human 2",
13: "Human 3",
}
self.controller.model.user_name_from_id = mocker.Mock(
side_effect=lambda param: mocked_user_name_from_id.get(param, "(No name)")
)
self.user_info_view = UserInfoView(
self.controller, 10000, "User Info (up/down scrolls)", "USER_INFO"
)
@pytest.mark.parametrize(
[
"to_vary_in_each_user",
"expected_key",
"expected_value",
],
[
({}, "Email", "[email protected]"),
({"email": ""}, "Email", None),
({"date_joined": "2021-03-18 16:52:48"}, "Date joined", "2021-03-18"),
({}, "Date joined", None),
({"timezone": "America/Los_Angeles"}, "Timezone", "America/Los Angeles"),
({}, "Timezone", None),
(
{"is_bot": True, "bot_type": 1, "bot_owner_name": "Test Owner"},
"Owner",
"Test Owner",
),
({}, "Owner", None),
(
{"last_active": "Tue Mar 13 10:55:22"},
"Last active",
"Tue Mar 13 10:55:22",
),
({}, "Last active", None),
({"is_bot": True, "bot_type": 1}, "Role", "Generic Bot"),
({"is_bot": True, "bot_type": 2}, "Role", "Incoming Webhook Bot"),
({"is_bot": True, "bot_type": 3}, "Role", "Outgoing Webhook Bot"),
({"is_bot": True, "bot_type": 4}, "Role", "Embedded Bot"),
({"role": 100}, "Role", "Owner"),
({"role": 200}, "Role", "Administrator"),
({"role": 300}, "Role", "Moderator"),
({"role": 600}, "Role", "Guest"),
({"role": 400}, "Role", "Member"),
],
ids=[
"user_email",
"user_empty_email",
"user_date_joined",
"user_empty_date_joined",
"user_timezone",
"user_empty_timezone",
"user_bot_owner",
"user_empty_bot_owner",
"user_last_active",
"user_empty_last_active",
"user_is_generic_bot",
"user_is_incoming_webhook_bot",
"user_is_outgoing_webhook_bot",
"user_is_embedded_bot",
"user_is_owner",
"user_is_admin",
"user_is_moderator",
"user_is_guest",
"user_is_member",
],
)
def test__fetch_user_data(
self,
to_vary_in_each_user: Dict[str, Any],
expected_key: str,
expected_value: Optional[str],
) -> None:
data = dict(self.user_data, **to_vary_in_each_user)
self.controller.model.get_user_info.return_value = data
display_data, custom_profile_data = self.user_info_view._fetch_user_data(
self.controller, 1
)
assert display_data.get(expected_key, None) == expected_value
@pytest.mark.parametrize(
[
"to_vary_in_each_user",
"expected_value",
],
[
case(
[],
{},
id="user_has_no_custom_profile_data",
),
case(
[
{
"label": "Biography",
"value": "Simplicity",
"type": 2,
"order": 2,
},
{
"label": "Mentor",
"value": [11, 12],
"type": 6,
"order": 7,
},
],
{"Biography": "Simplicity", "Mentor": "Human 1, Human 2"},
id="user_has_custom_profile_data",
),
],
)
def test__fetch_user_data__custom_profile_data(
self,
to_vary_in_each_user: List[CustomProfileData],
expected_value: Dict[str, str],
) -> None:
data = dict(self.user_data)
data["custom_profile_data"] = to_vary_in_each_user
self.controller.model.get_user_info.return_value = data
display_data, custom_profile_data = self.user_info_view._fetch_user_data(
self.controller, 1
)
assert custom_profile_data == expected_value
def test__fetch_user_data_USER_NOT_FOUND(self, mocker: MockerFixture) -> None:
mocker.patch.object(self.controller.model, "get_user_info", return_value=dict())
display_data, custom_profile_data = self.user_info_view._fetch_user_data(
self.controller, 1
)
assert display_data["Name"] == "(Unavailable)"
assert display_data["Error"] == "User data not found"
assert custom_profile_data == {}
@pytest.mark.parametrize(
"key", {*keys_for_command("EXIT_POPUP"), *keys_for_command("USER_INFO")}
)
def test_keypress_exit_popup(
self, key: str, widget_size: Callable[[Widget], urwid_Size]
) -> None:
size = widget_size(self.user_info_view)
self.user_info_view.keypress(size, key)
assert self.controller.exit_popup.called
def test_keypress_exit_popup_invalid_key(
self, widget_size: Callable[[Widget], urwid_Size]
) -> None:
key = "a"
size = widget_size(self.user_info_view)
self.user_info_view.keypress(size, key)
assert not self.controller.exit_popup.called
class TestFullRenderedMsgView:
@pytest.fixture(autouse=True)
def mock_external_classes(
self,
mocker: MockerFixture,
msg_box: MessageBox,
message_info_content: MessageInfoPopupContent,
) -> None:
self.controller = mocker.Mock()
mocker.patch.object(
self.controller, "maximum_popup_dimensions", return_value=(64, 64)
)
mocker.patch(MODULE + ".MessageBox", return_value=msg_box)
# NOTE: Given that the FullRenderedMsgView just uses the message ID from
# the message data currently, message_fixture is not used to avoid
# adding extra test runs unnecessarily.
self.message_info_content = message_info_content
self.message_info_content["message"] = Message(id=1)
self.full_rendered_message = FullRenderedMsgView(
controller=self.controller,
title="Full Rendered Message",
message_info_content=message_info_content,
)
def test_init(self, msg_box: MessageBox) -> None:
assert self.full_rendered_message.title == "Full Rendered Message"
assert self.full_rendered_message.controller == self.controller
assert (
self.full_rendered_message.message_info_content == self.message_info_content
)
assert self.full_rendered_message.header.widget_list == msg_box.header
assert self.full_rendered_message.footer.widget_list == msg_box.footer
@pytest.mark.parametrize("key", keys_for_command("MSG_INFO"))
def test_keypress_exit_popup(
self, key: str, widget_size: Callable[[Widget], urwid_Size]
) -> None:
size = widget_size(self.full_rendered_message)
self.full_rendered_message.keypress(size, key)
assert self.controller.exit_popup.called
def test_keypress_exit_popup_invalid_key(
self, widget_size: Callable[[Widget], urwid_Size]
) -> None:
size = widget_size(self.full_rendered_message)
key = "a"
self.full_rendered_message.keypress(size, key)
assert not self.controller.exit_popup.called
@pytest.mark.parametrize(
"key",
{
*keys_for_command("FULL_RENDERED_MESSAGE"),
*keys_for_command("EXIT_POPUP"),
},
)
def test_keypress_show_msg_info(
self,
key: str,
widget_size: Callable[[Widget], urwid_Size],
message_info_content: MessageInfoPopupContent,
) -> None:
size = widget_size(self.full_rendered_message)
self.full_rendered_message.keypress(size, key)
self.controller.show_msg_info.assert_called_once_with(self.message_info_content)
class TestFullRawMsgView:
@pytest.fixture(autouse=True)
def mock_external_classes(
self,
mocker: MockerFixture,
msg_box: MessageBox,
message_info_content: MessageInfoPopupContent,
) -> None:
self.controller = mocker.Mock()
mocker.patch.object(
self.controller, "maximum_popup_dimensions", return_value=(64, 64)
)
self.controller.model.fetch_raw_message_content = mocker.Mock(
return_value="This is a `raw` message content :+1:"
)
mocker.patch(MODULE + ".MessageBox", return_value=msg_box)
# NOTE: Given that the FullRawMsgView just uses the message ID from
# the message data currently, message_fixture is not used to avoid
# adding extra test runs unnecessarily.
self.message_info_content = message_info_content
self.message_info_content["message"] = Message(id=1)
self.full_raw_message = FullRawMsgView(
controller=self.controller,
title="Full Raw Message",
message_info_content=self.message_info_content,
)
def test_init(self, msg_box: MessageBox) -> None:
assert self.full_raw_message.title == "Full Raw Message"
assert self.full_raw_message.controller == self.controller
assert self.full_raw_message.message_info_content == self.message_info_content
assert self.full_raw_message.header.widget_list == msg_box.header
assert self.full_raw_message.footer.widget_list == msg_box.footer
@pytest.mark.parametrize("key", keys_for_command("MSG_INFO"))
def test_keypress_exit_popup(
self, key: str, widget_size: Callable[[Widget], urwid_Size]
) -> None:
size = widget_size(self.full_raw_message)
self.full_raw_message.keypress(size, key)
assert self.controller.exit_popup.called
def test_keypress_exit_popup_invalid_key(
self, widget_size: Callable[[Widget], urwid_Size]
) -> None:
size = widget_size(self.full_raw_message)
key = "a"
self.full_raw_message.keypress(size, key)
assert not self.controller.exit_popup.called
@pytest.mark.parametrize(
"key",
{
*keys_for_command("FULL_RAW_MESSAGE"),
*keys_for_command("EXIT_POPUP"),
},
)
def test_keypress_show_msg_info(
self, key: str, widget_size: Callable[[Widget], urwid_Size]
) -> None:
size = widget_size(self.full_raw_message)
self.full_raw_message.keypress(size, key)
self.controller.show_msg_info.assert_called_once_with(self.message_info_content)
class TestEditHistoryView:
@pytest.fixture(autouse=True)
def mock_external_classes(
self, mocker: MockerFixture, message_info_content: MessageInfoPopupContent
) -> None:
self.controller = mocker.Mock()
mocker.patch.object(
self.controller, "maximum_popup_dimensions", return_value=(64, 64)
)
self.controller.model.fetch_message_history = mocker.Mock(return_value=[])
self.controller.model.formatted_local_time.return_value = "Tue Mar 13 10:55:22"
mocker.patch(LISTWALKER, return_value=[])
# NOTE: Given that the EditHistoryView just uses the message ID from
# the message data currently, message_fixture is not used to avoid
# adding extra test runs unnecessarily.
self.message_info_content = message_info_content
self.message_info_content["message"] = Message(id=1)
self.edit_history_view = EditHistoryView(
controller=self.controller,
title="Edit History",
message_info_content=self.message_info_content,
)
def test_init(self) -> None:
assert self.edit_history_view.controller == self.controller
assert self.edit_history_view.title == "Edit History"
assert self.edit_history_view.message_info_content == self.message_info_content
self.controller.model.fetch_message_history.assert_called_once_with(
message_id=self.message_info_content["message"]["id"],
)
@pytest.mark.parametrize("key", keys_for_command("MSG_INFO"))
def test_keypress_exit_popup(
self, key: str, widget_size: Callable[[Widget], urwid_Size]
) -> None:
size = widget_size(self.edit_history_view)
self.edit_history_view.keypress(size, key)
assert self.controller.exit_popup.called
def test_keypress_exit_popup_invalid_key(
self, widget_size: Callable[[Widget], urwid_Size]
) -> None:
size = widget_size(self.edit_history_view)
key = "a"
self.edit_history_view.keypress(size, key)
assert not self.controller.exit_popup.called
@pytest.mark.parametrize(
"key", {*keys_for_command("EDIT_HISTORY"), *keys_for_command("EXIT_POPUP")}
)
def test_keypress_show_msg_info(
self, key: str, widget_size: Callable[[Widget], urwid_Size]
) -> None:
size = widget_size(self.edit_history_view)
self.edit_history_view.keypress(size, key)
self.controller.show_msg_info.assert_called_once_with(self.message_info_content)
@pytest.mark.parametrize(
"snapshot",
[
{
"content": "Howdy!",
"timestamp": 1530129134,
"topic": "party at my house",
# ...
}
],
)
@pytest.mark.parametrize(
"user_id, user_name_from_id_called",
[
(1001, True),
(None, False),
],
ids=[
"with_user_id",
"without_user_id",
],
)
def test__make_edit_block(
self,
mocker: MockerFixture,
snapshot: Dict[str, Any],
user_id: Optional[int],
user_name_from_id_called: bool,
tag: EditHistoryTag = "(Current Version)",
) -> None:
self._get_author_prefix = mocker.patch(
MODULE + ".EditHistoryView._get_author_prefix",
)
snapshot = dict(**snapshot, user_id=user_id) if user_id else snapshot
contents = self.edit_history_view._make_edit_block(snapshot, tag)
assert isinstance(contents[0], Columns) # Header.
assert isinstance(contents[0][0], Text) # Header: Topic.
assert isinstance(contents[0][1], Text) # Header: Tag.
assert isinstance(contents[1], Columns) # Subheader.
assert isinstance(contents[1][0], Text) # Subheader: Author.
assert isinstance(contents[1][1], Text) # Subheader: Timestamp.
assert isinstance(contents[2], Text) # Content.
assert contents[0][1].text == tag
assert (
self.controller.model.user_name_from_id.called == user_name_from_id_called
)
@pytest.mark.parametrize(
"snapshot",
[
{
"content": "Howdy!",
"timestamp": 1530129134,
"topic": "party at my house",
# ...
}
],
)
@pytest.mark.parametrize(
"to_vary_in_snapshot, tag, expected_author_prefix",
[
(
{},
"(Original Version)",
"Posted",
),
(
{
"prev_content": "Hi!",
"prev_topic": "no party at my house",
},
"",
"Content & Topic edited",
),
(
{
"prev_content": "Hi!",
},
"",
"Content edited",
),
(
{
"prev_topic": "no party at my house",
},
"",
"Topic edited",
),
(
{
"prev_content": "Howdy!",
"prev_topic": "party at my house",
},
"",
"Edited but no changes made",
),
(
{
"prev_content": "Hi!",
"prev_topic": "party at my house",
},
"",
"Content edited",
),
(
{
"prev_content": "Howdy!",
"prev_topic": "no party at my house",
},
"",
"Topic edited",
),
],
ids=[
"posted",
"content_&_topic_edited",
"content_edited",
"topic_edited",
"false_alarm_content_&_topic",
"content_edited_with_false_alarm_topic",
"topic_edited_with_false_alarm_content",
],
)
def test__get_author_prefix(
self,
snapshot: Dict[str, Any],
to_vary_in_snapshot: Dict[str, Any],
tag: EditHistoryTag,
expected_author_prefix: str,
) -> None:
snapshot = dict(**snapshot, **to_vary_in_snapshot)
return_value = EditHistoryView._get_author_prefix(snapshot, tag)
assert return_value == expected_author_prefix
class TestEditModeView:
@pytest.fixture(params=EDIT_MODE_CAPTIONS.keys())
def edit_mode_view(self, mocker: MockerFixture, request: Any) -> EditModeView:
button_launch_mode = request.param
button = mocker.Mock(mode=button_launch_mode)
controller = mocker.Mock()
controller.maximum_popup_dimensions.return_value = (64, 64)
return EditModeView(controller, button)
def test_init(self, edit_mode_view: EditModeView) -> None:
pass # Just test init succeeds
@pytest.mark.parametrize(
"index_in_widgets, mode",
[
(0, "change_one"),
(1, "change_later"),
(2, "change_all"),
],
)
@pytest.mark.parametrize("key", keys_for_command("ACTIVATE_BUTTON"))
def test_select_edit_mode(
self,
edit_mode_view: EditModeView,
widget_size: Callable[[Widget], urwid_Size],
index_in_widgets: int,
mode: str,
key: str,
) -> None:
mode_button = edit_mode_view.edit_mode_button
if mode_button.mode == mode:
pytest.skip("button already selected")
radio_button = edit_mode_view.widgets[index_in_widgets]
size = widget_size(radio_button)
radio_button.keypress(size, key)
mode_button.set_selected_mode.assert_called_once_with(mode)
class TestMarkdownHelpView:
@pytest.fixture(autouse=True)
def mock_external_classes(self, mocker: MockerFixture) -> None:
self.controller = mocker.Mock()
mocker.patch.object(
self.controller, "maximum_popup_dimensions", return_value=(64, 64)
)
self.controller.model.server_url = "https://chat.zulip.org/"
self.markdown_help_view = MarkdownHelpView(
self.controller,
"Markdown Help Menu",
)
def test_keypress_any_key(
self, widget_size: Callable[[Widget], urwid_Size]
) -> None:
key = "a"
size = widget_size(self.markdown_help_view)
self.markdown_help_view.keypress(size, key)
assert not self.controller.exit_popup.called
@pytest.mark.parametrize(
"key", {*keys_for_command("EXIT_POPUP"), *keys_for_command("MARKDOWN_HELP")}
)
def test_keypress_exit_popup(
self, key: str, widget_size: Callable[[Widget], urwid_Size]
) -> None:
size = widget_size(self.markdown_help_view)
self.markdown_help_view.keypress(size, key)
assert self.controller.exit_popup.called
class TestHelpView:
@pytest.fixture(autouse=True)
def mock_external_classes(self, mocker: MockerFixture) -> None:
self.controller = mocker.Mock()
mocker.patch.object(
self.controller, "maximum_popup_dimensions", return_value=(64, 64)
)
mocker.patch(LISTWALKER, return_value=[])
self.help_view = HelpView(self.controller, "Help Menu")
def test_keypress_any_key(
self, widget_size: Callable[[Widget], urwid_Size]
) -> None:
key = "a"
size = widget_size(self.help_view)
self.help_view.keypress(size, key)
assert not self.controller.exit_popup.called
@pytest.mark.parametrize(
"key", {*keys_for_command("EXIT_POPUP"), *keys_for_command("HELP")}
)
def test_keypress_exit_popup(
self, key: str, widget_size: Callable[[Widget], urwid_Size]
) -> None:
size = widget_size(self.help_view)
self.help_view.keypress(size, key)
assert self.controller.exit_popup.called
class TestMsgInfoView:
@pytest.fixture(autouse=True)
def mock_external_classes(
self,
mocker: MockerFixture,
message_fixture: Message,
message_info_content: MessageInfoPopupContent,
) -> None:
self.controller = mocker.Mock()
mocker.patch.object(
self.controller, "maximum_popup_dimensions", return_value=(64, 64)
)
mocker.patch(LISTWALKER, return_value=[])
# The subsequent patches (index and initial_data) set
# show_edit_history_label to False for this autoused fixture.
self.controller.model.index = {"edited_messages": set()}
self.controller.model.initial_data = {
"realm_allow_edit_history": False,
}
self.controller.model.formatted_local_time.side_effect = [
"Tue Mar 13 10:55:22",
"Tue Mar 13 10:55:37",
]
self.message_info_content = message_info_content
self.message_info_content["message"] = message_fixture
self.msg_info_view = MsgInfoView(
self.controller,
"Message Information",
self.message_info_content,
)
def test_init(self, message_fixture: Message) -> None:
assert self.msg_info_view.message_info_content == self.message_info_content
def test_pop_up_info_order(self, message_fixture: Message) -> None:
topic_links = OrderedDict([("https://bar.com", ("topic", 1, True))])
message_links = OrderedDict([("image.jpg", ("image", 1, True))])
message_info_content = MessageInfoPopupContent(
message=message_fixture,
topic_links=topic_links,
message_links=message_links,