def test_get_plugin_removed(self): from pas.plugins.headers.utils import get_plugin from pas.plugins.headers.utils import PLUGIN_ID pas = api.portal.get_tool("acl_users") pas._delObject(PLUGIN_ID) self.assertIsNone(get_plugin(self.portal))
def test_get_plugin_with_bad_plugin(self): from pas.plugins.headers.utils import get_plugin from pas.plugins.headers.utils import PLUGIN_ID from Products.PluggableAuthService.plugins.BasePlugin import BasePlugin pas = api.portal.get_tool("acl_users") pas._delObject(PLUGIN_ID) pas._setObject(PLUGIN_ID, BasePlugin()) self.assertIsNone(get_plugin(self.portal))
def test_get_plugin_proper(self): from pas.plugins.headers.plugins import HeaderPlugin from pas.plugins.headers.utils import get_plugin from pas.plugins.headers.utils import PLUGIN_ID plugin = get_plugin(self.portal) self.assertIsNotNone(plugin) self.assertEqual(plugin.id, PLUGIN_ID) self.assertIsInstance(plugin, HeaderPlugin) self.assertEqual(plugin, getattr(self.portal.acl_users, PLUGIN_ID))
def export_properties(context): """Export HeaderPlugin properties.""" site = context.getSite() logger = context.getLogger(PLUGIN_ID) plugin = get_plugin(site) if plugin is None: return body = json.dumps(dict(plugin.propertyItems()), sort_keys=True, indent=4) context.writeDataFile(FILENAME, body, "application/json") logger.info("Exported HeaderPlugin properties.")
def test_import_no_plugin(self): """Test that import does not fail when plugin is not there.""" from pas.plugins.headers.exportimport import import_properties from pas.plugins.headers.utils import get_plugin self._removePlugin() settings = { "purge": True, "deny_unauthorized": True, "userid_header": "my_uid", } import_properties(self._makeContext(json.dumps(settings))) self.assertIsNone(get_plugin(self.portal))
def import_properties(context): """Import HeaderPlugin properties.""" site = context.getSite() body = context.readDataFile(FILENAME) logger = context.getLogger(PLUGIN_ID) if not body: logger.info("%s not found or empty.", FILENAME) return plugin = get_plugin(site) if plugin is None: logger.debug("Plugin not found.") return # Maybe catch ValueError. But it gives a clear error message. props = json.loads(body) if not isinstance(props, dict): # logger.error('%s does not contain a json dictionary.', FILENAME) # return raise ValueError("{0} does not contain a json dictionary.".format(FILENAME)) purge = props.pop("purge", False) if purge: for prop_name in plugin.propertyIds(): default = getattr(HeaderPlugin, prop_name) setattr(plugin, prop_name, default) logger.info("Purge: HeaderPlugin properties reset to defaults.") for prop_name, prop_value in props.items(): if prop_name not in plugin.propertyIds(): logger.info("Ignoring unknown property id %s.", prop_name) continue # When saving in the ZMI, you always get a string, # so we want this for import too. # So on Python 2 it is bytes, on Python 3 it is text. prop_value = safe_make_string(prop_value) logger.debug("Setting %s to %r", prop_name, prop_value) # Note that lists are automatically turned into tuples. plugin._setPropValue(prop_name, prop_value) logger.info("Imported HeaderPlugin properties.")
def setUp(self): """Custom shared utility setup for tests.""" self.portal = self.layer["portal"] self.plugin = get_plugin(self.portal)
def test_get_plugin_no_pas(self): from pas.plugins.headers.utils import get_plugin self.portal._delObject("acl_users") self.assertIsNone(get_plugin(self.portal))