source: products/quintagroup.gauth/trunk/quintagroup/gauth/tests.py @ 2626

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

Prepare to release/tagging

  • Property svn:eol-style set to native
File size: 7.4 KB
Line 
1import re
2import sys
3import unittest
4import gdata.service
5
6from zope.component import queryUtility, queryAdapter
7from zope.component import getSiteManager, getGlobalSiteManager
8from Testing import ZopeTestCase as ztc
9
10from Products.Five import zcml
11from Products.Five import fiveconfigure
12from Products.PloneTestCase import PloneTestCase as ptc
13from Products.PloneTestCase.layer import PloneSite
14from Products.PloneTestCase.PloneTestCase import portal_owner
15from Products.PloneTestCase.PloneTestCase import default_password
16
17ptc.setupPloneSite()
18
19import quintagroup.gauth
20from quintagroup.gauth.utility import SafeQuery
21from quintagroup.gauth.interfaces import IGAuthUtility
22from quintagroup.gauth.browser.configlet import IGAuthConfigletSchema
23
24class GauthLayer(PloneSite):
25    @classmethod
26    def setUp(cls):
27        fiveconfigure.debug_mode = True
28        import quintagroup.gauth
29        zcml.load_config('configure.zcml', quintagroup.gauth)
30        fiveconfigure.debug_mode = False
31        ztc.installPackage("quintagroup.gauth")
32
33    @classmethod
34    def tearDown(cls):
35        pass
36
37
38class TestCase(ptc.PloneTestCase):
39    layer = GauthLayer
40
41
42class FunctionalTestCase(ptc.FunctionalTestCase):
43    layer = GauthLayer
44
45    def _getauth(self):
46        # Fix authenticator for the form
47        try:
48            authenticator = self.portal.restrictedTraverse("@@authenticator")
49        except:
50            handle  = ""
51        else:
52            html = authenticator.authenticator()
53            handle = re.search('value="(.*)"', html).groups()[0]
54        return handle
55
56
57class TestInstall(TestCase):
58
59    def afterSetUp(self):
60        self.loginAsPortalOwner()
61        self.addProduct("quintagroup.gauth")
62       
63    def testProperties(self):
64        pp = self.portal.portal_properties
65        self.assert_("gauth_properties" in pp.objectIds())
66        self.assert_(bool(pp.gauth_properties.hasProperty("gauth_email")))
67
68    def testConfiglet(self):
69        cp = self.portal.portal_controlpanel
70        aifs = [ai['id'] for ai in cp.listActionInfos(
71                check_visibility=0, check_permissions=0, check_condition=0)]
72        self.assert_("quintagroup.gauth" in aifs, aifs)
73
74    def testUtility(self):
75        lsm = getSiteManager(self.portal)
76        gsm = getGlobalSiteManager()
77        lgauth = lsm.queryUtility(IGAuthUtility)
78        ggauth = gsm.queryUtility(IGAuthUtility)
79        self.assertEqual(ggauth, None)
80        self.assertNotEqual(lgauth, None)
81
82    def testActionIcons(self):
83        ait = self.portal.portal_actionicons
84        ai = ait.getActionInfo("controlpanel", "quintagroup.gauth")
85        self.assertNotEqual(ai, None)
86
87
88class TestUninstall(TestCase):
89
90    def afterSetUp(self):
91        self.loginAsPortalOwner()
92        self.addProduct("quintagroup.gauth")
93        qi = self.portal.portal_quickinstaller
94        # qi.installProducts(products=["quintagroup.gauth",])
95        qi.uninstallProducts(products=["quintagroup.gauth",])
96
97    def testProperties(self):
98        pp = self.portal.portal_properties
99        self.assert_(not "gauth_properties" in pp.objectIds())
100
101    def testConfiglet(self):
102        cp = self.portal.portal_controlpanel
103        aifs = [ai['id'] for ai in cp.listActionInfos(
104                check_visibility=0, check_permissions=0, check_condition=0)]
105        self.assert_(not "quintagroup.gauth" in aifs)
106
107    def testUtility(self):
108        lsm = getSiteManager(self.portal)
109        gsm = getGlobalSiteManager()
110        lgauth = lsm.queryUtility(IGAuthUtility)
111        ggauth = gsm.queryUtility(IGAuthUtility)
112        self.assertEqual(ggauth, None)
113        self.assertEqual(lgauth, None)
114
115    def testActionIcons(self):
116        ait = self.portal.portal_actionicons
117        ai = ait.queryActionInfo("controlpanel", "quintagroup.gauth", default=None)
118        self.assert_(ai == None)
119
120
121class TestConfiglet(FunctionalTestCase):
122
123    def afterSetUp(self):
124        self.loginAsPortalOwner()
125        self.addProduct("quintagroup.gauth")
126        self.basic_auth = portal_owner + ":" + default_password
127        self.get_url = self.portal.id+'/@@gauth-controlpanel'
128        self.save_url = self.portal.id+'/@@gauth-controlpanel?form.actions.save=1' \
129            '&_authenticator=%s' % self._getauth()
130
131    def test_presentEmail(self):
132        res = self.publish(self.get_url, self.basic_auth).getBody()
133        self.assert_(re.match(".*<input\s[^>]*name=\"form.gauth_email\"[^>]*>", res, re.I|re.S))
134
135    def test_presentPassword(self):
136        res = self.publish(self.get_url, self.basic_auth).getBody()
137        self.assert_(re.match(".*<input\s[^>]*name=\"form.gauth_pass\"[^>]*>", res, re.I|re.S))
138
139    def test_update(self):
140        temail, tpass = "tester@test.com", "secret"
141        gauth_util = queryUtility(IGAuthUtility)
142        url = self.save_url + '&form.gauth_email='+temail + '&form.gauth_pass='+tpass
143        self.publish(url, self.basic_auth)
144        self.assert_(gauth_util.email == temail)
145        self.assert_(gauth_util.password == tpass)
146
147
148class TestUtility(FunctionalTestCase):
149
150    def afterSetUp(self):
151        self.loginAsPortalOwner()
152        self.addProduct("quintagroup.gauth")
153        sm = getSiteManager(self.portal)
154        self.gauthutil = sm.queryUtility(IGAuthUtility)
155        self.gauthconfiglet = queryAdapter(self.portal, IGAuthConfigletSchema)
156
157    def testEmail(self):
158        self.assertEqual(bool(self.gauthutil.email), False)
159        self.gauthconfiglet.gauth_email = "tester@test.com"
160        self.assertEqual(self.gauthutil.email, "tester@test.com")
161
162    def testPassword(self):
163        self.assertEqual(bool(self.gauthutil.password), False)
164        self.gauthconfiglet.gauth_pass = u"secret"
165        self.assertEqual(self.gauthutil.password, "secret")
166
167
168out = ""
169class DummyService(object):
170    doraise = False
171
172    def ProgrammaticLogin(self):
173        global out
174        out += "\nCall ProgrammaticLogin"
175        self.doraise = False
176
177    def Action(self, *args, **kwargs):
178        global out
179        out += "\nCall Action with: args='%s', kwargs='%s'" % (str(args), str(kwargs))
180        if self.doraise:
181            raise gdata.service.RequestError("Token is expired")
182
183class TestSafeQuery(unittest.TestCase):
184
185    def setUp(self):
186        global out
187        self.serv = DummyService()
188        self.args = "test_arg",
189        self.kwargs = {"kw1_key": "kw1_val"}
190        self.sq = SafeQuery()
191        out = ""
192
193    def testMethodCall(self):
194        self.sq.safeQuery(self.serv, self.serv.Action, *self.args, **self.kwargs)
195        res = filter(None, out.split("\n"))
196        self.assertEqual(res[0],
197            "Call Action with: args='%s', kwargs='%s'" % (str(self.args), str(self.kwargs)))
198
199    def testProgrammaticLogin(self):
200        self.serv.doraise = True
201        self.sq.safeQuery(self.serv, self.serv.Action, *self.args, **self.kwargs)
202        res = filter(None, out.split("\n"))
203        self.assertEqual(res[0], "Call Action with: args='%s', kwargs='%s'" % (
204            str(self.args), str(self.kwargs)))
205        self.assertEqual(res[1], "Call ProgrammaticLogin")
206        self.assertEqual(res[2], "Call Action with: args='%s', kwargs='%s'" % (
207            str(self.args), str(self.kwargs)))
208
209
210def test_suite():
211    from unittest import TestSuite, makeSuite
212    suite = TestSuite()
213    suite.addTest(makeSuite(TestInstall))
214    suite.addTest(makeSuite(TestUninstall))
215    suite.addTest(makeSuite(TestConfiglet))
216    suite.addTest(makeSuite(TestUtility))
217    suite.addTest(makeSuite(TestSafeQuery))
218    return suite
219
220
221if __name__ == '__main__':
222    unittest.main(defaultTest='test_suite')
Note: See TracBrowser for help on using the repository browser.