source: products/qPloneComments/trunk/tests/testQPloneCommentsNotificationRecipients.py @ 1

Last change on this file since 1 was 1, checked in by myroslav, 18 years ago

Building directory structure

  • Property svn:eol-style set to native
File size: 6.7 KB
Line 
1#   
2# Test configuration form working
3#
4
5import os, sys, string
6
7if __name__ == '__main__':
8    execfile(os.path.join(sys.path[0], 'framework.py'))
9
10from Products.PloneTestCase import PloneTestCase
11from Products.CMFCore.utils import getToolByName
12from Products.CMFCore.CMFCorePermissions import ManagePortal, ReplyToItem
13from Products.MailHost.MailHost import MailBase
14
15import re
16
17from Products.qPloneComments.utils import getMsg
18from testQPloneCommentsModeration import USERS, COMMON_USERS_IDS, DM_USERS_IDS
19from helperNotify import *
20from common import *
21
22PRODUCT = 'qPloneComments'
23PROPERTY_SHEET = "qPloneComments"
24
25PloneTestCase.installProduct(PRODUCT)
26PloneTestCase.setupPloneSite()
27
28USERS = {# Common Members
29         'admin':{'passw': 'secret_admin', 'roles': ['Manager']},
30         'owner':{'passw': 'secret_creator', 'roles': ['Member']},
31         'replier1':{'passw': 'secret_member', 'roles': ['Member']},
32         'replier2':{'passw': 'secret_member', 'roles': ['Member']},
33         # Members for discussion manager group
34         'dm_admin':{'passw': 'secret_dm_admin', 'roles': ['Manager']},
35        }
36DM_USERS_IDS = [u for u in USERS.keys() if u.startswith('dm_')]
37
38REXP_TO = re.compile("To:\s*(.*?)$",re.M)
39REXP_SUBJ = re.compile("Subject:\s*(.*?)$",re.M)
40
41class TestNotificationRecipients(PloneTestCase.FunctionalTestCase):
42    """ Test is notifications sends to right recipients. """
43
44    def prepareRequest4Reply(self, member_id):
45        self.login(member_id)
46        self.request = self.app.REQUEST
47        self.request.form['Creator'] = self.membership.getAuthenticatedMember().getUserName()
48        self.request.form['subject'] = "Reply of '%s'" % self.request.form['Creator']
49        self.request.form['body_text'] = "text of reply"
50
51
52    def afterSetUp(self):
53        self.loginAsPortalOwner()
54
55        self.qi = self.portal.portal_quickinstaller
56        self.qi.installProduct(PRODUCT)
57        # VERY IMPORTANT to guarantee product skin's content visibility
58        self._refreshSkinData()
59
60        '''Preparation for functional testing'''
61        self.membership = getToolByName(self.portal, 'portal_membership', None)
62        self.discussion = getToolByName(self.portal, 'portal_discussion', None)
63        # Allow discussion for Document
64        portal_types = getToolByName(self.portal, 'portal_types', None)
65        doc_fti = portal_types.getTypeInfo('Document')
66        doc_fti._updateProperty('allow_discussion', 1)
67
68        portal_properties = getToolByName(self.portal, 'portal_properties', None)
69        self.prefs = portal_properties[PROPERTY_SHEET]
70
71        # Add users and add members to DiscussionManager group
72        addMembers(self.portal, USERS)
73        add2Group(self.portal, 'DiscussionManager', DM_USERS_IDS)
74        self.createMemberarea('owner')
75
76        ## Prepare mail sending - enter an e-mail adress, and allow all possible notifications
77        setProperties(self.prefs, 'enable_moderation', 'enable_approve_notification',
78                                  'enable_approve_user_notification','enable_reply_user_notification',
79                                  'enable_published_notification', 'enable_rejected_user_notification')
80        self.prefs._updateProperty('email_discussion_manager', 'discussion.manager@test.com')
81
82        ## Add testing document to portal
83        self.login('owner')
84        self.portal.Members['owner'].invokeFactory('Document', id='my_doc', title="Test document")
85        self.my_doc = self.portal.Members['owner']['my_doc']
86        self.my_doc.edit(text_format='plain', text='hello world')
87
88        ## Create talkback for document and Prepare REQUEST
89        self.discussion.getDiscussionFor(self.my_doc)
90
91        prepareMailSendTest()
92
93    def checkToANDSubj(self, mails, to, subj):
94        messages = [m for m in mails if REXP_TO.search(m) and REXP_TO.search(m).group(1)==to]
95        self.assert_(len(messages) > 0, "No message sent to '%s' recipient" % to)
96        self.assert_([1 for m in messages if REXP_SUBJ.search(m) and REXP_SUBJ.search(m).group(1)==subj],\
97                     "There is no message for '%s' recipient with '%s' subject" % (to,subj))
98
99    def test_Reply(self):
100        cleanOutputDir()
101        self.prepareRequest4Reply('replier1')
102        self.my_doc.discussion_reply('A Reply for my_doc' ,'text of reply for my_doc')
103
104        mails = getMails()
105        self.assertEqual(len(mails), 1)
106        self.checkToANDSubj(mails, to="discussion.manager@test.com", subj="New comment awaits moderation")
107
108
109    def test_PublishReply(self):
110        self.prepareRequest4Reply('replier1')
111        self.my_doc.discussion_reply('A Reply for my_doc' ,'text of reply for my_doc')
112        self.login('dm_admin')
113        reply = self.discussion.getDiscussionFor(self.my_doc).getReplies()[0]
114        cleanOutputDir()
115
116        reply.discussion_publish_comment()
117        mails = getMails()
118        self.assertEqual(len(mails), 2)
119        self.checkToANDSubj(mails, to="owner@test.com", subj="New comment added")
120        self.checkToANDSubj(mails, to="replier1@test.com", subj="Your comment on 'Test document' is now published")
121
122    def test_Publish2ndReply(self):
123        self.prepareRequest4Reply('replier1')
124        self.my_doc.discussion_reply('A Reply for my_doc' ,'text of reply for my_doc')
125        self.login('dm_admin')
126        reply = self.discussion.getDiscussionFor(self.my_doc).getReplies()[0]
127        reply.discussion_publish_comment()
128        self.prepareRequest4Reply('replier2')
129        reply.discussion_reply('A Reply for reply for my_doc' ,'text of reply on reply for my_doc')
130        self.login('dm_admin')
131        reply2 = self.discussion.getDiscussionFor(reply).getReplies()[0]
132        cleanOutputDir()
133
134        reply2.discussion_publish_comment()
135        mails = getMails()
136        self.assertEqual(len(mails), 3)
137        self.checkToANDSubj(mails, to="owner@test.com", subj="New comment added")
138        self.checkToANDSubj(mails, to="replier1@test.com", subj="Someone replied to your comment on 'Test document'")
139        self.checkToANDSubj(mails, to="replier2@test.com", subj="Your comment on 'Test document' is now published")
140
141    def test_DeleteReply(self):
142        self.prepareRequest4Reply('replier1')
143        self.my_doc.discussion_reply('A Reply for my_doc' ,'text of reply for my_doc')
144        self.login('dm_admin')
145        reply = self.discussion.getDiscussionFor(self.my_doc).getReplies()[0]
146        cleanOutputDir()
147
148        reply.deleteDiscussion()
149        mails = getMails()
150        self.assertEqual(len(mails), 1)
151        self.checkToANDSubj(mails, to="replier1@test.com", subj="Your comment on 'Test document' was not approved.")
152
153
154TESTS = [TestNotificationRecipients]
155
156def test_suite():
157    from unittest import TestSuite, makeSuite
158    suite = TestSuite()
159    for t in TESTS:
160        suite.addTest(makeSuite(t))
161    return suite
162
163if __name__ == '__main__':
164    framework()
165
Note: See TracBrowser for help on using the repository browser.