[1992] | 1 | import string |
---|
[2450] | 2 | from os.path import abspath, dirname |
---|
[3144] | 3 | import unittest |
---|
[1992] | 4 | |
---|
[3144] | 5 | from Products.PloneTestCase import PloneTestCase as ptc |
---|
| 6 | from quintagroup.captcha.core.config import LAYER_STATIC_CAPTCHAS, \ |
---|
| 7 | LAYER_DYNAMIC_CAPTCHAS, PRODUCT_NAME |
---|
| 8 | |
---|
| 9 | from quintagroup.captcha.core import utils |
---|
[3132] | 10 | from quintagroup.captcha.core.utils import getWord, decrypt, parseKey |
---|
[1992] | 11 | |
---|
| 12 | from testStatic import TestStaticValidator |
---|
| 13 | |
---|
[2450] | 14 | # Check PIL |
---|
| 15 | try: |
---|
| 16 | from PIL import ImageFont |
---|
| 17 | fontspath = abspath(dirname(dirname(__file__))) + '/data/fonts/vera/' |
---|
[3128] | 18 | font = ImageFont.truetype(fontspath + 'VeraBd.ttf', 27) |
---|
[2450] | 19 | except ImportError: |
---|
| 20 | DYNAMIC_WORKABLE = False |
---|
| 21 | else: |
---|
| 22 | DYNAMIC_WORKABLE = True |
---|
| 23 | |
---|
| 24 | |
---|
[1992] | 25 | class 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 | |
---|
| 42 | class 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] | 51 | class 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] | 96 | class TestDynamicValidator(DynamicMixin, TestStaticValidator): |
---|
| 97 | |
---|
| 98 | def afterSetUp(self): |
---|
| 99 | TestStaticValidator.afterSetUp(self) |
---|
| 100 | self.switchToDynamic() |
---|
| 101 | |
---|
| 102 | |
---|
| 103 | def 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 |
---|