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

Last change on this file since 3606 was 3606, checked in by vmaksymiv, 7 years ago

PPP fixes

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