-
Notifications
You must be signed in to change notification settings - Fork 2k
/
test_email_notifications.py
131 lines (105 loc) · 5.17 KB
/
test_email_notifications.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
import json
import email.mime.text
import ckan.model as model
import ckan.lib.email_notifications as email_notifications
import ckan.lib.base
import ckan.lib.mailer
import ckan.tests.pylons_controller
import ckan.tests.mock_mail_server as mock_mail_server
import paste
import pylons.test
class TestEmailNotifications(mock_mail_server.SmtpServerHarness,
ckan.tests.pylons_controller.PylonsTestCase):
@classmethod
def setup_class(cls):
mock_mail_server.SmtpServerHarness.setup_class()
ckan.tests.pylons_controller.PylonsTestCase.setup_class()
ckan.tests.CreateTestData.create()
cls.app = paste.fixture.TestApp(pylons.test.pylonsapp)
joeadmin = model.User.get('joeadmin')
cls.joeadmin = {'id': joeadmin.id,
'apikey': joeadmin.apikey,
}
@classmethod
def teardown_class(self):
mock_mail_server.SmtpServerHarness.teardown_class()
ckan.tests.pylons_controller.PylonsTestCase.teardown_class()
model.repo.rebuild_db()
def mime_encode(self, msg, recipient_name):
sender_name = ckan.lib.base.g.site_title
sender_url = ckan.lib.base.g.site_url
body = ckan.lib.mailer.add_msg_niceties(
recipient_name, msg, sender_name, sender_url)
encoded_body = email.mime.text.MIMEText(
body.encode('utf-8'), 'plain', 'utf-8').get_payload().strip()
return encoded_body
def test_01_no_email_notifications_after_registration(self):
'''A new user who isn't following anything shouldn't get any emails.'''
# Clear any emails already sent due to CreateTestData.create().
email_notifications.get_and_send_notifications_for_all_users()
self.clear_smtp_messages()
# Register a new user.
params = {'name': 'sara',
'email': 'sara@sararollins.com',
'password': 'sara',
'fullname': 'Sara Rollins',
}
extra_environ = {'Authorization': str(self.joeadmin['apikey'])}
response = self.app.post('/api/action/user_create',
params=json.dumps(params), extra_environ=extra_environ).json
assert response['success'] is True
# Save the user for later tests to use.
TestEmailNotifications.sara = response['result']
# No notification emails should be sent to anyone at this point.
email_notifications.get_and_send_notifications_for_all_users()
assert len(self.get_smtp_messages()) == 0
def test_02_one_new_activity(self):
'''A user with one new activity should get one email.'''
# Make Sara follow something, have to do this to get new activity.
params = {'id': 'warandpeace'}
extra_environ = {'Authorization': str(self.sara['apikey'])}
response = self.app.post('/api/action/follow_dataset',
params=json.dumps(params), extra_environ=extra_environ).json
assert response['success'] is True
# Make someone else update the dataset Sara's following, this should
# create a new activity on Sara's dashboard.
params = {'name': 'warandpeace', 'notes': 'updated'}
extra_environ = {'Authorization': str(self.joeadmin['apikey'])}
response = self.app.post('/api/action/package_update',
params=json.dumps(params), extra_environ=extra_environ).json
assert response['success'] is True
# Run the email notifier job, it should send one notification email
# to Sara.
email_notifications.get_and_send_notifications_for_all_users()
assert len(self.get_smtp_messages()) == 1
email = self.get_smtp_messages()[0]
assert email[1] == 'info@test.ckan.net'
assert email[2] == ['sara@sararollins.com']
assert 'Subject: =?utf-8?q?You_have_new_activity' in email[3]
encoded_body = self.mime_encode(
"You have new activity", "Sara Rollins")
assert encoded_body in email[3]
# TODO: Check that body contains link to dashboard and email prefs.
def test_03_multiple_new_activities(self):
'''Test that a user with multiple new activities gets just one email.
'''
def test_04_no_repeat_email_notifications(self):
'''Test that a user does not get a second email notification for the
same new activity.
'''
def test_05_no_email_notifications_when_disabled_site_wide(self):
'''Users should not get email notifications when the feature is
disabled site-wide by a sysadmin.'''
def test_06_enable_email_notifications_sitewide(self):
'''When a sysadamin enables email notifications site wide, users
should not get emails for new activities from before email
notifications were enabled.
'''
def test_07_no_email_notifications_when_disabled_by_user(self):
'''Users should not get email notifications when they have disabled
the feature in their user preferences.'''
def test_08_enable_email_notifications_by_user(self):
'''When a user enables email notifications in her user preferences,
she should not get emails for new activities from before email
notifications were enabled.
'''