source: products/quintagroup.captcha.core/trunk/quintagroup/captcha/core/tests/testDynamic.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.7 KB
Line 
1import string
2from os.path import abspath, dirname
3from base import *
4
5from DateTime import DateTime
6from Products.CMFFormController.ControllerState import ControllerState
7
8from testStatic import TestStaticValidator
9
10# Check PIL
11try:
12    from PIL import ImageFont
13    fontspath = abspath(dirname(dirname(__file__))) + '/data/fonts/vera/'
14    font = ImageFont.truetype(fontspath + 'VeraBd.ttf', 27)
15except ImportError:
16    DYNAMIC_WORKABLE = False
17else:
18    DYNAMIC_WORKABLE = True
19
20
21class 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
38class 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
47class 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        words = utils.basic_english.words.split()
72        self.assertTrue(index >= 0 and index <= len(words))
73        # encrypted key must be equals to word from the dictionary,
74        # under index position and must be not empty string
75        self.assertFalse(getWord(index) == "")
76
77    def test_GetImage(self):
78        # getCaptchaImage function must return image coded in hashkey same to
79        # image get by 'key' after parsing decrypted key
80        decrypted_key = decrypt(self.captcha_key, self.hashkey)
81        parsed_key = parseKey(decrypted_key)
82
83        img_html = self.publish(
84            self.portal.absolute_url(1) + "/getCaptchaImage/%s" % self.hashkey)
85
86        img_ctype = img_html.getHeader('content-type')
87        self.assertTrue(img_ctype == 'image/jpeg', "Wrong content type for " \
88            "generated image: %s, must be 'image/jpeg'" % img_ctype)
89        self.assertTrue(img_html.status == 200, "Wrong response status: " \
90            "'%s', must be '200'" % img_html.status)
91
92
93class TestDynamicValidator(DynamicMixin, TestStaticValidator):
94
95    def afterSetUp(self):
96        TestStaticValidator.afterSetUp(self)
97        self.switchToDynamic()
98
99
100def test_suite():
101    suite = unittest.TestSuite()
102    suite.addTest(unittest.makeSuite(TestPIL))
103    if DYNAMIC_WORKABLE:
104        suite.addTest(unittest.makeSuite(TestDynamic))
105        suite.addTest(unittest.makeSuite(TestDynamicValidator))
106    return suite
Note: See TracBrowser for help on using the repository browser.