def test_overriden_ajax_post(self):
view_name = 'thumber_tests:example_form'
path = reverse(view_name)
http_referer = 'http://example.com{0}'.format(path)
# Get the form view, and 'follow' so that the session cookie gets set on the client
response = self.client.get(path, follow=True)
self.assertIn(settings.SESSION_COOKIE_NAME, response.cookies)
# Post with thumber_token=ajax for a JSON response
data = {'satisfied': 'False', 'thumber_token': 'ajax'}
response = self.client.post(path, data, HTTP_REFERER=http_referer)
self.assertEquals(response['Content-Type'], 'application/json')
# Check we got a success message in our json
json = response.json()
self.assertIn('success', json)
self.assertEquals(json['success'], True)
pk = json['id']
self.assertEquals(Feedback.objects.count(), 1)
feedback = Feedback.objects.all()[0]
self.assertEquals(feedback.view_name, view_name)
self.assertEquals(feedback.url, http_referer)
self.assertEquals(feedback.satisfied, False)
# Resbumit now with the ID, and set the comment
data = {'thumber_token': 'ajax', 'id': pk, 'comment': 'test comment'}
response = self.client.post(path, data, HTTP_REFERER=http_referer)
self.assertEquals(response['Content-Type'], 'application/json')
json = response.json()
self.assertIn('success', json)
self.assertEquals(json['success'], True)
# There should still only be 1 model, and it shoudl now have the test comment
self.assertEquals(Feedback.objects.count(), 1)
feedback = Feedback.objects.all()[0]
self.assertEquals(feedback.comment, 'test comment')
评论列表
文章目录