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