-
Notifications
You must be signed in to change notification settings - Fork 16
/
test_account_created_email.py
85 lines (66 loc) · 3.26 KB
/
test_account_created_email.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
from portality import models
from portality import constants
from portality.bll import exceptions
from doajtest.helpers import DoajTestCase
from portality.events.consumers.account_created_email import AccountCreatedEmail
from doajtest.fixtures import AccountFixtureFactory
import uuid
from io import StringIO
import logging
import re
# A regex string for searching the log entries
email_log_regex = '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'
class TestAccountCreatedEmail(DoajTestCase):
def setUp(self):
super(TestAccountCreatedEmail, self).setUp()
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(TestAccountCreatedEmail, 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_consumes(self):
source = AccountFixtureFactory.make_publisher_source()
acc = models.Account(**source)
acc.clear_password()
reset_token = uuid.uuid4().hex
acc.set_reset_token(reset_token, 86400)
event = models.Event(constants.EVENT_ACCOUNT_CREATED, context={"account" : acc.data})
assert AccountCreatedEmail.consumes(event)
event = models.Event(constants.EVENT_ACCOUNT_CREATED)
assert not AccountCreatedEmail.consumes(event)
event = models.Event("test:event", context={"application" : "2345"})
assert not AccountCreatedEmail.consumes(event)
def test_consume_success(self):
self._make_and_push_test_context("/")
source = AccountFixtureFactory.make_publisher_source()
acc = models.Account(**source)
acc.clear_password()
reset_token = uuid.uuid4().hex
acc.set_reset_token(reset_token, 86400)
event = models.Event(constants.EVENT_ACCOUNT_CREATED, context={"account": acc.data})
AccountCreatedEmail.consume(event)
# 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
template = re.escape('account_created.jinja2')
to = re.escape(acc.email)
subject = "Directory of Open Access Journals - account created, please verify your email address"
email_matched = re.search(email_log_regex % (template, to, subject),
info_stream_contents,
re.DOTALL)
assert bool(email_matched)
assert len(re.findall(email_count_string, info_stream_contents)) == 1
def test_consume_fail(self):
source = AccountFixtureFactory.make_publisher_source()
acc = models.Account(**source)
# we don't set the reset token
event = models.Event(constants.EVENT_ACCOUNT_PASSWORD_RESET, context={"account": acc.data})
with self.assertRaises(exceptions.NoSuchPropertyException):
AccountCreatedEmail.consume(event)