source: products/quintagroup.plonecaptchas/branches/plone3/quintagroup/plonecaptchas/tests/testForms.py @ 3610

Last change on this file since 3610 was 3610, checked in by vmaksymiv, 11 years ago

PPP fixes

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