@@ -34,7 +34,7 @@ def model(self, mocker, initial_data, user_profile,
34
34
return_value = [])
35
35
mocker .patch ('zulipterminal.model.Model.'
36
36
'_stream_info_from_subscriptions' ,
37
- return_value = ({}, set (), [], []))
37
+ return_value = ({}, set (), [], [], set () ))
38
38
# NOTE: PATCH WHERE USED NOT WHERE DEFINED
39
39
self .classify_unread_counts = mocker .patch (
40
40
'zulipterminal.model.classify_unread_counts' ,
@@ -130,7 +130,7 @@ def test_init_InvalidAPIKey_response(self, mocker, initial_data):
130
130
return_value = [])
131
131
mocker .patch ('zulipterminal.model.Model.'
132
132
'_stream_info_from_subscriptions' ,
133
- return_value = ({}, set (), [], []))
133
+ return_value = ({}, set (), [], [], set () ))
134
134
self .classify_unread_counts = mocker .patch (
135
135
'zulipterminal.model.classify_unread_counts' ,
136
136
return_value = [])
@@ -152,7 +152,7 @@ def test_init_ZulipError_exception(self, mocker, initial_data,
152
152
return_value = [])
153
153
mocker .patch ('zulipterminal.model.Model.'
154
154
'_stream_info_from_subscriptions' ,
155
- return_value = ({}, set (), [], []))
155
+ return_value = ({}, set (), [], [], set () ))
156
156
self .classify_unread_counts = mocker .patch (
157
157
'zulipterminal.model.classify_unread_counts' ,
158
158
return_value = [])
@@ -557,7 +557,7 @@ def test_success_get_messages(self, mocker, messages_successful_response,
557
557
return_value = [])
558
558
mocker .patch ('zulipterminal.model.Model.'
559
559
'_stream_info_from_subscriptions' ,
560
- return_value = ({}, set (), [], []))
560
+ return_value = ({}, set (), [], [], set () ))
561
561
self .classify_unread_counts = mocker .patch (
562
562
'zulipterminal.model.classify_unread_counts' ,
563
563
return_value = [])
@@ -597,7 +597,7 @@ def test_get_message_false_first_anchor(
597
597
return_value = [])
598
598
mocker .patch ('zulipterminal.model.Model.'
599
599
'_stream_info_from_subscriptions' ,
600
- return_value = ({}, set (), [], []))
600
+ return_value = ({}, set (), [], [], set () ))
601
601
self .classify_unread_counts = mocker .patch (
602
602
'zulipterminal.model.classify_unread_counts' ,
603
603
return_value = [])
@@ -631,7 +631,7 @@ def test_fail_get_messages(self, mocker, error_response,
631
631
return_value = [])
632
632
mocker .patch ('zulipterminal.model.Model.'
633
633
'_stream_info_from_subscriptions' ,
634
- return_value = ({}, set (), [], []))
634
+ return_value = ({}, set (), [], [], set () ))
635
635
self .classify_unread_counts = mocker .patch (
636
636
'zulipterminal.model.classify_unread_counts' ,
637
637
return_value = [])
@@ -666,6 +666,29 @@ def test_toggle_stream_muted_status(self, mocker, model,
666
666
self .display_error_if_present .assert_called_once_with (response ,
667
667
self .controller )
668
668
669
+ @pytest .mark .parametrize ('initial_desktop_notified_streams, value' , [
670
+ ({315 }, True ),
671
+ ({205 , 315 }, False ),
672
+ (set (), True ),
673
+ ({205 }, False ),
674
+ ], ids = ['desktop_notify_enable_205' , 'desktop_notify_disable_205' ,
675
+ 'first_notify_enable_205' , 'last_notify_disable_205' ])
676
+ def test_toggle_stream_desktop_notification (
677
+ self , mocker , model , initial_desktop_notified_streams ,
678
+ value , response = {'result' : 'success' }):
679
+ model .desktop_notifs_enabled_streams = initial_desktop_notified_streams
680
+ model .client .update_subscription_settings .return_value = response
681
+ model .toggle_stream_desktop_notification (205 )
682
+ request = [{
683
+ 'stream_id' : 205 ,
684
+ 'property' : 'desktop_notifications' ,
685
+ 'value' : value
686
+ }]
687
+ (model .client .update_subscription_settings
688
+ .assert_called_once_with (request ))
689
+ self .display_error_if_present .assert_called_once_with (response ,
690
+ self .controller )
691
+
669
692
@pytest .mark .parametrize ('flags_before, expected_operator' , [
670
693
([], 'add' ),
671
694
(['starred' ], 'remove' ),
@@ -716,7 +739,7 @@ def test__update_initial_data_raises_exception(self, mocker, initial_data):
716
739
return_value = [])
717
740
mocker .patch ('zulipterminal.model.Model.'
718
741
'_stream_info_from_subscriptions' ,
719
- return_value = ({}, set (), [], []))
742
+ return_value = ({}, set (), [], [], set () ))
720
743
self .classify_unread_counts = mocker .patch (
721
744
'zulipterminal.model.classify_unread_counts' ,
722
745
return_value = [])
@@ -750,26 +773,32 @@ def test_get_all_users(self, mocker, initial_data, user_list, user_dict,
750
773
self .client .register .return_value = initial_data
751
774
mocker .patch ('zulipterminal.model.Model.'
752
775
'_stream_info_from_subscriptions' ,
753
- return_value = ({}, set (), [], []))
776
+ return_value = ({}, set (), [], [], set () ))
754
777
self .classify_unread_counts = mocker .patch (
755
778
'zulipterminal.model.classify_unread_counts' ,
756
779
return_value = [])
757
780
model = Model (self .controller )
758
781
assert model .user_dict == user_dict
759
782
assert model .users == user_list
760
783
761
- @pytest .mark .parametrize ('muted' , powerset ([1 , 2 , 99 , 1000 ]))
784
+ @pytest .mark .parametrize ('muted, desktop_notifs_enabled' , list (
785
+ zip (powerset ([1 , 2 , 99 , 1000 ]), powerset ([1 , 2 , 99 , 1000 ])))
786
+ )
762
787
def test__stream_info_from_subscriptions (self , initial_data , streams ,
763
- muted ):
764
- subs = [dict (entry , in_home_view = entry ['stream_id' ] not in muted )
788
+ muted , desktop_notifs_enabled ):
789
+ subs = [dict (entry , in_home_view = entry ['stream_id' ] not in muted ,
790
+ desktop_notifications = entry ['stream_id' ] in
791
+ desktop_notifs_enabled )
765
792
for entry in initial_data ['subscriptions' ]]
766
- by_id , muted_streams , pinned , unpinned = (
793
+ (by_id , muted_streams , pinned , unpinned ,
794
+ desktop_notifs_enabled_streams ) = (
767
795
Model ._stream_info_from_subscriptions (subs ))
768
796
assert len (by_id )
769
797
assert all (msg_id == msg ['stream_id' ] for msg_id , msg in by_id .items ())
770
798
assert muted_streams == muted
771
799
assert pinned == [] # FIXME generalize/parametrize
772
800
assert unpinned == streams # FIXME generalize/parametrize
801
+ assert desktop_notifs_enabled_streams == desktop_notifs_enabled
773
802
774
803
def test__handle_message_event_with_Falsey_log (self , mocker ,
775
804
model , message_fixture ):
@@ -939,50 +968,61 @@ def test__update_topic_index(self, topic_name, topic_order_initial,
939
968
assert model .index ['topics' ][86 ] == topic_order_final
940
969
941
970
# TODO: Ideally message_fixture would use standardized ids?
942
- @pytest .mark .parametrize (['user_id' , 'vary_each_msg' , 'stream_setting' ,
943
- 'types_when_notify_called' ], [
971
+ @pytest .mark .parametrize (['user_id' , 'vary_each_msg' ,
972
+ 'desktop_notification_status' ,
973
+ 'types_when_notify_called' ,
974
+ 'is_stream_muted' ], [
944
975
(5140 , {'flags' : ['mentioned' , 'wildcard_mentioned' ]}, True ,
945
- []), # message_fixture sender_id is 5140
976
+ [], False ), # message_fixture sender_id is 5140
946
977
(5179 , {'flags' : ['mentioned' ]}, False ,
947
- ['stream' , 'private' ]),
978
+ ['stream' , 'private' ], False ),
948
979
(5179 , {'flags' : ['wildcard_mentioned' ]}, False ,
949
- ['stream' , 'private' ]),
980
+ ['stream' , 'private' ], False ),
950
981
(5179 , {'flags' : []}, True ,
951
- ['stream' ]),
982
+ ['stream' ], False ),
952
983
(5179 , {'flags' : []}, False ,
953
- ['private' ]),
984
+ ['private' ], False ),
985
+ (5140 , {'flags' : []}, True ,
986
+ ['stream' ], True ),
987
+ (5179 , {'flags' : ['mentioned' ]}, True ,
988
+ ['stream' ], True )
954
989
], ids = [
955
990
'not_notified_since_self_message' ,
956
991
'notified_stream_and_private_since_directly_mentioned' ,
957
992
'notified_stream_and_private_since_wildcard_mentioned' ,
958
993
'notified_stream_since_stream_has_desktop_notifications' ,
959
994
'notified_private_since_private_message' ,
995
+ 'not_notified_stream_since_muted_stream' ,
996
+ 'notified_muted_stream_since_directly_mentioned'
960
997
])
961
998
def test_notify_users_calling_msg_type (self , mocker , model ,
962
999
message_fixture ,
963
1000
user_id ,
964
1001
vary_each_msg ,
965
- stream_setting ,
1002
+ desktop_notification_status ,
1003
+ is_stream_muted ,
966
1004
types_when_notify_called ):
967
1005
message_fixture .update (vary_each_msg )
968
1006
model .user_id = user_id
969
- if 'stream_id' in message_fixture :
970
- model . stream_dict . update (
971
- { message_fixture [ 'stream_id' ]:
972
- { 'desktop_notifications' : stream_setting }}
973
- )
1007
+ mocker . patch ( 'zulipterminal.model.Model.'
1008
+ 'is_desktop_notifications_enabled' ,
1009
+ return_value = desktop_notification_status )
1010
+ mocker . patch ( 'zulipterminal.model.Model.'
1011
+ 'is_muted_stream' , return_value = is_stream_muted )
974
1012
notify = mocker .patch ('zulipterminal.model.notify' )
975
1013
976
1014
model .notify_user (message_fixture )
977
1015
1016
+ target = None
978
1017
if message_fixture ['type' ] in types_when_notify_called :
979
1018
who = message_fixture ['type' ]
980
- if who == 'stream' :
1019
+ if who == 'stream' and not is_stream_muted :
981
1020
target = 'PTEST -> Test'
982
1021
elif who == 'private' :
983
1022
target = 'you'
984
1023
if len (message_fixture ['display_recipient' ]) > 2 :
985
1024
target += ', Bar Bar'
1025
+ if target is not None :
986
1026
title = 'Test Organization Name:\n Foo Foo (to {})' .format (target )
987
1027
# TODO: Test message content too?
988
1028
notify .assert_called_once_with (title , mocker .ANY )
@@ -1770,6 +1810,34 @@ def set_from_list_of_dict(data):
1770
1810
update_left_panel .assert_called_once_with ()
1771
1811
model .controller .update_screen .assert_called_once_with ()
1772
1812
1813
+ @pytest .mark .parametrize ('event, final_desktop_notified_streams, ' , [
1814
+ (
1815
+ {'property' : 'desktop_notifications' ,
1816
+ 'op' : 'update' ,
1817
+ 'stream_id' : 19 ,
1818
+ 'value' : False },
1819
+ {15 }
1820
+ ),
1821
+ (
1822
+ {'property' : 'desktop_notifications' ,
1823
+ 'op' : 'update' ,
1824
+ 'stream_id' : 30 ,
1825
+ 'value' : True },
1826
+ {15 , 19 , 30 }
1827
+ )
1828
+ ], ids = [
1829
+ 'remove_19' , 'add_30'
1830
+ ])
1831
+ def test__handle_subscription_event_desktop_notified_streams (
1832
+ self , model , mocker , stream_button , event ,
1833
+ final_desktop_notified_streams ):
1834
+ model .desktop_notifs_enabled_streams = {15 , 19 }
1835
+ model ._handle_subscription_event (event )
1836
+
1837
+ assert (model .desktop_notifs_enabled_streams
1838
+ == final_desktop_notified_streams )
1839
+ model .controller .update_screen .assert_called_once_with ()
1840
+
1773
1841
@pytest .mark .parametrize (['event' , 'feature_level' ,
1774
1842
'stream_id' , 'expected_subscribers' ], [
1775
1843
({'op' : 'peer_add' , 'stream_id' : 99 , 'user_id' : 12 }, None ,
@@ -1903,6 +1971,20 @@ def test_is_muted_stream(self, muted_streams, stream_id, is_muted,
1903
1971
model .muted_streams = muted_streams
1904
1972
assert model .is_muted_stream (stream_id ) == is_muted
1905
1973
1974
+ @pytest .mark .parametrize (['desktop_notified_streams' ,
1975
+ 'stream_id' , 'is_enabled' ], [
1976
+ ({1 }, 1 , True ),
1977
+ ({1 , 3 , 7 }, 2 , False ),
1978
+ (set (), 1 , False ),
1979
+ ], ids = ['single_stream' , 'multiple_streams' , 'no_stream' ])
1980
+ def test_is_desktop_notifications_enabled (self , desktop_notified_streams ,
1981
+ stream_id , is_enabled ,
1982
+ stream_dict , model ):
1983
+ model .stream_dict = stream_dict
1984
+ model .desktop_notifs_enabled_streams = desktop_notified_streams
1985
+ assert (model .is_desktop_notifications_enabled (stream_id )
1986
+ == is_enabled )
1987
+
1906
1988
@pytest .mark .parametrize ('topic, is_muted' , [
1907
1989
((1 , 'stream muted & unmuted topic' ), False ),
1908
1990
((2 , 'muted topic' ), True ),
0 commit comments