[2616] | 1 | import re |
---|
[2613] | 2 | import sys |
---|
[2577] | 3 | import unittest |
---|
[2626] | 4 | import gdata.service |
---|
[2577] | 5 | |
---|
[2610] | 6 | from zope.component import queryUtility, queryAdapter |
---|
| 7 | from zope.component import getSiteManager, getGlobalSiteManager |
---|
[2577] | 8 | from Testing import ZopeTestCase as ztc |
---|
| 9 | |
---|
[2616] | 10 | from Products.Five import zcml |
---|
[2577] | 11 | from Products.Five import fiveconfigure |
---|
| 12 | from Products.PloneTestCase import PloneTestCase as ptc |
---|
| 13 | from Products.PloneTestCase.layer import PloneSite |
---|
[2608] | 14 | from Products.PloneTestCase.PloneTestCase import portal_owner |
---|
| 15 | from Products.PloneTestCase.PloneTestCase import default_password |
---|
| 16 | |
---|
[2577] | 17 | ptc.setupPloneSite() |
---|
| 18 | |
---|
| 19 | import quintagroup.gauth |
---|
[2613] | 20 | from quintagroup.gauth.utility import SafeQuery |
---|
[2609] | 21 | from quintagroup.gauth.interfaces import IGAuthUtility |
---|
[2610] | 22 | from quintagroup.gauth.browser.configlet import IGAuthConfigletSchema |
---|
[2577] | 23 | |
---|
[2605] | 24 | class GauthLayer(PloneSite): |
---|
| 25 | @classmethod |
---|
| 26 | def setUp(cls): |
---|
| 27 | fiveconfigure.debug_mode = True |
---|
[2616] | 28 | import quintagroup.gauth |
---|
| 29 | zcml.load_config('configure.zcml', quintagroup.gauth) |
---|
[2605] | 30 | fiveconfigure.debug_mode = False |
---|
[2616] | 31 | ztc.installPackage("quintagroup.gauth") |
---|
[2577] | 32 | |
---|
[2605] | 33 | @classmethod |
---|
| 34 | def tearDown(cls): |
---|
| 35 | pass |
---|
[2577] | 36 | |
---|
| 37 | |
---|
[2605] | 38 | class TestCase(ptc.PloneTestCase): |
---|
| 39 | layer = GauthLayer |
---|
[2577] | 40 | |
---|
| 41 | |
---|
[2607] | 42 | class FunctionalTestCase(ptc.FunctionalTestCase): |
---|
[2605] | 43 | layer = GauthLayer |
---|
[2577] | 44 | |
---|
[2605] | 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 |
---|
[2604] | 55 | |
---|
| 56 | |
---|
[2605] | 57 | class TestInstall(TestCase): |
---|
[2609] | 58 | |
---|
[2605] | 59 | def afterSetUp(self): |
---|
| 60 | self.loginAsPortalOwner() |
---|
[2607] | 61 | self.addProduct("quintagroup.gauth") |
---|
| 62 | |
---|
[2605] | 63 | def testProperties(self): |
---|
| 64 | pp = self.portal.portal_properties |
---|
| 65 | self.assert_("gauth_properties" in pp.objectIds()) |
---|
[2607] | 66 | self.assert_(bool(pp.gauth_properties.hasProperty("gauth_email"))) |
---|
[2604] | 67 | |
---|
[2605] | 68 | def testConfiglet(self): |
---|
| 69 | cp = self.portal.portal_controlpanel |
---|
[2607] | 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) |
---|
[2604] | 73 | |
---|
[2609] | 74 | def testUtility(self): |
---|
| 75 | lsm = getSiteManager(self.portal) |
---|
[2610] | 76 | gsm = getGlobalSiteManager() |
---|
| 77 | lgauth = lsm.queryUtility(IGAuthUtility) |
---|
| 78 | ggauth = gsm.queryUtility(IGAuthUtility) |
---|
| 79 | self.assertEqual(ggauth, None) |
---|
| 80 | self.assertNotEqual(lgauth, None) |
---|
[2604] | 81 | |
---|
[2616] | 82 | def testActionIcons(self): |
---|
| 83 | ait = self.portal.portal_actionicons |
---|
| 84 | ai = ait.getActionInfo("controlpanel", "quintagroup.gauth") |
---|
| 85 | self.assertNotEqual(ai, None) |
---|
[2610] | 86 | |
---|
[2616] | 87 | |
---|
| 88 | class 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 |
---|
[2625] | 117 | ai = ait.queryActionInfo("controlpanel", "quintagroup.gauth", default=None) |
---|
| 118 | self.assert_(ai == None) |
---|
[2616] | 119 | |
---|
| 120 | |
---|
[2608] | 121 | class 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" |
---|
[2609] | 141 | gauth_util = queryUtility(IGAuthUtility) |
---|
[2608] | 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 | |
---|
[2609] | 147 | |
---|
[2610] | 148 | class 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 | |
---|
[2613] | 168 | out = "" |
---|
| 169 | class 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 | |
---|
| 183 | class 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 | |
---|
[2577] | 210 | def test_suite(): |
---|
[2605] | 211 | from unittest import TestSuite, makeSuite |
---|
| 212 | suite = TestSuite() |
---|
| 213 | suite.addTest(makeSuite(TestInstall)) |
---|
[2616] | 214 | suite.addTest(makeSuite(TestUninstall)) |
---|
[2608] | 215 | suite.addTest(makeSuite(TestConfiglet)) |
---|
[2610] | 216 | suite.addTest(makeSuite(TestUtility)) |
---|
[2613] | 217 | suite.addTest(makeSuite(TestSafeQuery)) |
---|
[2605] | 218 | return suite |
---|
[2577] | 219 | |
---|
| 220 | |
---|
| 221 | if __name__ == '__main__': |
---|
| 222 | unittest.main(defaultTest='test_suite') |
---|