source: products/quintagroup.plonecomments/trunk/quintagroup/plonecomments/tests/testQPloneCommentsNotification.py @ 3112

Last change on this file since 3112 was 3112, checked in by kroman0, 13 years ago

Merged fixes for pyflakes and pylint

File size: 9.5 KB
Line 
1#
2# Test configuration form working
3#
4import re
5from quintagroup.plonecomments.tests.helperNotify import setProperties, \
6    testMailExistance
7from quintagroup.plonecomments.utils import getMsg
8from quintagroup.plonecomments.tests.base import FunctionalTestCase
9from quintagroup.plonecomments.tests.config import PROPERTY_SHEET
10from Products.CMFPlone.tests.utils import MockMailHost
11
12REXP_TO = re.compile("To:\s*(.*?)$",re.M)
13REXP_SUBJ = re.compile("Subject:\s*(.*?)$",re.M)
14
15
16class TestNotification(FunctionalTestCase):
17
18    def setApprovePublished(self, swithA=1,swithP=1):
19        self.prefs._updateProperty('enable_approve_notification', swithA)
20        self.prefs._updateProperty('enable_published_notification', swithP)
21
22    def beforeTearDown(self):
23        self.portal.MailHost = self.portal._original_MailHost
24
25    def afterSetUp(self):
26        self.portal._original_MailHost = self.portal.MailHost
27        self.portal.MailHost = mailhost = MockMailHost('MailHost')
28
29        self.loginAsPortalOwner()
30
31        # VERY IMPORTANT to guarantee product skin's content visibility
32        self._refreshSkinData()
33
34        '''Preparation for functional testing'''
35        self.discussion = getToolByName(self.portal, 'portal_discussion', None)
36        # Allow discussion for Document
37        portal_types = getToolByName(self.portal, 'portal_types', None)
38        doc_fti = portal_types.getTypeInfo('Document')
39        doc_fti._updateProperty('allow_discussion', 1)
40
41        # Make sure Documents are visible by default
42        # XXX only do this for plone 3
43        self.portal.portal_workflow.setChainForPortalTypes(('Document',), 'plone_workflow')
44
45        portal_properties = getToolByName(self.portal, 'portal_properties', None)
46        self.prefs = portal_properties[PROPERTY_SHEET]
47
48        # Add Manager user - 'dm' and add him to Discussion Manager group
49        self.portal.portal_membership.addMember('dm', 'secret' , ['Manager'], [])
50        portal_groups = getToolByName(self.portal, 'portal_groups')
51        dm_group = portal_groups.getGroupById('DiscussionManager')
52        dm_group.addMember('dm')
53        self.logout()
54        self.login('dm')
55        # For prepare mail sending - enter an e-mail adress
56        self.portal.email_from_address = 'mail@plone.test'
57        self.prefs._updateProperty('email_discussion_manager', 'discussion.manager@test.com')
58        member = self.portal.portal_membership.getAuthenticatedMember()
59        member.setMemberProperties({'email':'creator@test.com'})
60
61        # Add testing document to portal
62        my_doc = self.portal.invokeFactory('Document', id='my_doc', title='Doc')
63        self.my_doc = self.portal['my_doc']
64        self.my_doc.edit(text_format='plain', text='hello world')
65        # Create talkback for document and Prepare REQUEST
66        self.discussion.getDiscussionFor(self.my_doc)
67        self.request = self.app.REQUEST
68        self.request.form['Creator'] = self.portal.portal_membership.getAuthenticatedMember().getUserName()
69        self.request.form['subject'] = "Reply 1"
70        self.request.form['body_text'] = "text of reply"
71
72        #prepareMailSendTest()
73
74    def test_bug_parent_reply(self):
75        setProperties(self.prefs, 'enable_reply_user_notification')
76        self.my_doc.discussion_reply('A Reply for my_doc' ,'text of reply for my_doc')
77        parent_reply = self.discussion.getDiscussionFor(self.my_doc).getReplies()[0]
78        parent_reply.discussion_reply('reply', 'text')
79
80    def test_bug_mistakable_names(self):
81        setProperties(self.prefs, 'enable_reply_user_notification')
82        self.my_doc.discussion_reply('A Reply for my_doc' ,'text of reply for my_doc')
83        parent_reply = self.discussion.getDiscussionFor(self.my_doc).getReplies()[0]
84
85        args={'mto': 'user_email@gmail.com',
86              'mfrom': 'admin_email@gmail.com',
87              'obj': parent_reply,
88              'organization_name': 'org_name',
89              'name': parent_reply.getOwnerTuple()[1]}
90
91        msg = getMsg(self.portal, 'reply_notify_template', args)
92        patt = re.compile('\\n([^,]*?),\\n\\n')
93        m = patt.search(msg)
94        if m:
95            name = m.group(1)
96            self.assertEqual(parent_reply.getOwnerTuple()[1], name)
97        else:
98            raise "No name"
99
100    def test_notificafion_disabled(self):
101        self.portal.MailHost.reset()
102        setProperties(self.prefs)
103        self.my_doc.discussion_reply('A Reply for my_doc' ,'text of reply for my_doc')
104        self.failIf(testMailExistance(self), 'Mail was sended when all notification was disabled.')
105
106    def test_published_comment_notification(self):
107        self.portal.MailHost.reset()
108        setProperties(self.prefs, 'enable_published_notification')
109        self.my_doc.discussion_reply('A Reply for my_doc' ,'text of reply for my_doc')
110        self.failUnless(testMailExistance(self), 'Mail was not sended when enable_published_notification.')
111
112    def test_approve_comment_notification(self):
113        self.portal.MailHost.reset()
114        setProperties(self.prefs, 'enable_approve_notification')
115        self.my_doc.discussion_reply('A Reply for my_doc' ,'text of reply for my_doc')
116        self.failUnless(testMailExistance(self), 'Mail was not sended when enable_approve_notification.')
117
118    def test_reply_comment_user_notification(self):
119        self.portal.MailHost.reset()
120        setProperties(self.prefs, 'enable_reply_user_notification')
121        self.my_doc.discussion_reply('A Reply for my_doc' ,'text of reply for my_doc')
122        self.failIf(testMailExistance(self), 'Mail was sended for simple reply when enable_reply_user_notification.')
123
124        reply = self.discussion.getDiscussionFor(self.my_doc).getReplies()[0]
125        reply.discussion_reply('A Reply for comment' ,'text of reply for comment')
126        reply_for_comment = self.discussion.getDiscussionFor(self.my_doc).getReplies()[0]
127        self.failUnless(testMailExistance(self), 'Mail was not sended when enable_reply_user_notification.')
128
129    def test_rejected_comment_notification(self):
130        self.portal.MailHost.reset()
131        setProperties(self.prefs, 'enable_rejected_user_notification', 'enable_moderation')
132        self.my_doc.discussion_reply('A Reply for my_doc' ,'text of reply for my_doc')
133        self.failIf(testMailExistance(self), 'Mail was sended when enable_rejected_user_notification was enabled.')
134
135        reply = self.discussion.getDiscussionFor(self.my_doc).getReplies()[0]
136        self.portal.REQUEST.set('ids', [reply.getId()])
137        self.portal.prefs_recent_comments_delete()
138        self.failUnless(testMailExistance(self), 'Mail was not sended when enable_rejected_user_notification.')
139
140    def test_approve_comment_user__notification(self):
141        self.portal.MailHost.reset()
142        setProperties(self.prefs, 'enable_approve_user_notification')
143        self.my_doc.discussion_reply('A Reply for my_doc' ,'text of reply for my_doc')
144        self.failUnless(testMailExistance(self), 'Mail was not sended when enable_approve_user_notification.')
145
146    def test_bug_notification_on_single_reply_publish(self):
147        """ Bug: no notification sent on publishing single comment.
148            Must be 3 mails: for replier about replying on his commen;
149                             for replier about publishig his comment;
150                             for document creator about adding new comment.
151        """
152        properties = ['enable_approve_user_notification', 'enable_reply_user_notification',
153                      'enable_published_notification']
154        setProperties(self.prefs, *properties)
155        #setProperties(self.prefs, 'enable_published_notification', )
156        self.my_doc.discussion_reply('A Reply for my_doc' ,'text of reply for my_doc')
157        reply = self.discussion.getDiscussionFor(self.my_doc).getReplies()[0]
158        reply.discussion_reply('A Reply for reply for my_doc' ,'text of reply on reply for my_doc')
159        reply2 = self.discussion.getDiscussionFor(reply).getReplies()[0]
160
161        self.portal.MailHost.reset()
162        reply2.discussion_publish_comment()
163        mails = [str(m) for m in self.portal.MailHost.messages]
164        self.failUnless([1 for m in mails if REXP_SUBJ.search(m) and 'replied' in REXP_SUBJ.search(m).group(1)],
165            'No notification for reply.' % properties)
166        self.failUnless([1 for m in mails if REXP_SUBJ.search(m) and 'added' in REXP_SUBJ.search(m).group(1)],
167            'No notification for adding comment.' % properties)
168        self.failUnless([1 for m in mails if REXP_SUBJ.search(m) and 'published' in REXP_SUBJ.search(m).group(1)],
169            'No notification for publishing comment.' % properties)
170
171    def test_bug_notification_on_single_reply_delete(self):
172        """ Bug: no notification sent on deleting single comment.
173            Mail about rejecing comment should be sent to comentator.
174        """
175        properties = ['enable_rejected_user_notification',]
176        setProperties(self.prefs, *properties)
177        #setProperties(self.prefs, 'enable_published_notification', )
178        self.my_doc.discussion_reply('A Reply for my_doc' ,'text of reply for my_doc')
179        reply = self.discussion.getDiscussionFor(self.my_doc).getReplies()[0]
180
181        self.portal.MailHost.reset()
182        reply.deleteDiscussion()
183        mails = [str(m) for m in self.portal.MailHost.messages]
184        subject = 'Your comment on Doc was not approved'
185        self.failUnless([1 for m in mails if REXP_SUBJ.search(m) and REXP_SUBJ.search(m).group(1)==subject],
186            'No notification for rejecting comment.' % properties)
187
188
189def test_suite():
190    from unittest import TestSuite, makeSuite
191    suite = TestSuite()
192    suite.addTest(makeSuite(TestNotification))
193    return suite
Note: See TracBrowser for help on using the repository browser.