source: products/quintagroup.plonecaptchas/branches/split2packs/quintagroup/plonecaptchas/tests/testForms.py @ 2039

Last change on this file since 2039 was 2039, checked in by mylan, 14 years ago

#175: Tests: Fix tests for JoinForm? in plone-4

  • Property svn:eol-style set to native
File size: 5.8 KB
Line 
1from base import *
2from DateTime import DateTime
3from base64 import b64decode
4from urllib import urlencode
5from StringIO import StringIO
6from DateTime import DateTime
7
8from plone.app.controlpanel.security import ISecuritySchema
9
10# BBB for plone v<3.1, where plone.protect not used yet
11PROTECT_SUPPORT = True
12try:
13    from plone import protect
14except ImportError:
15    PROTECT_SUPPORT = False
16
17# USE PATCH FROM quintagroup.captcha.core
18# patch to use test images and dictionary
19testPatch()
20
21class TestFormMixin(FunctionalTestCase):
22
23    def afterSetUp(self):
24        self.loginAsPortalOwner()
25        self.addProduct(PRODUCT_NAME)
26        # Add test_captcha layer from quintagroup.captcah.core
27        addTestLayer(self)
28        # Prepare form data
29        self.basic_auth = ':'.join((portal_owner,default_password))
30        self.form_url = ''
31        self.form_method = "POST"
32        self.hasAuthenticator = False
33        self.form_data = self.getFormData()
34        # Prepare captcha related test data
35        self.captcha_key = self.portal.captcha_key
36        self.hashkey = self.portal.getCaptcha()
37        self.form_data['hashkey'] = self.hashkey
38        self.form_data['key'] = ''
39
40    def getFormData(self):
41        raise NotImplementedError(
42            "getFormData not implemented")
43       
44    def publishForm(self):
45        stdin_data = None
46        form_url = self.portal.absolute_url(1) + self.form_url
47        # Prepare form data
48        if PROTECT_SUPPORT and self.hasAuthenticator:
49            self.form_data['_authenticator'] = self._getauth()
50        form_data = urlencode(self.form_data)
51        if self.form_method.upper() == 'GET':
52            form_url += "?%s" % form_data
53        else:
54            stdin_data = StringIO(form_data)
55        # Publish form and get response
56        response = self.publish(form_url, self.basic_auth,
57            request_method=self.form_method, stdin=stdin_data)
58        return response
59
60    def _getauth(self):
61        # Fix authenticator for the form
62        authenticator = self.portal.restrictedTraverse("@@authenticator")
63        html = authenticator.authenticator()
64        handle = re.search('value="(.*)"', html).groups()[0]
65        return handle
66
67    def getStatusMessage(self, resp):
68        default = ''
69        if resp.getStatus()/100 == 2:
70            return resp.getBody()
71        elif resp.getStatus()/100 == 3:
72            now = DateTime()
73            sm = resp.getCookie('statusmessages')
74            if DateTime(sm.get('expires', now)) < now:
75                return default
76            return b64decode(sm.get('value',default))
77        return default
78       
79    def testImage(self):
80        self.form_data = {}
81        self.form_method = 'GET'
82        response = self.publishForm().getBody()
83        patt = re.compile(IMAGE_PATT  % self.portal.absolute_url())
84        match_obj = patt.search(response)
85        img_url = match_obj.group(1)
86
87        content_type = self.publish('/plone' + img_url).getHeader('content-type')
88        self.assertTrue(content_type.startswith('image'),
89            "Wrong captcha image content type")
90
91    def testSubmitRightCaptcha(self):
92        key = getWord(int(parseKey(decrypt(self.captcha_key, self.hashkey))['key'])-1)
93        self.form_data['key'] = key
94        response = self.publishForm()
95        self.assertFalse(NOT_VALID.search(self.getStatusMessage(response)))
96
97    def testSubmitWrongCaptcha(self):
98        self.form_data['key'] = 'wrong word'
99        response = self.publishForm()
100        self.assertTrue(NOT_VALID.search(self.getStatusMessage(response)))
101
102    def testSubmitRightCaptchaTwice(self):
103        key = getWord(int(parseKey(decrypt(self.captcha_key, self.hashkey))['key'])-1)
104        self.form_data['key'] = key
105
106        self.publishForm()
107        response = self.publishForm()
108        self.assertTrue(NOT_VALID.search(self.getStatusMessage(response)))
109
110
111class TestDiscussionForm(TestFormMixin):
112
113    def afterSetUp(self):
114        TestFormMixin.afterSetUp(self)
115        self.portal.invokeFactory('Document', 'index_html')
116        self.portal['index_html'].allowDiscussion(True)
117        self.form_url = '/index_html/discussion_reply_form'
118       
119    def getFormData(self):
120        return {'form.submitted' : '1',
121                'subject': 'testing',
122                'Creator': portal_owner,
123                'body_text': 'Text in Comment',
124                'discussion_reply:method': 'Save',
125                'form.button.form_submit' : 'Save'}
126
127
128class TestJoinForm(TestFormMixin):
129
130    def afterSetUp(self):
131        TestFormMixin.afterSetUp(self)
132        ISecuritySchema(self.portal).enable_self_reg = True
133        self.hasAuthenticator = True
134        self.form_url = '/' + JF_NAME
135        self.basic_auth = ":"
136        self.logout()
137
138    def getFormData(self):
139        return {"last_visit:date" : str(DateTime()),
140                "prev_visit:date" : str(DateTime()-1),
141                "came_from_prefs" : "",
142                "fullname" : "Tester",
143                "username" : "tester",
144                "email" : "tester@test.com",
145                'form.button.Register':'Register',
146                'form.submitted':'1'}
147
148
149class TestSendtoForm(TestFormMixin):
150
151    def afterSetUp(self):
152        TestFormMixin.afterSetUp(self)
153        self.portal.invokeFactory('Document', 'index_html')
154        self.portal['index_html'].allowDiscussion(True)
155        self.form_url = '/index_html/sendto_form'
156       
157    def getFormData(self):
158        return {'form.submitted' : '1',
159                "send_to_address" : "recipient@test.com",
160                "send_from_address" : "sender@test.com",
161                'comment': 'Text in Comment',
162                'form.button.Send' : 'Save'}
163
164
165def test_suite():
166    suite = unittest.TestSuite()
167    suite.addTest(unittest.makeSuite(TestDiscussionForm))
168    suite.addTest(unittest.makeSuite(TestJoinForm))
169    suite.addTest(unittest.makeSuite(TestSendtoForm))
170    return suite
Note: See TracBrowser for help on using the repository browser.