-
Notifications
You must be signed in to change notification settings - Fork 16
/
test_application_processor_emails.py
1383 lines (1083 loc) · 67.4 KB
/
test_application_processor_emails.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
# FIXME: these tests are no longer as useful as they once were because we always send the notification template
# FIXME more: these tests are horribly broken and need a good going over for this notifications release 2022-07-21
import logging
import re
from io import StringIO
from copy import deepcopy
from portality import constants
from doajtest.fixtures import EditorGroupFixtureFactory, AccountFixtureFactory, ApplicationFixtureFactory, JournalFixtureFactory
from doajtest.helpers import DoajTestCase
from portality import models
from portality.forms.application_forms import ApplicationFormFactory, JournalFormFactory
from portality.bll import DOAJ
from doajtest.mocks.bll_notification import InterceptNotifications
from portality.events.consumers.application_assed_inprogress_notify import ApplicationAssedInprogressNotify
UPDATE_REQUEST_SOURCE_TEST_1 = ApplicationFixtureFactory.make_update_request_source()
UPDATE_REQUEST_SOURCE_TEST_2 = ApplicationFixtureFactory.make_update_request_source()
UPDATE_REQUEST_SOURCE_TEST_3 = ApplicationFixtureFactory.make_update_request_source()
APPLICATION_SOURCE_TEST_1 = ApplicationFixtureFactory.make_application_source()
APPLICATION_SOURCE_TEST_2 = ApplicationFixtureFactory.make_application_source()
APPLICATION_SOURCE_TEST_3 = ApplicationFixtureFactory.make_application_source()
APPLICATION_FORM = ApplicationFixtureFactory.make_application_form()
JOURNAL_SOURCE_TEST_1 = JournalFixtureFactory.make_journal_source()
JOURNAL_SOURCE_TEST_2 = JournalFixtureFactory.make_journal_source()
EDITOR_GROUP_SOURCE = EditorGroupFixtureFactory.make_editor_group_source()
EDITOR_SOURCE = AccountFixtureFactory.make_editor_source()
ASSED1_SOURCE = AccountFixtureFactory.make_assed1_source()
ASSED2_SOURCE = AccountFixtureFactory.make_assed2_source()
ASSED3_SOURCE = AccountFixtureFactory.make_assed3_source()
#####################################################################
# Mocks required to make some of the lookups work
#####################################################################
@classmethod
def editor_group_pull_by_key(cls, field, value):
eg = models.EditorGroup(**EDITOR_GROUP_SOURCE)
return eg
@classmethod
def editor_group_pull(self, _id):
return models.EditorGroup(**EDITOR_GROUP_SOURCE)
@classmethod
def editor_account_pull(self, _id):
if _id == 'eddie':
return models.Account(**EDITOR_SOURCE)
if _id == 'associate':
return models.Account(**ASSED1_SOURCE)
if _id == 'associate_2':
return models.Account(**ASSED2_SOURCE)
if _id == 'associate_3':
return models.Account(**ASSED3_SOURCE)
else:
return ACTUAL_ACCOUNT_PULL(_id)
ACTUAL_ACCOUNT_PULL = models.Account.pull
# A regex string for searching the log entries
email_log_regex = r'template.*%s.*to:\[u{0,1}\'%s.*subject:.*%s'
# A string present in each email log entry (for counting them)
email_count_string = 'Email template'
NOTIFICATIONS_INTERCEPT = []
class TestPublicApplicationEmails(DoajTestCase):
def setUp(self):
super(TestPublicApplicationEmails, self).setUp()
# Register a new log handler so we can inspect the info logs
self.info_stream = StringIO()
self.read_info = logging.StreamHandler(self.info_stream)
self.read_info.setLevel(logging.INFO)
self.app_test.logger.addHandler(self.read_info)
self.notifications_service = DOAJ.notificationsService
NOTIFICATIONS_INTERCEPT.clear()
DOAJ.notificationsService = lambda : InterceptNotifications(NOTIFICATIONS_INTERCEPT)
self.svc = DOAJ.notificationsService()
def tearDown(self):
super(TestPublicApplicationEmails, self).tearDown()
# Blank the info_stream and remove the error handler from the app
self.info_stream.truncate(0)
self.app_test.logger.removeHandler(self.read_info)
DOAJ.notificationsService = self.notifications_service
def test_01_public_application_email(self):
application = models.Application(**APPLICATION_SOURCE_TEST_1)
account = models.Account()
account.set_id("testing")
account.set_email("testing@example.com")
account.save(blocking=True)
application.set_owner(account.id)
# Construct an application form
fc = ApplicationFormFactory.context("public")
processor = fc.processor(source=application)
# Emails are sent during the finalise stage, and requires the app context to build URLs
with self.app_test.test_request_context():
processor.finalise(account)
# Use the captured info stream to get email send logs
info_stream_contents = self.info_stream.getvalue()
# We expect one email sent:
# * to the applicant, informing them the application was received
public_template = re.escape('notification_email.jinja2')
public_to = re.escape(account.email)
public_subject = re.escape("Directory of Open Access Journals - Your application (" + ", ".join(issn for issn in processor.source.bibjson().issns()) + ") to DOAJ has been received")
public_email_matched = re.search(email_log_regex % (public_template, public_to, public_subject),
info_stream_contents,
re.DOTALL)
assert bool(public_email_matched), info_stream_contents
assert len(re.findall(email_count_string, info_stream_contents)) == 1
class TestApplicationReviewEmails(DoajTestCase):
def setUp(self):
super(TestApplicationReviewEmails, self).setUp()
self.editor_group_pull_by_key = models.EditorGroup.pull_by_key
self.editor_group_pull = models.EditorGroup.pull
models.EditorGroup.pull_by_key = editor_group_pull_by_key
models.EditorGroup.pull = editor_group_pull
self.editor_account_pull = models.Account.pull
models.Account.pull = editor_account_pull
# Register a new log handler so we can inspect the info logs
self.info_stream = StringIO()
self.read_info = logging.StreamHandler(self.info_stream)
self.read_info.setLevel(logging.INFO)
self.app_test.logger.addHandler(self.read_info)
self.enable_publisher_email = self.app_test.config["ENABLE_PUBLISHER_EMAIL"]
self.app_test.config["ENABLE_PUBLISHER_EMAIL"] = True
self.notifications_service = DOAJ.notificationsService
NOTIFICATIONS_INTERCEPT.clear()
DOAJ.notificationsService = lambda: InterceptNotifications(NOTIFICATIONS_INTERCEPT)
self.svc = DOAJ.notificationsService()
def tearDown(self):
super(TestApplicationReviewEmails, self).tearDown()
models.EditorGroup.pull_by_key = self.editor_group_pull_by_key
models.EditorGroup.pull = self.editor_group_pull
models.Account.pull = self.editor_account_pull
# Blank the info_stream and remove the error handler from the app
self.info_stream.truncate(0)
self.app_test.logger.removeHandler(self.read_info)
self.app_test.config["ENABLE_PUBLISHER_EMAIL"] = self.enable_publisher_email
DOAJ.notificationsService = self.notifications_service
def test_01_maned_review_emails(self):
""" Ensure the Managing Editor's application review form sends the right emails"""
acc = models.Account()
acc.set_id("contextuser")
acc.add_role("admin")
ctx = self._make_and_push_test_context(acc=acc)
# If an application has been set to 'ready' but is returned to 'in progress',
# an email is sent to the editor and assigned associate editor
ready_application = models.Application(**APPLICATION_SOURCE_TEST_1)
ready_application.set_application_status(constants.APPLICATION_STATUS_READY)
ready_application.remove_current_journal()
owner = models.Account()
owner.set_id(ready_application.owner)
owner.set_name("Test Name")
owner.set_email("test@example.com")
owner.save(blocking=True)
# Construct an application form
fc = ApplicationFormFactory.context("admin")
processor = fc.processor(source=ready_application)
# Make changes to the application status via the form
processor.form.application_status.data = constants.APPLICATION_STATUS_IN_PROGRESS
# Emails are sent during the finalise stage, and requires the app context to build URLs
processor.finalise(acc)
# Use the captured info stream to get email send logs
info_stream_contents = self.info_stream.getvalue()
# Prove we went from to and from the right statuses
assert processor.source.application_status == constants.APPLICATION_STATUS_READY
assert processor.target.application_status == constants.APPLICATION_STATUS_IN_PROGRESS
# We expect two emails sent:
# * to the editor, informing them an application has been bounced from ready back to in progress.
# * to the associate editor, informing them the same
assert len(NOTIFICATIONS_INTERCEPT) == 2 # this will change when more of this file is run through the notifications system
### editor in progress notification and email
notification = NOTIFICATIONS_INTERCEPT.pop()
assert notification.who == "eddie"
assert notification.long is not None # this and action are hard to predict, so lets just check they are set
assert notification.action is not None
assert notification.classification == constants.NOTIFICATION_CLASSIFICATION_STATUS_CHANGE
editor_template = re.escape('notification_email.jinja2')
editor_to = re.escape('eddie@example.com')
editor_subject = re.escape("Application (" + ", ".join(issn for issn in processor.source.bibjson().issns()) + ") reverted to 'In Progress' by Managing Editor\n")
editor_email_matched = re.search(email_log_regex % (editor_template, editor_to, editor_subject),
info_stream_contents,
re.DOTALL)
assert bool(editor_email_matched)
### associate editor in progress notification and email
assert len(NOTIFICATIONS_INTERCEPT) == 1 # this will change when more of this file is run through the notifications system
notification = NOTIFICATIONS_INTERCEPT.pop()
assert notification.who == "associate"
assert notification.long is not None # this and action are hard to predict, so lets just check they are set
assert notification.action is not None
assert notification.classification == constants.NOTIFICATION_CLASSIFICATION_STATUS_CHANGE
assoc_editor_template = re.escape('email/notification_email.jinja2')
assoc_editor_to = re.escape('associate@example.com')
assoc_editor_subject = re.escape(self.svc.short_notification(ApplicationAssedInprogressNotify.ID).replace("{issns}", ", ".join(issn for issn in processor.source.bibjson().issns())) + "\n")# "an application assigned to you has not passed review."
assoc_editor_email_matched = re.search(email_log_regex % (assoc_editor_template, assoc_editor_to, assoc_editor_subject),
info_stream_contents,
re.DOTALL)
assert bool(assoc_editor_email_matched)
assert len(re.findall(email_count_string, info_stream_contents)) == 2
###
# Clear the stream for the next part
self.info_stream.truncate(0)
# If our ManEd is doing an editor's job - setting from 'completed' but is returned to 'in progress',
# an email is sent to the editor in charge of the application's group and assigned associate editor
completed_application = models.Suggestion(**APPLICATION_SOURCE_TEST_1)
completed_application.set_application_status(constants.APPLICATION_STATUS_COMPLETED)
# Construct an application form
fc = ApplicationFormFactory.context("admin")
processor = fc.processor(source=completed_application)
# fc = formcontext.ApplicationFormFactory.get_form_context(
# role="admin",
# source=completed_application
# )
# assert isinstance(fc, formcontext.ManEdApplicationReview)
# Make changes to the application status via the form
processor.form.application_status.data = constants.APPLICATION_STATUS_IN_PROGRESS
# Emails are sent during the finalise stage, and requires the app context to build URLs
processor.finalise(acc)
# Use the captured info stream to get email send logs
info_stream_contents = self.info_stream.getvalue()
# Prove we went from to and from the right statuses
assert processor.source.application_status == constants.APPLICATION_STATUS_COMPLETED
assert processor.target.application_status == constants.APPLICATION_STATUS_IN_PROGRESS
# We expect two emails sent:
# * to the editor, informing them an application has been bounced from completed back to in progress.
# * to the associate editor, informing them the same
assert len(NOTIFICATIONS_INTERCEPT) == 2 # this will change when more of this file is run through the notifications system
### editor in progress notification and email
notification = NOTIFICATIONS_INTERCEPT.pop()
assert notification.who == "eddie"
assert notification.long is not None # this and action are hard to predict, so lets just check they are set
assert notification.action is not None
assert notification.classification == constants.NOTIFICATION_CLASSIFICATION_STATUS_CHANGE
editor_template = re.escape('email/notification_email.jinja2')
editor_to = re.escape('eddie@example.com')
editor_subject = re.escape("Directory of Open Access Journals - Application ({}) reverted to 'In Progress' by Managing Editor".format(', '.join(issn for issn in processor.source.bibjson().issns())))
editor_email_matched = re.search(email_log_regex % (editor_template, editor_to, editor_subject),
info_stream_contents,
re.DOTALL)
assert bool(editor_email_matched)
### associate editor in progress notification and email
assert len(NOTIFICATIONS_INTERCEPT) == 1 # this will change when more of this file is run through the notifications system
notification = NOTIFICATIONS_INTERCEPT.pop()
assert notification.who == "associate"
assert notification.long is not None # this and action are hard to predict, so lets just check they are set
assert notification.action is not None
assert notification.classification == constants.NOTIFICATION_CLASSIFICATION_STATUS_CHANGE
assoc_editor_template = re.escape('email/notification_email.jinja2')
assoc_editor_to = re.escape('associate@example.com')
assoc_editor_subject = re.escape(self.svc.short_notification(ApplicationAssedInprogressNotify.ID).replace("{issns}", ", ".join(issn for issn in processor.source.bibjson().issns())) + "\n") # "an application assigned to you has not passed review."
assoc_editor_email_matched = re.search(
email_log_regex % (assoc_editor_template, assoc_editor_to, assoc_editor_subject),
info_stream_contents,
re.DOTALL)
assert bool(assoc_editor_email_matched)
assert len(re.findall(email_count_string, info_stream_contents)) == 2
###
# Clear the stream for the next part
self.info_stream.truncate(0)
# When an application is assigned to an associate editor for the first time, email the assoc_ed and publisher.
# Refresh the application form - create a fresh application without an editor assigned.
no_ed = deepcopy(APPLICATION_SOURCE_TEST_1)
del no_ed['admin']['editor']
del no_ed['admin']['related_journal']
no_ed['admin']['application_status'] = 'pending'
no_ed['admin']['application_type'] = 'new_application'
no_ed = models.Application(**no_ed)
fc = ApplicationFormFactory.context("admin")
processor = fc.processor(source=no_ed)
# fc = formcontext.ApplicationFormFactory.get_form_context(role="admin", source=no_ed)
# Assign the associate editor and save the form
processor.form.editor.data = "associate_3"
processor.finalise(acc)
info_stream_contents = self.info_stream.getvalue()
# check the associate was changed
assert processor.target.editor == "associate_3"
# We expect 2 emails to be sent:
# * to the AssEd who's been assigned,
# * and to the publisher informing them there's an editor assigned.
assEd_template = re.escape('email/notification_email.jinja2')
assEd_to = re.escape(models.Account.pull('associate_3').email)
assEd_subject = re.escape('Directory of Open Access Journals - New application ({}) assigned to you'.format(', '.join(issn for issn in processor.source.bibjson().issns())))
assEd_email_matched = re.search(email_log_regex % (assEd_template, assEd_to, assEd_subject),
info_stream_contents,
re.DOTALL)
assert bool(assEd_email_matched)
publisher_template = re.escape('email/notification_email.jinja2')
publisher_to = re.escape(owner.email)
publisher_subject = re.escape('Directory of Open Access Journals - Your application ({}) has been assigned to an editor for review'.format(', '.join(issn for issn in processor.source.bibjson().issns())))
publisher_email_matched = re.search(email_log_regex % (publisher_template, publisher_to, publisher_subject),
info_stream_contents,
re.DOTALL)
assert bool(publisher_email_matched)
assert len(re.findall(email_count_string, info_stream_contents)) == 2
# Clear the stream for the next part
self.info_stream.truncate(0)
# Refresh the application form
fc = ApplicationFormFactory.context("admin")
processor = fc.processor(source=ready_application)
# fc = formcontext.ApplicationFormFactory.get_form_context(role="admin", source=ready_application)
# Next, if we change the editor group or assigned editor, emails should be sent to editors, & NOT the publisher
processor.form.editor_group.data = "Test Editor Group"
processor.form.editor.data = "associate_3"
processor.finalise(acc)
info_stream_contents = self.info_stream.getvalue()
# check the associate was changed
assert processor.target.editor == "associate_3"
# We expect 2 emails to be sent:
# * to the editor of the assigned group,
# * to the AssEd who's been assigned
editor_template = re.escape('email/notification_email.jinja2')
editor_to = re.escape('eddie@example.com')
editor_subject = re.escape('Directory of Open Access Journals - New application ({}) assigned to your group'.format(', '.join(issn for issn in processor.source.bibjson().issns())))
editor_email_matched = re.search(email_log_regex % (editor_template, editor_to, editor_subject),
info_stream_contents,
re.DOTALL)
assert bool(editor_email_matched)
assEd_template = re.escape('email/notification_email.jinja2')
assEd_to = re.escape(models.Account.pull('associate_3').email)
assEd_subject = re.escape('Directory of Open Access Journals - New application ({}) assigned to you'.format(', '.join(issn for issn in processor.source.bibjson().issns())))
assEd_email_matched = re.search(email_log_regex % (assEd_template, assEd_to, assEd_subject),
info_stream_contents,
re.DOTALL)
assert bool(assEd_email_matched)
assert len(re.findall(email_count_string, info_stream_contents)) == 2
# Clear the stream for the next part
self.info_stream.truncate(0)
# A Managing Editor will notify other ManEds when they set an application to 'Ready'
# Refresh the application form
pending_application = models.Suggestion(**APPLICATION_SOURCE_TEST_2)
jid = pending_application.current_journal
journal_source = JOURNAL_SOURCE_TEST_1
journal_source["id"] = jid
journal_source['admin']["in_doaj"] = True
current_journal = models.Journal(**journal_source)
current_journal.save()
# Create a ManEd to be in charge of the Editor Group for this application
acc = models.Account(**AccountFixtureFactory.make_managing_editor_source())
acc.set_id('manny') # to match the Editor Group defaults # Fixme: better fixtures!
acc.save(blocking=True)
fc = ApplicationFormFactory.context("admin")
processor = fc.processor(source=pending_application)
# fc = formcontext.ApplicationFormFactory.get_form_context(role="admin", source=pending_application)
# Make changes to the application status via the form
processor.form.application_status.data = constants.APPLICATION_STATUS_READY
processor.finalise(acc)
info_stream_contents = self.info_stream.getvalue()
# We expect one email to be sent here:
# * to the ManEd in charge of the assigned Editor Group, saying an application is ready
manEd_template = re.escape('email/notification_email.jinja2')
manEd_to = re.escape(acc.email)
manEd_subject = re.escape('Directory of Open Access Journals - Application ({}) marked as ready'.format(', '.join(issn for issn in processor.source.bibjson().issns())))
manEd_email_matched = re.search(email_log_regex % (manEd_template, manEd_to, manEd_subject),
info_stream_contents,
re.DOTALL)
assert bool(manEd_email_matched)
assert len(re.findall(email_count_string, info_stream_contents)) == 1
# Clear the stream for the next part
self.info_stream.truncate(0)
# Finally, a Managing Editor will also trigger emails to the publisher when they accept an Application
# Refresh the application form
fc = ApplicationFormFactory.context("admin")
processor = fc.processor(source=no_ed)
processor.form.application_status.data = constants.APPLICATION_STATUS_ACCEPTED
processor.finalise(acc)
info_stream_contents = self.info_stream.getvalue()
# We expect 1 email to be sent:
# * to the publisher, informing them of the journal's acceptance
publisher_template = re.escape('email/notification_email.jinja2')
publisher_to = re.escape(owner.email)
publisher_subject = re.escape('Directory of Open Access Journals - Your journal ({}) has been accepted'.format(', '.join(issn for issn in processor.source.bibjson().issns())))
publisher_email_matched = re.search(email_log_regex % (publisher_template, publisher_to, publisher_subject),
info_stream_contents,
re.DOTALL)
assert bool(publisher_email_matched), (publisher_email_matched, info_stream_contents)
assert len(re.findall(email_count_string, info_stream_contents)) == 1
ctx.pop()
def test_02_ed_review_emails(self):
""" Ensure the Editor's application review form sends the right emails"""
acc = models.Account()
acc.set_id("contextuser")
acc.add_role("editor")
ctx = self._make_and_push_test_context(acc=acc)
# If an application has been set to 'ready' from another status, the ManEds are notified
pending_application = models.Suggestion(**APPLICATION_SOURCE_TEST_2)
pending_application.remove_current_journal()
owner = models.Account()
owner.set_id(pending_application.owner)
owner.set_name("Test Name")
owner.set_email("test@example.com")
owner.save(blocking=True)
maned = models.Account()
maned.set_id(EDITOR_GROUP_SOURCE.get("maned"))
maned.set_email("maned@example.com")
maned.save(blocking=True)
# Construct an application form
fc = ApplicationFormFactory.context("editor")
processor = fc.processor(source=pending_application)
# fc = formcontext.ApplicationFormFactory.get_form_context(
# role="editor",
# source=pending_application
# )
# assert isinstance(fc, formcontext.EditorApplicationReview)
# Make changes to the application status via the form
processor.form.application_status.data = constants.APPLICATION_STATUS_READY
processor.finalise()
info_stream_contents = self.info_stream.getvalue()
# We expect one email to be sent here:
# * to the ManEds, saying an application is ready
manEd_template = 'email/notification_email.jinja2'
manEd_to = re.escape("maned@example.com")
manEd_subject = re.escape('Application ({}) marked as ready'.format(', '.join(issn for issn in processor.source.bibjson().issns())))
manEd_email_matched = re.search(email_log_regex % (manEd_template, manEd_to, manEd_subject),
info_stream_contents,
re.DOTALL)
assert bool(manEd_email_matched)
assert len(re.findall(email_count_string, info_stream_contents)) == 1
# Clear the stream for the next part
self.info_stream.truncate(0)
# When an application is assigned to an associate editor for the first time, email the assoc_ed and publisher.
# Refresh the application form
no_ed = deepcopy(pending_application.data)
del no_ed['admin']['editor']
no_ed = models.Suggestion(**no_ed)
fc = ApplicationFormFactory.context("editor")
processor = fc.processor(source=no_ed)
# fc = formcontext.ApplicationFormFactory.get_form_context(role="admin", source=no_ed)
# Assign the associate editor and save the form
processor.form.editor.data = "associate_3"
processor.finalise()
info_stream_contents = self.info_stream.getvalue()
# check the associate was changed
assert processor.target.editor == "associate_3"
# We expect 2 emails to be sent:
# * to the AssEd who's been assigned,
# * and to the publisher informing them there's an editor assigned.
assEd_template = 'email/notification_email.jinja2'
assEd_to = re.escape(models.Account.pull('associate_3').email)
assEd_subject = re.escape('New application ({}) assigned to you'.format(', '.join(issn for issn in processor.source.bibjson().issns())))
assEd_email_matched = re.search(email_log_regex % (assEd_template, assEd_to, assEd_subject),
info_stream_contents,
re.DOTALL)
assert bool(assEd_email_matched)
publisher_template = 'email/notification_email.jinja2'
publisher_to = re.escape(owner.email)
publisher_subject = re.escape('Your update request ({}) has been assigned to an editor for review'.format(', '.join(issn for issn in processor.source.bibjson().issns())))
publisher_email_matched = re.search(email_log_regex % (publisher_template, publisher_to, publisher_subject),
info_stream_contents,
re.DOTALL)
assert bool(publisher_email_matched)
assert len(re.findall(email_count_string, info_stream_contents)) == 2
# Clear the stream for the next part
self.info_stream.truncate(0)
# Editors can also reassign applications to different associate editors.
fc = ApplicationFormFactory.context("editor")
processor = fc.processor(source=models.Suggestion(**APPLICATION_SOURCE_TEST_2))
# fc = formcontext.ApplicationFormFactory.get_form_context(role="editor", source=models.Suggestion(**APPLICATION_SOURCE_TEST_2))
# assert isinstance(fc, formcontext.EditorApplicationReview)
processor.form.editor.data = "associate_2"
processor.finalise()
info_stream_contents = self.info_stream.getvalue()
# check the associate was changed
assert processor.target.editor == "associate_2"
# We expect 1 email to be sent:
# * to the AssEd who's been assigned,
assEd_template = 'email/notification_email.jinja2'
assEd_to = re.escape(models.Account.pull('associate_2').email)
assEd_subject = re.escape('New application ({}) assigned to you'.format(', '.join(issn for issn in processor.source.bibjson().issns())))
assEd_email_matched = re.search(email_log_regex % (assEd_template, assEd_to, assEd_subject),
info_stream_contents,
re.DOTALL)
assert bool(assEd_email_matched)
assert len(re.findall(email_count_string, info_stream_contents)) == 1
# Clear the stream for the next part
self.info_stream.truncate(0)
# When an editor changes the state from 'completed' to 'in progress', the assigned associate is emailed.
completed_application = models.Suggestion(**UPDATE_REQUEST_SOURCE_TEST_2)
completed_application.set_application_status(constants.APPLICATION_STATUS_COMPLETED)
# Construct an application form
fc = ApplicationFormFactory.context("editor")
processor = fc.processor(source=completed_application)
# fc = formcontext.ApplicationFormFactory.get_form_context(
# role="editor",
# source=completed_application
# )
# assert isinstance(fc, formcontext.EditorApplicationReview)
# Make changes to the application status via the form
processor.form.application_status.data = constants.APPLICATION_STATUS_IN_PROGRESS
# Emails are sent during the finalise stage, and requires the app context to build URLs
processor.finalise()
# Use the captured info stream to get email send logs
info_stream_contents = self.info_stream.getvalue()
# Prove we went from to and from the right statuses
assert processor.source.application_status == constants.APPLICATION_STATUS_COMPLETED
assert processor.target.application_status == constants.APPLICATION_STATUS_IN_PROGRESS
# We expect two email to be sent:
# * to the associate editor, informing them the application has been bounced back to in progress.
# * to the editor telling them an application has reverted to in progress
assoc_editor_template = re.escape('email/notification_email.jinja2')
assoc_editor_to = re.escape('associate@example.com')
assoc_editor_subject = re.escape('One of your applications ({}) has not passed review'.format(', '.join(issn for issn in processor.source.bibjson().issns())))
assoc_editor_email_matched = re.search(
email_log_regex % (assoc_editor_template, assoc_editor_to, assoc_editor_subject),
info_stream_contents,
re.DOTALL)
assert bool(assoc_editor_email_matched)
assert len(re.findall(email_count_string, info_stream_contents)) == 2
ctx.pop()
def test_03_assoc_ed_review_emails(self):
""" Ensure the Associate Editor's application review form sends the right emails"""
acc = models.Account()
acc.set_id("contextuser")
acc.add_role("associate_editor")
ctx = self._make_and_push_test_context(acc=acc)
# If an application has been set to 'in progress' from 'pending', the publisher is notified
pending_application = models.Suggestion(**APPLICATION_SOURCE_TEST_3)
pending_application.remove_current_journal()
owner = models.Account()
owner.set_id(pending_application.owner)
owner.set_name("Test Name")
owner.set_email("test@example.com")
owner.save(blocking=True)
# Construct an application form
fc = ApplicationFormFactory.context("associate_editor")
processor = fc.processor(source=pending_application)
# Make changes to the application status via the form
processor.form.application_status.data = constants.APPLICATION_STATUS_IN_PROGRESS
processor.finalise()
info_stream_contents = self.info_stream.getvalue()
# We expect one email to be sent here:
# * to the publisher, notifying that an editor is viewing their application
publisher_template = re.escape('email/notification_email.jinja2')
publisher_to = re.escape(owner.email)
publisher_subject = re.escape('Directory of Open Access Journals - Your submission ({}) is under review'.format(', '.join(issn for issn in processor.source.bibjson().issns())))
publisher_email_matched = re.search(email_log_regex % (publisher_template, publisher_to, publisher_subject),
info_stream_contents,
re.DOTALL)
assert bool(publisher_email_matched)
assert len(re.findall(email_count_string, info_stream_contents)) == 1
# Clear the stream for the next part
self.info_stream.truncate(0)
# When the application is then set to 'completed', the editor in charge of this group is informed
processor.form.application_status.data = constants.APPLICATION_STATUS_COMPLETED
processor.finalise()
info_stream_contents = self.info_stream.getvalue()
# We expect one email sent:
# * to the editor, informing them an application has been completed by an Associate Editor
editor_template = re.escape('notification_email.jinja2')
editor_to = re.escape('eddie@example.com')
editor_subject = re.escape('Directory of Open Access Journals - Application ({}) marked as completed'.format(', '.join(issn for issn in processor.source.bibjson().issns())))
editor_email_matched = re.search(email_log_regex % (editor_template, editor_to, editor_subject),
info_stream_contents,
re.DOTALL)
assert bool(editor_email_matched)
assert len(re.findall(email_count_string, info_stream_contents)) == 1
ctx.pop()
class TestUpdateRequestReviewEmails(DoajTestCase):
def setUp(self):
super(TestUpdateRequestReviewEmails, self).setUp()
self.editor_group_pull_by_key = models.EditorGroup.pull_by_key
self.editor_group_pull = models.EditorGroup.pull
models.EditorGroup.pull_by_key = editor_group_pull_by_key
models.EditorGroup.pull = editor_group_pull
self.editor_account_pull = models.Account.pull
models.Account.pull = editor_account_pull
# Register a new log handler so we can inspect the info logs
self.info_stream = StringIO()
self.read_info = logging.StreamHandler(self.info_stream)
self.read_info.setLevel(logging.INFO)
self.app_test.logger.addHandler(self.read_info)
self.enable_publisher_email = self.app_test.config["ENABLE_PUBLISHER_EMAIL"]
self.app_test.config["ENABLE_PUBLISHER_EMAIL"] = True
self.notifications_service = DOAJ.notificationsService
NOTIFICATIONS_INTERCEPT.clear()
DOAJ.notificationsService = lambda: InterceptNotifications(NOTIFICATIONS_INTERCEPT)
self.svc = DOAJ.notificationsService()
def tearDown(self):
super(TestUpdateRequestReviewEmails, self).tearDown()
models.EditorGroup.pull_by_key = self.editor_group_pull_by_key
models.EditorGroup.pull = self.editor_group_pull
models.Account.pull = self.editor_account_pull
# Blank the info_stream and remove the error handler from the app
self.info_stream.truncate(0)
self.app_test.logger.removeHandler(self.read_info)
self.app_test.config["ENABLE_PUBLISHER_EMAIL"] = self.enable_publisher_email
def test_01_maned_review_emails(self):
""" Ensure the Managing Editor's application review form sends the right emails"""
acc = models.Account()
acc.set_id("contextuser")
acc.add_role("admin")
ctx = self._make_and_push_test_context(acc=acc)
# If an application has been set to 'ready' but is returned to 'in progress',
# an email is sent to the editor and assigned associate editor
ready_application = models.Suggestion(**UPDATE_REQUEST_SOURCE_TEST_1)
ready_application.set_application_status(constants.APPLICATION_STATUS_READY)
jid = ready_application.current_journal
journal_source = JOURNAL_SOURCE_TEST_1
journal_source["id"] = jid
journal_source['admin']["in_doaj"] = True
current_journal = models.Journal(**journal_source)
current_journal.save()
owner = models.Account()
owner.set_id(ready_application.owner)
owner.set_name("Test Name")
owner.set_email("test@example.com")
owner.save(blocking=True)
maned = models.Account()
maned.set_id(EDITOR_GROUP_SOURCE.get("maned"))
maned.set_email("maned@example.com")
maned.save(blocking=True)
# Construct an application form
fc = ApplicationFormFactory.context("admin")
processor = fc.processor(source=ready_application)
# fc = formcontext.ApplicationFormFactory.get_form_context(
# role="admin",
# source=ready_application
# )
# assert isinstance(fc, formcontext.ManEdApplicationReview)
# Make changes to the application status via the form
processor.form.application_status.data = constants.APPLICATION_STATUS_IN_PROGRESS
# Emails are sent during the finalise stage, and requires the app context to build URLs
processor.finalise(acc)
# Use the captured info stream to get email send logs
info_stream_contents = self.info_stream.getvalue()
# Prove we went from to and from the right statuses
assert processor.source.application_status == constants.APPLICATION_STATUS_READY
assert processor.target.application_status == constants.APPLICATION_STATUS_IN_PROGRESS
# We expect two emails sent:
# * to the editor, informing them an application has been bounced from ready back to in progress.
# * to the associate editor, informing them the same
assert len(
NOTIFICATIONS_INTERCEPT) == 2 # this will change when more of this file is run through the notifications system
### editor in progress notification and email
notification = NOTIFICATIONS_INTERCEPT.pop()
assert notification.who == "eddie"
assert notification.long is not None # this and action are hard to predict, so lets just check they are set
assert notification.action is not None
assert notification.classification == constants.NOTIFICATION_CLASSIFICATION_STATUS_CHANGE
editor_template = re.escape('email/notification_email.jinja2')
editor_to = re.escape('eddie@example.com')
editor_subject = re.escape("Application ({}) reverted to 'In Progress' by Managing Editor".format(', '.join(issn for issn in processor.source.bibjson().issns())))
editor_email_matched = re.search(email_log_regex % (editor_template, editor_to, editor_subject),
info_stream_contents,
re.DOTALL)
assert bool(editor_email_matched)
### associate editor in progress notification and email
assert len(
NOTIFICATIONS_INTERCEPT) == 1 # this will change when more of this file is run through the notifications system
notification = NOTIFICATIONS_INTERCEPT.pop()
assert notification.who == "associate"
assert notification.long is not None # this and action are hard to predict, so lets just check they are set
assert notification.action is not None
assert notification.classification == constants.NOTIFICATION_CLASSIFICATION_STATUS_CHANGE
assoc_editor_template = re.escape('email/notification_email.jinja2')
assoc_editor_to = re.escape('associate@example.com')
assoc_editor_subject = re.escape(self.svc.short_notification(
ApplicationAssedInprogressNotify.ID).replace("{issns}", ", ".join(issn for issn in processor.target.bibjson().issns())) + "\n") # "an application assigned to you has not passed review."
assoc_editor_email_matched = re.search(
email_log_regex % (assoc_editor_template, assoc_editor_to, assoc_editor_subject),
info_stream_contents,
re.DOTALL)
assert bool(assoc_editor_email_matched)
assert len(re.findall(email_count_string, info_stream_contents)) == 2
# Clear the stream for the next part
self.info_stream.truncate(0)
# If our ManEd is doing an editor's job - setting from 'completed' but is returned to 'in progress',
# an email is sent to the editor in charge of the application's group and assigned associate editor
completed_application = models.Suggestion(**UPDATE_REQUEST_SOURCE_TEST_1)
completed_application.set_application_status(constants.APPLICATION_STATUS_COMPLETED)
# Construct an application form
fc = ApplicationFormFactory.context("admin")
processor = fc.processor(source=completed_application)
# fc = formcontext.ApplicationFormFactory.get_form_context(
# role="admin",
# source=completed_application
# )
# assert isinstance(fc, formcontext.ManEdApplicationReview)
# Make changes to the application status via the form
processor.form.application_status.data = constants.APPLICATION_STATUS_IN_PROGRESS
# Emails are sent during the finalise stage, and requires the app context to build URLs
processor.finalise(acc)
# Use the captured info stream to get email send logs
info_stream_contents = self.info_stream.getvalue()
# Prove we went from to and from the right statuses
assert processor.source.application_status == constants.APPLICATION_STATUS_COMPLETED
assert processor.target.application_status == constants.APPLICATION_STATUS_IN_PROGRESS
# We expect two emails sent:
# * to the editor, informing them an application has been bounced from completed back to in progress.
# * to the associate editor, informing them the same
assert len(
NOTIFICATIONS_INTERCEPT) == 2 # this will change when more of this file is run through the notifications system
### editor in progress notification and email
notification = NOTIFICATIONS_INTERCEPT.pop()
assert notification.who == "eddie"
assert notification.long is not None # this and action are hard to predict, so lets just check they are set
assert notification.action is not None
assert notification.classification == constants.NOTIFICATION_CLASSIFICATION_STATUS_CHANGE
editor_template = re.escape('email/notification_email.jinja2')
editor_to = re.escape('eddie@example.com')
editor_subject = re.escape("Application ({}) reverted to 'In Progress' by Managing Editor".format(', '.join(issn for issn in processor.source.bibjson().issns())))
editor_email_matched = re.search(email_log_regex % (editor_template, editor_to, editor_subject),
info_stream_contents,
re.DOTALL)
assert bool(editor_email_matched)
### associate editor in progress notification and email
assert len(
NOTIFICATIONS_INTERCEPT) == 1 # this will change when more of this file is run through the notifications system
notification = NOTIFICATIONS_INTERCEPT.pop()
assert notification.who == "associate"
assert notification.long is not None # this and action are hard to predict, so lets just check they are set
assert notification.action is not None
assert notification.classification == constants.NOTIFICATION_CLASSIFICATION_STATUS_CHANGE
assoc_editor_template = re.escape('email/notification_email.jinja2')
assoc_editor_to = re.escape('associate@example.com')
assoc_editor_subject = re.escape(self.svc.short_notification(
ApplicationAssedInprogressNotify.ID).replace("{issns}", ", ".join(issn for issn in processor.source.bibjson().issns())) + "\n") # "an application assigned to you has not passed review."
assoc_editor_email_matched = re.search(
email_log_regex % (assoc_editor_template, assoc_editor_to, assoc_editor_subject),
info_stream_contents,
re.DOTALL)
assert bool(assoc_editor_email_matched)
assert len(re.findall(email_count_string, info_stream_contents)) == 2
# Clear the stream for the next part
self.info_stream.truncate(0)
# When an application is assigned to an associate editor for the first time, email the assoc_ed and publisher.
# Refresh the application form
no_ed = models.Suggestion(**deepcopy(ready_application.data))
del no_ed['admin']['editor']
fc = ApplicationFormFactory.context("admin")
processor = fc.processor(source=no_ed)
# fc = formcontext.ApplicationFormFactory.get_form_context(role="admin", source=no_ed)
# Assign the associate editor and save the form
processor.form.editor.data = "associate_3"
processor.finalise(acc)
info_stream_contents = self.info_stream.getvalue()
# check the associate was changed
assert processor.target.editor == "associate_3"
# We expect 2 emails to be sent:
# * to the AssEd who's been assigned,
# * and to the publisher informing them there's an editor assigned.
assEd_template = 'email/notification_email.jinja2'
assEd_to = re.escape(models.Account.pull('associate_3').email)
assEd_subject = re.escape('New application ({}) assigned to you'.format(', '.join(issn for issn in processor.source.bibjson().issns())))
assEd_email_matched = re.search(email_log_regex % (assEd_template, assEd_to, assEd_subject),
info_stream_contents,
re.DOTALL)
assert bool(assEd_email_matched)
publisher_template = 'email/notification_email.jinja2'
publisher_to = re.escape(owner.email)
publisher_subject = re.escape('Your update request ({}) has been assigned to an editor for review'.format(', '.join(issn for issn in processor.source.bibjson().issns())))
publisher_email_matched = re.search(email_log_regex % (publisher_template, publisher_to, publisher_subject),
info_stream_contents,
re.DOTALL)
assert bool(publisher_email_matched)
assert len(re.findall(email_count_string, info_stream_contents)) == 2
# Clear the stream for the next part
self.info_stream.truncate(0)
# Refresh the application form
fc = ApplicationFormFactory.context("admin")
processor = fc.processor(source=ready_application)
# fc = formcontext.ApplicationFormFactory.get_form_context(role="admin", source=ready_application)
# Next, if we change the editor group or assigned editor, emails should be sent to editors, & NOT the publisher
processor.form.editor_group.data = "Test Editor Group"
processor.form.editor.data = "associate_3"
processor.finalise(acc)
info_stream_contents = self.info_stream.getvalue()
# check the associate was changed
assert processor.target.editor == "associate_3"
# We expect 2 emails to be sent:
# * to the editor of the assigned group,
# * to the AssEd who's been assigned
editor_template = re.escape('email/notification_email.jinja2')
editor_to = re.escape('eddie@example.com')
editor_subject = re.escape('New application ({}) assigned to your group'.format(', '.join(issn for issn in processor.source.bibjson().issns())))
editor_email_matched = re.search(email_log_regex % (editor_template, editor_to, editor_subject),
info_stream_contents,
re.DOTALL)
assert bool(editor_email_matched)
assEd_template = 'email/notification_email.jinja2'
assEd_to = re.escape(models.Account.pull('associate_3').email)
assEd_subject = re.escape('New application ({}) assigned to you'.format(', '.join(issn for issn in processor.source.bibjson().issns())))
assEd_email_matched = re.search(email_log_regex % (assEd_template, assEd_to, assEd_subject),
info_stream_contents,
re.DOTALL)
assert bool(assEd_email_matched)
assert len(re.findall(email_count_string, info_stream_contents)) == 2
# Clear the stream for the next part
self.info_stream.truncate(0)
# A Managing Editor will notify other ManEds when they set an application to 'Ready'
# Refresh the application form
pending_application = models.Suggestion(**UPDATE_REQUEST_SOURCE_TEST_2)
fc = ApplicationFormFactory.context("admin")
processor = fc.processor(source=pending_application)
# fc = formcontext.ApplicationFormFactory.get_form_context(role="admin", source=pending_application)
# Make changes to the application status via the form