1 | import unittest |
---|
2 | import re |
---|
3 | from urllib import urlencode |
---|
4 | from StringIO import StringIO |
---|
5 | |
---|
6 | from Products.PloneTestCase.PloneTestCase import portal_owner |
---|
7 | from Products.PloneTestCase.PloneTestCase import default_password |
---|
8 | |
---|
9 | from quintagroup.plonecaptchas.tests.base import FunctionalTestCase |
---|
10 | from quintagroup.plonecaptchas.config import PRODUCT_NAME |
---|
11 | |
---|
12 | from quintagroup.captcha.core.tests.testWidget import IMAGE_PATT, NOT_VALID |
---|
13 | from quintagroup.captcha.core.tests.testWidget import addTestLayer |
---|
14 | from quintagroup.captcha.core.tests.base import testPatch |
---|
15 | from quintagroup.captcha.core.utils import getWord, decrypt, parseKey |
---|
16 | |
---|
17 | from plone.app.controlpanel.security import ISecuritySchema |
---|
18 | |
---|
19 | # BBB for plone v<3.1, where plone.protect not used yet |
---|
20 | PROTECT_SUPPORT = True |
---|
21 | try: |
---|
22 | from plone import protect |
---|
23 | # pyflakes fix (pyflakes message: 'protect' imported but unused) |
---|
24 | protect |
---|
25 | except ImportError: |
---|
26 | PROTECT_SUPPORT = False |
---|
27 | |
---|
28 | # USE PATCH FROM quintagroup.captcha.core |
---|
29 | # patch to use test images and dictionary |
---|
30 | testPatch() |
---|
31 | |
---|
32 | |
---|
33 | class TestFormMixin(FunctionalTestCase): |
---|
34 | |
---|
35 | formkey_key = "key" |
---|
36 | formkey_hashkey = "hashkey" |
---|
37 | |
---|
38 | def afterSetUp(self): |
---|
39 | self.loginAsPortalOwner() |
---|
40 | self.addProduct(PRODUCT_NAME) |
---|
41 | # Add test_captcha layer from quintagroup.captcah.core |
---|
42 | addTestLayer(self) |
---|
43 | # Prepare form data |
---|
44 | self.basic_auth = ':'.join((portal_owner, default_password)) |
---|
45 | self.form_url = '' |
---|
46 | self.form_method = "POST" |
---|
47 | self.hasAuthenticator = False |
---|
48 | self.form_data = self.getFormData() |
---|
49 | # Prepare captcha related test data |
---|
50 | self.captcha_key = self.portal.captcha_key |
---|
51 | self.hashkey = self.portal.getCaptcha() |
---|
52 | self.form_data[self.formkey_hashkey] = self.hashkey |
---|
53 | self.form_data[self.formkey_key] = '' |
---|
54 | |
---|
55 | def getFormData(self): |
---|
56 | raise NotImplementedError( |
---|
57 | "getFormData not implemented") |
---|
58 | |
---|
59 | def publishForm(self): |
---|
60 | stdin_data = None |
---|
61 | form_url = self.portal.absolute_url(1) + self.form_url |
---|
62 | # Prepare form data |
---|
63 | if PROTECT_SUPPORT and self.hasAuthenticator: |
---|
64 | self.form_data['_authenticator'] = self._getauth() |
---|
65 | form_data = urlencode(self.form_data) |
---|
66 | if self.form_method.upper() == 'GET': |
---|
67 | form_url += "?%s" % form_data |
---|
68 | else: |
---|
69 | stdin_data = StringIO(form_data) |
---|
70 | # Publish form and get response |
---|
71 | response = self.publish(form_url, self.basic_auth, |
---|
72 | request_method=self.form_method, stdin=stdin_data) |
---|
73 | return response |
---|
74 | |
---|
75 | def _getauth(self): |
---|
76 | # Fix authenticator for the form |
---|
77 | authenticator = self.portal.restrictedTraverse("@@authenticator") |
---|
78 | html = authenticator.authenticator() |
---|
79 | handle = re.search('value="(.*)"', html).groups()[0] |
---|
80 | return handle |
---|
81 | |
---|
82 | def elog(self, name="", response=""): |
---|
83 | open("/tmp/test.%s.html" % name, "w").write(response) |
---|
84 | logs = self.portal.error_log.getLogEntries() |
---|
85 | if len(logs) > 0: |
---|
86 | i = 0 |
---|
87 | while logs: |
---|
88 | l = logs.pop() |
---|
89 | i += 1 |
---|
90 | open("/tmp/test.%s.error.%d.html" % (l["id"], i), |
---|
91 | "w").write(l["tb_html"]) |
---|
92 | |
---|
93 | def testImage(self): |
---|
94 | self.form_data = {} |
---|
95 | self.form_method = "GET" |
---|
96 | response = self.publishForm().getBody() |
---|
97 | patt = re.compile(IMAGE_PATT % self.portal.absolute_url()) |
---|
98 | match_obj = patt.search(response) |
---|
99 | self.elog("image", response) |
---|
100 | img_url = match_obj.group(1) |
---|
101 | |
---|
102 | content_type = self.publish( |
---|
103 | '/plone' + img_url).getHeader('content-type') |
---|
104 | self.assertTrue(content_type.startswith('image'), |
---|
105 | "Wrong captcha image content type") |
---|
106 | |
---|
107 | def testSubmitRightCaptcha(self): |
---|
108 | key = getWord(int(parseKey(decrypt(self.captcha_key, |
---|
109 | self.hashkey))['key']) - 1) |
---|
110 | self.form_data[self.formkey_key] = key |
---|
111 | |
---|
112 | response = self.publishForm().getBody() |
---|
113 | self.elog("right", response) |
---|
114 | self.assertFalse(NOT_VALID.search(response)) |
---|
115 | |
---|
116 | def testSubmitWrongCaptcha(self): |
---|
117 | self.form_data[self.formkey_key] = 'wrong word' |
---|
118 | response = self.publishForm().getBody() |
---|
119 | self.elog("wrong", response) |
---|
120 | self.assertTrue(NOT_VALID.search(response)) |
---|
121 | |
---|
122 | def testSubmitRightCaptchaTwice(self): |
---|
123 | key = getWord(int(parseKey(decrypt(self.captcha_key, |
---|
124 | self.hashkey))['key']) - 1) |
---|
125 | self.form_data[self.formkey_key] = key |
---|
126 | |
---|
127 | response1 = self.publishForm().getBody() |
---|
128 | self.elog("right1", response1) |
---|
129 | response2 = self.publishForm().getBody() |
---|
130 | self.elog("right2", response2) |
---|
131 | self.assertTrue(NOT_VALID.search(response2)) |
---|
132 | |
---|
133 | |
---|
134 | class TestDiscussionForm(TestFormMixin): |
---|
135 | |
---|
136 | def afterSetUp(self): |
---|
137 | TestFormMixin.afterSetUp(self) |
---|
138 | self.portal.invokeFactory('Document', 'index_html') |
---|
139 | self.portal['index_html'].allowDiscussion(True) |
---|
140 | self.form_url = '/index_html/discussion_reply_form' |
---|
141 | |
---|
142 | def getFormData(self): |
---|
143 | return {'form.submitted': '1', |
---|
144 | 'subject': 'testing', |
---|
145 | 'Creator': portal_owner, |
---|
146 | 'body_text': 'Text in Comment', |
---|
147 | 'discussion_reply:method': 'Save', |
---|
148 | 'form.button.form_submit': 'Save'} |
---|
149 | |
---|
150 | |
---|
151 | class TestRegisterForm(TestFormMixin): |
---|
152 | |
---|
153 | formkey_key = "form.captcha" |
---|
154 | formkey_hashkey = "form..hashkey" |
---|
155 | |
---|
156 | def afterSetUp(self): |
---|
157 | TestFormMixin.afterSetUp(self) |
---|
158 | ISecuritySchema(self.portal).enable_self_reg = True |
---|
159 | self.portal._updateProperty('validate_email', False) |
---|
160 | self.hasAuthenticator = True |
---|
161 | self.form_url = '/@@register' |
---|
162 | self.basic_auth = ":" |
---|
163 | self.logout() |
---|
164 | |
---|
165 | def getFormData(self): |
---|
166 | return {"form.fullname": "Tester", |
---|
167 | "form.username": "tester", |
---|
168 | "form.email": "tester@test.com", |
---|
169 | "form.password": "123456", |
---|
170 | "form.password_ctl": "123456", |
---|
171 | 'form.actions.register': 'Register'} |
---|
172 | |
---|
173 | |
---|
174 | class TestSendtoForm(TestFormMixin): |
---|
175 | |
---|
176 | def afterSetUp(self): |
---|
177 | TestFormMixin.afterSetUp(self) |
---|
178 | self.portal.invokeFactory('Document', 'index_html') |
---|
179 | self.portal['index_html'].allowDiscussion(True) |
---|
180 | self.form_url = '/index_html/sendto_form' |
---|
181 | |
---|
182 | def getFormData(self): |
---|
183 | return {'form.submitted': '1', |
---|
184 | "send_to_address": "recipient@test.com", |
---|
185 | "send_from_address": "sender@test.com", |
---|
186 | 'comment': 'Text in Comment', |
---|
187 | 'form.button.Send': 'Save'} |
---|
188 | |
---|
189 | |
---|
190 | def send_patch(self, *args, **kwargs): |
---|
191 | """This patch prevent breakage on sending.""" |
---|
192 | |
---|
193 | |
---|
194 | class TestContactInfo(TestFormMixin): |
---|
195 | |
---|
196 | def afterSetUp(self): |
---|
197 | TestFormMixin.afterSetUp(self) |
---|
198 | # preparation to form correct working |
---|
199 | self.portal._updateProperty('email_from_address', 'manager@test.com') |
---|
200 | self.logout() |
---|
201 | self.form_url = '/contact-info' |
---|
202 | self.orig_mh_send = self.portal.MailHost.send |
---|
203 | self.portal.MailHost.send = send_patch |
---|
204 | |
---|
205 | def beforeTearDown(self): |
---|
206 | self.portal.MailHost.send = self.orig_mh_send |
---|
207 | |
---|
208 | def getFormData(self): |
---|
209 | return {'form.submitted': '1', |
---|
210 | "sender_fullname": "tester", |
---|
211 | "sender_from_address": "sender@test.com", |
---|
212 | 'subject': 'Subject', |
---|
213 | 'message': 'Message', |
---|
214 | 'form.button.Send': 'Save'} |
---|
215 | |
---|
216 | |
---|
217 | def test_suite(): |
---|
218 | suite = unittest.TestSuite() |
---|
219 | suite.addTest(unittest.makeSuite(TestDiscussionForm)) |
---|
220 | suite.addTest(unittest.makeSuite(TestRegisterForm)) |
---|
221 | suite.addTest(unittest.makeSuite(TestSendtoForm)) |
---|
222 | suite.addTest(unittest.makeSuite(TestContactInfo)) |
---|
223 | return suite |
---|