source: products/qPloneCaptchas/tags/1.1.0/tests/testCaptchaWidget.py

Last change on this file was 1, checked in by myroslav, 18 years ago

Building directory structure

  • Property svn:eol-style set to native
File size: 4.3 KB
Line 
1#
2# Tests for qPloneCaptchas
3#
4
5import os, sys, re
6if __name__ == '__main__':
7    execfile(os.path.join(sys.path[0], 'framework.py'))
8
9from Products.PloneTestCase import PloneTestCase
10from AccessControl.SecurityManagement import newSecurityManager
11from Products.qPloneCaptchas.utils import getWord, decrypt, parseKey
12
13PloneTestCase.installProduct('qPloneCaptchas')
14PloneTestCase.setupPloneSite()
15
16class TestCaptchaWidget(PloneTestCase.FunctionalTestCase):
17
18    def afterSetUp(self):
19        self.loginAsPortalOwner()
20        self.addProduct('qPloneCaptchas')
21        self.portal.invokeFactory('Document', 'index_html')
22        self.portal['index_html'].allow_discussion = True
23        self.absolute_url = self.portal['index_html'].absolute_url_path()
24
25        self.basic_auth = 'portal_manager:secret'
26        uf = self.app.acl_users
27        uf.userFolderAddUser('portal_manager', 'secret', ['Manager'], [])
28        user = uf.getUserById('portal_manager')
29        if not hasattr(user, 'aq_base'):
30            user = user.__of__(uf)
31        newSecurityManager(None, user)
32        self.captcha_key = self.portal.captcha_key
33
34    def testImage(self):
35        path = '%s/discussion_reply_form'%self.absolute_url
36        resp1 = self.publish(path, self.basic_auth, request_method='GET').getBody()
37        patt = re.compile('<img\s+src="%s(/getCaptchaImage/[0-9a-fA-F]+)"'%self.portal.absolute_url())
38        match_obj = patt.search(resp1)
39        img_url = match_obj.group(1)
40        content_type = self.publish('/plone'+img_url, self.basic_auth).getHeader('content-type')
41        self.assert_(content_type.startswith('image'))
42
43    def testSubmitRightCaptcha(self):
44        hashkey = self.portal.getCaptcha()
45        key = getWord(int(parseKey(decrypt(self.captcha_key, hashkey))['key']))
46        parameters = 'form.submitted=1&Creator=test_user&key=%s'%key
47        path = '%s/discussion_reply_form?%s'%(self.absolute_url, parameters)
48        extra = {'hashkey': hashkey,
49                 'subject': 'testing',
50                 'body_text': 'Text in Comment',
51                 'discussion_reply:method': 'Save'}
52
53        response = self.publish(path, self.basic_auth, extra=extra, request_method='GET').getBody()
54        patt = re.compile("Please re\-enter validation code")
55        match_obj = patt.match(response)
56        self.assert_(not match_obj)
57
58    def testSubmitWrongCaptcha(self):
59        hashkey = self.portal.getCaptcha()
60        parameters = 'form.submitted=1&Creator=test_user&key=fdfgh'
61        path = '%s/discussion_reply_form?%s'%(self.absolute_url, parameters)
62        extra = {'hashkey': hashkey,
63                 'subject': 'testing',
64                 'body_text': 'Text in Comment',
65                 'discussion_reply:method': 'Save'}
66
67        response = self.publish(path, self.basic_auth, extra=extra, request_method='GET').getBody()
68        patt = re.compile("Please re\-enter validation code")
69        match_obj = patt.search(response)
70        self.assert_(match_obj)
71
72    def testSubmitRightCaptchaTwice(self):
73        hashkey = self.portal.getCaptcha()
74        key = getWord(int(parseKey(decrypt(self.captcha_key, hashkey))['key']))
75        parameters = 'form.submitted=1&Creator=test_user&key=%s'%key
76        path = '%s/discussion_reply_form?%s'%(self.absolute_url, parameters)
77        extra = {'hashkey': hashkey,
78                 'subject': 'testing',
79                 'body_text': 'Text in Comment',
80                 'discussion_reply:method': 'Save'}
81
82        self.publish(path, self.basic_auth, extra=extra, request_method='GET')
83        response = self.publish(path, self.basic_auth, extra=extra, request_method='GET').getBody()
84        patt = re.compile(".*?Comment\+added")
85        match_obj = patt.match(response)
86        self.assert_(not match_obj)
87
88class TestInstallation(PloneTestCase.FunctionalTestCase):
89
90    def afterSetUp(self):
91        self.loginAsPortalOwner()
92        self.addProduct('qPloneCaptchas')
93
94    def testCaptchaKey(self):
95        ck = getattr(self.portal, 'captcha_key')
96        self.assert_(ck)
97        self.assertEqual(len(ck), 8)
98
99    def testCaptchaTool(self):
100        self.assert_('portal_captchas' in self.portal.objectIds())
101
102
103def test_suite():
104    from unittest import TestSuite, makeSuite
105    suite = TestSuite()
106    suite.addTest(makeSuite(TestCaptchaWidget))
107    suite.addTest(makeSuite(TestInstallation))
108    return suite
109
110if __name__ == '__main__':
111    framework()
Note: See TracBrowser for help on using the repository browser.