source: products/quintagroup.pfg.captcha/branches/migration/quintagroup/pfg/captcha/tests/testInstalled.py @ 2732

Last change on this file since 2732 was 2732, checked in by mylan, 14 years ago

#222: Splited tests.py module into 2 layers with notinstalled and installed quintagroup.pfg.captcha packages

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