From acacc8281a4c87149bb00550533c29a02ecc8296 Mon Sep 17 00:00:00 2001 From: Vincent Fretin Date: Sat, 20 Sep 2014 16:21:55 +0200 Subject: [PATCH] add a test for delete-own-comment view --- .../discussion/tests/test_comments_viewlet.py | 56 ++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/plone/app/discussion/tests/test_comments_viewlet.py b/plone/app/discussion/tests/test_comments_viewlet.py index 0aff711..6619b1a 100644 --- a/plone/app/discussion/tests/test_comments_viewlet.py +++ b/plone/app/discussion/tests/test_comments_viewlet.py @@ -26,7 +26,7 @@ from Products.CMFCore.utils import getToolByName from Products.CMFPlone.tests import dummy -from plone.app.testing import TEST_USER_ID, setRoles +from plone.app.testing import TEST_USER_ID, TEST_USER_NAME, setRoles from plone.app.testing import logout from plone.app.testing import login @@ -266,6 +266,60 @@ class TestCommentForm(unittest.TestCase): self.assertEqual(0, len([x for x in conversation.getComments()])) setRoles(self.portal, TEST_USER_ID, ['Manager']) + def test_delete_own_comment(self): + """Delete own comment as logged-in user. + """ + + # Allow discussion + self.portal.doc1.allow_discussion = True + self.viewlet = CommentsViewlet(self.context, self.request, None, None) + + def make_request(form={}): + request = TestRequest() + request.form.update(form) + alsoProvides(request, IFormLayer) + alsoProvides(request, IAttributeAnnotatable) + return request + + provideAdapter( + adapts=(Interface, IBrowserRequest), + provides=Interface, + factory=CommentForm, + name=u"comment-form" + ) + + # The form is submitted successfully, if the required text field is + # filled out + form_request = make_request(form={'form.widgets.text': u'bar'}) + + commentForm = getMultiAdapter( + (self.context, form_request), + name=u"comment-form" + ) + + commentForm.update() + data, errors = commentForm.extractData() # pylint: disable-msg=W0612 + self.assertEqual(len(errors), 0) + self.assertFalse(commentForm.handleComment(commentForm, "foo")) + + # Delete the last comment + conversation = IConversation(self.context) + comment = [x for x in conversation.getComments()][-1] + deleteView = getMultiAdapter( + (comment, self.request), + name=u"delete-own-comment" + ) + # try to delete last comment with johndoe + setRoles(self.portal, 'johndoe', ['Member']) + login(self.portal, 'johndoe') + self.assertRaises(Unauthorized, comment.restrictedTraverse, "@@delete-own-comment") + self.assertEqual(1, len([x for x in conversation.getComments()])) + # try to delete last comment with the same user that created it + login(self.portal, TEST_USER_NAME) + setRoles(self.portal, TEST_USER_ID, ['Member']) + deleteView() + self.assertEqual(0, len([x for x in conversation.getComments()])) + def test_add_anonymous_comment(self): self.portal.doc1.allow_discussion = True