def test_clean_raises_validation_error_if_compute_resources_is_NONE(self): """ Test whether overriden clean method raises a ValidationError when compute resources is None (the user didn't picked up any in the UI) . """ (pl_meta, tf) = pl_admin.PluginMeta.objects.get_or_create(name='test_plg', type='fs') plugin = pl_admin.Plugin() plugin.meta = pl_meta plugin.version = self.plugin_version # mock manager's register_plugin method with mock.patch.object(pl_admin.PluginManager, 'register_plugin', return_value=plugin) as register_plugin_mock: plugin_admin = pl_admin.PluginAdmin(pl_admin.Plugin, pl_admin.admin.site) form = plugin_admin.form form.instance = plugin form.cleaned_data = { 'name': 'test_plg', 'version': self.plugin_version, 'compute_resources': None } with self.assertRaises(forms.ValidationError): form.clean(form)
def test_clean_validate_name_version_and_save_plugin_descriptors(self): """ Test whether overriden clean method validates the full set of plugin descriptors and save the newly created plugin to the DB. """ # mock manager's register_plugin method (pl_meta, tf) = pl_admin.PluginMeta.objects.get_or_create(name=self.plugin_name, type='fs') (plugin, tf) = pl_admin.Plugin.objects.get_or_create( meta=pl_meta, version=self.plugin_version) plugin.compute_resources.set([self.compute_resource]) plugin.save() with mock.patch.object(pl_admin.PluginManager, 'register_plugin', return_value=plugin) as register_plugin_mock: plugin_admin = pl_admin.PluginAdmin(pl_admin.Plugin, pl_admin.admin.site) form = plugin_admin.form form.instance = self.plugin form.cleaned_data = { 'name': self.plugin_name, 'version': self.plugin_version, 'compute_resources': [self.compute_resource] } self.assertIsNone(form.instance.pk) form.clean(form) self.assertEqual(form.instance, plugin) self.assertIsNotNone(form.instance.pk) register_plugin_mock.assert_called_with(self.plugin_name, self.plugin_version, 'host')
def test_register_plugins_from_file(self): """ Test whether custom register_plugins_from_file method registers plugins from the ChRIS store that have been specified in a text file. """ plugin_admin = pl_admin.PluginAdmin(pl_admin.Plugin, pl_admin.admin.site) file_content = b'simplefsapp host\n simpledsapp 1.0.6 host\n http://chris-store.local:8010/api/v1/1/ host\n' with mock.patch.object(pl_admin.PluginManager, 'register_plugin', return_value=None) as register_plugin_mock: with mock.patch.object( pl_admin.PluginManager, 'register_plugin_by_url', return_value=None) as register_plugin_by_url_mock: summary = { 'success': [{ 'plugin_name': 'simplefsapp' }, { 'plugin_name': 'simpledsapp' }, { 'plugin_name': 'http://chris-store.local:8010/api/v1/1/' }], 'error': [] } with io.BytesIO(file_content) as f: self.assertEqual( plugin_admin.register_plugins_from_file(f), summary) register_plugin_mock.assert_called_with( 'simpledsapp', '1.0.6', 'host') register_plugin_by_url_mock.assert_called_with( 'http://chris-store.local:8010/api/v1/1/', 'host') with mock.patch.object(pl_admin.PluginManager, 'register_plugin_by_url', side_effect=Exception('Error')): summary = { 'success': [{ 'plugin_name': 'simplefsapp' }, { 'plugin_name': 'simpledsapp' }], 'error': [{ 'plugin_name': 'http://chris-store.local:8010/api/v1/1/', 'code': 'Error' }] } with io.BytesIO(file_content) as f: self.assertEqual( plugin_admin.register_plugins_from_file(f), summary)
def test_add_plugins_from_file_view(self): """ Test whether custom add_plugins_from_file_view view passes a summary dict in the context for valid form POST or otherwise an empty form. """ plugin_admin = pl_admin.PluginAdmin(pl_admin.Plugin, pl_admin.admin.site) summary = {'success': [{'plugin_name': 'my_plugin'}], 'error': []} plugin_admin.register_plugins_from_file = mock.Mock( return_value=summary) request_mock = mock.Mock() request_mock.META = {'SCRIPT_NAME': ''} with mock.patch.object(pl_admin, 'render', return_value=mock.Mock()) as render_mock: request_mock.method = 'GET' plugin_admin.add_plugins_from_file_view(request_mock) render_mock.assert_called_with( request_mock, 'admin/plugins/plugin/add_plugins_from_file.html', { 'site_title': 'ChRIS Admin', 'site_header': 'ChRIS Administration', 'site_url': ANY, 'has_permission': ANY, 'available_apps': ANY, 'is_popup': ANY, 'opts': ANY, 'file_form': ANY }) request_mock.method = 'POST' request_mock.FILES = {'file': 'f'} with mock.patch.object(pl_admin.UploadFileForm, 'is_valid', return_value=True) as is_valid_mock: plugin_admin.add_plugins_from_file_view(request_mock) plugin_admin.register_plugins_from_file.assert_called_with( request_mock.FILES['file']) render_mock.assert_called_with( request_mock, 'admin/plugins/plugin/add_plugins_from_file_result.html', { 'site_title': 'ChRIS Admin', 'site_header': 'ChRIS Administration', 'site_url': ANY, 'has_permission': ANY, 'available_apps': ANY, 'is_popup': ANY, 'opts': ANY, 'summary': summary })
def test_change_view(self): """ Test whether overriden change_view view shows all plugin fields in readonly mode except the 'compute_resources' field that is shown in editable mode. """ plugin_admin = pl_admin.PluginAdmin(pl_admin.Plugin, pl_admin.admin.site) request_mock = mock.Mock() with mock.patch.object(pl_admin.admin.ModelAdmin, 'change_view', return_value=None) as change_view_mock: plugin_admin.change_view(request_mock, 1) self.assertNotIn('compute_resources', plugin_admin.readonly_fields) self.assertEqual(len(plugin_admin.fieldsets), 2) change_view_mock.assert_called_with(plugin_admin, request_mock, 1, '', None)
def test_add_view(self): """ Test whether overriden add_view view only shows the required fields in the add plugin page and in editable mode. """ plugin_admin = pl_admin.PluginAdmin(pl_admin.Plugin, pl_admin.admin.site) request_mock = mock.Mock() with mock.patch.object(pl_admin.admin.ModelAdmin, 'add_view', return_value=None) as add_view_mock: plugin_admin.add_view(request_mock) self.assertEqual(len(plugin_admin.fieldsets), 3) self.assertEqual(len(plugin_admin.readonly_fields), 0) add_view_mock.assert_called_with(plugin_admin, request_mock, '', None)
def test_delete_queryset(self): """ Test whether overriden delete_queryset method deletes plugin metas if their last associated plugin is deleted. """ (compute_resource, tf) = pl_admin.ComputeResource.objects.get_or_create( name="host", compute_url=COMPUTE_RESOURCE_URL) (pl_meta, tf) = pl_admin.PluginMeta.objects.get_or_create(name='pacspull', type='fs') (plg, tf) = pl_admin.Plugin.objects.get_or_create(meta=pl_meta, version='0.1') plg.compute_resources.set([compute_resource]) plg.save() plugin_admin = pl_admin.PluginAdmin(pl_admin.Plugin, pl_admin.admin.site) request_mock = mock.Mock() plugin_admin.delete_queryset(request_mock, pl_meta.plugins.all()) with self.assertRaises(pl_admin.PluginMeta.DoesNotExist): pl_admin.PluginMeta.objects.get(name='pacspull')
def test_clean_raises_validation_error_if_cannot_register_plugin(self): """ Test whether overriden clean method raises a ValidationError when there is a validation error or it cannot save a new plugin to the DB. """ # mock manager's register_plugin method with mock.patch.object(pl_admin.PluginManager, 'register_plugin', side_effect=Exception) as register_plugin_mock: plugin_admin = pl_admin.PluginAdmin(pl_admin.Plugin, pl_admin.admin.site) form = plugin_admin.form form.instance = self.plugin form.cleaned_data = { 'name': self.plugin_name, 'version': self.plugin_version, 'compute_resources': [self.compute_resource] } self.assertIsNone(form.instance.pk) with self.assertRaises(forms.ValidationError): form.clean(form)
def test_clean_validate_url_and_save_plugin_descriptors(self): """ Test whether overriden clean method validates the full set of plugin descriptors and save the newly created plugin to the DB. """ # mock manager's register_plugin_by_url method (pl_meta, tf) = pl_admin.PluginMeta.objects.get_or_create(name=self.plugin_name, type='fs') (plugin, tf) = pl_admin.Plugin.objects.get_or_create( meta=pl_meta, version=self.plugin_version) plugin.compute_resources.set([self.compute_resource]) plugin.save() plugin_store_url = "http://127.0.0.1:8010/api/v1/1/" with mock.patch.object( pl_admin.PluginManager, 'register_plugin_by_url', return_value=plugin) as register_plugin_by_url_mock: plugin_admin = pl_admin.PluginAdmin(pl_admin.Plugin, pl_admin.admin.site) form = plugin_admin.form form.instance = self.plugin form.cleaned_data = { 'url': plugin_store_url, 'compute_resources': plugin.compute_resources.all() } self.assertIsNone(form.instance.pk) form.clean(form) self.assertEqual(form.cleaned_data.get('name'), self.plugin_name) self.assertEqual(form.cleaned_data.get('version'), self.plugin_version) self.assertNotIn('url', form.cleaned_data) self.assertEqual(form.instance, plugin) self.assertIsNotNone(form.instance.pk) register_plugin_by_url_mock.assert_called_with( plugin_store_url, 'host')