1 | import string |
---|
2 | from base import * |
---|
3 | |
---|
4 | from Products.CMFCore.DirectoryView import registerDirectory |
---|
5 | from Products.CMFCore.DirectoryView import addDirectoryViews |
---|
6 | from Products.CMFCore.DirectoryView import DirectoryView |
---|
7 | |
---|
8 | NOT_VALID = re.compile("Please re\-enter validation code") |
---|
9 | |
---|
10 | class 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 | # index of word number starts from 1, but index of dictionary starts from 0 |
---|
47 | key = getWord(int(parseKey(decrypt(self.captcha_key, hashkey))['key'])-1 ) |
---|
48 | parameters = 'form.submitted=1&key=%s' % key |
---|
49 | path = '%s/test_form?%s' % (self.absolute_url, parameters) |
---|
50 | extra = {'hashkey': hashkey, |
---|
51 | 'form.button.Save': 'Save'} |
---|
52 | response = self.publish(path, self.basic_auth, extra=extra, request_method='GET').getBody() |
---|
53 | self.assert_(not NOT_VALID.search(response)) |
---|
54 | |
---|
55 | def testSubmitWrongCaptcha(self): |
---|
56 | hashkey = self.portal.getCaptcha() |
---|
57 | parameters = 'form.submitted=1&key=fdfgh' |
---|
58 | path = '%s/test_form?%s' % (self.absolute_url, parameters) |
---|
59 | extra = {'hashkey': hashkey, |
---|
60 | 'form.button.Save': 'Save'} |
---|
61 | response = self.publish(path, self.basic_auth, extra=extra, request_method='GET').getBody() |
---|
62 | self.assert_(NOT_VALID.search(response)) |
---|
63 | |
---|
64 | def testSubmitRightCaptchaTwice(self): |
---|
65 | hashkey = self.portal.getCaptcha() |
---|
66 | key = getWord(int(parseKey(decrypt(self.captcha_key, hashkey))['key'])-1) |
---|
67 | parameters = 'form.submitted=1&key=%s'%key |
---|
68 | path = '%s/test_form?%s'%(self.absolute_url, parameters) |
---|
69 | extra = {'hashkey': hashkey, |
---|
70 | 'form.button.Save': 'Save'} |
---|
71 | self.publish(path, self.basic_auth, extra=extra, request_method='GET') |
---|
72 | response = self.publish(path, self.basic_auth, extra=extra, request_method='GET').getBody() |
---|
73 | |
---|
74 | self.assert_(NOT_VALID.search(response)) |
---|
75 | |
---|
76 | |
---|
77 | def test_suite(): |
---|
78 | suite = unittest.TestSuite() |
---|
79 | suite.addTest(unittest.makeSuite(TestCaptchaWidget)) |
---|
80 | return suite |
---|