5
5
from django .test import TestCase
6
6
from django .test .utils import override_settings
7
7
from django_factory_boy import auth
8
-
8
+ from journalmanager import models as jmodels
9
9
from journalmanager .tests import modelfactories
10
10
from editorialmanager import notifications
11
11
from . import modelfactories as editorial_modelfactories
@@ -21,7 +21,7 @@ def setUp(self):
21
21
self .editor = auth .UserF (is_active = True )
22
22
self .journal = modelfactories .JournalFactory .create (editor = self .editor )
23
23
self .issue = modelfactories .IssueFactory (journal = self .journal )
24
- self .expected_recipients = [self .editor , ]
24
+ self .expected_recipients = [self .editor . email , ]
25
25
self .expected_subject_sufix_by_action = {
26
26
'issue_add_no_replicated_board' : "Issue Board can't be replicated" ,
27
27
'issue_add_replicated_board' : "Issue has a new replicated board" ,
@@ -53,7 +53,27 @@ def test_each_action(self):
53
53
self .assertEqual (new_email .subject , expected_subject )
54
54
self .assertEqual (len (self .expected_recipients ), len (new_email .to ))
55
55
for recipient in self .expected_recipients :
56
- self .assertIn (recipient .email , new_email .to )
56
+ self .assertIn (recipient , new_email .to )
57
+
58
+ @override_settings (CELERY_EAGER_PROPAGATES_EXCEPTIONS = True , CELERY_ALWAYS_EAGER = True , BROKER_BACKEND = 'memory' )
59
+ def test_each_action_with_disable_notifications_for_editor (self ):
60
+ """
61
+ for each action, test notifications are sent, but editor will have disable notifications
62
+ """
63
+ editor_profile = self .editor .get_profile ()
64
+ editor_profile .email_notifications = False
65
+ editor_profile .save ()
66
+ # update self.expected_recipients
67
+ self .expected_recipients .remove (editor_profile .user .email )
68
+
69
+ for action in self .ACTIONS :
70
+ # with
71
+ expected_subject = self ._make_subject (action )
72
+ # when
73
+ result = notifications .issue_board_replica (issue = self .issue , action = action )
74
+ # then
75
+ # no mail sent, since the only recipient: editor, "choose" to not receive emails
76
+ self .assertIsNone (result )
57
77
58
78
59
79
class BoardMembersMessageTests (TestCase ):
@@ -75,13 +95,22 @@ def setUp(self):
75
95
self .librarian2 .groups .add (self .librarian_group )
76
96
self .librarian2 .save ()
77
97
98
+ self .collection = modelfactories .CollectionFactory .create ()
78
99
self .editor = auth .UserF (is_active = True )
79
100
self .journal = modelfactories .JournalFactory .create (editor = self .editor )
80
101
self .issue = modelfactories .IssueFactory (journal = self .journal )
81
102
self .board = editorial_modelfactories .EditorialBoardFactory .create (issue = self .issue )
82
103
self .member = editorial_modelfactories .EditorialMemberFactory .create (board = self .board )
83
104
84
- self .expected_recipients = [self .librarian1 , self .librarian2 , ]
105
+ # link journal to collection
106
+ jmodels .Membership .objects .create (journal = self .journal , collection = self .collection , created_by = auth .UserF (is_active = True ))
107
+
108
+ # link librarians and collection
109
+ self .collection .add_user (self .librarian1 )
110
+ self .collection .add_user (self .librarian2 )
111
+
112
+ self .expected_recipients = []
113
+ self .expected_bcc_recipients = [self .librarian1 .email , self .librarian2 .email , ]
85
114
self .expected_subject_sufix_by_action = {
86
115
'board_add_member' : "Member of the journal board, was added" ,
87
116
'board_edit_member' : "Member of the journal board, was edited" ,
@@ -99,7 +128,39 @@ def test_each_action(self):
99
128
for action in self .ACTIONS :
100
129
# with
101
130
expected_subject = self ._make_subject (action )
102
- message = ''
131
+ message = 'Audit Log change message goes here!'
132
+ email_count += 1
133
+ # when
134
+ result = notifications .board_members_send_email_by_action (self .member , self .editor , message , action )
135
+ # then
136
+ self .assertTrue (result )
137
+ self .assertEqual (len (mail .outbox ), email_count )
138
+ new_email = mail .outbox [- 1 ]
139
+ self .assertEqual (new_email .subject , expected_subject )
140
+ self .assertEqual (len (self .expected_recipients ), len (new_email .to ))
141
+ self .assertEqual (len (self .expected_bcc_recipients ), len (new_email .bcc ))
142
+ for recipient in self .expected_recipients :
143
+ self .assertIn (recipient , new_email .to )
144
+ for recipient in self .expected_bcc_recipients :
145
+ self .assertIn (recipient , new_email .bcc )
146
+
147
+
148
+ @override_settings (CELERY_EAGER_PROPAGATES_EXCEPTIONS = True , CELERY_ALWAYS_EAGER = True , BROKER_BACKEND = 'memory' )
149
+ def test_each_action_with_disable_notifications_for_one_librarian (self ):
150
+ """
151
+ for each action, test notifications are sent, but librarian2 will have disable notifications
152
+ """
153
+ librarian2_profile = self .librarian2 .get_profile ()
154
+ librarian2_profile .email_notifications = False
155
+ librarian2_profile .save ()
156
+ # remove it from expected_bcc_recipients
157
+ self .expected_bcc_recipients .remove (librarian2_profile .user .email )
158
+
159
+ email_count = 0
160
+ for action in self .ACTIONS :
161
+ # with
162
+ expected_subject = self ._make_subject (action )
163
+ message = 'Audit Log change message goes here!'
103
164
email_count += 1
104
165
# when
105
166
result = notifications .board_members_send_email_by_action (self .member , self .editor , message , action )
@@ -109,5 +170,8 @@ def test_each_action(self):
109
170
new_email = mail .outbox [- 1 ]
110
171
self .assertEqual (new_email .subject , expected_subject )
111
172
self .assertEqual (len (self .expected_recipients ), len (new_email .to ))
173
+ self .assertEqual (len (self .expected_bcc_recipients ), len (new_email .bcc ))
112
174
for recipient in self .expected_recipients :
113
- self .assertIn (recipient .email , new_email .to )
175
+ self .assertIn (recipient , new_email .to )
176
+ for recipient in self .expected_bcc_recipients :
177
+ self .assertIn (recipient , new_email .bcc )
0 commit comments