source: products/qPloneCaptchas/tags/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: 3.9 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 testSubmitRightCaptcha(self):
35        hashkey = self.portal.getCaptcha()
36        key = getWord(int(parseKey(decrypt(self.captcha_key, hashkey))['key']))
37        parameters = 'form.submitted=1&Creator=test_user&key=%s'%key
38        path = '%s/discussion_reply_form?%s'%(self.absolute_url, parameters)
39        extra = {'hashkey': hashkey,
40                 'subject': 'testing',
41                 'body_text': 'Text in Comment',
42                 'discussion_reply:method': 'Save'}
43
44        response = self.publish(path, self.basic_auth, extra=extra, request_method='GET').getBody()
45        patt = re.compile("Please re\-enter validation code")
46        match_obj = patt.match(response)
47        self.assert_(not match_obj)
48
49
50   
51    def testSubmitWrongCaptcha(self):
52        hashkey = self.portal.getCaptcha()
53        parameters = 'form.submitted=1&Creator=test_user&key=fdfgh'
54        path = '%s/discussion_reply_form?%s'%(self.absolute_url, parameters)
55        extra = {'hashkey': hashkey,
56                 'subject': 'testing',
57                 'body_text': 'Text in Comment',
58                 'discussion_reply:method': 'Save'}
59
60        response = self.publish(path, self.basic_auth, extra=extra, request_method='GET').getBody()
61        patt = re.compile("Please re\-enter validation code")
62        match_obj = patt.search(response)
63        self.assert_(match_obj)
64   
65    def testSubmitRightCaptchaTwice(self):
66        hashkey = self.portal.getCaptcha()
67        key = getWord(int(parseKey(decrypt(self.captcha_key, hashkey))['key']))
68        parameters = 'form.submitted=1&Creator=test_user&key=%s'%key
69        path = '%s/discussion_reply_form?%s'%(self.absolute_url, parameters)
70        extra = {'hashkey': hashkey,
71                 'subject': 'testing',
72                 'body_text': 'Text in Comment',
73                 'discussion_reply:method': 'Save'}
74
75        self.publish(path, self.basic_auth, extra=extra, request_method='GET')
76        response = self.publish(path, self.basic_auth, extra=extra, request_method='GET').getBody()
77        patt = re.compile(".*?Comment\+added")
78        match_obj = patt.match(response)
79        self.assert_(not match_obj)
80   
81class TestInstallation(PloneTestCase.FunctionalTestCase):
82
83    def afterSetUp(self):
84        self.loginAsPortalOwner()
85        self.addProduct('qPloneCaptchas')
86   
87    def testCaptchaKey(self):
88        ck = getattr(self.portal, 'captcha_key')
89        self.assert_(ck)
90        self.assertEqual(len(ck), 8)
91   
92    def testCaptchaTool(self):
93        self.assert_('portal_captchas' in self.portal.objectIds())
94   
95
96def test_suite():
97    from unittest import TestSuite, makeSuite
98    suite = TestSuite()
99    suite.addTest(makeSuite(TestCaptchaWidget))
100    suite.addTest(makeSuite(TestInstallation))
101    return suite
102
103if __name__ == '__main__':
104    framework()
Note: See TracBrowser for help on using the repository browser.