source: products/quintagroup.captcha.core/trunk/quintagroup/captcha/core/tests/testWidget.py @ 3128

Last change on this file since 3128 was 3128, checked in by vmaksymiv, 13 years ago

pep8 fixes

  • Property svn:eol-style set to native
File size: 3.9 KB
Line 
1import string
2from base import *
3
4from Products.CMFCore.DirectoryView import registerDirectory
5from Products.CMFCore.DirectoryView import addDirectoryViews
6from Products.CMFCore.DirectoryView import DirectoryView
7
8NOT_VALID = re.compile("Please re\-enter validation code")
9IMAGE_PATT = '\s+src="%s(/getCaptchaImage/[0-9a-fA-F]+)"'
10
11# patch to use test images and dictionary
12testPatch()
13
14
15def addTestLayer(self):
16    # Install test_captcha skin layer
17    registerDirectory('tests', GLOBALS)
18    skins = self.portal.portal_skins
19    addDirectoryViews(skins, 'tests', GLOBALS)
20    skinName = skins.getDefaultSkin()
21    paths = map(string.strip, skins.getSkinPath(skinName).split(','))
22    paths.insert(paths.index('custom') + 1, 'test_captcha')
23    skins.addSkinSelection(skinName, ','.join(paths))
24    self._refreshSkinData()
25
26
27class TestCaptchaWidget(ptc.FunctionalTestCase):
28
29    def afterSetUp(self):
30        self.loginAsPortalOwner()
31        self.addProduct(PRODUCT_NAME)
32        addTestLayer(self)
33        self.portal.invokeFactory('Document', 'index_html')
34        self.portal['index_html'].allowDiscussion(True)
35        self.absolute_url = self.portal['index_html'].absolute_url_path()
36
37        self.basic_auth = ':'.join((portal_owner, default_password))
38        self.captcha_key = self.portal.captcha_key
39
40    def testImage(self):
41        path = '%s/test_form' % self.absolute_url
42        response = self.publish(path, self.basic_auth,
43                                request_method='GET').getBody()
44        patt = re.compile(IMAGE_PATT % self.portal.absolute_url())
45        match_obj = patt.search(response)
46
47        img_url = match_obj.group(1)
48        res = self.publish('/plone' + img_url, self.basic_auth)
49        content_type = res.getHeader('content-type')
50        self.assert_(content_type.startswith('image'))
51
52    def testSubmitRightCaptcha(self):
53        hashkey = self.portal.getCaptcha()
54        # index of word number starts from 1,
55        # but index of dictionary starts from 0
56        decrypted_key = decrypt(self.captcha_key, hashkey)
57        key = getWord(int(parseKey(decrypted_key)['key']) - 1)
58        parameters = 'form.submitted=1&key=%s' % key
59        path = '%s/test_form?%s' % (self.absolute_url, parameters)
60        extra = {'hashkey': hashkey,
61                 'form.button.Save': 'Save'}
62        response = self.publish(path, self.basic_auth, extra=extra,
63                                request_method='GET').getBody()
64        self.assert_(not NOT_VALID.search(response))
65
66    def testSubmitWrongCaptcha(self):
67        hashkey = self.portal.getCaptcha()
68        parameters = 'form.submitted=1&key=fdfgh'
69        path = '%s/test_form?%s' % (self.absolute_url, parameters)
70        extra = {'hashkey': hashkey,
71                 'form.button.Save': 'Save'}
72        response = self.publish(path, self.basic_auth, extra=extra,
73                                request_method='GET').getBody()
74        self.assert_(NOT_VALID.search(response))
75
76    def testSubmitRightCaptchaTwice(self):
77        hashkey = self.portal.getCaptcha()
78        decrypted_key = decrypt(self.captcha_key, hashkey)
79        key = getWord(int(parseKey(decrypted_key)['key']) - 1)
80        parameters = 'form.submitted=1&key=%s' % key
81        path = '%s/test_form?%s' % (self.absolute_url, parameters)
82        extra = {'hashkey': hashkey,
83                 'form.button.Save': 'Save'}
84        self.publish(path, self.basic_auth, extra=extra, request_method='GET')
85        response = self.publish(path, self.basic_auth, extra=extra,
86                                request_method='GET').getBody()
87
88        self.assert_(NOT_VALID.search(response))
89
90    def testCaptchaWidget(self):
91        # captcha core related issue, described in
92        # in http://plone.org/products/plone-comments/issues/5
93        resp = self.publish(self.portal.absolute_url(1) + "/captcha_widget")
94        self.assertEqual(resp.status / 100, 2)
95
96
97def test_suite():
98    suite = unittest.TestSuite()
99    suite.addTest(unittest.makeSuite(TestCaptchaWidget))
100    return suite
Note: See TracBrowser for help on using the repository browser.