Exemplo n.º 1
0
class PluginManagerTests(unittest.TestCase):
    def setUp(self):
        self.app = Flask(__name__)
        self.app.config['TESTING'] = True
        self.plugin_manager = PluginManager()
        self.plugin_manager.init_app(self.app)

    def test_find_plugins(self):
        found_plugins = self.plugin_manager.find_plugins()
        self.assertEqual(len(found_plugins), 3)
        expected_plugins = [
            'TestOnePlugin', 'TestTwoPlugin', 'TestThreePlugin'
        ]
        self.assertEquals(sorted(found_plugins), sorted(expected_plugins))

    def test_plugins(self):
        self.plugin_manager._plugins = None
        self.assertEquals(self.plugin_manager._plugins, None)

        self.assertEquals(sorted(self.plugin_manager.plugins.keys()),
                          ["test1", "test2"])

    def test_all_plugins(self):
        self.plugin_manager._all_plugins = None
        self.assertEquals(self.plugin_manager._all_plugins, None)

        self.assertEquals(sorted(self.plugin_manager.all_plugins.keys()),
                          ["test1", "test2", "test3"])
Exemplo n.º 2
0
class PluginManagerGetPlugins(unittest.TestCase):
    def setUp(self):
        self.app = Flask(__name__)
        self.app.config['TESTING'] = True
        self.plugin_manager = PluginManager()
        self.plugin_manager.init_app(self.app)

    def test_get_enabled_plugins(self):
        with self.app.test_request_context():
            plugins = get_enabled_plugins()

        self.assertEquals(set(plugins),
                          set(self.plugin_manager.plugins.values()))

    def test_get_all_plugins(self):
        with self.app.test_request_context():
            plugins = get_all_plugins()

        self.assertEquals(len(plugins), 3)

    def test_get_plugin(self):
        with self.app.test_request_context():
            plugin = get_plugin("test1")

        self.assertEquals(plugin, self.plugin_manager.plugins["test1"])

    def test_get_plugin_from_all(self):
        with self.app.test_request_context():
            plugin = get_plugin_from_all("test3")

        self.assertFalse(plugin.enabled)
Exemplo n.º 3
0
class PluginManagerManagementMethodsTests(unittest.TestCase):
    def setUp(self):
        self.app = Flask(__name__)
        self.app.config['TESTING'] = True
        self.plugin_manager = PluginManager()
        self.plugin_manager.init_app(self.app)

        # Call load_plugins to reload them without calling setup_plugins()
        self.plugin_manager.load_plugins()

    def test_setup_plugins(self):
        plugin = self.plugin_manager.plugins["test1"]
        self.assertFalse(plugin.setup_called)
        self.plugin_manager.setup_plugins()
        self.assertTrue(plugin.setup_called)

    def test_install_plugins(self):
        plugin = self.plugin_manager.plugins["test1"]
        self.assertFalse(plugin.install_called)
        self.plugin_manager.install_plugins()
        self.assertTrue(plugin.install_called)

    def test_uninstall_plugins(self):
        plugin = self.plugin_manager.plugins["test1"]
        self.assertFalse(plugin.uninstall_called)
        self.plugin_manager.uninstall_plugins()
        self.assertTrue(plugin.uninstall_called)
Exemplo n.º 4
0
class PluginManagerOnePluginTests(unittest.TestCase):
    """Tests the plugin.setup(), plugin.enabled()... methods"""
    def setUp(self):
        self.app = Flask(__name__)
        self.app.config['TESTING'] = True
        self.plugin_manager = PluginManager()
        self.plugin_manager.init_app(self.app)

    def tearDown(self):
        # Make sure plugin "test1" is enabled
        plugin_path = self.plugin_manager.all_plugins["test1"].path
        if os.path.exists(os.path.join(plugin_path, "DISABLED")):
            os.remove(os.path.join(plugin_path, "DISABLED"))
            self.plugin_manager.load_plugins()

    def test_plugin_disable_and_enable(self):
        plugin = self.plugin_manager.plugins["test1"]
        self.assertFalse(plugin.disable())
        plugin = self.plugin_manager.all_plugins["test1"]
        self.assertTrue(plugin.enable())

    def test_plugin_setup(self):
        plugin = self.plugin_manager.plugins["test1"]
        plugin.setup()
        self.assertTrue(plugin.setup_called)

    def test_plugin_install(self):
        plugin = self.plugin_manager.plugins["test1"]
        plugin.install()
        self.assertTrue(plugin.install_called)

    def test_plugin_uninstall(self):
        plugin = self.plugin_manager.plugins["test1"]
        plugin.uninstall()
        self.assertTrue(plugin.uninstall_called)
Exemplo n.º 5
0
class PluginManagerTests(unittest.TestCase):

    def setUp(self):
        self.app = Flask(__name__)
        self.app.config['TESTING'] = True
        self.plugin_manager = PluginManager()
        self.plugin_manager.init_app(self.app)

    def test_find_plugins(self):
        found_plugins = self.plugin_manager.find_plugins()
        self.assertEqual(len(found_plugins), 3)
        expected_plugins = ['TestOnePlugin', 'TestTwoPlugin',
                            'TestThreePlugin']
        self.assertEquals(sorted(found_plugins), sorted(expected_plugins))

    def test_plugins(self):
        self.plugin_manager._plugins = None
        self.assertEquals(self.plugin_manager._plugins, None)

        self.assertEquals(
            sorted(self.plugin_manager.plugins.keys()),
            ["test1", "test2"]
        )

    def test_all_plugins(self):
        self.plugin_manager._all_plugins = None
        self.assertEquals(self.plugin_manager._all_plugins, None)

        self.assertEquals(
            sorted(self.plugin_manager.all_plugins.keys()),
            ["test1", "test2", "test3"]
        )
Exemplo n.º 6
0
class PluginManagerGetPlugins(unittest.TestCase):

    def setUp(self):
        self.app = Flask(__name__)
        self.app.config['TESTING'] = True
        self.plugin_manager = PluginManager()
        self.plugin_manager.init_app(self.app)

    def test_get_enabled_plugins(self):
        with self.app.test_request_context():
            plugins = get_enabled_plugins()

        self.assertEquals(
            set(plugins),
            set(self.plugin_manager.plugins.values())
        )

    def test_get_all_plugins(self):
        with self.app.test_request_context():
            plugins = get_all_plugins()

        self.assertEquals(len(plugins), 3)

    def test_get_plugin(self):
        with self.app.test_request_context():
            plugin = get_plugin("test1")

        self.assertEquals(plugin, self.plugin_manager.plugins["test1"])

    def test_get_plugin_from_all(self):
        with self.app.test_request_context():
            plugin = get_plugin_from_all("test3")

        self.assertFalse(plugin.enabled)
Exemplo n.º 7
0
class PluginManagerManagementMethodsTests(unittest.TestCase):
    def setUp(self):
        self.app = Flask(__name__)
        self.app.config['TESTING'] = True
        self.plugin_manager = PluginManager()
        self.plugin_manager.init_app(self.app)

        # Call load_plugins to reload them without calling setup_plugins()
        self.plugin_manager.load_plugins()

    def test_setup_plugins(self):
        plugin = self.plugin_manager.plugins["test1"]
        self.assertFalse(plugin.setup_called)
        self.plugin_manager.setup_plugins()
        self.assertTrue(plugin.setup_called)

    def test_install_plugins(self):
        plugin = self.plugin_manager.plugins["test1"]
        self.assertFalse(plugin.install_called)
        self.plugin_manager.install_plugins()
        self.assertTrue(plugin.install_called)

    def test_uninstall_plugins(self):
        plugin = self.plugin_manager.plugins["test1"]
        self.assertFalse(plugin.uninstall_called)
        self.plugin_manager.uninstall_plugins()
        self.assertTrue(plugin.uninstall_called)
Exemplo n.º 8
0
class PluginManagerOnePluginTests(unittest.TestCase):
    """Tests the plugin.setup(), plugin.enabled()... methods"""
    def setUp(self):
        self.app = Flask(__name__)
        self.app.config['TESTING'] = True
        self.plugin_manager = PluginManager()
        self.plugin_manager.init_app(self.app)

    def tearDown(self):
        # Make sure plugin "test1" is enabled
        plugin_path = self.plugin_manager.all_plugins["test1"].path
        if os.path.exists(os.path.join(plugin_path, "DISABLED")):
            os.remove(os.path.join(plugin_path, "DISABLED"))
            self.plugin_manager.load_plugins()

    def test_plugin_disable_and_enable(self):
        plugin = self.plugin_manager.plugins["test1"]
        self.assertFalse(plugin.disable())
        plugin = self.plugin_manager.all_plugins["test1"]
        self.assertTrue(plugin.enable())

    def test_plugin_setup(self):
        plugin = self.plugin_manager.plugins["test1"]
        plugin.setup()
        self.assertTrue(plugin.setup_called)

    def test_plugin_install(self):
        plugin = self.plugin_manager.plugins["test1"]
        plugin.install()
        self.assertTrue(plugin.install_called)

    def test_plugin_uninstall(self):
        plugin = self.plugin_manager.plugins["test1"]
        plugin.uninstall()
        self.assertTrue(plugin.uninstall_called)
Exemplo n.º 9
0
class WekoPlugins(object):
    """weko-plugins extension."""
    def __init__(self, app=None):
        """Extension initialization.

        :param app: The Flask application. (Default: ``None``)
        """
        if app:
            self.init_app(app)

    def init_app(self, app):
        """Flask application initialization.

        :param app: The Flask application.
        """
        self.init_config(app)
        self.plugin_manager = PluginManager()
        sys.path.append('/code')
        root_path = app.root_path
        app.root_path = os.path.join('/code', 'plugins')
        self.plugin_manager.init_app(app,
                                     base_app_folder='plugins',
                                     plugin_folder='plugin')
        app.root_path = root_path
        app.register_blueprint(blueprint)
        # Register Jinja2 template filters for plugins formatting
        app.add_template_global(current_plugins, name='current_plugins')
        app.extensions['weko-plugins'] = self

    def init_config(self, app):
        """Initialize configuration.

        :param app: The Flask application.
        """
        # Use theme's base template if theme is installed
        if 'BASE_EDIT_TEMPLATE' in app.config:
            app.config.setdefault(
                'WEKO_PLUGINS_BASE_TEMPLATE',
                app.config['BASE_EDIT_TEMPLATE'],
            )
        for k in dir(config):
            if k.startswith('WEKO_PLUGINS_'):
                app.config.setdefault(k, getattr(config, k))

    def get_enabled_plugins(self):
        """
        Return all enabled plugins list.

        :return: Pluging List Info

        """
        plugins = get_enabled_plugins()
        return tuple(
            map(
                lambda plugin: (plugin.name, plugin.identifier)
                if plugin.enabled else None, plugins))
Exemplo n.º 10
0
class PluginManagerOtherDirectoryTests(unittest.TestCase):
    def setUp(self):
        self.app = Flask(__name__)
        self.app.config['TESTING'] = True
        self.plugin_manager = PluginManager()

    def test_wrong_plugin(self):
        # should raise an exception because the plugin in the "plugs" folder
        # have set the __plugin__ variable not correctly.
        with self.assertRaises(PluginError):
            self.plugin_manager.init_app(self.app, plugin_folder="plugs")
Exemplo n.º 11
0
class PluginManagerOtherDirectoryTests(unittest.TestCase):
    def setUp(self):
        self.app = Flask(__name__)
        self.app.config['TESTING'] = True
        self.plugin_manager = PluginManager()

    def test_wrong_plugin(self):
        # should raise an exception because the plugin in the "plugs" folder
        # have set the __plugin__ variable not correctly.
        with self.assertRaises(PluginError):
            self.plugin_manager.init_app(self.app, plugin_folder="plugs")
Exemplo n.º 12
0
class PluginManagerEnableDisableTests(unittest.TestCase):
    def setUp(self):
        self.app = Flask(__name__)
        self.app.config['TESTING'] = True
        self.plugin_manager = PluginManager()
        self.plugin_manager.init_app(self.app)

    def test_disable_plugins(self):
        plugin = self.plugin_manager.plugins["test1"]
        self.assertTrue(plugin.enabled)
        self.assertEquals(self.plugin_manager.disable_plugins([plugin]), 1)
        self.assertTrue(os.path.exists(os.path.join(plugin.path, "DISABLED")))

    def test_enable_plugins(self):
        plugin = self.plugin_manager.all_plugins["test1"]
        self.assertFalse(plugin.enabled)
        self.plugin_manager.enable_plugins([plugin])
        self.assertTrue(plugin.enabled)
Exemplo n.º 13
0
class PluginManagerEnableDisableTests(unittest.TestCase):
    def setUp(self):
        self.app = Flask(__name__)
        self.app.config['TESTING'] = True
        self.plugin_manager = PluginManager()
        self.plugin_manager.init_app(self.app)

    def test_disable_plugins(self):
        plugin = self.plugin_manager.plugins["test1"]
        self.assertTrue(plugin.enabled)
        self.assertEquals(self.plugin_manager.disable_plugins([plugin]), 1)
        self.assertTrue(os.path.exists(os.path.join(plugin.path, "DISABLED")))

    def test_enable_plugins(self):
        plugin = self.plugin_manager.all_plugins["test1"]
        self.assertFalse(plugin.enabled)
        self.plugin_manager.enable_plugins([plugin])
        self.assertTrue(plugin.enabled)
Exemplo n.º 14
0
    def test_init_app(self):
        plugin_manager = PluginManager()
        plugin_manager.init_app(self.app)

        self.assertIsInstance(plugin_manager, PluginManager)
Exemplo n.º 15
0
# [START app]

from flask import Flask
from flask_plugins import PluginManager

app = Flask(__name__, static_url_path='/public', static_folder='public')
plugin_manager = PluginManager()
plugin_manager.init_app(app)

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=9060, debug=True)
Exemplo n.º 16
0
    def test_init_app(self):
        plugin_manager = PluginManager()
        plugin_manager.init_app(self.app)

        self.assertIsInstance(plugin_manager, PluginManager)