Esempio n. 1
0
 def runTest(self):
     """ Submits an empty diff to the page and ensures that nothing has changed """
     bundle = self.bundle_channels[0]
     before = {repr(b) for b in ChannelBundle.select()}
     post_params = {"diff": json.dumps({})}
     assert self.testApp.post("/channels/%d/manage_bundle" % bundle.id,
                              post_params,
                              status=303).body is not None
     after = {repr(b) for b in ChannelBundle.select()}
     assert after == before
Esempio n. 2
0
 def runTest(self):
     """ Tries to add to the bundle a channel that does not exist and
         verifies that it is handled correctly for the user """
     bundle = self.bundle_channels[0]
     before = {dump_bundle(b) for b in ChannelBundle.select()}
     diff = {-1: True}
     post_params = {"diff": json.dumps(diff)}
     assert self.testApp.post("/channels/%d/manage_bundle" % bundle.id,
                              post_params,
                              status=403).body is not None
     after = {dump_bundle(b) for b in ChannelBundle.select()}
     assert after == before
Esempio n. 3
0
 def assert_no_creation(status=200):
     assert PluginChannel.selectBy(
         name=channel_params['name']).getOne(None) is None
     assert self.testApp.post('/channels',
                              params=channel_params,
                              status=status).body is not None
     assert PluginChannel.selectBy(
         name=channel_params['name']).getOne(None) is None
     assert ChannelBundle.selectBy(
         name=bundle_params['name']).getOne(None) is None
     assert self.testApp.post('/channels',
                              params=bundle_params,
                              status=status).body is not None
     assert ChannelBundle.selectBy(
         name=bundle_params['name']).getOne(None) is None
Esempio n. 4
0
 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
Esempio n. 5
0
    def render_page(self, channel):
        current_user = User.get(self.session['user']['id'])

        now = datetime.now()
        last_update = self.plugin_manager.get_last_update(channel.id) or now
        if type(channel) is PluginChannel and last_update + timedelta(
                minutes=channel.cache_validity) < now:
            last_update = now
        try:
            vertical = channel.get_config_param(
                'vertical') if type(channel) is PluginChannel else False
        except KeyError:
            vertical = False
        return self.renderer.channeld(
            channel=channel,
            channel_type=type(channel).__name__,
            current_user=current_user,
            bundles=ChannelBundle.select().filter(
                ChannelBundle.q.id != channel.id),
            can_force_update=UserPermissions.administrator
            in current_user.highest_permission_level
            or (type(channel) is PluginChannel
                and channel.has_contrib(current_user)),
            last_update=last_update,
            vertical=vertical)
Esempio n. 6
0
 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)
Esempio n. 7
0
 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 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]
Esempio n. 9
0
 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)
Esempio n. 10
0
def create_database():
    Building.createTable()
    Channel.createTable()
    Plugin.createTable()
    User.createTable()
    PluginChannel.createTable()
    ChannelBundle.createTable()
    Role.createTable()
    Screen.createTable()
    ScreenMac.createTable()
    Subscription.createTable()
    Template.createTable()
    Asset.createTable()
    PluginParamAccessRights.createTable()
    LogStat.createTable()
    DBVersion.createTable()
    DBVersion(version=database_version)
    User(username="******", fullname="ICTV Admin", email="admin@ictv", super_admin=True, disabled=False)
Esempio n. 11
0
 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))
Esempio n. 12
0
 def runTest(self):
     """ Tries to add a bundle to itself and plugin_channels with disabled plugins to the bundle and checks that
         nothing has been added """
     bundle = self.bundle_channels[0]
     before = {dump_bundle(b) for b in ChannelBundle.select()}
     diff = {bundle.id: True}
     post_params = {"diff": json.dumps(diff)}
     assert self.testApp.post("/channels/%d/manage_bundle" % bundle.id,
                              post_params,
                              status=303).body is not None
     after = {dump_bundle(b) for b in ChannelBundle.select()}
     assert after == before
     channel = self.plugin_channels[0]
     channel.plugin.activated = "no"
     diff = {channel.id: True}
     post_params = {"diff": json.dumps(diff)}
     assert self.testApp.post("/channels/%d/manage_bundle" % bundle.id,
                              post_params,
                              status=303).body is not None
     assert after == before
Esempio n. 13
0
    def runTest(self):
        fake_plugin = Plugin.byName('fake_plugin')
        user = User(fullname='User', email='test@localhost')

        assert fake_plugin.channels_number == 0
        pc1 = PluginChannel(plugin=fake_plugin,
                            name='Plugin Channel 1',
                            subscription_right='public')
        assert fake_plugin.channels_number == 1
        pc2 = PluginChannel(plugin=fake_plugin,
                            name='Plugin Channel 2',
                            subscription_right='public')
        assert fake_plugin.channels_number == 2
        pc2.destroySelf()
        assert fake_plugin.channels_number == 1

        pc2 = PluginChannel(plugin=fake_plugin,
                            name='Plugin Channel 2',
                            subscription_right='public')
        pc3 = PluginChannel(plugin=fake_plugin,
                            name='Plugin Channel 3',
                            subscription_right='public')
        bundle_channel = ChannelBundle(name='Bundle',
                                       subscription_right='public')
        bundle_channel.add_channel(pc3)
        building = Building(name='building')
        screen1 = Screen(name='Screen1', building=building)
        screen2 = Screen(name='Screen2', building=building)
        screen3 = Screen(name='Screen3', building=building)

        assert fake_plugin.screens_number == 0
        screen1.subscribe_to(user, pc1)
        assert fake_plugin.screens_number == 1
        screen1.subscribe_to(user, pc2)
        assert fake_plugin.screens_number == 1
        screen2.subscribe_to(user, pc3)
        assert fake_plugin.screens_number == 2
        screen2.subscribe_to(user, bundle_channel)
        assert fake_plugin.screens_number == 2
        screen3.subscribe_to(user, bundle_channel)
        assert fake_plugin.screens_number == 3
Esempio n. 14
0
 def _get_screens_number(self):
     """ Return the number of screens that are subscribed to channels of this plugin. """
     plugin_channels = PluginChannel.select().filter(
         PluginChannel.q.plugin == self)
     screens = set(plugin_channels.throughTo.subscriptions.throughTo.screen.
                   distinct())
     bundles = set(c for c in ChannelBundle.select()
                   if any(bc.plugin == self for bc in c.flatten()))
     for b in bundles:
         screens |= set(Subscription.select().filter(
             Subscription.q.channel == b).throughTo.screen.distinct())
     return len(screens)
Esempio n. 15
0
 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
Esempio n. 16
0
 def setUp(self):
     super(ChannelsPageTestCase, self).setUp()
     Channel.deleteMany(None)
     self.fake_plugin = Plugin.byName('fake_plugin')
     self.pc1 = PluginChannel(plugin=self.fake_plugin,
                              name='PC 1',
                              subscription_right='public')
     self.pc2 = PluginChannel(plugin=self.fake_plugin,
                              name='PC 2',
                              subscription_right='public')
     self.pc3 = PluginChannel(plugin=self.fake_plugin,
                              name='PC 3',
                              subscription_right='public')
     self.bundle = ChannelBundle(name='Bundle', subscription_right='public')
     self.building = Building(name='Building')
     self.screen = Screen(name='Screen', building=self.building)
     self.user_nothing = User(email='nothing@localhost', disabled=False)
     self.user_contrib = User(email='contrib@localhost', disabled=False)
     self.pc1.give_permission_to_user(self.user_contrib,
                                      UserPermissions.channel_contributor)
     self.user_chan_admin = User(email='chan_admin@localhost',
                                 disabled=False)
     self.user_chan_admin2 = User(email='chan_admin2@localhost',
                                  disabled=False)
     self.pc1.give_permission_to_user(self.user_chan_admin,
                                      UserPermissions.channel_administrator)
     self.pc2.give_permission_to_user(self.user_chan_admin2,
                                      UserPermissions.channel_administrator)
     self.user_screen_admin = User(email='screen_admin@locahost',
                                   disabled=False)
     self.screen.safe_add_user(self.user_screen_admin)
     self.user_admin = User(email='admin@localhost',
                            disabled=False,
                            admin=True)
     self.user_super_admin = User(email='super_admin@localhost',
                                  disabled=False,
                                  admin=True,
                                  super_admin=True)
     self.users = [
         self.user_nothing, self.user_contrib, self.user_chan_admin,
         self.user_chan_admin2, self.user_screen_admin, self.user_admin,
         self.user_super_admin
     ]
Esempio n. 17
0
    def runTest(self):
        """ Tests the Screen SQLObject """
        Channel.deleteMany(None)
        Screen.deleteMany(None)
        channel = Channel(name='Channel',
                          subscription_right='public',
                          secret='abcdef')
        plugin_channel = PluginChannel(name='Channel2',
                                       plugin=Plugin.byName('fake_plugin'),
                                       subscription_right='public')
        plugin_channel2 = PluginChannel(name='Channel3',
                                        plugin=Plugin.byName('fake_plugin'),
                                        subscription_right='public')
        bundle_channel = ChannelBundle(name='Channel4',
                                       subscription_right='public')
        bundle_channel.add_channel(plugin_channel2)
        building = Building(name='building')
        screen = Screen(name='Screen', building=building, secret='abcdef')
        screen2 = Screen(name='Screen2', building=building)
        user = User(fullname='User', email='test@localhost')
        user2 = User(fullname='User2', email='test2@localhost')

        # Miscellaneous test
        assert screen.get_view_link() == '/screens/%d/view/abcdef' % screen.id
        assert screen.get_client_link(
        ) == '/screens/%d/client/abcdef' % screen.id
        assert screen.get_macs_string() == ''
        assert screen not in user.screens
        screen.safe_add_user(user)
        assert screen in user.screens
        assert screen not in user2.screens
        screen.removeUser(user)
        assert screen not in user2.screens

        # Test subscription
        assert not screen.is_subscribed_to(channel)
        screen.subscribe_to(user, channel)
        assert screen.is_subscribed_to(channel)
        sub = screen.subscriptions[0]
        screen.subscribe_to(user2, channel, weight=42)
        assert sub.created_by == user2
        assert sub.weight == 42
        assert screen.is_subscribed_to(channel)
        assert list(screen.subscribed_channels) == [channel]
        screen.unsubscribe_from(user2, channel)
        assert not screen.is_subscribed_to(channel)

        # Test macs
        ScreenMac(screen=screen, mac='00b16b00b500')
        ScreenMac(screen=screen, mac='00b16b00b501')
        assert screen.get_macs_string(
        ) == '00:b1:6b:00:b5:00;00:b1:6b:00:b5:01'

        # Test get_visible_screens_of()
        assert list(Screen.get_visible_screens_of(user)) == list(
            Screen.get_visible_screens_of(user2)) == []
        user.admin = True
        assert list(Screen.get_visible_screens_of(user)) == [screen, screen2]
        assert list(Screen.get_visible_screens_of(user2)) == []
        user.admin = False
        user2.super_admin = True
        assert list(Screen.get_visible_screens_of(user2)) == [screen, screen2]
        assert list(Screen.get_visible_screens_of(user)) == []
        user2.super_admin = False
        screen.safe_add_user(user)
        screen2.safe_add_user(user2)
        assert list(Screen.get_visible_screens_of(user)) == [screen]
        assert list(Screen.get_visible_screens_of(user2)) == [screen2]

        # Test channel content
        screen.subscribe_to(user, plugin_channel)
        screen.subscribe_to(user, bundle_channel)
        screen_content = screen.get_channels_content(self.ictv_app)
        assert len(screen_content) == 2
        assert len(screen_content[0].get_slides()) == 1
        assert screen_content[0].get_slides()[0].get_content() == {
            'background-1': {
                'size': 'contain',
                'src': ''
            },
            'title-1': {
                'text': 'Channel2'
            },
            'text-1': {
                'text': ''
            }
        }
        assert len(screen_content[1].get_slides()) == 1
        assert screen_content[1].get_slides()[0].get_content() == {
            'background-1': {
                'size': 'contain',
                'src': ''
            },
            'title-1': {
                'text': 'Channel3'
            },
            'text-1': {
                'text': ''
            }
        }
        screen.shuffle = True
        assert len(screen.get_channels_content(self.ictv_app)) == 2
Esempio n. 18
0
    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)
Esempio n. 19
0
    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
Esempio n. 20
0
    def POST(self):
        """ Handles channel creation, editing, deletion, configuration and user permissions. """
        form = web.input()
        current_user = User.get(self.session['user']['id'])
        channel = None
        try:
            if form.action.startswith('create') or form.action.startswith(
                    'edit'):
                # Prepare creation or edition of channels/bundles
                name = form.name.strip()
                description = form.description if form.description and form.description.strip(
                ) else None
                enabled = form.get('enabled') == 'on'
                if form.subscription_right not in [
                        'public', 'restricted', 'private'
                ]:
                    raise ImmediateFeedback(form.action,
                                            'invalid_subscription_right')
                if len(name) < 3:
                    raise ImmediateFeedback(form.action, 'invalid_name')

            if form.action.startswith('create'):
                if UserPermissions.administrator not in current_user.highest_permission_level:
                    logger.warning(
                        'user %s tried to create a channel without being admin',
                        current_user.log_name)
                    raise web.forbidden()

                try:
                    if form.action == 'create-channel':
                        try:
                            plugin_id = int(form.plugin)
                        except ValueError:
                            raise ImmediateFeedback(form.action,
                                                    'invalid_plugin')
                        p = Plugin.get(plugin_id)
                        channel = PluginChannel(
                            name=name,
                            plugin=p,
                            subscription_right=form.subscription_right,
                            description=description,
                            enabled=enabled)
                        if p.webapp:
                            self.plugin_manager.add_mapping(self.app, channel)

                    elif form.action == 'create-bundle':
                        channel = ChannelBundle(
                            name=name,
                            description=description,
                            subscription_right=form.subscription_right,
                            enabled=enabled)
                    else:
                        raise web.badrequest()
                except SQLObjectNotFound:
                    raise ImmediateFeedback(form.action, 'invalid_plugin')
                except DuplicateEntryError:
                    raise ImmediateFeedback(form.action, 'name_already_exists')
                logger.info('channel ' + channel.name + ' created by ' +
                            current_user.log_name)
            elif form.action.startswith('edit'):
                if UserPermissions.administrator not in current_user.highest_permission_level:
                    raise web.forbidden()
                try:
                    form.id = int(form.id)
                    channel = (PluginChannel if form.action == 'edit-channel'
                               else Channel).get(form.id)
                except (SQLObjectNotFound, ValueError):
                    raise ImmediateFeedback(form.action, 'invalid_id')

                previous_state = {
                    'name': channel.name,
                    'description': channel.description,
                    'subscription_right': channel.subscription_right,
                    'enabled': channel.enabled
                }
                new_state = {
                    'name': name,
                    'description': description,
                    'subscription_right': form.subscription_right,
                    'enabled': enabled
                }
                state_diff = dict(
                    set(new_state.items()) - set(previous_state.items()))

                if form.action == 'edit-channel':
                    try:
                        plugin_id = int(form.plugin)
                        p = Plugin.get(plugin_id)
                        add_mapping = p.webapp and channel.plugin != p
                        previous_state['plugin'] = channel.plugin
                        new_state['plugin'] = p
                    except (SQLObjectNotFound, ValueError):
                        raise ImmediateFeedback(form.action, 'invalid_plugin')
                elif form.action == 'edit-bundle':
                    pass  # There is nothing more to edit for a bundle than a channel
                else:
                    raise web.badrequest()

                try:
                    channel.set(**new_state)
                except DuplicateEntryError:
                    channel.set(**previous_state)  # Rollback
                    raise ImmediateFeedback(form.action, 'name_already_exists')

                logger.info(
                    '[Channel %s (%d)] ' % (channel.name, channel.id) +
                    current_user.log_name + ' edited the channel.\n'
                    'Previous state: %s\n' % str({
                        k: v
                        for k, v in previous_state.items() if k in state_diff
                    }) + 'New state: %s' % str(state_diff))

                if form.action == 'edit-channel' and add_mapping:
                    self.plugin_manager.add_mapping(self.app, channel)
            elif form.action.startswith('delete'):
                if not current_user.super_admin:
                    logger.warning(
                        'the user %s tried to delete a channel without having the rights to do it',
                        current_user.log_name)
                    raise web.forbidden()
                try:
                    form.id = int(form.id)
                    channel = Channel.get(form.id)
                    if channel.subscriptions.count(
                    ) > 0 and 'confirm-delete' not in form:
                        raise ImmediateFeedback(
                            form.action, 'channel_has_subscriptions', {
                                'channel': {
                                    'name':
                                    channel.name,
                                    'id':
                                    channel.id,
                                    'description':
                                    channel.description,
                                    'subscription_right':
                                    channel.subscription_right
                                },
                                'plugin_id':
                                channel.plugin.id
                                if form.action == 'delete-channel' else None,
                                'subscriptions':
                                [(s.screen.id, s.screen.name,
                                  s.screen.building.name)
                                 for s in channel.subscriptions]
                            })
                    form.name = channel.name
                    channel_name = channel.name
                    channel.destroySelf()
                    logger.info('the channel %s has been deleted by user %s',
                                channel_name, current_user.log_name)
                except (SQLObjectNotFound, ValueError) as e:
                    print(e)
                    raise ImmediateFeedback(form.action, 'invalid_id')
            elif form.action == 'add-users-channel':
                try:
                    if 'users' not in form:
                        raise web.badrequest()
                    form.users = json.loads(form.users)
                    form.id = int(form.id)
                    channel = PluginChannel.get(form.id)
                    if not channel.has_admin(
                            current_user
                    ) and UserPermissions.administrator not in current_user.highest_permission_level:
                        raise web.forbidden()
                    form.name = channel.name
                    for user_id, diff in form.users.items():
                        user_id = int(user_id)
                        user = User.get(user_id)
                        if 'permission' in diff:
                            permission_level = diff['permission']
                            new_permission_level = UserPermissions(
                                permission_level)
                            old_permission_level = channel.get_channel_permissions_of(
                                user)
                            if new_permission_level == UserPermissions.no_permission \
                                    and (UserPermissions.administrator in current_user.highest_permission_level or old_permission_level == UserPermissions.channel_contributor):
                                channel.remove_permission_to_user(user)
                                logger.info(
                                    'permissions of user %s concerning channel %s have been removed by user %s',
                                    user.log_name, channel.name,
                                    current_user.log_name)
                            elif (new_permission_level == UserPermissions.channel_contributor and channel.has_admin(
                                    current_user) and old_permission_level == UserPermissions.no_permission) \
                                    or (new_permission_level in UserPermissions.channel_administrator and UserPermissions.administrator in current_user.highest_permission_level):
                                channel.give_permission_to_user(
                                    user, new_permission_level)
                                logger.info(
                                    'permissions of user %s concerning channel %s have been set to %s by user %s',
                                    user.log_name, channel.name,
                                    UserPermissions.get_permission_string(
                                        new_permission_level),
                                    current_user.log_name)
                        if 'authorized_subscriber' in diff:
                            authorized_subscriber = diff[
                                'authorized_subscriber']
                            if authorized_subscriber and (
                                    user
                                    not in channel.authorized_subscribers):
                                channel.addUser(user)
                                logger.info(
                                    'the user %s has been added to channel %s as authorized subscriber by user %s',
                                    user.log_name, channel.name,
                                    current_user.log_name)
                            elif not authorized_subscriber and (
                                    user in channel.authorized_subscribers):
                                channel.removeUser(user)
                                logger.info(
                                    'the user %s has been removed from channel %s as authorized subscriber by user %s',
                                    user.log_name, channel.name,
                                    current_user.log_name)
                except (SQLObjectNotFound, ValueError):
                    raise ImmediateFeedback(form.action, 'invalid_id')
                except (KeyError, json.JSONDecodeError):
                    raise ImmediateFeedback(form.action, 'invalid_users')
            elif form.action == 'configure':
                try:
                    form.id = int(form.id)
                    channel = PluginChannel.get(form.id)
                    pattern = re.compile(r'list\[.*\]')
                    if UserPermissions.administrator in current_user.highest_permission_level or UserPermissions.channel_administrator in channel.get_channel_permissions_of(
                            current_user) or channel.has_contrib(current_user):
                        for k, v in [(k, v) for k, v in
                                     channel.plugin.channels_params.items()
                                     if channel.get_access_rights_for(
                                         k, current_user)[1]]:
                            # Iterates on the parameters the current user can write to
                            if v['type'] == 'bool':
                                value = k in form and form[k] == 'on'
                            elif v['type'] == 'int':
                                value = int(form[k])
                                if not (v.get('min', float('-inf')) <= value <=
                                        v.get('max', float('inf'))):
                                    continue
                            elif pattern.match(v['type']):
                                inner_type = v['type'][5:-1]
                                if inner_type == 'string':
                                    value = web.input(**{k: ['']})[k]
                                elif pattern.match(inner_type):
                                    inner_type = inner_type[5:-1]
                                    if inner_type == 'string':
                                        delimiter = form[k + '-delimiter']
                                        values = web.input(**{k: ['']})[k]
                                        lists = []
                                        l = []
                                        for v in values:
                                            if v == delimiter:
                                                lists.append(l)
                                                l = []
                                            else:
                                                l.append(v)
                                        value = lists

                            elif k in form:
                                if v['type'] == 'template' and form[k] == '~':
                                    value = None
                                else:
                                    value = form[k]
                            else:
                                continue

                            if channel.get_config_param(k) != value:
                                channel.plugin_config[k] = value
                                logger.info(
                                    'the %s parameter of channel %s has been changed to %s by user %s',
                                    k, channel.name, value,
                                    current_user.log_name)

                        if current_user.super_admin:
                            channel.cache_activated = 'cache-activated' in form and form[
                                'cache-activated'] == 'on'
                            channel.cache_validity = int(
                                form['cache-validity']
                            ) if 'cache-validity' in form and form[
                                'cache-validity'] else channel.cache_validity
                            channel.keep_noncomplying_capsules = 'keep-capsules' in form and form[
                                'keep-capsules'] == 'on'

                        channel.plugin_config = channel.plugin_config  # Force SQLObject update
                        try:
                            self.plugin_manager.invalidate_cache(
                                channel.plugin.name, channel.id)
                            self.plugin_manager.get_plugin(
                                channel.plugin.name).get_content(channel.id)
                        except MisconfiguredParameters as e:
                            for faulty_param in e:
                                add_feedback(form.action, faulty_param[0],
                                             faulty_param)
                            raise web.seeother('/channels/%d' % channel.id)
                        except Exception as e:
                            add_feedback(form.action, 'general_error', str(e))
                            raise web.seeother('/channels/%d' % channel.id)
                    else:
                        raise web.forbidden()
                    form.name = channel.name
                except (SQLObjectNotFound, ValueError):
                    raise ImmediateFeedback(form.action, 'invalid_id')
            if channel:
                form.name = channel.name
            add_feedback(form.action, 'ok')

        except ImmediateFeedback:
            if channel is not None and channel.enabled:
                form.enabled = 'on'
        store_form(form)
        return self.render_page(current_user)
Esempio n. 21
0
 def preview_channel(self):
     c = ChannelBundle(name='A', subscription_right='public')
     r = self.testApp.get('/preview/channels/1/' + c.secret)
     assert_equal(r.status, 200)
Esempio n. 22
0
    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()
Esempio n. 23
0
    def runTest(self):
        """ Tests the ChannelBundle SQLObject """
        fake_plugin = Plugin.byName('fake_plugin')
        fake_plugin.cache_activated_default = False
        pc1 = PluginChannel(name='Channel1',
                            plugin=fake_plugin,
                            subscription_right='public')
        pc2 = PluginChannel(name='Channel2',
                            plugin=fake_plugin,
                            subscription_right='public')
        pc3 = PluginChannel(name='Channel3',
                            plugin=fake_plugin,
                            subscription_right='public')
        channel_bundle = ChannelBundle(name='Channel Bundle',
                                       subscription_right='public')
        channel_bundle2 = ChannelBundle(name='Channel Bundle 2',
                                        subscription_right='public')
        channel_bundle3 = ChannelBundle(name='Channel Bundle 3',
                                        subscription_right='public')

        # Test basic functionality
        channel_bundle.add_channel(pc1)
        channel_bundle.add_channel(pc1)
        channel_bundle.add_channel(pc2)
        channel_bundle.add_channel(pc2)
        channel_bundle.add_channel(pc3)
        channel_bundle.add_channel(pc3)
        assert list(channel_bundle.flatten()) == [pc1, pc2, pc3]

        channel_bundle.remove_channel(pc2)
        assert list(channel_bundle.flatten()) == [pc1, pc3]

        bundle_content = [
            self.ictv_app.plugin_manager.get_plugin_content(pc)
            for pc in channel_bundle.flatten()
        ]
        channels_content = [
            self.ictv_app.plugin_manager.get_plugin_content(pc1),
            self.ictv_app.plugin_manager.get_plugin_content(pc3)
        ]
        for content1, content2 in zip(bundle_content, channels_content):
            capsule1 = content1[0]
            capsule2 = content2[0]
            assert capsule1 == capsule2

        channel_bundle.remove_channel(pc2)
        channel_bundle.remove_channel(pc3)
        channel_bundle.remove_channel(pc1)
        assert list(channel_bundle.flatten()) == []

        assert channel_bundle.get_type_name() == 'Bundle'

        # Test cycle detection
        channel_bundle.add_channel(channel_bundle2)
        channel_bundle2.add_channel(channel_bundle3)
        with pytest.raises(ValueError):
            channel_bundle3.add_channel(channel_bundle)
        with pytest.raises(ValueError):
            channel_bundle.add_channel(channel_bundle)
Esempio n. 24
0
    def runTest(self):
        """ Tests the Channel SQLObject """
        Channel.deleteMany(None)
        fake_plugin = Plugin(name='fake_plugin', activated='notfound')
        channel = Channel(name='Channel',
                          subscription_right='public',
                          secret='abcdef')
        plugin_channel = PluginChannel(name='Channel2',
                                       plugin=fake_plugin,
                                       subscription_right='public')
        bundle_channel = ChannelBundle(name='Channel3',
                                       subscription_right='public')
        building = Building(name='building')
        screen = Screen(name='Screen', building=building)
        user = User(fullname='User', email='test@localhost')
        user2 = User(fullname='User2', email='test2@localhost')

        # Test can_subscribe()
        def test_channel_subscription(c, u):
            def assert_subscription_no_perm():
                assert c.can_subscribe(u)
                c.subscription_right = 'restricted'
                assert not c.can_subscribe(u)
                c.subscription_right = 'private'
                assert not c.can_subscribe(u)
                c.subscription_right = 'public'
                assert c.can_subscribe(u)

            def assert_subscription_admin():
                assert c.can_subscribe(u)
                c.subscription_right = 'restricted'
                assert c.can_subscribe(u)
                c.subscription_right = 'private'
                assert c.can_subscribe(u)
                c.subscription_right = 'public'
                assert c.can_subscribe(u)

            def assert_subscription_super_admin():
                assert c.can_subscribe(u)
                c.subscription_right = 'restricted'
                assert c.can_subscribe(u)
                c.subscription_right = 'private'
                assert c.can_subscribe(u)
                c.subscription_right = 'public'
                assert c.can_subscribe(u)

            assert_subscription_no_perm()
            user.admin = True
            assert_subscription_admin()
            user.admin = False
            user.super_admin = True
            assert_subscription_super_admin()
            user.super_admin = False

        test_channel_subscription(channel, user)
        test_channel_subscription(plugin_channel, user)
        test_channel_subscription(bundle_channel, user)

        # Test get_channels_authorized_subscribers_as_json()
        def test_channel_subscribers(c):
            def assert_no_users():
                assert Channel.get_channels_authorized_subscribers_as_json(
                    [c]) == '{"%d": []}' % c.id

            def assert_only_user(u):
                assert Channel.get_channels_authorized_subscribers_as_json(
                    [c]) == '{"%d": [%d]}' % (c.id, u.id)

            def assert_users(*users):
                for u in json.loads(
                        Channel.get_channels_authorized_subscribers_as_json(
                            [c]))[str(c.id)]:
                    assert u in [u.id for u in users]

            assert_no_users()
            c.safe_add_user(user)
            assert_only_user(user)
            # check that there is no duplicate
            c.safe_add_user(user)
            assert_only_user(user)
            c.removeUser(user)
            c.safe_add_user(user)
            assert_only_user(user)
            c.safe_add_user(user2)
            assert_users(user, user2)
            c.removeUser(user)
            assert_only_user(user2)
            c.removeUser(user2)
            assert_no_users()

        test_channel_subscribers(channel)
        test_channel_subscribers(plugin_channel)
        test_channel_subscribers(bundle_channel)

        # Test get_visible_channels_of()
        def test_visible_channels(u):
            def assert_no_channels():
                assert Channel.get_visible_channels_of(u) == set()

            def assert_channel(c):
                assert Channel.get_visible_channels_of(u) == {c}

            def assert_all_channels():
                assert Channel.get_visible_channels_of(u) == {
                    channel, plugin_channel, bundle_channel
                }

            def assert_public_channels():
                channel.subscription_right = 'restricted'
                assert Channel.get_visible_channels_of(u) == {
                    plugin_channel, bundle_channel
                }
                channel.subscription_right = 'private'
                assert Channel.get_visible_channels_of(u) == {
                    plugin_channel, bundle_channel
                }
                channel.subscription_right = 'public'

            assert_no_channels()
            u.admin = True
            assert_all_channels()
            u.admin = False
            assert_no_channels()
            u.super_admin = True
            assert_all_channels()
            u.super_admin = False
            assert_no_channels()

            screen.addUser(u)
            assert_public_channels()
            screen.removeUser(u)
            assert_no_channels()

            channel.subscription_right = 'restricted'
            assert_no_channels()
            channel.addUser(u)
            assert_channel(channel)
            screen.addUser(u)
            assert_all_channels()
            channel.removeUser(u)
            screen.removeUser(u)
            assert_no_channels()
            channel.subscription_right = 'public'

            plugin_channel.give_permission_to_user(
                u, UserPermissions.channel_contributor)
            assert_channel(plugin_channel)
            plugin_channel.give_permission_to_user(
                u, UserPermissions.channel_administrator)
            assert_channel(plugin_channel)
            plugin_channel.remove_permission_to_user(u)
            assert_no_channels()

        test_visible_channels(user)
        test_visible_channels(user2)

        # Test get_screens_channels_from()
        def test_screens_channels(u):
            def assert_no_channels():
                assert set(Channel.get_screens_channels_from(u)) == set()

            def assert_channel(c):
                assert set(Channel.get_screens_channels_from(u)) == {c}

            def assert_channels(*channels):
                assert set(
                    Channel.get_screens_channels_from(u)) == set(channels)

            def assert_all_channels():
                assert set(Channel.get_screens_channels_from(u)) == {
                    channel, plugin_channel, bundle_channel
                }

            assert_all_channels()
            channel.subscription_right = 'restricted'
            plugin_channel.subscription_right = 'restricted'
            bundle_channel.subscription_right = 'restricted'
            assert_no_channels()
            u.super_admin = True
            assert_all_channels()
            u.super_admin = False
            assert_no_channels()
            channel.addUser(u)
            assert_channel(channel)
            channel.removeUser(u)
            assert_no_channels()
            screen.addUser(user)
            screen.subscribe_to(user, channel)
            assert_channel(channel)
            screen.subscribe_to(user, bundle_channel)
            assert_channels(channel, bundle_channel)

            channel.subscription_right = 'public'
            plugin_channel.subscription_right = 'public'
            bundle_channel.subscription_right = 'public'

        test_screens_channels(user)

        # Test get_preview_link()
        assert channel.get_preview_link(
        ) == '/preview/channels/%d/abcdef' % channel.id