1 | # |
---|
2 | # Test configuration form working |
---|
3 | # |
---|
4 | |
---|
5 | from Products.PloneTestCase import PloneTestCase |
---|
6 | from Products.CMFCore.utils import getToolByName |
---|
7 | from Products.CMFCore.permissions import ManagePortal, ReplyToItem |
---|
8 | |
---|
9 | from Products.qPloneComments.utils import getMsg |
---|
10 | |
---|
11 | import re |
---|
12 | from common import * |
---|
13 | from helperNotify import * |
---|
14 | from email.Header import Header |
---|
15 | from testQPloneCommentsModeration import USERS, COMMON_USERS_IDS, DM_USERS_IDS |
---|
16 | |
---|
17 | |
---|
18 | PRODUCT = 'qPloneComments' |
---|
19 | PROPERTY_SHEET = "qPloneComments" |
---|
20 | |
---|
21 | PloneTestCase.installProduct(PRODUCT) |
---|
22 | PloneTestCase.setupPloneSite() |
---|
23 | |
---|
24 | USERS = {# Common Members |
---|
25 | 'admin':{'passw': 'secret_admin', 'roles': ['Manager']}, |
---|
26 | 'owner':{'passw': 'secret_creator', 'roles': ['Member']}, |
---|
27 | 'replier1':{'passw': 'secret_member', 'roles': ['Member']}, |
---|
28 | 'replier2':{'passw': 'secret_member', 'roles': ['Member']}, |
---|
29 | # Members for discussion manager group |
---|
30 | 'dm_admin':{'passw': 'secret_dm_admin', 'roles': ['Manager']}, |
---|
31 | } |
---|
32 | DM_USERS_IDS = [u for u in USERS.keys() if u.startswith('dm_')] |
---|
33 | |
---|
34 | REXP_TO = re.compile("To:\s*(.*?)$",re.M) |
---|
35 | REXP_SUBJ = re.compile("Subject:\s*(.*?)$",re.M) |
---|
36 | |
---|
37 | |
---|
38 | class TestNotificationRecipients(PloneTestCase.FunctionalTestCase): |
---|
39 | """ Test is notifications sends to right recipients. """ |
---|
40 | |
---|
41 | def prepareRequest4Reply(self, member_id): |
---|
42 | self.login(member_id) |
---|
43 | self.request = self.app.REQUEST |
---|
44 | self.request.form['Creator'] = self.membership.getAuthenticatedMember().getUserName() |
---|
45 | self.request.form['subject'] = "Reply of '%s'" % self.request.form['Creator'] |
---|
46 | self.request.form['body_text'] = "text of reply" |
---|
47 | |
---|
48 | def afterSetUp(self): |
---|
49 | self.loginAsPortalOwner() |
---|
50 | |
---|
51 | self.qi = self.portal.portal_quickinstaller |
---|
52 | self.qi.installProduct(PRODUCT) |
---|
53 | # VERY IMPORTANT to guarantee product skin's content visibility |
---|
54 | self._refreshSkinData() |
---|
55 | |
---|
56 | '''Preparation for functional testing''' |
---|
57 | self.membership = getToolByName(self.portal, 'portal_membership', None) |
---|
58 | self.discussion = getToolByName(self.portal, 'portal_discussion', None) |
---|
59 | # Allow discussion for Document |
---|
60 | portal_types = getToolByName(self.portal, 'portal_types', None) |
---|
61 | doc_fti = portal_types.getTypeInfo('Document') |
---|
62 | doc_fti._updateProperty('allow_discussion', 1) |
---|
63 | |
---|
64 | # Make sure Documents are visible by default |
---|
65 | # XXX only do this for plone 3 |
---|
66 | self.portal.portal_workflow.setChainForPortalTypes(('Document',), 'plone_workflow') |
---|
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 | self.portal.email_from_address = 'mail@plone.test' |
---|
78 | setProperties(self.prefs, 'enable_moderation', 'enable_approve_notification', |
---|
79 | 'enable_approve_user_notification','enable_reply_user_notification', |
---|
80 | 'enable_published_notification', 'enable_rejected_user_notification') |
---|
81 | self.prefs._updateProperty('email_discussion_manager', 'discussion.manager@test.com') |
---|
82 | self.prefs._updateProperty('email_subject_prefix', 'PREFIX') |
---|
83 | |
---|
84 | # Add testing document to portal |
---|
85 | self.login('owner') |
---|
86 | self.portal.Members['owner'].invokeFactory('Document', id='my_doc', title="Test document") |
---|
87 | self.my_doc = self.portal.Members['owner']['my_doc'] |
---|
88 | self.my_doc.edit(text_format='plain', text='hello world') |
---|
89 | |
---|
90 | # Create talkback for document and Prepare REQUEST |
---|
91 | self.discussion.getDiscussionFor(self.my_doc) |
---|
92 | |
---|
93 | prepareMailSendTest() |
---|
94 | |
---|
95 | def checkToANDSubj(self, mails, to, subj): |
---|
96 | messages = [m for m in mails if REXP_TO.search(m) and REXP_TO.search(m).group(1)==to] |
---|
97 | self.assert_(len(messages) > 0, "No message sent to '%s' recipient" % to) |
---|
98 | mangled = str(Header(subj, 'utf-8')) |
---|
99 | self.assert_([1 for m in messages if REXP_SUBJ.search(m) and REXP_SUBJ.search(m).group(1)==mangled],\ |
---|
100 | "There is no message for '%s' recipient with '%s' subject" % (to,subj)) |
---|
101 | |
---|
102 | def test_Reply(self): |
---|
103 | cleanOutputDir() |
---|
104 | self.prepareRequest4Reply('replier1') |
---|
105 | self.my_doc.discussion_reply('A Reply for my_doc' ,'text of reply for my_doc') |
---|
106 | |
---|
107 | mails = getMails() |
---|
108 | self.assertEqual(len(mails), 1) |
---|
109 | self.checkToANDSubj(mails, to="discussion.manager@test.com", |
---|
110 | subj="[PREFIX] New comment awaits moderation") |
---|
111 | |
---|
112 | def test_PublishReply(self): |
---|
113 | self.prepareRequest4Reply('replier1') |
---|
114 | self.my_doc.discussion_reply('A Reply for my_doc' ,'text of reply for my_doc') |
---|
115 | self.login('dm_admin') |
---|
116 | reply = self.discussion.getDiscussionFor(self.my_doc).getReplies()[0] |
---|
117 | cleanOutputDir() |
---|
118 | |
---|
119 | reply.discussion_publish_comment() |
---|
120 | mails = getMails() |
---|
121 | self.assertEqual(len(mails), 2) |
---|
122 | self.checkToANDSubj(mails, to="owner@test.com", subj="[PREFIX] New comment added") |
---|
123 | self.checkToANDSubj(mails, to="replier1@test.com", subj='Your comment on "Test document" is now published') |
---|
124 | |
---|
125 | def test_Publish2ndReply(self): |
---|
126 | self.prepareRequest4Reply('replier1') |
---|
127 | self.my_doc.discussion_reply('A Reply for my_doc' ,'text of reply for my_doc') |
---|
128 | self.login('dm_admin') |
---|
129 | reply = self.discussion.getDiscussionFor(self.my_doc).getReplies()[0] |
---|
130 | reply.discussion_publish_comment() |
---|
131 | self.prepareRequest4Reply('replier2') |
---|
132 | reply.discussion_reply('A Reply for reply for my_doc' ,'text of reply on reply for my_doc') |
---|
133 | self.login('dm_admin') |
---|
134 | reply2 = self.discussion.getDiscussionFor(reply).getReplies()[0] |
---|
135 | cleanOutputDir() |
---|
136 | |
---|
137 | reply2.discussion_publish_comment() |
---|
138 | mails = getMails() |
---|
139 | self.assertEqual(len(mails), 3) |
---|
140 | self.checkToANDSubj(mails, to="owner@test.com", subj="[PREFIX] New comment added") |
---|
141 | self.checkToANDSubj(mails, to="replier1@test.com", subj='Someone replied to your comment on "Test document"') |
---|
142 | self.checkToANDSubj(mails, to="replier2@test.com", subj='Your comment on "Test document" is now published') |
---|
143 | |
---|
144 | def test_DeleteReply(self): |
---|
145 | self.prepareRequest4Reply('replier1') |
---|
146 | self.my_doc.discussion_reply('A Reply for my_doc' ,'text of reply for my_doc') |
---|
147 | self.login('dm_admin') |
---|
148 | reply = self.discussion.getDiscussionFor(self.my_doc).getReplies()[0] |
---|
149 | cleanOutputDir() |
---|
150 | |
---|
151 | reply.deleteDiscussion() |
---|
152 | mails = getMails() |
---|
153 | self.assertEqual(len(mails), 1) |
---|
154 | self.checkToANDSubj(mails, to="replier1@test.com", subj='Your comment on "Test document" was not approved') |
---|
155 | |
---|
156 | |
---|
157 | def test_suite(): |
---|
158 | from unittest import TestSuite, makeSuite |
---|
159 | suite = TestSuite() |
---|
160 | suite.addTest(makeSuite(TestNotificationRecipients)) |
---|
161 | return suite |
---|