[2076] | 1 | import re |
---|
[2074] | 2 | import unittest |
---|
| 3 | |
---|
[2075] | 4 | from zope.interface import alsoProvides |
---|
| 5 | from zope.schema.interfaces import IField |
---|
| 6 | from zope.component import queryMultiAdapter |
---|
| 7 | |
---|
[2076] | 8 | from z3c.form import form |
---|
[2075] | 9 | from z3c.form.interfaces import IFormLayer |
---|
| 10 | from z3c.form.interfaces import IValidator |
---|
| 11 | from z3c.form.interfaces import IFieldWidget |
---|
| 12 | from z3c.form.interfaces import IErrorViewSnippet |
---|
| 13 | |
---|
[2074] | 14 | from Products.Five import zcml |
---|
| 15 | from Products.Five import fiveconfigure |
---|
| 16 | from Testing import ZopeTestCase as ztc |
---|
| 17 | from Products.PloneTestCase.layer import onsetup |
---|
| 18 | from Products.PloneTestCase import PloneTestCase as ptc |
---|
| 19 | |
---|
[3571] | 20 | from quintagroup.captcha.core.utils import decrypt, getWord, parseKey, ConversionError |
---|
[2077] | 21 | from quintagroup.captcha.core.tests.base import testPatch |
---|
| 22 | from quintagroup.captcha.core.tests.testWidget import addTestLayer |
---|
| 23 | |
---|
[2075] | 24 | from quintagroup.z3cform.captcha import Captcha |
---|
| 25 | from quintagroup.z3cform.captcha import CaptchaWidget |
---|
[2077] | 26 | from quintagroup.z3cform.captcha import CaptchaWidgetFactory |
---|
| 27 | from quintagroup.z3cform.captcha.validator import CaptchaValidator |
---|
[2075] | 28 | |
---|
[3546] | 29 | |
---|
[2074] | 30 | @onsetup |
---|
| 31 | def setup_product(): |
---|
| 32 | fiveconfigure.debug_mode = True |
---|
[2078] | 33 | # Register z3c namespace first (work before plone-3.3) |
---|
[2074] | 34 | import z3c.form |
---|
| 35 | zcml.load_config('meta.zcml', z3c.form) |
---|
| 36 | # Now register quintagroup.z3cform.captcha package |
---|
| 37 | import quintagroup.captcha.core |
---|
| 38 | import quintagroup.z3cform.captcha |
---|
| 39 | zcml.load_config('configure.zcml', quintagroup.z3cform.captcha) |
---|
| 40 | fiveconfigure.debug_mode = False |
---|
| 41 | ztc.installPackage('quintagroup.captcha.core') |
---|
| 42 | |
---|
| 43 | setup_product() |
---|
[3546] | 44 | ptc.setupPloneSite(extension_profiles=['quintagroup.captcha.core:default', ]) |
---|
[2074] | 45 | |
---|
| 46 | |
---|
[2075] | 47 | class TestRegistrations(ptc.PloneTestCase): |
---|
| 48 | |
---|
| 49 | def afterSetUp(self): |
---|
| 50 | self.request = self.app.REQUEST |
---|
| 51 | alsoProvides(self.request, IFormLayer) |
---|
| 52 | |
---|
| 53 | def testCaptchaFieldInterface(self): |
---|
| 54 | self.assertEqual(IField.implementedBy(Captcha), True) |
---|
| 55 | |
---|
| 56 | def testCaptchaWidgetInterface(self): |
---|
[3546] | 57 | self.assertEqual( |
---|
| 58 | IFieldWidget.implementedBy(CaptchaWidgetFactory), True) |
---|
[2075] | 59 | |
---|
| 60 | def testWidgetRegistration(self): |
---|
| 61 | cfield = Captcha() |
---|
| 62 | cwidget = queryMultiAdapter((cfield, self.request), IFieldWidget) |
---|
| 63 | self.assertNotEqual(cwidget, None) |
---|
| 64 | |
---|
| 65 | def testValidatorRegistration(self): |
---|
| 66 | cfield = Captcha() |
---|
[3546] | 67 | cvalidator = queryMultiAdapter( |
---|
| 68 | (None, self.request, None, cfield, None), |
---|
| 69 | IValidator) |
---|
[2075] | 70 | self.assertNotEqual(cvalidator, None) |
---|
| 71 | |
---|
| 72 | def testErrorViewRegistration(self): |
---|
| 73 | cfield = Captcha() |
---|
| 74 | cwidget = queryMultiAdapter((cfield, self.request), IFieldWidget) |
---|
| 75 | error = ValueError() |
---|
| 76 | eview = queryMultiAdapter( |
---|
| 77 | (error, self.request, cwidget, cfield, None, None), |
---|
| 78 | IErrorViewSnippet) |
---|
| 79 | self.assertNotEqual(eview, None) |
---|
| 80 | |
---|
| 81 | |
---|
[2076] | 82 | class TestCaptchaWidget(ptc.PloneTestCase): |
---|
| 83 | |
---|
| 84 | def afterSetUp(self): |
---|
| 85 | self.request = self.app.REQUEST |
---|
| 86 | alsoProvides(self.request, IFormLayer) |
---|
| 87 | |
---|
| 88 | cform = form.BaseForm(self.portal, self.request) |
---|
| 89 | cform.prefix = "" |
---|
| 90 | cwidget = CaptchaWidget(self.request) |
---|
| 91 | cwidget.form = cform |
---|
| 92 | self.html = cwidget.render() |
---|
| 93 | |
---|
| 94 | def testHidden(self): |
---|
| 95 | HIDDENTAG = '<input\s+[^>]*(?:' \ |
---|
| 96 | '(?:type="hidden"\s*)|' \ |
---|
| 97 | '(?:name="hashkey"\s*)|' \ |
---|
| 98 | '(?:value="(?P<value>[0-9a-fA-F]+)"\s*)' \ |
---|
| 99 | '){3}/>' |
---|
[3546] | 100 | open('/tmp/z3c.form.html', 'w').write(self.html) |
---|
[2076] | 101 | hidden = re.search(HIDDENTAG, self.html) |
---|
| 102 | self.assertTrue(hidden and hidden.group('value')) |
---|
| 103 | |
---|
| 104 | def testImg(self): |
---|
| 105 | IMAGETAG = '<img\s+[^>]*src=\"' \ |
---|
| 106 | '(?P<src>[^\"]*/getCaptchaImage/[0-9a-fA-F]+)' \ |
---|
| 107 | '\"[^>]*>' |
---|
| 108 | img = re.search(IMAGETAG, self.html) |
---|
| 109 | self.assertTrue(img and img.group('src')) |
---|
| 110 | |
---|
| 111 | def testTextField(self): |
---|
| 112 | FIELDTAG = '<input\s+[^>]*type=\"text\"\s*[^>]*>' |
---|
| 113 | self.assertEqual(re.search(FIELDTAG, self.html) is not None, True) |
---|
| 114 | |
---|
[3546] | 115 | |
---|
[2077] | 116 | class TestCaptchaValidator(ptc.PloneTestCase): |
---|
| 117 | |
---|
| 118 | def afterSetUp(self): |
---|
| 119 | self.request = self.app.REQUEST |
---|
| 120 | alsoProvides(self.request, IFormLayer) |
---|
| 121 | # prepare context |
---|
| 122 | self.loginAsPortalOwner() |
---|
| 123 | testPatch() |
---|
| 124 | addTestLayer(self) |
---|
| 125 | self.captcha_key = self.portal.captcha_key |
---|
| 126 | # prepare captcha data |
---|
| 127 | self.hashkey = self.portal.getCaptcha() |
---|
| 128 | self.request.form['hashkey'] = self.hashkey |
---|
| 129 | # prepare validator |
---|
| 130 | cform = form.BaseForm(self.portal, self.request) |
---|
| 131 | cform.prefix = "" |
---|
| 132 | cwidget = CaptchaWidget(self.request) |
---|
| 133 | cwidget.form = cform |
---|
[3546] | 134 | self.validator = CaptchaValidator(self.portal, self.request, None, |
---|
| 135 | None, cwidget) |
---|
[2077] | 136 | |
---|
| 137 | def testSubmitRightCaptcha(self): |
---|
| 138 | decrypted = decrypt(self.captcha_key, self.hashkey) |
---|
[3546] | 139 | key = getWord(int(parseKey(decrypted)['key']) - 1) |
---|
[2077] | 140 | try: |
---|
[3546] | 141 | self.validator.validate(key) |
---|
[2077] | 142 | except ConversionError, e: |
---|
| 143 | self.fail("Rised unexpected %s error on right captcha submit" % e) |
---|
| 144 | |
---|
| 145 | def testSubmitWrongCaptcha(self): |
---|
| 146 | try: |
---|
[3546] | 147 | self.validator.validate("wrong key") |
---|
[2077] | 148 | except ValueError, e: |
---|
| 149 | self.assertEqual(str(e), u'Please re-enter validation code.') |
---|
| 150 | else: |
---|
| 151 | self.fail("No ValueError rised on wrong captcha key submit") |
---|
| 152 | |
---|
| 153 | def testSubmitRightCaptchaTwice(self): |
---|
| 154 | decrypted = decrypt(self.captcha_key, self.hashkey) |
---|
[3546] | 155 | key = getWord(int(parseKey(decrypted)['key']) - 1) |
---|
[2077] | 156 | self.validator.validate(key) |
---|
| 157 | try: |
---|
[3546] | 158 | self.validator.validate(key) |
---|
[2077] | 159 | except ValueError, e: |
---|
| 160 | self.assertEqual(str(e), u'Please re-enter validation code.') |
---|
| 161 | else: |
---|
[3546] | 162 | self.fail("No ValueError rised on right captcha key " |
---|
[2077] | 163 | "submitting twice") |
---|
| 164 | |
---|
| 165 | |
---|
[2074] | 166 | def test_suite(): |
---|
| 167 | suite = unittest.TestSuite() |
---|
[2075] | 168 | suite.addTest(unittest.makeSuite(TestRegistrations)) |
---|
[2076] | 169 | suite.addTest(unittest.makeSuite(TestCaptchaWidget)) |
---|
[2077] | 170 | suite.addTest(unittest.makeSuite(TestCaptchaValidator)) |
---|
[2074] | 171 | return suite |
---|