source: products/quintagroup.pfg.captcha/trunk/quintagroup/pfg/captcha/tests/testInstalled.py @ 3168

Last change on this file since 3168 was 3168, checked in by vmaksymiv, 8 years ago

pep8, pyflakes fixes

  • Property svn:eol-style set to native
File size: 5.7 KB
Line 
1import string
2import unittest
3
4from Products.CMFCore.permissions import View
5from Products.Archetypes.atapi import StringField
6from Products.Archetypes.Registry import availableWidgets
7from Products.validation import validation
8
9from quintagroup.pfg.captcha import CaptchaField
10from quintagroup.pfg.captcha import CaptchaWidget
11from quintagroup.pfg.captcha import CaptchaValidator
12from quintagroup.pfg.captcha.widget import CAPTCHA_MACRO
13from quintagroup.pfg.captcha.field import CAPTCHA_ID, HIDDEN_FIELDS
14from quintagroup.pfg.captcha.tests.base import TestCase, REQUIREMENTS
15
16_marker = object()
17
18
19class 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
58class 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_field')
64
65    def testId(self):
66        """CaptchaField has always CAPTCHA_ID id."""
67        self.assertEqual(CAPTCHA_ID in self.ff1, True)
68        self.assertNotEqual('captcha_field' in self.ff1, True)
69
70    def testSchema(self):
71        cf = getattr(self.ff1, CAPTCHA_ID)
72        schema = cf.Schema()
73        for field in HIDDEN_FIELDS:
74            visibility = schema[field].widget.visible
75            self.assertEqual(visibility, {'view': 'invisible',
76                                          'edit': 'invisible'},
77                '"%s" field is not hidden, but %s:' % (field, visibility))
78
79    def testFGField(self):
80        cf = getattr(self.ff1, CAPTCHA_ID)
81        fgField = getattr(cf, 'fgField', _marker)
82        self.assertNotEqual(fgField, _marker)
83        # Test fgField properties
84        self.assertEqual(type(fgField), StringField)
85        self.assertEqual(bool(fgField.searchable), False)
86        self.assertEqual(fgField.write_permission, View)
87        self.assertEqual(type(fgField.widget), CaptchaWidget)
88        validators = [v.__class__ for v in fgField.validators._chain]
89        self.assertEqual(CaptchaValidator in validators, True)
90
91
92class TestCaptchaWidget(TestCase):
93
94    CF = CaptchaField.__module__ + '.CaptchaField'
95    CW = CaptchaWidget.__module__ + '.CaptchaWidget'
96
97    def afterSetUp(self):
98        self.widgets = dict(availableWidgets())
99
100    def testRegistration(self):
101        self.assertEqual(self.CW in self.widgets, True)
102        cw = self.widgets[self.CW]
103        self.assertEqual(self.CF in cw.used_for, True)
104
105    def testWidgetMacro(self):
106        widget_macro = self.widgets[self.CW].klass._properties['macro']
107        self.assertEqual(widget_macro, CAPTCHA_MACRO)
108
109    def testWidgetMacroAccessable(self):
110        macro = self.portal.restrictedTraverse(CAPTCHA_MACRO)
111        self.assertNotEqual(macro, None)
112
113
114class TestCaptchaValidator(TestCase):
115
116    def getValidator(self):
117        return validation.validatorFor('isCaptchaCorrect')
118
119    def testRegistration(self):
120        self.assertEqual(self.getValidator().__class__, CaptchaValidator)
121
122    def patchCoreValidator(self, status, error=""):
123        # Patch quintagroup.catpcha.core' captcha_validator
124        class MockState:
125            def __init__(self, status, error=""):
126                self.status = status
127                self.errors = {'key': [error, ]}
128
129        patch_validator = lambda: MockState(status, error)
130        self.portal.captcha_validator = patch_validator
131
132    def testValidationSuccess(self):
133        # PFG validator must call patched quintagroup.captcha.core'
134        # captcha_validator and successfully validate the data.
135        validator = self.getValidator()
136        self.patchCoreValidator("success")
137        result = validator('test', instance=self.portal)
138        self.assertEqual(result, 1)
139
140    def testValidationFailure(self):
141        # PFG validator must call patched quintagroup.captcha.core'
142        # captcha_validator and return error.
143        validator = self.getValidator()
144        self.patchCoreValidator("failure", "Wrong value")
145        result = validator('test', instance=self.portal)
146        self.assertEqual(result, "Wrong value")
147
148
149def test_suite():
150    suite = unittest.TestSuite()
151    suite.addTest(unittest.makeSuite(TestInstallations))
152    suite.addTest(unittest.makeSuite(TestCaptchaField))
153    suite.addTest(unittest.makeSuite(TestCaptchaWidget))
154    suite.addTest(unittest.makeSuite(TestCaptchaValidator))
155    return suite
Note: See TracBrowser for help on using the repository browser.