[2096] | 1 | import re |
---|
[2097] | 2 | import string |
---|
[2096] | 3 | import unittest |
---|
| 4 | |
---|
| 5 | from Products.Five import zcml |
---|
| 6 | from Products.Five import fiveconfigure |
---|
| 7 | from Testing import ZopeTestCase as ztc |
---|
| 8 | from Products.PloneTestCase.layer import onsetup |
---|
| 9 | from Products.PloneTestCase import PloneTestCase as ptc |
---|
[2098] | 10 | from Products.CMFCore.permissions import View |
---|
| 11 | from Products.Archetypes.atapi import StringField |
---|
[2099] | 12 | from Products.Archetypes.Registry import availableWidgets |
---|
[2100] | 13 | from Products.validation import validation |
---|
[2096] | 14 | |
---|
[2099] | 15 | from quintagroup.pfg.captcha import CaptchaField |
---|
[2098] | 16 | from quintagroup.pfg.captcha import CaptchaWidget |
---|
| 17 | from quintagroup.pfg.captcha import CaptchaValidator |
---|
[2099] | 18 | from quintagroup.pfg.captcha.widget import CAPTCHA_MACRO |
---|
[2098] | 19 | from quintagroup.pfg.captcha.field import CAPTCHA_ID, HIDDEN_FIELDS |
---|
| 20 | |
---|
| 21 | _marker = object() |
---|
[2100] | 22 | PACKAGES = [ |
---|
[2096] | 23 | 'quintagroup.captcha.core', |
---|
| 24 | 'quintagroup.pfg.captcha', |
---|
| 25 | ] |
---|
[2100] | 26 | PROFILES = [p+':default' for p in PACKAGES] |
---|
[2101] | 27 | REQUIREMENTS = ['PloneFormGen',] + PACKAGES |
---|
[2096] | 28 | |
---|
| 29 | @onsetup |
---|
| 30 | def setup_product(): |
---|
| 31 | fiveconfigure.debug_mode = True |
---|
| 32 | import quintagroup.pfg.captcha |
---|
| 33 | zcml.load_config('configure.zcml', quintagroup.pfg.captcha) |
---|
| 34 | fiveconfigure.debug_mode = False |
---|
[2098] | 35 | ztc.installProduct('PloneFormGen') |
---|
[2096] | 36 | ztc.installPackage('quintagroup.pfg.captcha') |
---|
| 37 | ztc.installPackage('quintagroup.captcha.core') |
---|
| 38 | |
---|
| 39 | setup_product() |
---|
[2731] | 40 | ptc.setupPloneSite() #products=['PloneFormGen',], extension_profiles=PROFILES) |
---|
[2096] | 41 | |
---|
| 42 | |
---|
[2731] | 43 | class PFGCaptchaTestCaseMixin(object): |
---|
[2097] | 44 | |
---|
[2731] | 45 | def installPFGCaptcha(self): |
---|
| 46 | for p in REQUIREMENTS: |
---|
| 47 | self.addProduct(p) |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | class PFGCaptchaAutoInstallTestCase(PFGCaptchaTestCaseMixin, ptc.PloneTestCase): |
---|
| 51 | def afterSetUp(self): |
---|
| 52 | self.installPFGCaptcha() |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | class TestInstallations(PFGCaptchaAutoInstallTestCase): |
---|
| 56 | |
---|
[2097] | 57 | def testInstalledProducts(self): |
---|
| 58 | qi = self.portal.portal_quickinstaller |
---|
| 59 | installed = [p['id'] for p in qi.listInstalledProducts()] |
---|
[2101] | 60 | for p in REQUIREMENTS: |
---|
[2097] | 61 | if p.startswith('Products'): |
---|
| 62 | p = p[9:] |
---|
| 63 | self.assertEqual(p in installed, True, |
---|
| 64 | '"%s" product not installed' % p) |
---|
| 65 | |
---|
| 66 | def testType(self): |
---|
| 67 | pt = self.portal.portal_types |
---|
| 68 | self.assertEqual("CaptchaField" in pt.objectIds(), True) |
---|
| 69 | |
---|
| 70 | def testPortalFactory(self): |
---|
| 71 | pf = self.portal.portal_factory |
---|
| 72 | self.assertEqual("CaptchaField" in pf.getFactoryTypes(), True) |
---|
| 73 | |
---|
| 74 | def testWorkflow(self): |
---|
| 75 | pw = self.portal.portal_workflow |
---|
| 76 | default_chain = pw.getDefaultChain() |
---|
| 77 | cf_chain = pw.getChainForPortalType('CaptchaField') |
---|
| 78 | self.assertNotEqual(cf_chain == default_chain , True) |
---|
| 79 | |
---|
| 80 | def testNotToList(self): |
---|
| 81 | navtree = self.portal.portal_properties.navtree_properties |
---|
| 82 | mtNotToList = navtree.getProperty("metaTypesNotToList") |
---|
| 83 | self.assertEqual('CaptchaField' in mtNotToList, True) |
---|
| 84 | |
---|
| 85 | def testSkins(self): |
---|
| 86 | ps = self.portal.portal_skins |
---|
| 87 | self.assertEqual("qplonecaptchafield" in ps.objectIds(), True) |
---|
| 88 | for sname, spath in ps.getSkinPaths(): |
---|
| 89 | paths = filter(None, map(string.strip, spath.split(','))) |
---|
| 90 | self.assertEqual("qplonecaptchafield" in paths, True, |
---|
| 91 | '"qplonecaptchafield" layer not present in "%s" skin' % sname) |
---|
| 92 | |
---|
[2098] | 93 | |
---|
[2731] | 94 | class TestCaptchaField(PFGCaptchaAutoInstallTestCase): |
---|
[2098] | 95 | |
---|
| 96 | def afterSetUp(self): |
---|
[2731] | 97 | super(TestCaptchaField, self).afterSetUp() |
---|
[2098] | 98 | self.folder.invokeFactory('FormFolder', 'ff1') |
---|
| 99 | self.ff1 = getattr(self.folder, 'ff1') |
---|
| 100 | self.ff1.invokeFactory('CaptchaField', 'captcha_field') |
---|
[2099] | 101 | |
---|
[2098] | 102 | def testId(self): |
---|
| 103 | """CaptchaField has always CAPTCHA_ID id.""" |
---|
| 104 | self.assertEqual(CAPTCHA_ID in self.ff1, True) |
---|
| 105 | self.assertNotEqual('captcha_field' in self.ff1, True) |
---|
| 106 | |
---|
| 107 | def testSchema(self): |
---|
| 108 | cf = getattr(self.ff1, CAPTCHA_ID) |
---|
| 109 | schema = cf.Schema() |
---|
| 110 | for field in HIDDEN_FIELDS: |
---|
| 111 | visibility = schema[field].widget.visible |
---|
| 112 | self.assertEqual(visibility, {'view':'invisible','edit':'invisible'}, |
---|
| 113 | '"%s" field is not hidden, but %s:' % (field, visibility)) |
---|
| 114 | |
---|
| 115 | def testFGField(self): |
---|
| 116 | cf = getattr(self.ff1, CAPTCHA_ID) |
---|
| 117 | fgField = getattr(cf, 'fgField', _marker) |
---|
| 118 | self.assertNotEqual(fgField, _marker) |
---|
| 119 | # Test fgField properties |
---|
| 120 | self.assertEqual(type(fgField), StringField) |
---|
| 121 | self.assertEqual(bool(fgField.searchable), False ) |
---|
[2099] | 122 | self.assertEqual(fgField.write_permission, View) |
---|
[2098] | 123 | self.assertEqual(type(fgField.widget), CaptchaWidget) |
---|
| 124 | validators = [v.__class__ for v in fgField.validators._chain] |
---|
| 125 | self.assertEqual(CaptchaValidator in validators, True) |
---|
[2097] | 126 | |
---|
[2099] | 127 | |
---|
[2731] | 128 | class TestCaptchaWidget(PFGCaptchaAutoInstallTestCase): |
---|
[2099] | 129 | |
---|
| 130 | CF = CaptchaField.__module__ + '.CaptchaField' |
---|
| 131 | CW = CaptchaWidget.__module__ + '.CaptchaWidget' |
---|
| 132 | |
---|
| 133 | def afterSetUp(self): |
---|
[2731] | 134 | super(TestCaptchaWidget, self).afterSetUp() |
---|
[2099] | 135 | self.widgets = dict(availableWidgets()) |
---|
| 136 | |
---|
| 137 | def testRegistration(self): |
---|
| 138 | self.assertEqual(self.CW in self.widgets, True) |
---|
| 139 | cw = self.widgets[self.CW] |
---|
| 140 | self.assertEqual(self.CF in cw.used_for, True) |
---|
| 141 | |
---|
| 142 | def testWidgetMacro(self): |
---|
| 143 | widget_macro = self.widgets[self.CW].klass._properties['macro'] |
---|
| 144 | self.assertEqual(widget_macro, CAPTCHA_MACRO) |
---|
| 145 | |
---|
| 146 | def testWidgetMacroAccessable(self): |
---|
| 147 | macro = self.portal.restrictedTraverse(CAPTCHA_MACRO) |
---|
| 148 | self.assertNotEqual(macro, None) |
---|
| 149 | |
---|
[2100] | 150 | |
---|
[2731] | 151 | class TestCaptchaValidator(PFGCaptchaAutoInstallTestCase): |
---|
[2100] | 152 | |
---|
| 153 | def getValidator(self): |
---|
| 154 | return validation.validatorFor('isCaptchaCorrect') |
---|
| 155 | |
---|
| 156 | def testRegistration(self): |
---|
| 157 | self.assertEqual(self.getValidator().__class__, CaptchaValidator) |
---|
| 158 | |
---|
| 159 | def patchCoreValidator(self, status, error=""): |
---|
| 160 | # Patch quintagroup.catpcha.core' captcha_validator |
---|
| 161 | class MockState: |
---|
| 162 | def __init__(self, status, error=""): |
---|
| 163 | self.status = status |
---|
| 164 | self.errors = {'key':[error,]} |
---|
| 165 | |
---|
| 166 | patch_validator = lambda : MockState(status, error) |
---|
| 167 | self.portal.captcha_validator = patch_validator |
---|
| 168 | |
---|
| 169 | def testValidationSuccess(self): |
---|
| 170 | # PFG validator must call patched quintagroup.captcha.core' |
---|
| 171 | # captcha_validator and successfully validate the data. |
---|
| 172 | validator = self.getValidator() |
---|
| 173 | self.patchCoreValidator("success") |
---|
| 174 | result = validator('test', instance=self.portal) |
---|
| 175 | self.assertEqual(result, 1) |
---|
| 176 | |
---|
| 177 | def testValidationFailure(self): |
---|
| 178 | # PFG validator must call patched quintagroup.captcha.core' |
---|
| 179 | # captcha_validator and return error. |
---|
| 180 | validator = self.getValidator() |
---|
| 181 | self.patchCoreValidator("failure", "Wrong value") |
---|
| 182 | result = validator('test', instance=self.portal) |
---|
| 183 | self.assertEqual(result, "Wrong value") |
---|
| 184 | |
---|
| 185 | |
---|
[2096] | 186 | def test_suite(): |
---|
| 187 | suite = unittest.TestSuite() |
---|
[2097] | 188 | suite.addTest(unittest.makeSuite(TestInstallations)) |
---|
[2098] | 189 | suite.addTest(unittest.makeSuite(TestCaptchaField)) |
---|
[2099] | 190 | suite.addTest(unittest.makeSuite(TestCaptchaWidget)) |
---|
[2100] | 191 | suite.addTest(unittest.makeSuite(TestCaptchaValidator)) |
---|
[2096] | 192 | return suite |
---|