def test_model(self): """Test the PluginConfig model.""" from plugin import registry from plugin.models import PluginConfig fixtures = PluginConfig.objects.all() # check if plugins were registered if not fixtures: registry.reload_plugins() fixtures = PluginConfig.objects.all() # check mixin registry plg = fixtures.first() mixin_dict = plg.mixins() self.assertIn('base', mixin_dict) self.assertDictContainsSubset( {'base': { 'key': 'base', 'human_name': 'base' }}, mixin_dict) # check reload on save with self.assertWarns(Warning) as cm: plg_inactive = fixtures.filter(active=False).first() plg_inactive.active = True plg_inactive.save() self.assertEqual(cm.warning.args[0], 'A reload was triggered')
def test_valid_plugin_slug(self): """Test that an valid plugin slug runs through.""" # load plugin configs fixtures = PluginConfig.objects.all() if not fixtures: registry.reload_plugins() fixtures = PluginConfig.objects.all() # get data url = reverse('api-plugin-setting-detail', kwargs={'plugin': 'sample', 'key': 'API_KEY'}) response = self.get(url, expected_code=200) # check the right setting came through self.assertTrue(response.data['key'], 'API_KEY') self.assertTrue(response.data['plugin'], 'sample') self.assertTrue(response.data['type'], 'string') self.assertTrue(response.data['description'], 'Key required for accessing external API') # Failure mode tests # Non - exsistant plugin url = reverse('api-plugin-setting-detail', kwargs={'plugin': 'doesnotexist', 'key': 'doesnotmatter'}) response = self.get(url, expected_code=404) self.assertIn("Plugin 'doesnotexist' not installed", str(response.data)) # Wrong key url = reverse('api-plugin-setting-detail', kwargs={'plugin': 'sample', 'key': 'doesnotexsist'}) response = self.get(url, expected_code=404) self.assertIn("Plugin 'sample' has no setting matching 'doesnotexsist'", str(response.data))
def run_reload(self, envs=None): """Helper function to reload InvenTree.""" # Set default - see B006 if envs is None: envs = {} from plugin import registry with self.in_env_context(envs): settings.USER_ADDED = False registry.reload_plugins()
def test_restart_flag(self): """Test that the restart flag is reset on start.""" import common.models from plugin import registry # set flag true common.models.InvenTreeSetting.set_setting('SERVER_RESTART_REQUIRED', False, None) # reload the app registry.reload_plugins() # now it should be false again self.assertFalse(common.models.InvenTreeSetting.get_setting('SERVER_RESTART_REQUIRED'))
def plugin_update(queryset, new_status: bool): """General function for bulk changing plugins.""" apps_changed = False # Run through all plugins in the queryset as the save method needs to be overridden for plugin in queryset: if plugin.active is not new_status: plugin.active = new_status plugin.save(no_reload=True) apps_changed = True # Reload plugins if they changed if apps_changed: pl_registry.reload_plugins()
def test_initial_install(self): """Test if install of plugins on startup works""" from plugin import registry # Check an install run response = registry.install_plugin_file() self.assertEqual(response, 'first_run') # Set dynamic setting to True and rerun to launch install InvenTreeSetting.set_setting('PLUGIN_ON_STARTUP', True, self.user) registry.reload_plugins() # Check that there was anotehr run response = registry.install_plugin_file() self.assertEqual(response, True)
def save(self, force_insert=False, force_update=False, *args, **kwargs): """Extend save method to reload plugins if the 'active' status changes.""" reload = kwargs.pop('no_reload', False) # check if no_reload flag is set ret = super().save(force_insert, force_update, *args, **kwargs) if not reload: if (self.active is False and self.__org_active is True) or \ (self.active is True and self.__org_active is False): if settings.PLUGIN_TESTING: warnings.warn('A reload was triggered') registry.reload_plugins() return ret
def run_reload(self, envs={}): from plugin import registry with self.in_env_context(envs): settings.USER_ADDED = False registry.reload_plugins()
def test_admin_action(self): """Test the PluginConfig action commands.""" from plugin import registry from plugin.models import PluginConfig url = reverse('admin:plugin_pluginconfig_changelist') fixtures = PluginConfig.objects.all() # check if plugins were registered -> in some test setups the startup has no db access print(f'[PLUGIN-TEST] currently {len(fixtures)} plugin entries found') if not fixtures: registry.reload_plugins() fixtures = PluginConfig.objects.all() print(f'Reloaded plugins - now {len(fixtures)} entries found') print([str(a) for a in fixtures]) fixtures = fixtures[0:1] # deactivate plugin response = self.client.post( url, { 'action': 'plugin_deactivate', 'index': 0, '_selected_action': [f.pk for f in fixtures], }, follow=True) self.assertEqual(response.status_code, 200) # deactivate plugin - deactivate again -> nothing will hapen but the nothing 'changed' function is triggered response = self.client.post( url, { 'action': 'plugin_deactivate', 'index': 0, '_selected_action': [f.pk for f in fixtures], }, follow=True) self.assertEqual(response.status_code, 200) # activate plugin response = self.client.post( url, { 'action': 'plugin_activate', 'index': 0, '_selected_action': [f.pk for f in fixtures], }, follow=True) self.assertEqual(response.status_code, 200) # activate everything fixtures = PluginConfig.objects.all() response = self.client.post( url, { 'action': 'plugin_activate', 'index': 0, '_selected_action': [f.pk for f in fixtures], }, follow=True) self.assertEqual(response.status_code, 200) fixtures = PluginConfig.objects.filter(active=True) # save to deactivate a plugin response = self.client.post(reverse('admin:plugin_pluginconfig_change', args=(fixtures.first().pk, )), { '_save': 'Save', }, follow=True) self.assertEqual(response.status_code, 200)