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

Last change on this file since 2137 was 2137, checked in by kroman0, 14 years ago

PEP8 fixes

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