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

Last change on this file since 3114 was 3114, checked in by kroman0, 11 years ago

Pyflakes and pep8 fixes

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