source: products/quintagroup.captcha.core/trunk/quintagroup/captcha/core/tests/testDynamic.py @ 3600

Last change on this file since 3600 was 3600, checked in by vmaksymiv, 12 years ago

PPP fixes

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