Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Commit

Permalink
Bug 1065624: Fix tests. Add support for py.test.
Browse files Browse the repository at this point in the history
Remove nose et. al.
  • Loading branch information
pmac committed Jul 24, 2015
1 parent 7bac5f3 commit c2731ea
Show file tree
Hide file tree
Showing 17 changed files with 203 additions and 442 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ before_script:
- flake8 news
- mysql -e 'create database basket;'
- python manage.py migrate --noinput
script: coverage run manage.py test news
script: py.test --cov news
before_install:
- git submodule update --init --recursive
install:
- pip install -r requirements/compiled.txt -r requirements/dev.txt
- pip install coveralls
after_success:
# Report coverage results to coveralls.io
- pip install coveralls
- coveralls
notifications:
irc:
Expand Down
8 changes: 5 additions & 3 deletions news/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ def send_welcomes(user_data, newsletter_slugs, format):


@et_task
def confirm_user(token):
def confirm_user(token, user_data=None):
"""
Confirm any pending subscriptions for the user with this token.
Expand All @@ -654,9 +654,11 @@ def confirm_user(token):
:raises: BasketError for fatal errors, NewsletterException for retryable
errors.
"""
user_data = get_user_data(token=token)
if user_data is None:
raise BasketError(MSG_USER_NOT_FOUND)
user_data = get_user_data(token=token)

if user_data is None:
raise BasketError(MSG_USER_NOT_FOUND)

if user_data['confirmed']:
log.info('In confirm_user, user with token %s '
Expand Down
63 changes: 18 additions & 45 deletions news/tests/test__utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
from django.test.client import RequestFactory

from basket import errors
from mock import Mock, patch
from mock import patch

from news import tasks
from news.models import BlockedEmail
from news.tasks import SUBSCRIBE
from news.utils import (
Expand Down Expand Up @@ -51,11 +50,11 @@ class UpdateUserTaskTests(TestCase):
def setUp(self):
self.factory = RequestFactory()

patcher = patch('news.utils.get_or_create_user_data')
self.lookup_subscriber = patcher.start()
patcher = patch('news.views.get_or_create_user_data')
self.get_or_create_user_data = patcher.start()
self.addCleanup(patcher.stop)

patcher = patch.object(tasks, 'update_user')
patcher = patch('news.views.update_user')
self.update_user = patcher.start()
self.addCleanup(patcher.stop)

Expand All @@ -76,7 +75,7 @@ def test_invalid_newsletter(self):
"""If an invalid newsletter is given, return a 400 error."""
request = self.factory.post('/')

with patch('news.utils.newsletter_slugs') as newsletter_slugs:
with patch('news.views.newsletter_slugs') as newsletter_slugs:
newsletter_slugs.return_value = ['foo', 'baz']
response = update_user_task(request, SUBSCRIBE, {'newsletters': 'foo,bar'})

Expand All @@ -86,17 +85,16 @@ def test_invalid_lang(self):
"""If the given lang is invalid, return a 400 error."""
request = self.factory.post('/')

with patch('news.utils.language_code_is_valid') as mock_language_code_is_valid:
with patch('news.views.language_code_is_valid') as mock_language_code_is_valid:
mock_language_code_is_valid.return_value = False
response = update_user_task(request, SUBSCRIBE, {'lang': 'pt-BR'})

self.assert_response_error(response, 400, errors.BASKET_INVALID_LANGUAGE)
mock_language_code_is_valid.assert_called_with('pt-BR')

def test_missing_email_and_sub(self):
def test_missing_email(self):
"""
If both the email and subscriber are missing, return a 400
error.
If the email is missing, return a 400 error.
"""
request = self.factory.post('/')
response = update_user_task(request, SUBSCRIBE)
Expand All @@ -114,7 +112,7 @@ def test_success_no_sync(self):
response = update_user_task(request, SUBSCRIBE, data, sync=False)
self.assert_response_ok(response)
self.update_user.delay.assert_called_with(data, 'a@example.com', None, SUBSCRIBE, True)
self.assertFalse(self.lookup_subscriber.called)
self.assertFalse(self.get_or_create_user_data.called)

def test_success_with_valid_newsletters(self):
"""
Expand All @@ -123,7 +121,7 @@ def test_success_with_valid_newsletters(self):
request = self.factory.post('/')
data = {'email': 'a@example.com', 'newsletters': 'foo,bar'}

with patch('news.utils.newsletter_and_group_slugs') as newsletter_slugs:
with patch('news.views.newsletter_and_group_slugs') as newsletter_slugs:
newsletter_slugs.return_value = ['foo', 'bar']
response = update_user_task(request, SUBSCRIBE, data, sync=False)
self.assert_response_ok(response)
Expand All @@ -133,23 +131,11 @@ def test_success_with_valid_lang(self):
request = self.factory.post('/')
data = {'email': 'a@example.com', 'lang': 'pt-BR'}

with patch('news.utils.language_code_is_valid') as mock_language_code_is_valid:
with patch('news.views.language_code_is_valid') as mock_language_code_is_valid:
mock_language_code_is_valid.return_value = True
response = update_user_task(request, SUBSCRIBE, data, sync=False)
self.assert_response_ok(response)

def test_success_with_subscriber(self):
"""
If no email is given but a subscriber is, use the subscriber's
email.
"""
request = self.factory.post('/')
request.subscriber = Mock(email='a@example.com')
response = update_user_task(request, SUBSCRIBE, {}, sync=False)

self.assert_response_ok(response)
self.update_user.delay.assert_called_with({}, 'a@example.com', None, SUBSCRIBE, True)

def test_success_with_request_data(self):
"""
If no data is provided, fall back to using the POST data from
Expand All @@ -162,34 +148,21 @@ def test_success_with_request_data(self):
self.assert_response_ok(response)
self.update_user.delay.assert_called_with(data, 'a@example.com', None, SUBSCRIBE, True)

def test_success_with_sync_and_subscriber(self):
"""
If sync is True, and a subscriber is provided, do not call
get_or_create_user_data and return an OK response with the token and
created == False.
"""
request = self.factory.post('/')
request.subscriber = Mock(email='a@example.com', token='mytoken')
response = update_user_task(request, SUBSCRIBE, {}, sync=True)

self.assert_response_ok(response, token='mytoken', created=False)
self.update_user.delay.assert_called_with({}, 'a@example.com', 'mytoken', SUBSCRIBE, True)

def test_success_with_sync_no_subscriber(self):
def test_success_with_sync(self):
"""
If sync is True, and a subscriber is not provided, look them up
with get_or_create_user_data and return an OK response with the token
and created from the fetched subscriber.
If sync is True look up the user with get_or_create_user_data and
return an OK response with the token and created from the fetched subscriber.
"""
request = self.factory.post('/')
data = {'email': 'a@example.com'}
subscriber = Mock(email='a@example.com', token='mytoken')
self.lookup_subscriber.return_value = subscriber, None, True
self.get_or_create_user_data.return_value = {'token': 'mytoken',
'email': 'a@example.com'}, True

response = update_user_task(request, SUBSCRIBE, data, sync=True)

self.assert_response_ok(response, token='mytoken', created=True)
self.update_user.delay.assert_called_with(data, 'a@example.com', 'mytoken', SUBSCRIBE, True)
self.update_user.delay.assert_called_with(data, 'a@example.com', 'mytoken',
SUBSCRIBE, True)


class TestGetAcceptLanguages(TestCase):
Expand Down
5 changes: 2 additions & 3 deletions news/tests/test_backend_exacttarget.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from django.test.utils import override_settings

from mock import patch, Mock
from nose.tools import ok_

from news.backends.exacttarget import logged_in

Expand All @@ -20,12 +19,12 @@ def test_sandbox_wsdl(self, client_mock):
self.test_function(Mock(client=None))

call_args = client_mock.call_args
ok_(call_args[0][0].endswith('et-sandbox-wsdl.txt'))
assert call_args[0][0].endswith('et-sandbox-wsdl.txt')

@override_settings(EXACTTARGET_USE_SANDBOX=False)
def test_prod_wsdl(self, client_mock):
"""When not using sandbox should not use sandbox wsdl file."""
self.test_function(Mock(client=None))

call_args = client_mock.call_args
ok_(call_args[0][0].endswith('et-wsdl.txt'))
assert call_args[0][0].endswith('et-wsdl.txt')
Loading

0 comments on commit c2731ea

Please sign in to comment.