source: products/quintagroup.plonecomments/branches/jquery/quintagroup/plonecomments/tests/testQPloneCommentsNotification.py @ 3110

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

Pylint fixes #2

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