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

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

PPP fixes

  • Property svn:eol-style set to native
File size: 3.8 KB
RevLine 
[1992]1import string
[2450]2from os.path import abspath, dirname
[3144]3import unittest
[1992]4
[3144]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
[3132]10from quintagroup.captcha.core.utils import getWord, decrypt, parseKey
[1992]11
12from testStatic import TestStaticValidator
13
[2450]14# Check PIL
15try:
16    from PIL import ImageFont
17    fontspath = abspath(dirname(dirname(__file__))) + '/data/fonts/vera/'
[3128]18    font = ImageFont.truetype(fontspath + 'VeraBd.ttf', 27)
[2450]19except ImportError:
20    DYNAMIC_WORKABLE = False
21else:
22    DYNAMIC_WORKABLE = True
23
24
[1992]25class DynamicMixin:
26    def switchToDynamic(self):
27        skins = self.portal.portal_skins
28        for skin in skins.getSkinSelections():
29            path = skins.getSkinPath(skin)
[3128]30            path = map(string.strip, string.split(path, ','))
[1992]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
[3128]37            path = string.join(path, ', ')
38            skins.addSkinSelection(skin, path)
[1992]39            self._refreshSkinData()
40
[2450]41
42class TestPIL(unittest.TestCase):
[3128]43
[2450]44    def testPILImageFont(self):
45        if not DYNAMIC_WORKABLE:
[3600]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.")
[2450]49
50
[1992]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'])
[3128]75        words = utils.basic_english.words.split()
76        self.assertTrue(index >= 0 and index <= len(words))
[1992]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
[3128]83        # image get by 'key' after parsing decrypted key
[1992]84
85        img_html = self.publish(
[3128]86            self.portal.absolute_url(1) + "/getCaptchaImage/%s" % self.hashkey)
[1992]87
88        img_ctype = img_html.getHeader('content-type')
[3600]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)
[1992]94
[2450]95
[1992]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()
[2450]105    suite.addTest(unittest.makeSuite(TestPIL))
106    if DYNAMIC_WORKABLE:
107        suite.addTest(unittest.makeSuite(TestDynamic))
108        suite.addTest(unittest.makeSuite(TestDynamicValidator))
[1992]109    return suite
Note: See TracBrowser for help on using the repository browser.