def get(self, bundle_id): try: bundle = ChannelBundle.get(bundle_id) u = User.get(self.session['user']['id']) except SQLObjectNotFound: resp.notfound() return self.render_page(bundle, u)
def post(self, channel_id): channel = Channel.get(int(channel_id)) form = self.form try: if form.action == 'add-channel-to-bundles': bundles_diff = json.loads(form.pop('diff', '{}')) for bundle_id, part_of in bundles_diff.items(): bundle = ChannelBundle.get(int(bundle_id)) if part_of: try: bundle.add_channel(channel) except ValueError: raise ImmediateFeedback(form.action, 'bundle_cycle', bundle.name) else: bundle.remove_channel(channel) add_feedback(form.action, 'ok') except ImmediateFeedback: pass form.was_bundle = type( channel ) == ChannelBundle # Hack to display the bundles tab instead form.data_edit = json.dumps([b.id for b in channel.bundles]) form.channel_id = channel_id form.name = channel.name store_form(form) resp.seeother('/channels')
def assert_edition(attrs=None, channel_params=channel_params, bundle_params=bundle_params, status=200): if attrs is None: attrs = [ 'name', 'description', 'enabled', 'subscription_right', 'plugin' ] assert self.testApp.post('/channels', params=channel_params, status=status).body is not None pc = PluginChannel.get(self.pc1.id) for attr in attrs: if attr in channel_params: assert get_attr(pc, attr) == channel_params[attr] assert self.testApp.post('/channels', params=bundle_params, status=status).body is not None bc = ChannelBundle.get(self.bundle.id) for attr in attrs: if attr in bundle_params: assert get_attr(bc, attr) == bundle_params[attr] if channel_params is not orig_plugin_channel_params and bundle_params is not orig_bundle_params: assert_edition(channel_params=orig_plugin_channel_params, bundle_params=orig_bundle_params) # Revert
def assert_no_edition(status=200): assert self.testApp.post('/channels', params=channel_params, status=status).body is not None assert orig_plugin_channel == repr(PluginChannel.get(self.pc1.id)) assert self.testApp.post('/channels', params=bundle_params, status=status).body is not None assert orig_bundle_channel == repr( ChannelBundle.get(self.bundle.id))
def post(self, bundle_id): def wrong_channel(channel, bundle, add): """ returns True if the channel to add is the bundle itself or if the channel is a PluginChannel with disabled plugin """ return bundle.id == channel.id or add and type(channel) is PluginChannel and channel.plugin.activated != "yes" form = self.form try: bundle = ChannelBundle.get(bundle_id) u = User.get(self.session['user']['id']) diff = json.loads(form.diff) if diff == {}: logger.info('user %s submitted empty diff for bundle management %s', u.log_name, bundle.name) raise ImmediateFeedback("manage_channels", 'nothing_changed') # Do the subscription/unsubscription for every channel in the diff contained = [] not_contained = [] try: changes = [(Channel.get(channel_id), add) for channel_id, add in diff.items()] except SQLObjectNotFound: logger.warning('user %s tried to add a channel which does not exist to bundle %d', u.log_name, bundle.id) resp.forbidden() # if somebody tries to add a channel with a disabled plugin or to add a bundle to itself wrong_channels = [(channel, add) for channel, add in changes if wrong_channel(channel, bundle, add)] if wrong_channels: channel, add = wrong_channels[0] if channel.id == bundle.id: logger.warning('user %s tried to %s bundle %d to itself', u.log_name, 'add' if add else "remove", bundle.id) raise ImmediateFeedback("manage_channels", "added_to_itself") else: logger.warning('user %s tried to %s channel %d with disabled plugin to bundle %d', u.log_name, 'add' if add else "remove", channel.id, bundle.id) raise ImmediateFeedback("manage_channels", "disabled_plugin") for channel, add in changes: if add: try: bundle.add_channel(channel) except ValueError: logger.warning("user %s has made changes in channels management of bundle %d that created a " "cycle of bundles by adding channel %d (%s)", u.log_name, bundle.id, channel.id, channel.name) form.channel_name = channel.name raise ImmediateFeedback("manage_channels", "bundle_cycle") contained.append(str(channel.id)) else: bundle.remove_channel(channel) not_contained.append(str(channel.id)) if contained and not_contained: message = "user %s has added to channel(s) %s and removed channel(s) %s to bundle %d" % \ (u.log_name, ', '.join(contained), ', '.join(not_contained), bundle.id) else: message = "user %s has %s channel(s) %s to bundle %d" % \ (u.log_name, "added" if contained else "removed", ', '.join(contained if contained else not_contained), bundle.id) logger.info(message) add_feedback("manage_channels", 'ok') except SQLObjectNotFound: resp.notfound() except ImmediateFeedback: pass store_form(form) resp.seeother("/channels/config/%s/manage_bundle" % bundle.id)
def runTest(self): """ Tests the channels deletion through the Channels page """ pc1_id = self.pc1.id bundle_id = self.bundle.id channel_params = {'action': 'delete-channel', 'id': pc1_id} bundle_params = {'action': 'delete-bundle', 'id': bundle_id} def assert_deletion(channel_params=channel_params, bundle_params=bundle_params, status=200): assert self.testApp.post('/channels', params=channel_params, status=status).body is not None assert PluginChannel.selectBy(id=pc1_id).getOne(None) is None assert self.testApp.post('/channels', params=bundle_params, status=status).body is not None assert ChannelBundle.selectBy(id=bundle_id).getOne(None) is None return (PluginChannel(plugin=self.fake_plugin, name='PC 1', subscription_right='public').id, ChannelBundle(name='Bundle', subscription_right='public').id) def assert_no_deletion(channel_params=channel_params, bundle_params=bundle_params, status=200): assert self.testApp.post('/channels', params=channel_params, status=status).body is not None assert PluginChannel.selectBy(id=pc1_id).getOne(None) is not None assert self.testApp.post('/channels', params=bundle_params, status=status).body is not None assert ChannelBundle.selectBy( id=bundle_id).getOne(None) is not None # Test basic functionality pc1_id, bundle_id = assert_deletion() channel_params['id'] = pc1_id bundle_params['id'] = bundle_id # Test insufficient permissions for channel edition for u in [ self.user_nothing, self.user_contrib, self.user_chan_admin, self.user_screen_admin, self.user_admin ]: self.ictv_app.test_user = {'email': u.email} assert_no_deletion(status=403) # Test sufficient permissions for channel edition for u in [self.user_super_admin]: self.ictv_app.test_user = {'email': u.email} pc1_id, bundle_id = assert_deletion() channel_params['id'] = pc1_id bundle_params['id'] = bundle_id # Test invalid id channel_params['id'] = bundle_params['id'] = -1 assert_no_deletion() channel_params['id'] = bundle_params['id'] = 'invalid' assert_no_deletion() channel_params['id'] = pc1_id bundle_params['id'] = bundle_id # Test subscriptions pc1_id, bundle_id = assert_deletion() channel_params['id'] = pc1_id bundle_params['id'] = bundle_id self.pc1 = PluginChannel.get(pc1_id) self.bundle = ChannelBundle.get(bundle_id) self.screen.subscribe_to(self.user_super_admin, self.pc1) assert self.testApp.post('/channels', params=channel_params, status=200).body is not None assert PluginChannel.selectBy(id=pc1_id).getOne(None) is not None self.screen.subscribe_to(self.user_super_admin, self.bundle) assert self.testApp.post('/channels', params=bundle_params, status=200).body is not None assert ChannelBundle.selectBy(id=bundle_id).getOne(None) is not None self.screen.unsubscribe_from(self.user_super_admin, self.pc1) assert self.testApp.post('/channels', params=channel_params, status=200).body is not None assert PluginChannel.selectBy(id=pc1_id).getOne(None) is None self.screen.unsubscribe_from(self.user_super_admin, self.bundle) assert self.testApp.post('/channels', params=bundle_params, status=200).body is not None assert ChannelBundle.selectBy(id=bundle_id).getOne(None) is None
def runTest(self): """ Tests the channels edition through the Channels page """ channel_params = { 'action': 'edit-channel', 'name': 'Plugin Channel test edition', 'description': 'Descr.', 'subscription_right': 'public', 'plugin': self.fake_plugin.id, 'id': self.pc1.id } bundle_params = dict(**channel_params) bundle_params.update({ 'action': 'edit-bundle', 'name': 'Channel Bundle test edition', 'id': self.bundle.id }) bundle_params.pop('plugin') def get_attr(o, a): if a == 'plugin': return o.plugin.id if a == 'enabled': return 'on' if o.enabled else '' return getattr(o, a) def assert_edition(attrs=None, channel_params=channel_params, bundle_params=bundle_params, status=200): if attrs is None: attrs = [ 'name', 'description', 'enabled', 'subscription_right', 'plugin' ] assert self.testApp.post('/channels', params=channel_params, status=status).body is not None pc = PluginChannel.get(self.pc1.id) for attr in attrs: if attr in channel_params: assert get_attr(pc, attr) == channel_params[attr] assert self.testApp.post('/channels', params=bundle_params, status=status).body is not None bc = ChannelBundle.get(self.bundle.id) for attr in attrs: if attr in bundle_params: assert get_attr(bc, attr) == bundle_params[attr] if channel_params is not orig_plugin_channel_params and bundle_params is not orig_bundle_params: assert_edition(channel_params=orig_plugin_channel_params, bundle_params=orig_bundle_params) # Revert def assert_no_edition(status=200): assert self.testApp.post('/channels', params=channel_params, status=status).body is not None assert orig_plugin_channel == repr(PluginChannel.get(self.pc1.id)) assert self.testApp.post('/channels', params=bundle_params, status=status).body is not None assert orig_bundle_channel == repr( ChannelBundle.get(self.bundle.id)) orig_plugin_channel_params = { 'action': 'edit-channel', 'name': 'Plugin Channel', 'description': 'Original descr.', 'subscription_right': 'public', 'plugin': self.fake_plugin.id, 'id': self.pc1.id } orig_bundle_params = dict(**orig_plugin_channel_params) orig_bundle_params.update({ 'action': 'edit-bundle', 'name': 'Channel Bundle test edition', 'id': self.bundle.id }) orig_bundle_params.pop('plugin') assert_edition(channel_params=orig_plugin_channel_params, bundle_params=orig_bundle_params) orig_plugin_channel = repr(PluginChannel.get(self.pc1.id)) orig_bundle_channel = repr(ChannelBundle.get(self.bundle.id)) # Test basic functionality assert_edition() # Test insufficient permissions for channel edition for u in [ self.user_nothing, self.user_contrib, self.user_chan_admin, self.user_screen_admin ]: self.ictv_app.test_user = {'email': u.email} assert_no_edition(403) # Test sufficient permissions for channel edition for u in [self.user_admin, self.user_super_admin]: self.ictv_app.test_user = {'email': u.email} assert_edition() # Test invalid id channel_params['id'] = bundle_params['id'] = -1 assert_no_edition() channel_params['id'] = bundle_params['id'] = 'invalid' assert_no_edition() channel_params['id'] = self.pc1.id bundle_params['id'] = self.bundle.id # Test invalid plugin channel_params['plugin'] = -1 assert self.testApp.post('/channels', params=channel_params, status=200).body is not None assert orig_plugin_channel == repr(PluginChannel.get(self.pc1.id)) channel_params['plugin'] = 'invalid' assert self.testApp.post('/channels', params=channel_params, status=200).body is not None assert orig_plugin_channel == repr(PluginChannel.get(self.pc1.id)) channel_params['plugin'] = self.fake_plugin.id # Test invalid action channel_params['action'] = bundle_params['action'] = 'edit-invalid' assert_no_edition(400) channel_params['action'] = 'edit-channel' bundle_params['action'] = 'edit-bundle' # Test duplicate name Channel(name='already taken', subscription_right='public') channel_params['name'] = bundle_params['name'] = 'already taken' assert_no_edition()