@@ -36,7 +36,7 @@ def model(self, mocker, initial_data, user_profile,
36
36
return_value = [])
37
37
mocker .patch ('zulipterminal.model.Model.'
38
38
'_stream_info_from_subscriptions' ,
39
- return_value = ({}, set (), [], []))
39
+ return_value = ({}, set (), [], [], set () ))
40
40
# NOTE: PATCH WHERE USED NOT WHERE DEFINED
41
41
self .classify_unread_counts = mocker .patch (
42
42
'zulipterminal.model.classify_unread_counts' ,
@@ -134,7 +134,7 @@ def test_init_InvalidAPIKey_response(self, mocker, initial_data):
134
134
return_value = [])
135
135
mocker .patch ('zulipterminal.model.Model.'
136
136
'_stream_info_from_subscriptions' ,
137
- return_value = ({}, set (), [], []))
137
+ return_value = ({}, set (), [], [], set () ))
138
138
self .classify_unread_counts = mocker .patch (
139
139
'zulipterminal.model.classify_unread_counts' ,
140
140
return_value = [])
@@ -156,7 +156,7 @@ def test_init_ZulipError_exception(self, mocker, initial_data,
156
156
return_value = [])
157
157
mocker .patch ('zulipterminal.model.Model.'
158
158
'_stream_info_from_subscriptions' ,
159
- return_value = ({}, set (), [], []))
159
+ return_value = ({}, set (), [], [], set () ))
160
160
self .classify_unread_counts = mocker .patch (
161
161
'zulipterminal.model.classify_unread_counts' ,
162
162
return_value = [])
@@ -585,7 +585,7 @@ def test_success_get_messages(self, mocker, messages_successful_response,
585
585
return_value = [])
586
586
mocker .patch ('zulipterminal.model.Model.'
587
587
'_stream_info_from_subscriptions' ,
588
- return_value = ({}, set (), [], []))
588
+ return_value = ({}, set (), [], [], set () ))
589
589
self .classify_unread_counts = mocker .patch (
590
590
'zulipterminal.model.classify_unread_counts' ,
591
591
return_value = [])
@@ -625,7 +625,7 @@ def test_get_message_false_first_anchor(
625
625
return_value = [])
626
626
mocker .patch ('zulipterminal.model.Model.'
627
627
'_stream_info_from_subscriptions' ,
628
- return_value = ({}, set (), [], []))
628
+ return_value = ({}, set (), [], [], set () ))
629
629
self .classify_unread_counts = mocker .patch (
630
630
'zulipterminal.model.classify_unread_counts' ,
631
631
return_value = [])
@@ -659,7 +659,7 @@ def test_fail_get_messages(self, mocker, error_response,
659
659
return_value = [])
660
660
mocker .patch ('zulipterminal.model.Model.'
661
661
'_stream_info_from_subscriptions' ,
662
- return_value = ({}, set (), [], []))
662
+ return_value = ({}, set (), [], [], set () ))
663
663
self .classify_unread_counts = mocker .patch (
664
664
'zulipterminal.model.classify_unread_counts' ,
665
665
return_value = [])
@@ -694,6 +694,29 @@ def test_toggle_stream_muted_status(self, mocker, model,
694
694
self .display_error_if_present .assert_called_once_with (response ,
695
695
self .controller )
696
696
697
+ @pytest .mark .parametrize ('initial_desktop_notified_streams, value' , [
698
+ ({315 }, True ),
699
+ ({205 , 315 }, False ),
700
+ (set (), True ),
701
+ ({205 }, False ),
702
+ ], ids = ['desktop_notify_enable_205' , 'desktop_notify_disable_205' ,
703
+ 'first_notify_enable_205' , 'last_notify_disable_205' ])
704
+ def test_toggle_stream_desktop_notification (
705
+ self , mocker , model , initial_desktop_notified_streams ,
706
+ value , response = {'result' : 'success' }):
707
+ model .desktop_notifs_enabled_streams = initial_desktop_notified_streams
708
+ model .client .update_subscription_settings .return_value = response
709
+ model .toggle_stream_desktop_notification (205 )
710
+ request = [{
711
+ 'stream_id' : 205 ,
712
+ 'property' : 'desktop_notifications' ,
713
+ 'value' : value
714
+ }]
715
+ (model .client .update_subscription_settings
716
+ .assert_called_once_with (request ))
717
+ self .display_error_if_present .assert_called_once_with (response ,
718
+ self .controller )
719
+
697
720
@pytest .mark .parametrize ('flags_before, expected_operator' , [
698
721
([], 'add' ),
699
722
(['starred' ], 'remove' ),
@@ -744,7 +767,7 @@ def test__update_initial_data_raises_exception(self, mocker, initial_data):
744
767
return_value = [])
745
768
mocker .patch ('zulipterminal.model.Model.'
746
769
'_stream_info_from_subscriptions' ,
747
- return_value = ({}, set (), [], []))
770
+ return_value = ({}, set (), [], [], set () ))
748
771
self .classify_unread_counts = mocker .patch (
749
772
'zulipterminal.model.classify_unread_counts' ,
750
773
return_value = [])
@@ -778,26 +801,32 @@ def test_get_all_users(self, mocker, initial_data, user_list, user_dict,
778
801
self .client .register .return_value = initial_data
779
802
mocker .patch ('zulipterminal.model.Model.'
780
803
'_stream_info_from_subscriptions' ,
781
- return_value = ({}, set (), [], []))
804
+ return_value = ({}, set (), [], [], set () ))
782
805
self .classify_unread_counts = mocker .patch (
783
806
'zulipterminal.model.classify_unread_counts' ,
784
807
return_value = [])
785
808
model = Model (self .controller )
786
809
assert model .user_dict == user_dict
787
810
assert model .users == user_list
788
811
789
- @pytest .mark .parametrize ('muted' , powerset ([1 , 2 , 99 , 1000 ]))
812
+ @pytest .mark .parametrize ('muted, desktop_notifs_enabled' , list (
813
+ zip (powerset ([1 , 2 , 99 , 1000 ]), powerset ([1 , 2 , 99 , 1000 ])))
814
+ )
790
815
def test__stream_info_from_subscriptions (self , initial_data , streams ,
791
- muted ):
792
- subs = [dict (entry , in_home_view = entry ['stream_id' ] not in muted )
816
+ muted , desktop_notifs_enabled ):
817
+ subs = [dict (entry , in_home_view = entry ['stream_id' ] not in muted ,
818
+ desktop_notifications = entry ['stream_id' ] in
819
+ desktop_notifs_enabled )
793
820
for entry in initial_data ['subscriptions' ]]
794
- by_id , muted_streams , pinned , unpinned = (
821
+ (by_id , muted_streams , pinned , unpinned ,
822
+ desktop_notifs_enabled_streams ) = (
795
823
Model ._stream_info_from_subscriptions (subs ))
796
824
assert len (by_id )
797
825
assert all (msg_id == msg ['stream_id' ] for msg_id , msg in by_id .items ())
798
826
assert muted_streams == muted
799
827
assert pinned == [] # FIXME generalize/parametrize
800
828
assert unpinned == streams # FIXME generalize/parametrize
829
+ assert desktop_notifs_enabled_streams == desktop_notifs_enabled
801
830
802
831
def test__handle_message_event_with_Falsey_log (self , mocker ,
803
832
model , message_fixture ):
@@ -984,50 +1013,61 @@ def test__update_topic_index(self, topic_name, topic_order_initial,
984
1013
assert model .index ['topics' ][86 ] == topic_order_final
985
1014
986
1015
# TODO: Ideally message_fixture would use standardized ids?
987
- @pytest .mark .parametrize (['user_id' , 'vary_each_msg' , 'stream_setting' ,
988
- 'types_when_notify_called' ], [
1016
+ @pytest .mark .parametrize (['user_id' , 'vary_each_msg' ,
1017
+ 'desktop_notification_status' ,
1018
+ 'types_when_notify_called' ,
1019
+ 'is_stream_muted' ], [
989
1020
(5140 , {'flags' : ['mentioned' , 'wildcard_mentioned' ]}, True ,
990
- []), # message_fixture sender_id is 5140
1021
+ [], False ), # message_fixture sender_id is 5140
991
1022
(5179 , {'flags' : ['mentioned' ]}, False ,
992
- ['stream' , 'private' ]),
1023
+ ['stream' , 'private' ], False ),
993
1024
(5179 , {'flags' : ['wildcard_mentioned' ]}, False ,
994
- ['stream' , 'private' ]),
1025
+ ['stream' , 'private' ], False ),
995
1026
(5179 , {'flags' : []}, True ,
996
- ['stream' ]),
1027
+ ['stream' ], False ),
997
1028
(5179 , {'flags' : []}, False ,
998
- ['private' ]),
1029
+ ['private' ], False ),
1030
+ (5140 , {'flags' : []}, True ,
1031
+ ['stream' ], True ),
1032
+ (5179 , {'flags' : ['mentioned' ]}, True ,
1033
+ ['stream' ], True )
999
1034
], ids = [
1000
1035
'not_notified_since_self_message' ,
1001
1036
'notified_stream_and_private_since_directly_mentioned' ,
1002
1037
'notified_stream_and_private_since_wildcard_mentioned' ,
1003
1038
'notified_stream_since_stream_has_desktop_notifications' ,
1004
1039
'notified_private_since_private_message' ,
1040
+ 'not_notified_stream_since_muted_stream' ,
1041
+ 'notified_muted_stream_since_directly_mentioned'
1005
1042
])
1006
1043
def test_notify_users_calling_msg_type (self , mocker , model ,
1007
1044
message_fixture ,
1008
1045
user_id ,
1009
1046
vary_each_msg ,
1010
- stream_setting ,
1047
+ desktop_notification_status ,
1048
+ is_stream_muted ,
1011
1049
types_when_notify_called ):
1012
1050
message_fixture .update (vary_each_msg )
1013
1051
model .user_id = user_id
1014
- if 'stream_id' in message_fixture :
1015
- model . stream_dict . update (
1016
- { message_fixture [ 'stream_id' ]:
1017
- { 'desktop_notifications' : stream_setting }}
1018
- )
1052
+ mocker . patch ( 'zulipterminal.model.Model.'
1053
+ 'is_desktop_notifications_enabled' ,
1054
+ return_value = desktop_notification_status )
1055
+ mocker . patch ( 'zulipterminal.model.Model.'
1056
+ 'is_muted_stream' , return_value = is_stream_muted )
1019
1057
notify = mocker .patch ('zulipterminal.model.notify' )
1020
1058
1021
1059
model .notify_user (message_fixture )
1022
1060
1061
+ target = None
1023
1062
if message_fixture ['type' ] in types_when_notify_called :
1024
1063
who = message_fixture ['type' ]
1025
- if who == 'stream' :
1064
+ if who == 'stream' and not is_stream_muted :
1026
1065
target = 'PTEST -> Test'
1027
1066
elif who == 'private' :
1028
1067
target = 'you'
1029
1068
if len (message_fixture ['display_recipient' ]) > 2 :
1030
1069
target += ', Bar Bar'
1070
+ if target is not None :
1031
1071
title = 'Test Organization Name:\n Foo Foo (to {})' .format (target )
1032
1072
# TODO: Test message content too?
1033
1073
notify .assert_called_once_with (title , mocker .ANY )
@@ -1822,6 +1862,34 @@ def set_from_list_of_dict(data):
1822
1862
update_left_panel .assert_called_once_with ()
1823
1863
model .controller .update_screen .assert_called_once_with ()
1824
1864
1865
+ @pytest .mark .parametrize ('event, final_desktop_notified_streams, ' , [
1866
+ (
1867
+ {'property' : 'desktop_notifications' ,
1868
+ 'op' : 'update' ,
1869
+ 'stream_id' : 19 ,
1870
+ 'value' : False },
1871
+ {15 }
1872
+ ),
1873
+ (
1874
+ {'property' : 'desktop_notifications' ,
1875
+ 'op' : 'update' ,
1876
+ 'stream_id' : 30 ,
1877
+ 'value' : True },
1878
+ {15 , 19 , 30 }
1879
+ )
1880
+ ], ids = [
1881
+ 'remove_19' , 'add_30'
1882
+ ])
1883
+ def test__handle_subscription_event_desktop_notified_streams (
1884
+ self , model , mocker , stream_button , event ,
1885
+ final_desktop_notified_streams ):
1886
+ model .desktop_notifs_enabled_streams = {15 , 19 }
1887
+ model ._handle_subscription_event (event )
1888
+
1889
+ assert (model .desktop_notifs_enabled_streams
1890
+ == final_desktop_notified_streams )
1891
+ model .controller .update_screen .assert_called_once_with ()
1892
+
1825
1893
@pytest .mark .parametrize (['event' , 'feature_level' ,
1826
1894
'stream_id' , 'expected_subscribers' ], [
1827
1895
({'op' : 'peer_add' , 'stream_id' : 99 , 'user_id' : 12 }, None ,
@@ -1979,6 +2047,20 @@ def test_is_muted_stream(self, muted_streams, stream_id, is_muted,
1979
2047
model .muted_streams = muted_streams
1980
2048
assert model .is_muted_stream (stream_id ) == is_muted
1981
2049
2050
+ @pytest .mark .parametrize (['desktop_notified_streams' ,
2051
+ 'stream_id' , 'is_enabled' ], [
2052
+ ({1 }, 1 , True ),
2053
+ ({1 , 3 , 7 }, 2 , False ),
2054
+ (set (), 1 , False ),
2055
+ ], ids = ['single_stream' , 'multiple_streams' , 'no_stream' ])
2056
+ def test_is_desktop_notifications_enabled (self , desktop_notified_streams ,
2057
+ stream_id , is_enabled ,
2058
+ stream_dict , model ):
2059
+ model .stream_dict = stream_dict
2060
+ model .desktop_notifs_enabled_streams = desktop_notified_streams
2061
+ assert (model .is_desktop_notifications_enabled (stream_id )
2062
+ == is_enabled )
2063
+
1982
2064
@pytest .mark .parametrize ('topic, is_muted' , [
1983
2065
((1 , 'stream muted & unmuted topic' ), False ),
1984
2066
((2 , 'muted topic' ), True ),
0 commit comments