-
Notifications
You must be signed in to change notification settings - Fork 16
/
test_bll_notifications_notify.py
91 lines (73 loc) · 2.83 KB
/
test_bll_notifications_notify.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
import re
import logging
from io import StringIO
from doajtest.helpers import DoajTestCase
from portality import models
from portality.bll import DOAJ
from portality.ui.messages import Messages
from portality.bll.exceptions import NoSuchObjectException, NoSuchPropertyException
# A regex string for searching the log entries
EMAIL_LOG_REGEX = 'template.*%s.*to:\[u{0,1}\'%s.*subject:.*%s'
class TestBLLNotifications(DoajTestCase):
def setUp(self):
super(TestBLLNotifications, 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)
def tearDown(self):
super(TestBLLNotifications, 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)
def test_01_notify(self):
acc = models.Account()
acc.set_id("testuser")
acc.set_email("test@example.com")
acc.save(blocking=True)
n = models.Notification()
n.who = "testuser"
n.long = "my message"
n.short = "short note"
n.action = "/test"
n.classification = "test_class"
n.created_by = "test:notify"
svc = DOAJ.notificationsService()
n = svc.notify(n)
assert n.id is not None
n2 = models.Notification.pull(n.id)
assert n2.who == "testuser"
assert n2.long == "my message"
assert n2.short == "short note"
assert n2.action == "/test"
assert n2.classification == "test_class"
assert n2.created_by == "test:notify"
assert not n2.is_seen()
# Use the captured info stream to get email send logs
info_stream_contents = self.info_stream.getvalue()
# check an email is sent
template = re.escape('email/notification_email.jinja2')
to = re.escape('test@example.com')
subject = n2.short
email_matched = re.search(
EMAIL_LOG_REGEX % (template, to, subject),
info_stream_contents,
re.DOTALL)
assert bool(email_matched)
def test_02_notify_errors(self):
n = models.Notification()
n.who = "testuser"
n.long = "my message"
n.short = "short note"
n.action = "/test"
n.classification = "test_class"
n.created_by = "test:notify"
svc = DOAJ.notificationsService()
with self.assertRaises(NoSuchObjectException):
n = svc.notify(n)
acc = models.Account()
acc.set_id("testuser")
acc.save(blocking=True)
with self.assertRaises(NoSuchPropertyException):
n = svc.notify(n)