def test_post(self): """ POSTing without AJAX should redirect to self.url on success and render self.template on error. """ with self.activate('en-US'): # test non-AJAX POST with valid form data request = self.factory.post(self.url, self.post_data) response = views.process_content_services_form( request, self.template, self.view) # should redirect to success URL self.assertEqual(response.status_code, 302) self.assertIn(self.url, response._headers['location'][1]) self.assertIn('text/html', response._headers['content-type'][1]) # test non-AJAX POST with invalid form data request = self.factory.post(self.url, self.invalid_post_data) # locale is not getting set via self.activate above...? request.locale = 'en-US' response = views.process_content_services_form( request, self.template, self.view) self.assertEqual(response.status_code, 200) self.assertIn('text/html', response._headers['content-type'][1])
def test_post(self): """ POSTing without AJAX should redirect to self.url on success and render self.template on error. """ with self.activate('en-US'): # test non-AJAX POST with valid form data request = self.factory.post(self.url, self.post_data) response = views.process_content_services_form(request, self.template, self.view) # should redirect to success URL self.assertEqual(response.status_code, 302) self.assertIn(self.url, response._headers['location'][1]) self.assertIn('text/html', response._headers['content-type'][1]) # test non-AJAX POST with invalid form data request = self.factory.post(self.url, self.invalid_post_data) # locale is not getting set via self.activate above...? request.locale = 'en-US' response = views.process_content_services_form(request, self.template, self.view) self.assertEqual(response.status_code, 200) self.assertIn('text/html', response._headers['content-type'][1])
def test_post_ajax_error_xss(self): """ POSTing with AJAX should return sanitized error messages. Bug 945845. """ with self.activate('en-US'): # test AJAX POST with valid form data post_data = self.post_data.copy() post_data['industry'] = '"><img src=x onerror=alert(1);>' escaped_data = '"><img src=x onerror=alert(1);>' request = self.factory.post(self.url, post_data, HTTP_X_REQUESTED_WITH='XMLHttpRequest') response = views.process_content_services_form( request, self.template, self.view) # decode JSON response resp_data = json.loads(response.content) self.assertEqual(resp_data['msg'], 'Form invalid') self.assertEqual(response.status_code, 400) self.assertTrue(post_data['industry'] not in resp_data['errors'] ['industry'][0]) self.assertTrue(escaped_data in resp_data['errors']['industry'][0]) self.assertEqual(response._headers['content-type'][1], 'application/json') ok_(not self.requests_mock.called)
def test_post_ajax_error_xss(self): """ POSTing with AJAX should return sanitized error messages. Bug 945845. """ with self.activate('en-US'): # test AJAX POST with valid form data post_data = self.post_data.copy() post_data['industry'] = '"><img src=x onerror=alert(1);>' escaped_data = '"><img src=x onerror=alert(1);>' request = self.factory.post(self.url, post_data, HTTP_X_REQUESTED_WITH='XMLHttpRequest') response = views.process_content_services_form(request, self.template, self.view) # decode JSON response resp_data = json.loads(response.content) self.assertEqual(resp_data['msg'], 'Form invalid') self.assertEqual(response.status_code, 400) self.assertTrue(post_data['industry'] not in resp_data['errors']['industry'][0]) self.assertTrue(escaped_data in resp_data['errors']['industry'][0]) self.assertEqual(response._headers['content-type'][1], 'application/json') ok_(not self.requests_mock.called)
def test_post_ajax_no_state(self): """ State field required if country is 'us'. """ with self.activate('en-US'): # test AJAX POST with non-us country self.post_data['state'] = '' self.post_data['country'] = 'de' request = self.factory.post(self.url, self.post_data, HTTP_X_REQUESTED_WITH='XMLHttpRequest') response = views.process_content_services_form( request, self.template, self.view) # decode JSON response resp_data = json.loads(response.content) self.assertEqual(resp_data['msg'], 'ok') self.assertEqual(response.status_code, 200) self.assertEqual(response._headers['content-type'][1], 'application/json') ok_(self.requests_mock.called) self.requests_mock.reset_mock() # test AJAX POST with us country and no state self.post_data['state'] = '' self.post_data['country'] = 'us' request = self.factory.post(self.url, self.post_data, HTTP_X_REQUESTED_WITH='XMLHttpRequest') response = views.process_content_services_form( request, self.template, self.view) # decode JSON response resp_data = json.loads(response.content) self.assertEqual(resp_data['msg'], 'Form invalid') self.assertEqual(response.status_code, 400) self.assertTrue('state' in resp_data['errors']['__all__'][0]) self.assertEqual(response._headers['content-type'][1], 'application/json') ok_(not self.requests_mock.called)
def test_get(self): """ A GET request should simply return a 200. """ request = self.factory.get(self.url) request.locale = 'en-US' response = views.process_content_services_form(request, self.template, self.view) self.assertEqual(response.status_code, 200)
def test_post_ajax_no_state(self): """ State field required if country is 'us'. """ with self.activate('en-US'): # test AJAX POST with non-us country self.post_data['state'] = '' self.post_data['country'] = 'de' request = self.factory.post(self.url, self.post_data, HTTP_X_REQUESTED_WITH='XMLHttpRequest') response = views.process_content_services_form(request, self.template, self.view) # decode JSON response resp_data = json.loads(response.content) self.assertEqual(resp_data['msg'], 'ok') self.assertEqual(response.status_code, 200) self.assertEqual(response._headers['content-type'][1], 'application/json') ok_(self.requests_mock.called) self.requests_mock.reset_mock() # test AJAX POST with us country and no state self.post_data['state'] = '' self.post_data['country'] = 'us' request = self.factory.post(self.url, self.post_data, HTTP_X_REQUESTED_WITH='XMLHttpRequest') response = views.process_content_services_form(request, self.template, self.view) # decode JSON response resp_data = json.loads(response.content) self.assertEqual(resp_data['msg'], 'Form invalid') self.assertEqual(response.status_code, 400) self.assertTrue('state' in resp_data['errors']['__all__'][0]) self.assertEqual(response._headers['content-type'][1], 'application/json') ok_(not self.requests_mock.called)
def test_get(self): """ A GET request should simply return a 200. """ request = self.factory.get(self.url) request.locale = 'en-US' response = views.process_content_services_form(request, self.template, self.view) self.assertEqual(response.status_code, 200)
def test_post_ajax(self): """ POSTing with AJAX should return success/error JSON. """ with self.activate('en-US'): # test AJAX POST with valid form data request = self.factory.post(self.url, self.post_data, HTTP_X_REQUESTED_WITH='XMLHttpRequest') response = views.process_content_services_form( request, self.template, self.view) # decode JSON response resp_data = json.loads(response.content) self.assertEqual(resp_data['msg'], 'ok') self.assertEqual(response.status_code, 200) self.assertEqual(response._headers['content-type'][1], 'application/json') ok_(self.requests_mock.called) # test AJAX POST with invalid form data request = self.factory.post(self.url, self.invalid_post_data, HTTP_X_REQUESTED_WITH='XMLHttpRequest') response = views.process_content_services_form( request, self.template, self.view) # decode JSON response resp_data = json.loads(response.content) self.assertEqual(resp_data['msg'], 'Form invalid') self.assertEqual(response.status_code, 400) self.assertTrue('email' in resp_data['errors']) self.assertEqual(response._headers['content-type'][1], 'application/json')
def test_post_ajax(self): """ POSTing with AJAX should return success/error JSON. """ with self.activate('en-US'): # test AJAX POST with valid form data request = self.factory.post(self.url, self.post_data, HTTP_X_REQUESTED_WITH='XMLHttpRequest') response = views.process_content_services_form(request, self.template, self.view) # decode JSON response resp_data = json.loads(response.content) self.assertEqual(resp_data['msg'], 'ok') self.assertEqual(response.status_code, 200) self.assertEqual(response._headers['content-type'][1], 'application/json') ok_(self.requests_mock.called) # test AJAX POST with invalid form data request = self.factory.post(self.url, self.invalid_post_data, HTTP_X_REQUESTED_WITH='XMLHttpRequest') response = views.process_content_services_form(request, self.template, self.view) # decode JSON response resp_data = json.loads(response.content) self.assertEqual(resp_data['msg'], 'Form invalid') self.assertEqual(response.status_code, 400) self.assertTrue('email' in resp_data['errors']) self.assertEqual(response._headers['content-type'][1], 'application/json')
def test_post_ajax_honeypot(self): """ POSTing with AJAX and honeypot should return success JSON. """ with self.activate('en-US'): self.post_data['office_fax'] = 'what is this?' request = self.factory.post(self.url, self.post_data, HTTP_X_REQUESTED_WITH='XMLHttpRequest') response = views.process_content_services_form(request, self.template, self.view) # decode JSON response resp_data = json.loads(response.content) self.assertEqual(resp_data['msg'], 'ok') self.assertEqual(response.status_code, 200) self.assertEqual(response._headers['content-type'][1], 'application/json') ok_(not self.requests_mock.called)
def test_post_ajax_honeypot(self): """ POSTing with AJAX and honeypot should return success JSON. """ with self.activate('en-US'): self.post_data['office_fax'] = 'what is this?' request = self.factory.post(self.url, self.post_data, HTTP_X_REQUESTED_WITH='XMLHttpRequest') response = views.process_content_services_form(request, self.template, self.view) # decode JSON response resp_data = json.loads(response.content) self.assertEqual(resp_data['msg'], 'ok') self.assertEqual(response.status_code, 200) self.assertEqual(response._headers['content-type'][1], 'application/json') ok_(not self.requests_mock.called)
def _req(form_kwargs): request = self.factory.post(self.url, self.post_data) views.process_content_services_form(request, self.template, self.view, {}, form_kwargs) return self.requests_mock.call_args[0][1]['lead_source']
def _req(form_kwargs): request = self.factory.post(self.url, self.post_data) views.process_content_services_form(request, self.template, self.view, {}, form_kwargs) return self.requests_mock.call_args[0][1]['lead_source']