[Zope-CVS] SVN: PluggableAuthService/trunk/plugins/ Add GenericSetup export / import support for DomainAuthHelper plugin.

Tres Seaver tseaver at palladion.com
Tue Nov 15 23:57:16 EST 2005


Log message for revision 40148:
  Add GenericSetup export / import support for DomainAuthHelper plugin.

Changed:
  U   PluggableAuthService/trunk/plugins/exportimport.py
  U   PluggableAuthService/trunk/plugins/tests/test_exportimport.py
  A   PluggableAuthService/trunk/plugins/xml/domainauth.xml

-=-
Modified: PluggableAuthService/trunk/plugins/exportimport.py
===================================================================
--- PluggableAuthService/trunk/plugins/exportimport.py	2005-11-16 03:52:08 UTC (rev 40147)
+++ PluggableAuthService/trunk/plugins/exportimport.py	2005-11-16 04:57:15 UTC (rev 40148)
@@ -256,3 +256,43 @@
                 'cookie_name': self.context.cookie_name,
                 'login_path': self.context.login_path,
                }
+
+class DomainAuthHelperExportImport(SimpleXMLExportImport):
+    """ Adapter for dumping / loading DomainAuthHelper to an XML file.
+    """
+    _FILENAME = 'domainauth.xml'
+    _ROOT_TAGNAME = 'domain-auth'
+
+    def _purgeContext(self):
+        self.context.__init__(self.context.id, self.context.title)
+
+    def _updateFromDOM(self, root):
+        for user in root.getElementsByTagName('user'):
+            user_id = user.attributes['user_id'].value
+
+            for match in user.getElementsByTagName('match'):
+                username = match.attributes['username'].value
+                match_type = match.attributes['match_type'].value
+                match_string = match.attributes['match_string'].value
+                role_tokens = match.attributes['roles'].value
+                roles = role_tokens.split(',')
+
+                self.context.manage_addMapping(user_id=user_id,
+                                               match_type=match_type,
+                                               match_string=match_string,
+                                               username=username,
+                                               roles=roles,
+                                              )
+
+    def _getExportInfo(self):
+        user_map = {}
+        for k, v in self.context._domain_map.items():
+            user_map[k] = matches = []
+            for match in v:
+                match = match.copy()
+                match['roles'] = ','.join(match['roles'])
+                matches.append(match)
+
+        return {'title': self.context.title,
+                'map': user_map
+               }

Modified: PluggableAuthService/trunk/plugins/tests/test_exportimport.py
===================================================================
--- PluggableAuthService/trunk/plugins/tests/test_exportimport.py	2005-11-16 03:52:08 UTC (rev 40147)
+++ PluggableAuthService/trunk/plugins/tests/test_exportimport.py	2005-11-16 04:57:15 UTC (rev 40148)
@@ -556,12 +556,202 @@
                                                      ))
             self.assertEqual( content_type, 'text/xml' )
 
+    class DomainAuthHelperExportImportTests(_TestBase):
+
+        def _getTargetClass(self):
+            from Products.PluggableAuthService.plugins.exportimport \
+                import DomainAuthHelperExportImport
+            return DomainAuthHelperExportImport
+
+        def _makePlugin(self, id, *args, **kw):
+            from Products.PluggableAuthService.plugins.DomainAuthHelper \
+                import DomainAuthHelper
+            return DomainAuthHelper(id, *args, **kw)
+
+        def test_listExportableItems(self):
+            plugin = self._makePlugin('lEI').__of__(self.root)
+            adapter = self._makeOne(plugin)
+
+            self.assertEqual(len(adapter.listExportableItems()), 0)
+            plugin.cookie_name = 'COOKIE_NAME'
+            plugin.login_path = 'LOGIN_PATH'
+            plugin.manage_addMapping(user_id='user_id',
+                                     match_type='equals',
+                                     match_string='host.example.com',
+                                     roles=['foo', 'bar'],
+                                    )
+            self.assertEqual(len(adapter.listExportableItems()), 0)
+
+        def test__getExportInfo_empty(self):
+            plugin = self._makePlugin('empty', None).__of__(self.root)
+            adapter = self._makeOne(plugin)
+
+            info = adapter._getExportInfo()
+            self.assertEqual(info['title'], None)
+            self.assertEqual(len(info['map']), 0)
+
+        def test_export_empty(self):
+            plugin = self._makePlugin('empty', None).__of__(self.root)
+            adapter = self._makeOne(plugin)
+
+            context = DummyExportContext(plugin)
+            adapter.export(context, 'plugins', False)
+
+            self.assertEqual( len( context._wrote ), 1 )
+            filename, text, content_type = context._wrote[ 0 ]
+            self.assertEqual( filename, 'plugins/empty.xml' )
+            self._compareDOM( text, _EMPTY_DOMAIN_AUTH )
+            self.assertEqual( content_type, 'text/xml' )
+
+        def test__getExportInfo_with_map(self):
+            TITLE = 'With Map'
+            USER_ID = 'some_user_id'
+            DOMAIN = 'host.example.com'
+            ROLES = ['foo', 'bar']
+            plugin = self._makePlugin('with_map', TITLE).__of__(self.root)
+            adapter = self._makeOne(plugin)
+
+            plugin.manage_addMapping(user_id=USER_ID,
+                                     match_type='equals',
+                                     match_string=DOMAIN,
+                                     roles=ROLES,
+                                    )
+
+            info = adapter._getExportInfo()
+            self.assertEqual(info['title'], TITLE)
+            user_map = info['map']
+            self.assertEqual(len(user_map), 1)
+            self.failUnless(USER_ID in user_map)
+            match_list = user_map[USER_ID]
+            self.assertEqual(len(match_list), 1)
+            match = match_list[0]
+            self.assertEqual(match['username'], USER_ID)
+            self.assertEqual(match['match_type'], 'equals')
+            self.assertEqual(match['match_string'], DOMAIN)
+            self.assertEqual(match['roles'], ','.join(ROLES))
+
+        def test_export_with_map(self):
+            TITLE = 'With Map'
+            USER_ID = 'some_user_id'
+            DOMAIN = 'host.example.com'
+            ROLES = ['foo', 'bar']
+            plugin = self._makePlugin('with_map', TITLE).__of__(self.root)
+            adapter = self._makeOne(plugin)
+
+            plugin.manage_addMapping(user_id=USER_ID,
+                                     match_type='equals',
+                                     match_string=DOMAIN,
+                                     roles=ROLES,
+                                    )
+
+            context = DummyExportContext(plugin)
+            adapter.export(context, 'plugins', False)
+
+            self.assertEqual(len(context._wrote), 1)
+            filename, text, content_type = context._wrote[0]
+            self.assertEqual(filename, 'plugins/with_map.xml' )
+            self._compareDOM(text,
+                             _FILLED_DOMAIN_AUTH %
+                              (TITLE,
+                               USER_ID,
+                               DOMAIN,
+                               'equals',
+                               ','.join(ROLES),
+                               USER_ID,
+                              ))
+            self.assertEqual( content_type, 'text/xml' )
+
+        def test_import_empty(self):
+            TITLE = 'With Map'
+            USER_ID = 'some_user_id'
+            DOMAIN = 'host.example.com'
+            ROLES = ['foo', 'bar']
+            plugin = self._makePlugin('empty').__of__(self.root)
+            adapter = self._makeOne(plugin)
+
+            context = DummyImportContext(plugin)
+            context._files['plugins/empty.xml'
+                          ] = _FILLED_DOMAIN_AUTH % (TITLE,
+                                                     USER_ID,
+                                                     DOMAIN,
+                                                     'equals',
+                                                     ','.join(ROLES),
+                                                     USER_ID,
+                                                    )
+            self.assertEqual(plugin.title, '')
+
+            adapter.import_(context, 'plugins', False)
+
+            self.assertEqual(len(plugin._domain_map), 1)
+            self.assertEqual(plugin.title, TITLE)
+
+            username, match_list = plugin._domain_map.items()[0]
+            self.assertEqual(username, USER_ID)
+            self.assertEqual(len(match_list), 1)
+            match = match_list[0]
+            self.assertEqual(match['username'], USER_ID)
+            self.assertEqual(match['match_string'], DOMAIN)
+            self.assertEqual(match['match_real'], DOMAIN)
+            self.assertEqual(match['match_type'], 'equals')
+            self.assertEqual(len(match['roles']), len(ROLES))
+            for role in ROLES:
+                self.failUnless(role in match['roles'])
+
+        def test_import_without_purge_leaves_existing_users(self):
+            TITLE = 'With Map'
+            USER_ID = 'some_user_id'
+            DOMAIN = 'host.example.com'
+            ROLES = ['foo', 'bar']
+            plugin = self._makePlugin('with_map', TITLE).__of__(self.root)
+            adapter = self._makeOne(plugin)
+
+            plugin.manage_addMapping(user_id=USER_ID,
+                                     match_type='equals',
+                                     match_string=DOMAIN,
+                                     roles=ROLES,
+                                    )
+
+            adapter = self._makeOne(plugin)
+
+            context = DummyImportContext(plugin, purge=False)
+            context._files['plugins/with_map.xml'] = _EMPTY_DOMAIN_AUTH
+
+            self.assertEqual(len(plugin._domain_map), 1)
+            adapter.import_(context, 'plugins', False)
+            self.assertEqual(len(plugin._domain_map), 1)
+            self.assertEqual(plugin.title, None)
+
+        def test_import_with_purge_wipes_existing_users(self):
+            TITLE = 'With Map'
+            USER_ID = 'some_user_id'
+            DOMAIN = 'host.example.com'
+            ROLES = ['foo', 'bar']
+            plugin = self._makePlugin('with_map', TITLE).__of__(self.root)
+
+            adapter = self._makeOne(plugin)
+
+            plugin.manage_addMapping(user_id=USER_ID,
+                                     match_type='equals',
+                                     match_string=DOMAIN,
+                                     roles=ROLES,
+                                    )
+            adapter = self._makeOne(plugin)
+
+            context = DummyImportContext(plugin, purge=True)
+            context._files['plugins/with_map.xml'] = _EMPTY_DOMAIN_AUTH
+
+            self.assertEqual(len(plugin._domain_map), 1)
+            adapter.import_(context, 'plugins', False)
+            self.assertEqual(len(plugin._domain_map), 0)
+            self.assertEqual(plugin.title, None)
+
     def test_suite():
         suite = unittest.TestSuite((
             unittest.makeSuite(ZODBUserManagerExportImportTests),
             unittest.makeSuite(ZODBGroupManagerExportImportTests),
             unittest.makeSuite(ZODBRoleManagerExportImportTests),
             unittest.makeSuite(CookieAuthHelperExportImportTests),
+            unittest.makeSuite(DomainAuthHelperExportImportTests),
                         ))
         return suite
 
@@ -667,5 +857,20 @@
 <cookie-auth title="%s" cookie_name="%s" login_path="%s" />
 """
 
+_EMPTY_DOMAIN_AUTH = """\
+<?xml version="1.0" ?>
+<domain-auth>
+</domain-auth>
+"""
+
+_FILLED_DOMAIN_AUTH = """\
+<?xml version="1.0" ?>
+<domain-auth title="%s">
+ <user user_id="%s">
+  <match match_string="%s" match_type="%s" roles="%s" username="%s"/>
+ </user>
+</domain-auth>
+"""
+
 if __name__ == '__main__':
     unittest.main(defaultTest='test_suite')

Added: PluggableAuthService/trunk/plugins/xml/domainauth.xml
===================================================================
--- PluggableAuthService/trunk/plugins/xml/domainauth.xml	2005-11-16 03:52:08 UTC (rev 40147)
+++ PluggableAuthService/trunk/plugins/xml/domainauth.xml	2005-11-16 04:57:15 UTC (rev 40148)
@@ -0,0 +1,22 @@
+<?xml version="1.0" ?>
+<domain-auth xmlns:tal="http://xml.zope.org/namespaces/tal"
+       title="PLUGIN TITLE"
+       tal:define="info options/info"
+       tal:attributes="title info/title;
+                      ">
+ <user user_id="USERID"
+       tal:repeat="user_id info/map"
+       tal:attributes="user_id user_id"
+       >
+  <match username="USERNAME"
+         match_type="MATCH_TYPE"
+         match_string="MATCH_STRING"
+         roles="ROLE1,ROLE2"
+         tal:repeat="match info/map/?user_id"
+         tal:attributes="username match/username;
+                         match_type match/match_type;
+                         match_string match/match_string;
+                         roles match/roles
+                        " />
+ </user>
+</domain-auth>


Property changes on: PluggableAuthService/trunk/plugins/xml/domainauth.xml
___________________________________________________________________
Name: svn:eol-style
   + native



More information about the Zope-CVS mailing list