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

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

#172: move tests module into tests package. Split into testInstall and testWidget. Add testing test_captcha skin layer with captcha test form

  • Property svn:eol-style set to native
File size: 3.4 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")
9
10class TestCaptchaWidget(ptc.FunctionalTestCase):
11
12    def addTestLayer(self):
13        # Install test_captcha skin layer
14        registerDirectory('tests', GLOBALS)
15        skins = self.portal.portal_skins
16        addDirectoryViews(skins, 'tests', GLOBALS)
17        skinName = skins.getDefaultSkin()
18        paths = map(string.strip, skins.getSkinPath(skinName).split(','))
19        paths.insert(paths.index('custom')+1, 'test_captcha')
20        skins.addSkinSelection(skinName, ','.join(paths))
21        self._refreshSkinData()
22
23    def afterSetUp(self):
24        self.loginAsPortalOwner()
25        self.addProduct(PRODUCT_NAME)
26        self.addTestLayer()
27        self.portal.invokeFactory('Document', 'index_html')
28        self.portal['index_html'].allowDiscussion(True)
29        self.absolute_url = self.portal['index_html'].absolute_url_path()
30
31        self.basic_auth = ':'.join((portal_owner,default_password))
32        self.captcha_key = self.portal.captcha_key
33
34    def testImage(self):
35        path = '%s/test_form' % self.absolute_url
36        response = self.publish(path, self.basic_auth, request_method='GET').getBody()
37        patt = re.compile('\s+src="%s(/getCaptchaImage/[0-9a-fA-F]+)"' % self.portal.absolute_url())
38        match_obj = patt.search(response)
39
40        img_url = match_obj.group(1)
41        content_type = self.publish('/plone' + img_url, self.basic_auth).getHeader('content-type')
42        self.assert_(content_type.startswith('image'))
43
44    def testSubmitRightCaptcha(self):
45        hashkey = self.portal.getCaptcha()
46        key = getWord(int(parseKey(decrypt(self.captcha_key, hashkey))['key']))
47        parameters = 'form.submitted=1&key=%s' % key
48        path = '%s/test_form?%s' % (self.absolute_url, parameters)
49        extra = {'hashkey': hashkey,
50                 'form.button.Save': 'Save'}
51        response = self.publish(path, self.basic_auth, extra=extra, request_method='GET').getBody()
52
53        open('/tmp/right.captcha.html','w').write(response)
54
55        self.assert_(not NOT_VALID.search(response))
56
57    def testSubmitWrongCaptcha(self):
58        hashkey = self.portal.getCaptcha()
59        parameters = 'form.submitted=1&key=fdfgh'
60        path = '%s/test_form?%s' % (self.absolute_url, parameters)
61        extra = {'hashkey': hashkey,
62                 'form.button.Save': 'Save'}
63        response = self.publish(path, self.basic_auth, extra=extra, request_method='GET').getBody()
64        self.assert_(NOT_VALID.search(response))
65
66    def testSubmitRightCaptchaTwice(self):
67        hashkey = self.portal.getCaptcha()
68        key = getWord(int(parseKey(decrypt(self.captcha_key, hashkey))['key']))
69        parameters = 'form.submitted=1&key=%s'%key
70        path = '%s/test_form?%s'%(self.absolute_url, parameters)
71        extra = {'hashkey': hashkey,
72                 'form.button.Save': 'Save'}
73        self.publish(path, self.basic_auth, extra=extra, request_method='GET')
74        response = self.publish(path, self.basic_auth, extra=extra, request_method='GET').getBody()
75
76        self.assert_(NOT_VALID.search(response))
77
78
79def test_suite():
80    suite = unittest.TestSuite()
81    suite.addTest(unittest.makeSuite(TestCaptchaWidget))
82    return suite
Note: See TracBrowser for help on using the repository browser.