def _pre_app_start(self): self.channel = PluginChannel( plugin=Plugin.selectBy(name='editor').getOne(), name='Editor API', subscription_right='public') self.channel2 = PluginChannel( plugin=Plugin.selectBy(name='editor').getOne(), name='Editor API 2', subscription_right='public', plugin_config={ 'enable_rest_api': True, 'api_key': 'aaaaaaaa' })
def setUp(self, fake_plugin_middleware=lambda: None, ictv_middleware=lambda: None): super().setUp(fake_plugin_middleware, ictv_middleware) building = Building(name="mytestbuilding") self.screen = Screen(name="mytestscreen", building=building, secret="secret") self.other_screen = Screen(name="myothertestscreen", building=building, secret="secret") self.plugins = [Plugin(name="%s%d" % (self.plugin_name, i), activated="yes") for i in range(self.n_elements - 1)] self.plugins.append(Plugin.selectBy(name="fake_plugin").getOne()) self.plugin_channels = [PluginChannel(name="%s%d" % (self.plugin_channel_name, i), plugin=self.plugins[i], subscription_right="public") for i in range(self.n_elements)] self.bundle_channels = [ChannelBundle(name="%s%d" % (self.bundle_channel_name, i), subscription_right="public") for i in range(self.n_elements)] other_channel = PluginChannel(name="other_channel", plugin=self.plugins[0], subscription_right="public") User(email=self.user_nothing_email, disabled=False) User(email=self.user_administrator_email, disabled=False, admin=True) User(email=self.user_super_administrator_email, disabled=False, admin=True, super_admin=True) screen_owner = User(email=self.user_screen_owner_email, disabled=False) self.screen.safe_add_user(screen_owner) other_screen_owner = User(email=self.user_other_screen_owner_email, disabled=False) self.other_screen.safe_add_user(other_screen_owner) contributor = User(email=self.user_contributor_email, disabled=False) [plugin_channel.give_permission_to_user(contributor) for plugin_channel in self.plugin_channels] contributor_other_channel = User(email=self.user_contributor_of_other_channel_email, disabled=False) other_channel.give_permission_to_user(contributor_other_channel) administrator_other_channel = User(email=self.user_administrator_of_other_channel_email, disabled=False) other_channel.give_permission_to_user(administrator_other_channel, UserPermissions.channel_administrator) channel_admin = User(email=self.user_channel_admin_email, disabled=False) [plugin_channel.give_permission_to_user(channel_admin, UserPermissions.channel_administrator) for plugin_channel in self.plugin_channels]
def get_logger(plugin_name, channel=None): """ Returns a Python Logger for plugins already configured with a FileHandler outputting the login the plugin directory. If a channel is provided, the default formatting outputs its name and id in a unified way for all plugins. """ # TODO: handle name interferences with core loggers logger_name = plugin_name + ('_plugin' if not channel else '_channels') logger = logging.getLogger(logger_name) logger.setLevel(logging.INFO) if not channel: formatter = logging.Formatter('%(levelname)s : %(asctime)s - %(message)s') else: formatter = logging.Formatter( '%(levelname)s : %(asctime)s [PluginChannel %(channel_name)s (%(channel_id)s)] - %(message)s') dirname = Plugin.selectBy(name=plugin_name).getOne().package_path if os.path.isdir(os.path.join(dirname)): if not logger.hasHandlers(): logger_file_path = os.path.join(dirname, logger_name + os.path.extsep + 'log') file_handler = logging.FileHandler(logger_file_path) file_handler.setFormatter(formatter) logger.addHandler(file_handler) logger.addHandler(StatHandler(logger_name, logger_file_path)) return logger else: raise Exception('the plugin with name %s is not a valid plugin' % plugin_name)
def f(): p = Plugin.selectBy(name='fake_plugin').getOne() p.activated = 'yes' p.channels_params['this_parameter_does_not_exists'] = None p.channels_params = p.channels_params # Force SQLObject update PluginParamAccessRights(plugin=p, name='this_parameter_does_not_exists')
def setUp( self ): # This test duplicates a lot of setUp code, it should be refactored or dropped from ictv.app import get_app from ictv.database import setup_database, create_database, load_plugins from ictv.plugin_manager.plugin_manager import PluginManager from paste.fixture import TestApp create_fake_plugin(self.fake_plugin_root) setup_database() create_database() load_plugins() Plugin.selectBy(name='fake_plugin').getOne().activated = 'notfound' Plugin.update_plugins(PluginManager.list_plugins()) self.ictv_app = get_app("/tmp/config.yaml") self.testApp = TestApp(self.ictv_app.wsgifunc())
def get_plugin_missing_dependencies(plugin_name): """ Returns the dependencies that are not met for the given plugin. If all are met or the plugin could not be found or has no dependencies, it returns an empty list. """ path = os.path.join(Plugin.selectBy(name=plugin_name).getOne().package_path, 'config.yaml') failed_import = [] if os.path.isfile(path): with open(path, 'r') as f: config = yaml.unsafe_load(f) if 'dependencies' in config['plugin']: for module_name in config['plugin']['dependencies']: try: importlib.import_module(module_name) except ImportError: failed_import.append(module_name) logger.info('Import failed for module %s when checking dependencies of plugins %s' % ( module_name, plugin_name)) else: return None return failed_import
def render_page(self, current_user=None, users=None): if current_user is None: current_user = User.get(self.session['user']['id']) if UserPermissions.channel_administrator in current_user.highest_permission_level: users = User.select() channels = list(Channel.get_visible_channels_of(current_user)) plugin_channels = [] bundles = [] for channel in channels: if type(channel) is PluginChannel: plugin_channels.append(channel) elif type(channel) is ChannelBundle: bundles.append(channel) return self.renderer.channels( plugin_channels=plugin_channels, bundles=bundles, current_user=current_user, users=users, activated_plugins=Plugin.selectBy(activated='yes'), found_plugins=Plugin.select(NOT(Plugin.q.activated == 'notfound')))
def runTest(self): """ Tests the Channel object. """ try: PluginChannel(name='test', plugin=Plugin(name='channel_plugin', activated='no'), subscription_right='restricted') PluginChannel( name='test2', plugin=Plugin.selectBy(name='channel_plugin').getOne(), subscription_right='restricted') c = PluginChannel.selectBy(name="test").getOne() assert_not_equal(None, c) c.set(name="testNew") assert_equal(c.name, "testNew") u = User(username='******', fullname='test test', email='*****@*****.**', super_admin=True, disabled=False) up = UserPermissions.channel_contributor c.give_permission_to_user(u, up) role = Role.selectBy(user=u, channel=c).getOne(None) assert_true(role != None) c.give_permission_to_user(u, up) role = Role.selectBy(user=u, channel=c).getOne(None) assert_true(role != None) getup = c.get_channel_permissions_of(u) assert_equal(getup, up) getupnoperm = c.get_channel_permissions_of(User.get(1)) assert_equal(getupnoperm, UserPermissions.no_permission) c.remove_permission_to_user(u) role = Role.selectBy(user=u, channel=c).getOne(None) assert_true(role == None) assert_false(c.has_admin(u)) up = UserPermissions.channel_administrator c.give_permission_to_user(u, up) assert_true(c.has_admin(u)) assert_true(c.has_contrib(u)) assert_is_not_none(c.get_admins()) assert_is_not_none(c.get_contribs()) assert_in(str(u.id), c.get_users_as_json()) c.remove_permission_to_user(u) role = Role.selectBy(user=u, channel=c).getOne(None) assert_true(role == None) c.give_permission_to_user(None, up) role = Role.selectBy(user=u).getOne(None) assert_true(role == None) tru = c.has_visible_params_for(u) assert_true(tru) u3 = User(username='******', fullname='test3 test2', email='*****@*****.**', super_admin=False, disabled=False) tru = c.has_visible_params_for(u3) assert_false(tru) t = PluginChannel.delete(c.id) assert_equal(t, None) # try to delete a channel used by a screen - Seems to work... sc = Screen(name='A', building=Building(name='A')) sc.subscribe_to(u, PluginChannel.get(2)) # t2 = PluginChannel.delete(2) c4 = PluginChannel.get(2) nbSub = c4.subscriptions.count() c4.set(enabled=False) assert_equal(nbSub, c4.subscriptions.count()) c4.set(enabled=True) c4.set(subscription_right="private") assert_equal(nbSub, c4.subscriptions.count()) c4.set(subscription_right="public") # todo seems working by bypassing the webinterface c4.set(cache_validity=-10) assert_true(c4.cache_validity < 0) sc.unsubscribe_from(u, PluginChannel.get(2)) u2 = User(username='******', fullname='test2 test2', email='*****@*****.**', super_admin=False, disabled=False) l = PluginChannel.get_screens_channels_from(u2) assert_is_not_none(l) temp = PluginChannel.get_visible_channels_of(u2) assert_is_not_none(temp) User.delete(u.id) User.delete(u2.id) User.delete(u3.id) except DuplicateEntryError: assert_true(False) return
def runTest(self): """ Tests the plugin config page. """ r = self.testApp.get('/plugins/%d/config' % Plugin.selectBy(name='fake_plugin').getOne().id, status=200) assert_not_equal(r.body, None)
def runTest(self): """ Tests the PluginChannel SQLObject """ Channel.deleteMany(None) fake_plugin = Plugin.selectBy(name='fake_plugin').getOne() plugin_channel = PluginChannel(name='MyPluginChannel', plugin=fake_plugin, subscription_right='public') user = User(fullname='User', email='test@localhost') user2 = User(fullname='User2', email='test2@localhost') assert plugin_channel.get_type_name() == 'Plugin fake_plugin' # Test user permissions def assert_no_permission(c, u): assert c.get_channel_permissions_of( u) == UserPermissions.no_permission assert u not in c.get_admins() and u not in c.get_contribs() def has_contrib(u, check_inlist=True): return plugin_channel.has_contrib(u) and ( not check_inlist or u in plugin_channel.get_contribs()) def has_admin(u): return plugin_channel.has_admin( u) and u in plugin_channel.get_admins() assert_no_permission(plugin_channel, user) assert_no_permission(plugin_channel, user2) plugin_channel.give_permission_to_user( user, UserPermissions.channel_contributor) role = Role.selectBy(user=user, channel=plugin_channel).getOne() assert has_contrib(user) assert not has_admin(user) assert not has_contrib(user2) assert not has_admin(user2) assert role.permission_level == UserPermissions.channel_contributor == plugin_channel.get_channel_permissions_of( user) assert json.loads(plugin_channel.get_users_as_json()) == { str(user.id): UserPermissions.channel_contributor.value } plugin_channel.give_permission_to_user( user, UserPermissions.channel_administrator) assert has_contrib(user, check_inlist=False) assert has_admin(user) assert not has_contrib(user2) assert not has_admin(user2) assert role.permission_level == UserPermissions.channel_administrator == plugin_channel.get_channel_permissions_of( user) assert json.loads(plugin_channel.get_users_as_json()) == { str(user.id): UserPermissions.channel_administrator.value } assert json.loads(PluginChannel.get_channels_users_as_json([plugin_channel])) == \ {str(plugin_channel.id): {str(user.id): UserPermissions.channel_administrator.value}} plugin_channel.remove_permission_to_user(user) plugin_channel.give_permission_to_user( user2, UserPermissions.channel_administrator) assert not has_contrib(user) assert not has_admin(user) assert has_contrib(user2, check_inlist=False) assert has_admin(user2) plugin_channel.remove_permission_to_user(user2) assert not has_contrib(user2) assert not has_admin(user2) # Test plugin config parameters assert plugin_channel.get_config_param( 'string_param') == 'default string' assert plugin_channel.get_config_param('int_param') == 1 assert plugin_channel.get_config_param('float_param') == float('-inf') assert plugin_channel.get_config_param('boolean_param') is True assert plugin_channel.get_config_param('template_param') is None with pytest.raises(KeyError): assert plugin_channel.get_config_param( 'this_param_does_not_exists') def assert_value_is_set(param, value): plugin_channel.plugin_config[param] = value plugin_channel.plugin_config = plugin_channel.plugin_config # Force SQLObject update assert plugin_channel.get_config_param(param) == value assert_value_is_set('string_param', 'Hello, world!') assert_value_is_set('int_param', 42) assert_value_is_set('float_param', 42.0) assert_value_is_set('boolean_param', False) assert_value_is_set('template_param', 'fake-template') # Test parameters access rights ppar = PluginParamAccessRights.selectBy(plugin=fake_plugin, name='int_param').getOne() ppar.channel_contributor_read = False ppar.channel_contributor_write = False ppar.channel_administrator_read = True ppar.channel_administrator_write = False ppar.administrator_read = True ppar.administrator_write = True user.super_admin = True assert plugin_channel.has_visible_params_for(user) for param in [ 'string_param', 'int_param', 'float_param', 'boolean_param', 'template_param' ]: assert plugin_channel.get_access_rights_for(param, user) == (True, True) user.super_admin = False assert not plugin_channel.has_visible_params_for(user) plugin_channel.give_permission_to_user( user, UserPermissions.channel_contributor) assert not plugin_channel.has_visible_params_for(user) assert plugin_channel.get_access_rights_for('int_param', user) == (False, False) plugin_channel.give_permission_to_user( user, UserPermissions.channel_administrator) assert plugin_channel.has_visible_params_for(user) assert plugin_channel.get_access_rights_for('int_param', user) == (True, False) user.admin = True assert plugin_channel.has_visible_params_for(user) assert plugin_channel.get_access_rights_for('int_param', user) == (True, True) plugin_channel.remove_permission_to_user(user) user.admin = False assert not plugin_channel.has_visible_params_for(user) assert plugin_channel.get_access_rights_for('int_param', user) == (False, False) # Test miscellaneous parameters assert plugin_channel.cache_activated is True plugin_channel.plugin.cache_activated_default = False assert plugin_channel.cache_activated is False plugin_channel.cache_activated = True assert plugin_channel.cache_activated is True assert plugin_channel.cache_validity is 60 plugin_channel.plugin.cache_validity_default = 120 assert plugin_channel.cache_validity is 120 plugin_channel.cache_validity = 42 assert plugin_channel.cache_validity is 42 assert plugin_channel.keep_noncomplying_capsules is False plugin_channel.plugin.keep_noncomplying_capsules_default = True assert plugin_channel.keep_noncomplying_capsules is True plugin_channel.keep_noncomplying_capsules = False assert plugin_channel.keep_noncomplying_capsules is False # Test flatten() plugin_channel.enabled = False assert plugin_channel.flatten() == [] assert plugin_channel.flatten(keep_disabled_channels=True) == [ plugin_channel ] plugin_channel.enabled = True assert plugin_channel.flatten() == [plugin_channel]
def install(): EditorCapsule.createTable(ifNotExists=True) EditorSlide.createTable(ifNotExists=True) AssetSlideMapping.createTable(ifNotExists=True) Plugin.selectBy(name='editor').getOne().version = 0