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