def _send_auth_comment(self, user): data = {"name": "", "email": "", "followup": True, "reply_to": 0, "comment": "Es war einmal eine kleine...", "honeypot": ""} data.update(self.form.initial) response = post_comment(data, auth_user=user) self.assertEqual(response.status_code, 201)
def test_post_returns_2xx_response(self): data = {"name": "Bob", "email": "*****@*****.**", "followup": True, "reply_to": 0, "level": 1, "order": 1, "comment": "Es war einmal eine kleine...", "honeypot": ""} data.update(self.form.initial) response = post_comment(data) self.assertEqual(response.status_code, 204) self.assertEqual(self.mock_mailer.call_count, 1)
def test_post_returns_unauthorize_response(self): data = {"name": "Bob", "email": "*****@*****.**", "followup": True, "reply_to": 0, "level": 1, "order": 1, "comment": "Es war einmal eine kleine...", "honeypot": ""} data.update(self.form.initial) response = post_comment(data) self.assertEqual(response.status_code, 400) self.assertEqual(response.rendered_content, b'"User not authenticated"') self.assertEqual(self.mock_mailer.call_count, 0)
def test_post_returns_201_response(self): data = {"name": "Bob", "email": "*****@*****.**", "followup": True, "reply_to": 0, "level": 1, "order": 1, "comment": "Es war einmal eine kleine...", "honeypot": ""} data.update(self.form.initial) response = post_comment(data) self.assertEqual(response.status_code, 201) data = json.loads(response.rendered_content) self.assertTrue('id' in data) self.assertEqual(data['id'], 1) # id of the new created comment.
def test_post_returns_4xx_response(self): # It uses an authenticated user, but the user has no mail address. self.user = User.objects.create_user("bob", "", "pwd") data = {"name": "", "email": "", "followup": True, "reply_to": 0, "level": 1, "order": 1, "comment": "Es war einmal eine kleine...", "honeypot": ""} data.update(self.form.initial) response = post_comment(data, auth_user=self.user) self.assertEqual(response.status_code, 400) self.assertTrue('name' in response.data) self.assertTrue('email' in response.data) self.assertEqual(self.mock_mailer.call_count, 0)
def test_post_comment_as_visitor_before_connecting_signal(self): data = { "name": "Joe Bloggs", "email": "*****@*****.**", "followup": True, "reply_to": 0, "comment": "This post comment request should fail" } data.update(self.form.initial) response = post_comment(data) self.assertEqual(response.status_code, 403) self.assertEqual( response.rendered_content, b'{"detail":"You do not have permission to perform this action."}' ) self.assertTrue(self.mock_mailer.call_count == 0)
def test_post_reply_to_exceeds_max_thread_level_returns_400_code(self): self.assertEqual(settings.COMMENTS_XTD_MAX_THREAD_LEVEL, 0) self.assertEqual(XtdComment.objects.count(), 0) self.post_parent_comment() self.assertEqual(XtdComment.objects.count(), 1) data = {"name": "Bob", "email": "*****@*****.**", "followup": True, "reply_to": self.cm.id, # This exceeds max thread level. "comment": "Es war einmal eine kleine...", "honeypot": ""} data.update(self.form.initial) response = post_comment(data) self.assertEqual(XtdComment.objects.count(), 1) # Comment not added. self.assertEqual(response.status_code, 400)