Skip to content

Commit

Permalink
[#1635] Small email notifications tests refactoring
Browse files Browse the repository at this point in the history
Save some lines
  • Loading branch information
Sean Hammond committed Nov 28, 2012
1 parent 12cea9b commit f63a00c
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions ckan/tests/lib/test_email_notifications.py
Expand Up @@ -12,6 +12,18 @@
import pylons.test


def post(app, action, apikey=None, status=200, **kwargs):
params = json.dumps(kwargs)
response = app.post('/api/action/{0}'.format(action), params=params,
extra_environ={'Authorization': str(apikey)}, status=status)
if status in (200,):
assert response.json['success'] is True
return response.json['result']
else:
assert response.json['success'] is False
return response.json['error']


class TestEmailNotifications(mock_mail_server.SmtpServerHarness,
ckan.tests.pylons_controller.PylonsTestCase):

Expand Down Expand Up @@ -59,18 +71,12 @@ def test_01_no_email_notifications_after_registration(self):
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
sara = post(self.app, 'user_create', apikey=self.joeadmin['apikey'],
name='sara', email='sara@sararollins.com', password='sara',
fullname='Sara Rollins')

# Save the user for later tests to use.
TestEmailNotifications.sara = response['result']
TestEmailNotifications.sara = sara

# No notification emails should be sent to anyone at this point.
email_notifications.get_and_send_notifications_for_all_users()
Expand All @@ -80,19 +86,13 @@ 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
post(self.app, 'follow_dataset', apikey=self.sara['apikey'],
id='warandpeace')

# 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
post(self.app, 'package_update', apikey=self.joeadmin['apikey'],
name='warandpeace', notes='updated')

# Run the email notifier job, it should send one notification email
# to Sara.
Expand All @@ -111,12 +111,8 @@ def test_03_multiple_new_activities(self):
# Make someone else update the dataset Sara's following three times,
# this should create three new activities on Sara's dashboard.
for i in range(1, 4):
params = {'name': 'warandpeace',
'notes': 'updated {0} times'.format(i)}
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
post(self.app, 'package_update', apikey=self.joeadmin['apikey'],
name='warandpeace', notes='updated {0} times'.format(i))

# Run the email notifier job, it should send one notification email
# to Sara.
Expand Down

0 comments on commit f63a00c

Please sign in to comment.