def test_plugin_removal(self,
                            name,
                            pull=False,
                            side_effect=None,
                            err_type=None,
                            err_msg=None):
        plugin_name = 'Test Plugin'

        # Mock libraries
        plugin_loader.Resource = MagicMock(name="Resource")

        self.resources_mock = MagicMock()
        plugin_loader.Resource.objects.filter.return_value = []

        plugin_loader.ResourcePlugin = MagicMock(name="ResourcePlugin")

        plugin_mock = MagicMock()
        plugin_mock.name = plugin_name
        plugin_mock.pull_accounting = pull
        plugin_mock.module = 'wstore.asset_manager.resource_plugins.tests.TestPlugin'
        plugin_mock.usage_called = False

        plugin_loader.ResourcePlugin.objects.get.return_value = plugin_mock

        plugin_loader.rmtree = MagicMock(name="rmtree")

        if side_effect is not None:
            side_effect(self)

        # Build plugin loader
        plugin_l = plugin_loader.PluginLoader()

        error = None
        try:
            plugin_l.uninstall_plugin('test_plugin')
        except Exception as e:
            error = e

        if err_type is None:
            self.assertEquals(error, None)
            # Check calls
            plugin_loader.ResourcePlugin.objects.get.assert_called_once_with(
                plugin_id='test_plugin')
            plugin_loader.Resource.objects.filter.assert_called_once_with(
                resource_type=plugin_name)
            plugin_loader.rmtree.assert_called_once_with(
                os.path.join(plugin_l._plugins_path, 'test_plugin'))
            plugin_mock.delete.assert_called_once_with()

            self.assertEquals(pull, plugin_mock.usage_called)
        else:
            self.assertTrue(isinstance(error, err_type))
            self.assertEquals(unicode(e), err_msg)
예제 #2
0
    def test_plugin_removal(self,
                            name,
                            side_effect=None,
                            err_type=None,
                            err_msg=None):

        # Mock libreries
        plugin_loader.Resource = MagicMock(name="Resource")

        self.resources_mock = MagicMock()
        plugin_loader.Resource.objects.filter.return_value = []

        plugin_loader.ResourcePlugin = MagicMock(name="ResourcePlugin")
        plugin_mock = MagicMock()
        plugin_loader.ResourcePlugin.objects.get.return_value = plugin_mock

        plugin_loader.rmtree = MagicMock(name="rmtree")

        if side_effect is not None:
            side_effect(self)

        # Build plugin loader
        plugin_l = plugin_loader.PluginLoader()

        error = None
        try:
            plugin_l.uninstall_plugin('test_plugin')
        except Exception as e:
            error = e

        if err_type is None:
            self.assertEquals(error, None)
            # Check calls
            plugin_loader.ResourcePlugin.objects.get.assert_called_once_with(
                plugin_id='test_plugin')
            plugin_loader.rmtree.assert_called_once_with(
                os.path.join(plugin_l._plugins_path, 'test_plugin'))
            plugin_mock.delete.assert_called_once_with()
        else:
            self.assertTrue(isinstance(error, err_type))
            self.assertEquals(unicode(e), err_msg)
    def test_plugin_installation(self,
                                 name,
                                 zip_file,
                                 expected=None,
                                 side_effect=None,
                                 err_type=None,
                                 err_msg=None):
        # Build plugin loader
        plugin_l = plugin_loader.PluginLoader()

        self.plugin_dir = os.path.join('wstore', 'test')

        # Mock plugin directory location
        plugin_l._plugins_path = self.plugin_dir
        plugin_l._plugins_module = 'wstore.test.'

        # Create a init file in the test dir
        open(os.path.join(self.plugin_dir, '__init__.py'), 'a').close()

        self.zip_path = os.path.join(self.plugin_dir, 'test_plugin_files')
        self.zip_path = os.path.join(self.zip_path, zip_file)

        if side_effect is not None:
            side_effect(self)

        self._to_remove = zip_file
        error = None
        try:
            plugin_l.install_plugin(self.zip_path)
        except Exception as e:
            error = e

        if err_type is None:
            self.assertEquals(error, None)
            # Check calls
            self.manager_mock.validate_plugin_info.assert_called_once_with(
                expected)

            # Check plugin model
            plugin_model = ResourcePlugin.objects.all()[0]
            self.assertEquals(plugin_model.name, expected['name'])
            self.assertEquals(plugin_model.plugin_id,
                              expected['name'].lower().replace(' ', '-'))
            self.assertEquals(plugin_model.author, expected['author'])
            self.assertEquals(plugin_model.version, expected['version'])
            self.assertEquals(plugin_model.formats, expected['formats'])

            self.assertEquals(
                plugin_model.module, 'wstore.test.' + plugin_model.plugin_id +
                '.' + expected['module'])
            self.assertEquals(plugin_model.media_types,
                              expected.get('media_types', []))
            self.assertEquals(plugin_model.form, expected.get('form', {}))

            # Check plugin files
            test_plugin_dir = os.path.join(self.plugin_dir,
                                           plugin_model.plugin_id)
            self.assertTrue(os.path.isdir(test_plugin_dir))
            self.assertTrue(
                os.path.isfile(os.path.join(test_plugin_dir, 'package.json')))
            self.assertTrue(
                os.path.isfile(os.path.join(test_plugin_dir, 'test.py')))

        else:
            self.assertTrue(isinstance(error, err_type))
            self.assertEquals(unicode(e), err_msg)