1 | import string |
---|
2 | from os.path import abspath, dirname |
---|
3 | from base import * |
---|
4 | |
---|
5 | from DateTime import DateTime |
---|
6 | from Products.CMFFormController.ControllerState import ControllerState |
---|
7 | |
---|
8 | from testStatic import TestStaticValidator |
---|
9 | |
---|
10 | # Check PIL |
---|
11 | try: |
---|
12 | from PIL import ImageFont |
---|
13 | fontspath = abspath(dirname(dirname(__file__))) + '/data/fonts/vera/' |
---|
14 | font = ImageFont.truetype(fontspath+'VeraBd.ttf', 27) |
---|
15 | except ImportError: |
---|
16 | DYNAMIC_WORKABLE = False |
---|
17 | else: |
---|
18 | DYNAMIC_WORKABLE = True |
---|
19 | |
---|
20 | |
---|
21 | class DynamicMixin: |
---|
22 | def switchToDynamic(self): |
---|
23 | skins = self.portal.portal_skins |
---|
24 | for skin in skins.getSkinSelections(): |
---|
25 | path = skins.getSkinPath(skin) |
---|
26 | path = map( string.strip, string.split( path,',' )) |
---|
27 | try: |
---|
28 | i = path.index(LAYER_STATIC_CAPTCHAS) |
---|
29 | path.remove(LAYER_STATIC_CAPTCHAS) |
---|
30 | path.insert(i, LAYER_DYNAMIC_CAPTCHAS) |
---|
31 | except ValueError: |
---|
32 | pass |
---|
33 | path = string.join( path, ', ' ) |
---|
34 | skins.addSkinSelection( skin, path ) |
---|
35 | self._refreshSkinData() |
---|
36 | |
---|
37 | |
---|
38 | class TestPIL(unittest.TestCase): |
---|
39 | |
---|
40 | def testPILImageFont(self): |
---|
41 | if not DYNAMIC_WORKABLE: |
---|
42 | self.fail("You can not use Dynamic Captchas, only Static one " \ |
---|
43 | "unless install PIL with _imagingft C module into python, " \ |
---|
44 | "that is used for the current Zope instance.") |
---|
45 | |
---|
46 | |
---|
47 | class TestDynamic(DynamicMixin, ptc.FunctionalTestCase): |
---|
48 | |
---|
49 | def afterSetUp(self): |
---|
50 | self.loginAsPortalOwner() |
---|
51 | self.addProduct(PRODUCT_NAME) |
---|
52 | self.skins = self.portal.portal_skins |
---|
53 | self.switchToDynamic() |
---|
54 | |
---|
55 | self.captcha_key = self.portal.captcha_key |
---|
56 | self.hashkey = self.portal.getCaptcha() |
---|
57 | |
---|
58 | def test_GetCaptcha_Date(self): |
---|
59 | # *date* must present after parsing decrypted key |
---|
60 | decrypted_key = decrypt(self.captcha_key, self.hashkey) |
---|
61 | parsed_key = parseKey(decrypted_key) |
---|
62 | self.assertTrue('date' in parsed_key.keys()) |
---|
63 | |
---|
64 | def test_GetCaptcha_Key(self): |
---|
65 | decrypted_key = decrypt(self.captcha_key, self.hashkey) |
---|
66 | parsed_key = parseKey(decrypted_key) |
---|
67 | # *key* must present after parsing decrypted key |
---|
68 | self.assertTrue('key' in parsed_key.keys()) |
---|
69 | # index start from 0 and lower or equals to captchas count |
---|
70 | index = int(parsed_key['key']) |
---|
71 | self.assertTrue(index >= 0 and index <= len(utils.basic_english.words.split())) |
---|
72 | # encrypted key must be equals to word from the dictionary, |
---|
73 | # under index position and must be not empty string |
---|
74 | self.assertFalse(getWord(index) == "") |
---|
75 | |
---|
76 | def test_GetImage(self): |
---|
77 | # getCaptchaImage function must return image coded in hashkey same to |
---|
78 | # image get by 'key' after parsing decrypted key |
---|
79 | decrypted_key = decrypt(self.captcha_key, self.hashkey) |
---|
80 | parsed_key = parseKey(decrypted_key) |
---|
81 | |
---|
82 | img_html = self.publish( |
---|
83 | self.portal.absolute_url(1)+"/getCaptchaImage/%s" % self.hashkey) |
---|
84 | |
---|
85 | img_ctype = img_html.getHeader('content-type') |
---|
86 | self.assertTrue(img_ctype == 'image/jpeg', "Wrong content type for " \ |
---|
87 | "generated image: %s, must be 'image/jpeg'" % img_ctype) |
---|
88 | self.assertTrue(img_html.status == 200, "Wrong response status: " \ |
---|
89 | "'%s', must be '200'" % img_html.status) |
---|
90 | |
---|
91 | |
---|
92 | class TestDynamicValidator(DynamicMixin, TestStaticValidator): |
---|
93 | |
---|
94 | def afterSetUp(self): |
---|
95 | TestStaticValidator.afterSetUp(self) |
---|
96 | self.switchToDynamic() |
---|
97 | |
---|
98 | |
---|
99 | def test_suite(): |
---|
100 | suite = unittest.TestSuite() |
---|
101 | suite.addTest(unittest.makeSuite(TestPIL)) |
---|
102 | if DYNAMIC_WORKABLE: |
---|
103 | suite.addTest(unittest.makeSuite(TestDynamic)) |
---|
104 | suite.addTest(unittest.makeSuite(TestDynamicValidator)) |
---|
105 | return suite |
---|