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