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

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

Pylint fixes #3

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