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