[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 | from zope.publisher.browser import TestRequest |
---|
| 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 | |
---|
[2075] | 21 | from quintagroup.z3cform.captcha import Captcha |
---|
| 22 | from quintagroup.z3cform.captcha import CaptchaWidget |
---|
| 23 | from quintagroup.z3cform.captcha.widget import CaptchaWidgetFactory |
---|
| 24 | |
---|
[2074] | 25 | @onsetup |
---|
| 26 | def setup_product(): |
---|
| 27 | fiveconfigure.debug_mode = True |
---|
| 28 | # Register z3c namespace first |
---|
| 29 | import z3c.form |
---|
| 30 | zcml.load_config('meta.zcml', z3c.form) |
---|
| 31 | # Now register quintagroup.z3cform.captcha package |
---|
| 32 | import quintagroup.captcha.core |
---|
| 33 | import quintagroup.z3cform.captcha |
---|
| 34 | zcml.load_config('configure.zcml', quintagroup.z3cform.captcha) |
---|
| 35 | fiveconfigure.debug_mode = False |
---|
| 36 | ztc.installPackage('quintagroup.captcha.core') |
---|
| 37 | |
---|
| 38 | setup_product() |
---|
| 39 | ptc.setupPloneSite(extension_profiles=['quintagroup.captcha.core:default',]) |
---|
| 40 | |
---|
| 41 | |
---|
[2075] | 42 | class TestRegistrations(ptc.PloneTestCase): |
---|
| 43 | |
---|
| 44 | def afterSetUp(self): |
---|
| 45 | super(TestRegistrations, self).afterSetUp() |
---|
| 46 | self.request = self.app.REQUEST |
---|
| 47 | alsoProvides(self.request, IFormLayer) |
---|
| 48 | |
---|
| 49 | def testCaptchaFieldInterface(self): |
---|
| 50 | self.assertEqual(IField.implementedBy(Captcha), True) |
---|
| 51 | |
---|
| 52 | def testCaptchaWidgetInterface(self): |
---|
| 53 | self.assertEqual(IFieldWidget.implementedBy(CaptchaWidgetFactory), True) |
---|
| 54 | |
---|
| 55 | def testWidgetRegistration(self): |
---|
| 56 | cfield = Captcha() |
---|
| 57 | cwidget = queryMultiAdapter((cfield, self.request), IFieldWidget) |
---|
| 58 | self.assertNotEqual(cwidget, None) |
---|
| 59 | |
---|
| 60 | def testValidatorRegistration(self): |
---|
| 61 | cfield = Captcha() |
---|
| 62 | cvalidator = queryMultiAdapter((None, self.request, None, cfield, None), |
---|
| 63 | IValidator) |
---|
| 64 | self.assertNotEqual(cvalidator, None) |
---|
| 65 | |
---|
| 66 | def testErrorViewRegistration(self): |
---|
| 67 | cfield = Captcha() |
---|
| 68 | cwidget = queryMultiAdapter((cfield, self.request), IFieldWidget) |
---|
| 69 | error = ValueError() |
---|
| 70 | eview = queryMultiAdapter( |
---|
| 71 | (error, self.request, cwidget, cfield, None, None), |
---|
| 72 | IErrorViewSnippet) |
---|
| 73 | self.assertNotEqual(eview, None) |
---|
| 74 | |
---|
| 75 | |
---|
[2076] | 76 | class TestCaptchaWidget(ptc.PloneTestCase): |
---|
| 77 | |
---|
| 78 | def afterSetUp(self): |
---|
| 79 | super(TestCaptchaWidget, self).afterSetUp() |
---|
| 80 | self.request = self.app.REQUEST |
---|
| 81 | alsoProvides(self.request, IFormLayer) |
---|
| 82 | |
---|
| 83 | cform = form.BaseForm(self.portal, self.request) |
---|
| 84 | cform.prefix = "" |
---|
| 85 | cwidget = CaptchaWidget(self.request) |
---|
| 86 | cwidget.form = cform |
---|
| 87 | self.html = cwidget.render() |
---|
| 88 | |
---|
| 89 | def testHidden(self): |
---|
| 90 | HIDDENTAG = '<input\s+[^>]*(?:' \ |
---|
| 91 | '(?:type="hidden"\s*)|' \ |
---|
| 92 | '(?:name="hashkey"\s*)|' \ |
---|
| 93 | '(?:value="(?P<value>[0-9a-fA-F]+)"\s*)' \ |
---|
| 94 | '){3}/>' |
---|
| 95 | open('/tmp/z3c.form.html','w').write(self.html) |
---|
| 96 | hidden = re.search(HIDDENTAG, self.html) |
---|
| 97 | self.assertTrue(hidden and hidden.group('value')) |
---|
| 98 | |
---|
| 99 | def testImg(self): |
---|
| 100 | IMAGETAG = '<img\s+[^>]*src=\"' \ |
---|
| 101 | '(?P<src>[^\"]*/getCaptchaImage/[0-9a-fA-F]+)' \ |
---|
| 102 | '\"[^>]*>' |
---|
| 103 | img = re.search(IMAGETAG, self.html) |
---|
| 104 | self.assertTrue(img and img.group('src')) |
---|
| 105 | |
---|
| 106 | def testTextField(self): |
---|
| 107 | FIELDTAG = '<input\s+[^>]*type=\"text\"\s*[^>]*>' |
---|
| 108 | self.assertEqual(re.search(FIELDTAG, self.html) is not None, True) |
---|
| 109 | |
---|
| 110 | |
---|
[2074] | 111 | def test_suite(): |
---|
| 112 | suite = unittest.TestSuite() |
---|
[2075] | 113 | suite.addTest(unittest.makeSuite(TestRegistrations)) |
---|
[2076] | 114 | suite.addTest(unittest.makeSuite(TestCaptchaWidget)) |
---|
[2074] | 115 | return suite |
---|