[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() |
---|
[2098] | 40 | ptc.setupPloneSite(products=['PloneFormGen',], extension_profiles=PROFILES) |
---|
[2096] | 41 | |
---|
| 42 | |
---|
[2097] | 43 | class TestInstallations(ptc.PloneTestCase): |
---|
| 44 | |
---|
| 45 | def testInstalledProducts(self): |
---|
| 46 | qi = self.portal.portal_quickinstaller |
---|
| 47 | installed = [p['id'] for p in qi.listInstalledProducts()] |
---|
[2101] | 48 | for p in REQUIREMENTS: |
---|
[2097] | 49 | if p.startswith('Products'): |
---|
| 50 | p = p[9:] |
---|
| 51 | self.assertEqual(p in installed, True, |
---|
| 52 | '"%s" product not installed' % p) |
---|
| 53 | |
---|
| 54 | def testType(self): |
---|
| 55 | pt = self.portal.portal_types |
---|
| 56 | self.assertEqual("CaptchaField" in pt.objectIds(), True) |
---|
| 57 | |
---|
| 58 | def testPortalFactory(self): |
---|
| 59 | pf = self.portal.portal_factory |
---|
| 60 | self.assertEqual("CaptchaField" in pf.getFactoryTypes(), True) |
---|
| 61 | |
---|
| 62 | def testWorkflow(self): |
---|
| 63 | pw = self.portal.portal_workflow |
---|
| 64 | default_chain = pw.getDefaultChain() |
---|
| 65 | cf_chain = pw.getChainForPortalType('CaptchaField') |
---|
| 66 | self.assertNotEqual(cf_chain == default_chain , True) |
---|
| 67 | |
---|
| 68 | def testNotToList(self): |
---|
| 69 | navtree = self.portal.portal_properties.navtree_properties |
---|
| 70 | mtNotToList = navtree.getProperty("metaTypesNotToList") |
---|
| 71 | self.assertEqual('CaptchaField' in mtNotToList, True) |
---|
| 72 | |
---|
| 73 | def testSkins(self): |
---|
| 74 | ps = self.portal.portal_skins |
---|
| 75 | self.assertEqual("qplonecaptchafield" in ps.objectIds(), True) |
---|
| 76 | for sname, spath in ps.getSkinPaths(): |
---|
| 77 | paths = filter(None, map(string.strip, spath.split(','))) |
---|
| 78 | self.assertEqual("qplonecaptchafield" in paths, True, |
---|
| 79 | '"qplonecaptchafield" layer not present in "%s" skin' % sname) |
---|
| 80 | |
---|
[2098] | 81 | |
---|
| 82 | class TestCaptchaField(ptc.PloneTestCase): |
---|
| 83 | |
---|
| 84 | def afterSetUp(self): |
---|
| 85 | self.folder.invokeFactory('FormFolder', 'ff1') |
---|
| 86 | self.ff1 = getattr(self.folder, 'ff1') |
---|
| 87 | self.ff1.invokeFactory('CaptchaField', 'captcha_field') |
---|
[2099] | 88 | |
---|
[2098] | 89 | def testId(self): |
---|
| 90 | """CaptchaField has always CAPTCHA_ID id.""" |
---|
| 91 | self.assertEqual(CAPTCHA_ID in self.ff1, True) |
---|
| 92 | self.assertNotEqual('captcha_field' in self.ff1, True) |
---|
| 93 | |
---|
| 94 | def testSchema(self): |
---|
| 95 | cf = getattr(self.ff1, CAPTCHA_ID) |
---|
| 96 | schema = cf.Schema() |
---|
| 97 | for field in HIDDEN_FIELDS: |
---|
| 98 | visibility = schema[field].widget.visible |
---|
| 99 | self.assertEqual(visibility, {'view':'invisible','edit':'invisible'}, |
---|
| 100 | '"%s" field is not hidden, but %s:' % (field, visibility)) |
---|
| 101 | |
---|
| 102 | def testFGField(self): |
---|
| 103 | cf = getattr(self.ff1, CAPTCHA_ID) |
---|
| 104 | fgField = getattr(cf, 'fgField', _marker) |
---|
| 105 | self.assertNotEqual(fgField, _marker) |
---|
| 106 | # Test fgField properties |
---|
| 107 | self.assertEqual(type(fgField), StringField) |
---|
| 108 | self.assertEqual(bool(fgField.searchable), False ) |
---|
[2099] | 109 | self.assertEqual(fgField.write_permission, View) |
---|
[2098] | 110 | self.assertEqual(type(fgField.widget), CaptchaWidget) |
---|
| 111 | validators = [v.__class__ for v in fgField.validators._chain] |
---|
| 112 | self.assertEqual(CaptchaValidator in validators, True) |
---|
[2097] | 113 | |
---|
[2099] | 114 | |
---|
| 115 | class TestCaptchaWidget(ptc.PloneTestCase): |
---|
| 116 | |
---|
| 117 | CF = CaptchaField.__module__ + '.CaptchaField' |
---|
| 118 | CW = CaptchaWidget.__module__ + '.CaptchaWidget' |
---|
| 119 | |
---|
| 120 | def afterSetUp(self): |
---|
| 121 | self.widgets = dict(availableWidgets()) |
---|
| 122 | |
---|
| 123 | def testRegistration(self): |
---|
| 124 | self.assertEqual(self.CW in self.widgets, True) |
---|
| 125 | cw = self.widgets[self.CW] |
---|
| 126 | self.assertEqual(self.CF in cw.used_for, True) |
---|
| 127 | |
---|
| 128 | def testWidgetMacro(self): |
---|
| 129 | widget_macro = self.widgets[self.CW].klass._properties['macro'] |
---|
| 130 | self.assertEqual(widget_macro, CAPTCHA_MACRO) |
---|
| 131 | |
---|
| 132 | def testWidgetMacroAccessable(self): |
---|
| 133 | macro = self.portal.restrictedTraverse(CAPTCHA_MACRO) |
---|
| 134 | self.assertNotEqual(macro, None) |
---|
| 135 | |
---|
[2100] | 136 | |
---|
| 137 | class TestCaptchaValidator(ptc.PloneTestCase): |
---|
| 138 | |
---|
| 139 | def getValidator(self): |
---|
| 140 | return validation.validatorFor('isCaptchaCorrect') |
---|
| 141 | |
---|
| 142 | def testRegistration(self): |
---|
| 143 | self.assertEqual(self.getValidator().__class__, CaptchaValidator) |
---|
| 144 | |
---|
| 145 | def patchCoreValidator(self, status, error=""): |
---|
| 146 | # Patch quintagroup.catpcha.core' captcha_validator |
---|
| 147 | class MockState: |
---|
| 148 | def __init__(self, status, error=""): |
---|
| 149 | self.status = status |
---|
| 150 | self.errors = {'key':[error,]} |
---|
| 151 | |
---|
| 152 | patch_validator = lambda : MockState(status, error) |
---|
| 153 | self.portal.captcha_validator = patch_validator |
---|
| 154 | |
---|
| 155 | def testValidationSuccess(self): |
---|
| 156 | # PFG validator must call patched quintagroup.captcha.core' |
---|
| 157 | # captcha_validator and successfully validate the data. |
---|
| 158 | validator = self.getValidator() |
---|
| 159 | self.patchCoreValidator("success") |
---|
| 160 | result = validator('test', instance=self.portal) |
---|
| 161 | self.assertEqual(result, 1) |
---|
| 162 | |
---|
| 163 | def testValidationFailure(self): |
---|
| 164 | # PFG validator must call patched quintagroup.captcha.core' |
---|
| 165 | # captcha_validator and return error. |
---|
| 166 | validator = self.getValidator() |
---|
| 167 | self.patchCoreValidator("failure", "Wrong value") |
---|
| 168 | result = validator('test', instance=self.portal) |
---|
| 169 | self.assertEqual(result, "Wrong value") |
---|
| 170 | |
---|
| 171 | |
---|
[2096] | 172 | def test_suite(): |
---|
| 173 | suite = unittest.TestSuite() |
---|
[2097] | 174 | suite.addTest(unittest.makeSuite(TestInstallations)) |
---|
[2098] | 175 | suite.addTest(unittest.makeSuite(TestCaptchaField)) |
---|
[2099] | 176 | suite.addTest(unittest.makeSuite(TestCaptchaWidget)) |
---|
[2100] | 177 | suite.addTest(unittest.makeSuite(TestCaptchaValidator)) |
---|
[2096] | 178 | return suite |
---|