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