def test_non_ascii_email(self, update_user_mock): """Should be able to accept valid email including non-ascii chars.""" req = self.rf.post("/news/subscribe/", data={"email": "dude@黒川.日本", "newsletters": "firefox-os"}) views.subscribe(req) update_user_mock.assert_called_with( req, views.SUBSCRIBE, data={"email": u"dude@黒川.日本", "newsletters": "firefox-os"}, optin=False, sync=False )
def test_non_ascii_email(self, update_user_mock): """Should be able to accept valid email including non-ascii chars.""" req = self.rf.post('/news/subscribe/', data={'email': 'dude@黒川.日本', 'newsletters': 'firefox-os'}) views.subscribe(req) update_user_mock.assert_called_with(req, views.SUBSCRIBE, data={ 'email': u'dude@黒川.日本', 'newsletters': 'firefox-os', }, optin=False, sync=False)
def test_non_ascii_email_fxos_malformed_post(self, update_user_mock): """Should be able to parse data from the raw request body including non-ascii chars.""" req = self.rf.generic('POST', '/news/subscribe/', data='email=dude@黒川.日本&newsletters=firefox-os', content_type='text/plain; charset=UTF-8') views.subscribe(req) update_user_mock.assert_called_with(req, views.SUBSCRIBE, data={ 'email': u'dude@黒川.日本', 'newsletters': 'firefox-os', }, optin=False, sync=False)
def test_non_ascii_email_fxos_malformed_post(self, update_user_mock): """Should be able to parse data from the raw request body including non-ascii chars.""" req = self.rf.generic( "POST", "/news/subscribe/", data="email=dude@黒川.日本&newsletters=firefox-os", content_type="text/plain; charset=UTF-8", ) views.subscribe(req) update_user_mock.assert_called_with( req, views.SUBSCRIBE, data={"email": u"dude@黒川.日本", "newsletters": "firefox-os"}, optin=False, sync=False )
def test_empty_email_invalid(self, update_user_mock): """Should report an error for missing or empty value.""" req = self.rf.post("/news/subscribe/", data={"email": "", "newsletters": "firefox-os"}) resp = views.subscribe(req) resp_data = json.loads(resp.content) self.assertEqual(resp_data["status"], "error") self.assertEqual(resp_data["code"], errors.BASKET_INVALID_EMAIL) self.assertFalse(update_user_mock.called) # no email at all req = self.rf.post("/news/subscribe/", data={"newsletters": "firefox-os"}) resp = views.subscribe(req) resp_data = json.loads(resp.content) self.assertEqual(resp_data["status"], "error") self.assertEqual(resp_data["code"], errors.BASKET_USAGE_ERROR) self.assertFalse(update_user_mock.called)
def test_deals_with_broken_post_data(self, update_user_mock): """Should be able to parse data from the raw request body. FxOS sends POST requests with the wrong mime-type, so request.POST is never filled out. We should parse the raw request body to get the data until this is fixed in FxOS in bug 949170. """ req = self.rf.generic('POST', '/news/subscribe/', data='[email protected]&newsletters=firefox-os', content_type='text/plain; charset=UTF-8') self.assertFalse(bool(req.POST)) views.subscribe(req) update_user_mock.assert_called_with(req, views.SUBSCRIBE, data={ 'email': '*****@*****.**', 'newsletters': 'firefox-os', }, optin=False, sync=False)
def test_sync_ssl_required(self): """ If sync set to 'Y' and the request isn't HTTPS, return a 401. """ request = self.factory.post("/", {"newsletters": "asdf", "sync": "Y", "email": "*****@*****.**"}) request.is_secure = lambda: False response = views.subscribe(request) self.assert_response_error(response, 401, errors.BASKET_SSL_REQUIRED)
def test_empty_email_invalid(self, update_user_mock): """Should report an error for missing or empty value.""" req = self.rf.post('/news/subscribe/', data={'email': '', 'newsletters': 'firefox-os'}) resp = views.subscribe(req) resp_data = json.loads(resp.content) self.assertEqual(resp_data['status'], 'error') self.assertEqual(resp_data['code'], errors.BASKET_INVALID_EMAIL) self.assertFalse(update_user_mock.called) # no email at all req = self.rf.post('/news/subscribe/', data={'newsletters': 'firefox-os'}) resp = views.subscribe(req) resp_data = json.loads(resp.content) self.assertEqual(resp_data['status'], 'error') self.assertEqual(resp_data['code'], errors.BASKET_USAGE_ERROR) self.assertFalse(update_user_mock.called)
def test_sync_ssl_required(self): """ If sync set to 'Y' and the request isn't HTTPS, return a 401. """ request = self.factory.post('/', {'newsletters': 'asdf', 'sync': 'Y', 'email': '*****@*****.**'}) request.is_secure = lambda: False response = views.subscribe(request) self.assert_response_error(response, 401, errors.BASKET_SSL_REQUIRED)
def test_blocked_email(self, get_block_list_mock): """Test basic success case with no optin or sync.""" get_block_list_mock.return_value = ["example.com"] request_data = {"newsletters": "news,lets", "optin": "N", "sync": "N", "email": "*****@*****.**"} request = self.factory.post("/", request_data) response = views.subscribe(request) self.assertEqual(response.status_code, 200) self.assertFalse(self.update_user_task.called)
def test_blocked_email(self, get_block_list_mock): """Test basic success case with no optin or sync.""" get_block_list_mock.return_value = ['example.com'] request_data = {'newsletters': 'news,lets', 'optin': 'N', 'sync': 'N', 'email': '*****@*****.**'} request = self.factory.post('/', request_data) response = views.subscribe(request) self.assertEqual(response.status_code, 200) self.assertFalse(self.update_user_task.called)
def test_sync_invalid_api_key(self): """ If sync is set to 'Y' and the request has an invalid API key, return a 401. """ request = self.factory.post("/", {"newsletters": "asdf", "sync": "Y", "email": "*****@*****.**"}) request.is_secure = lambda: True self.has_valid_api_key.return_value = False response = views.subscribe(request) self.assert_response_error(response, 401, errors.BASKET_AUTH_ERROR) self.has_valid_api_key.assert_called_with(request)
def test_success(self): """Test basic success case with no optin or sync.""" request_data = {'newsletters': 'news,lets', 'optin': 'N', 'sync': 'N', 'email': '*****@*****.**'} request = self.factory.post('/', request_data) response = views.subscribe(request) self.assertEqual(response, self.update_user_task.return_value) self.validate_email.assert_called_with(request_data['email']) self.update_user_task.assert_called_with(request, SUBSCRIBE, data=request_data, optin=False, sync=False)
def test_optin_valid_api_key_required(self): """ If optin is 'Y' but the API key isn't valid, disable optin. """ request_data = {"newsletters": "asdf", "optin": "Y", "email": "*****@*****.**"} request = self.factory.post("/", request_data) request.is_secure = lambda: True self.has_valid_api_key.return_value = False response = views.subscribe(request) self.assertEqual(response, self.update_user_task.return_value) self.update_user_task.assert_called_with(request, SUBSCRIBE, data=request_data, optin=False, sync=False)
def test_success_sync_optin(self): """Test success case with optin and sync.""" request_data = {"newsletters": "news,lets", "optin": "Y", "sync": "Y", "email": "*****@*****.**"} request = self.factory.post("/", request_data) request.is_secure = lambda: True self.has_valid_api_key.return_value = True response = views.subscribe(request) self.has_valid_api_key.assert_called_with(request) self.assertEqual(response, self.update_user_task.return_value) self.validate_email.assert_called_with("*****@*****.**") self.update_user_task.assert_called_with(request, SUBSCRIBE, data=request_data, optin=True, sync=True)
def test_optin_ssl_required(self): """ If optin is 'Y' but the request isn't HTTPS, disable optin. """ request_data = {'newsletters': 'asdf', 'optin': 'Y', 'email': '*****@*****.**'} request = self.factory.post('/', request_data) request.is_secure = lambda: False self.has_valid_api_key.return_value = True response = views.subscribe(request) self.assertEqual(response, self.update_user_task.return_value) self.update_user_task.assert_called_with(request, SUBSCRIBE, data=request_data, optin=False, sync=False)
def test_deals_with_broken_post_data(self, update_user_mock): """Should be able to parse data from the raw request body. FxOS sends POST requests with the wrong mime-type, so request.POST is never filled out. We should parse the raw request body to get the data until this is fixed in FxOS in bug 949170. """ req = self.rf.generic( 'POST', '/news/subscribe/', data='[email protected]&newsletters=firefox-os', content_type='text/plain; charset=UTF-8') self.assertFalse(bool(req.POST)) views.subscribe(req) update_user_mock.assert_called_with(req, views.SUBSCRIBE, data={ 'email': '*****@*****.**', 'newsletters': 'firefox-os', }, optin=False, sync=False)
def test_email_validation_error(self): """ If validate_email raises an EmailValidationError, return an invalid email response. """ request_data = {'newsletters': 'asdf', 'email': '*****@*****.**'} request = self.factory.post('/', request_data) error = utils.EmailValidationError('blah') self.validate_email.side_effect = error with patch('news.views.invalid_email_response') as invalid_email_response: response = views.subscribe(request) self.assertEqual(response, invalid_email_response.return_value) self.validate_email.assert_called_with(request_data['email']) invalid_email_response.assert_called_with(error)
def test_success_sync_optin(self): """Test success case with optin and sync.""" request_data = {'newsletters': 'news,lets', 'optin': 'Y', 'sync': 'Y', 'email': '*****@*****.**'} request = self.factory.post('/', request_data) request.is_secure = lambda: True self.has_valid_api_key.return_value = True response = views.subscribe(request) self.has_valid_api_key.assert_called_with(request) self.assertEqual(response, self.update_user_task.return_value) self.validate_email.assert_called_with('*****@*****.**') self.update_user_task.assert_called_with(request, SUBSCRIBE, data=request_data, optin=True, sync=True)
def test_email_validation_error(self): """ If validate_email raises an EmailValidationError, return an invalid email response. """ request_data = {"newsletters": "asdf", "email": "*****@*****.**"} request = self.factory.post("/", request_data) error = utils.EmailValidationError("blah") self.validate_email.side_effect = error with patch("news.views.invalid_email_response") as invalid_email_response: response = views.subscribe(request) self.assertEqual(response, invalid_email_response.return_value) self.validate_email.assert_called_with(request_data["email"]) invalid_email_response.assert_called_with(error)
def test_success(self): """Test basic success case with no optin or sync.""" request_data = { "newsletters": "news,lets", "optin": "N", "sync": "N", "email": "*****@*****.**", "first_name": "The", "last_name": "Dude", } request = self.factory.post("/", request_data) response = views.subscribe(request) self.assertEqual(response, self.update_user_task.return_value) self.validate_email.assert_called_with(request_data["email"]) self.update_user_task.assert_called_with(request, SUBSCRIBE, data=request_data, optin=False, sync=False)
def test_newsletters_missing(self): """If the newsletters param is missing, return a 400.""" request = self.factory.post("/") response = views.subscribe(request) self.assert_response_error(response, 400, errors.BASKET_USAGE_ERROR)