def test_success_with_valid_lang(self): """If the specified language is valid, return an OK response.""" request = self.factory.post('/') data = {'email': '*****@*****.**', 'lang': 'pt-BR'} with patch('news.utils.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_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: newsletter_slugs.return_value = ['foo', 'baz'] response = update_user_task(request, SUBSCRIBE, {'newsletters': 'foo,bar'}) self.assert_response_error(response, 400, errors.BASKET_INVALID_NEWSLETTER)
def test_missing_email_and_sub(self): """ If both the email and subscriber are missing, return a 400 error. """ request = self.factory.post('/') response = update_user_task(request, SUBSCRIBE) self.assert_response_error(response, 400, errors.BASKET_USAGE_ERROR)
def test_invalid_accept_lang(self): """If accept_lang param is provided but invalid, return a 400.""" request = self.factory.post('/') data = {'email': '*****@*****.**', 'accept_lang': 'the dude minds, man'} response = update_user_task(request, SUBSCRIBE, data, sync=False) self.assert_response_error(response, 400, errors.BASKET_INVALID_LANGUAGE) self.assertFalse(self.update_user.delay.called) self.assertFalse(self.lookup_subscriber.called)
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: 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_success_with_request_data(self): """ If no data is provided, fall back to using the POST data from the request. """ data = {'email': '*****@*****.**'} request = self.factory.post('/', data) response = update_user_task(request, SUBSCRIBE, sync=False) self.assert_response_ok(response) self.update_user.delay.assert_called_with(data, '*****@*****.**', None, SUBSCRIBE, True)
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='*****@*****.**') response = update_user_task(request, SUBSCRIBE, {}, sync=False) self.assert_response_ok(response) self.update_user.delay.assert_called_with({}, '*****@*****.**', None, SUBSCRIBE, True)
def test_success_with_valid_newsletters(self): """ If the specified newsletters are valid, return an OK response. """ request = self.factory.post('/') data = {'email': '*****@*****.**', 'newsletters': 'foo,bar'} with patch('news.utils.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)
def test_accept_lang(self, get_best_language_mock): """If accept_lang param is provided, should set the lang in data.""" get_best_language_mock.return_value = 'pt' request = self.factory.post('/') data = {'email': '*****@*****.**', 'accept_lang': 'pt-pt,fr;q=0.8'} after_data = {'email': '*****@*****.**', 'lang': 'pt'} response = update_user_task(request, SUBSCRIBE, data, sync=False) self.assert_response_ok(response) self.update_user.delay.assert_called_with(after_data, '*****@*****.**', None, SUBSCRIBE, True) self.assertFalse(self.lookup_subscriber.called)
def test_success_with_sync_and_subscriber(self): """ If sync is True, and a subscriber is provided, do not call lookup_subscriber and return an OK response with the token and created == False. """ request = self.factory.post('/') request.subscriber = Mock(email='*****@*****.**', 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({}, '*****@*****.**', 'mytoken', SUBSCRIBE, True)
def test_success_no_sync(self): """ If sync is False, do not generate a token via lookup_subscriber and return an OK response without a token. """ request = self.factory.post('/') data = {'email': '*****@*****.**'} response = update_user_task(request, SUBSCRIBE, data, sync=False) self.assert_response_ok(response) self.update_user.delay.assert_called_with(data, '*****@*****.**', None, SUBSCRIBE, True) self.assertFalse(self.lookup_subscriber.called)
def test_success_with_sync_no_subscriber(self): """ If sync is True, and a subscriber is not provided, look them up with lookup_subscriber and return an OK response with the token and created from the fetched subscriber. """ request = self.factory.post('/') data = {'email': '*****@*****.**'} subscriber = Mock(email='*****@*****.**', token='mytoken') self.lookup_subscriber.return_value = subscriber, None, 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, '*****@*****.**', 'mytoken', SUBSCRIBE, True)
def test_lang_overrides_accept_lang(self, get_best_language_mock): """ If lang is provided it was from the user, and accept_lang isn't as reliable, so we should prefer lang. """ get_best_language_mock.return_value = 'pt-BR' request = self.factory.post('/') data = {'email': '*****@*****.**', 'lang': 'de', 'accept_lang': 'pt-BR'} response = update_user_task(request, SUBSCRIBE, data, sync=False) self.assert_response_ok(response) # basically asserts that the data['lang'] value wasn't changed. self.update_user.delay.assert_called_with(data, '*****@*****.**', None, SUBSCRIBE, True) self.assertFalse(self.lookup_subscriber.called)