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