def post(self): """ Handles building creation, editing and deletion. """ current_user = User.get(self.session['user']['id']) form = self.form try: if form.action == 'delete': if not current_user.super_admin: raise flask.redirect('',code=403) try: id = int(form.id) except ValueError: logger.warning('user %s tries to delete a building with invalid id (id = %s)', current_user.log_name, str(form.id)) raise ImmediateFeedback(form.action, 'invalid_id') try: screens = Screen.selectBy(building=id) if screens.count() > 0: logger.warning('user %s tries to delete a building with screens still present in this building (id = %s)', current_user.log_name, str(id)) raise ImmediateFeedback(form.action, 'delete_building_but_screens_present', [(s.id, s.name) for s in screens]) Building.delete(id) logger.info("building %s has been deleted by user %s", str(id), current_user.log_name) except SQLObjectNotFound as e: logger.warning("user %s tries to delete a building with an id that doesn't exist (id = %s)", current_user.log_name, str(form.id)) raise ImmediateFeedback(form.action, 'no_id_matching') else: name = form.name.strip() if name == "": raise ImmediateFeedback(form.action, 'empty_name') if len(form.name) > Building.sqlmeta.columns['name'].length: raise ImmediateFeedback(form.action, 'too_long_name') if form.action == 'create': try: Building(name=form.name, city=form.city) logger.info("building %s has been created by user %s", form.name, current_user.log_name) except DuplicateEntryError: logger.warning("user %s tries to create a building with a name that already exists (name = %s)", current_user.log_name, form.name) raise ImmediateFeedback(form.action, 'name_already_exists') elif form.action == 'edit': try: id = int(form.id) except ValueError: logger.warning('user %s tries to edit a building with invalid id (id = %s)', current_user.log_name, str(form.id)) raise ImmediateFeedback(form.action, 'invalid_building_id') try: building = Building.get(id) except SQLObjectNotFound as e: logger.warning("user %s tries to edit a building with an id that doesn't exist (id = %s)", current_user.log_name, str(form.id)) raise ImmediateFeedback(form.action, 'no_id_matching') try: building.name = form.name building.city = form.city except DuplicateEntryError: logger.warning("user %s tries to edit the building %s with a name already present (name = %d)", current_user.log_name, str(form.id), form.name) raise ImmediateFeedback(form.action, 'name_already_exists') logger.info("building %s has been edited by user %s (new name = %s)", str(id), current_user.log_name, form.name) add_feedback(form.action, 'ok') except ImmediateFeedback: store_form(form) return self.render_page()
def runTest(self): results_before = set(Building.select()) # Create a duplicated building and verify that the error is handled properly for the user r = self.testApp.post('/buildings', params=self.post_params, status=200) results_after = set(Building.select()) # Verify that the building table has not changed assert results_after == results_before assert r.body is not None
def runTest(self): """ Tests that we can successfully delete a building via the Building page """ # Test the deletion of the building my_building = Building.selectBy(name=self.building_name).getOne() building_id = my_building.id post_params = {"action": "delete", "id": str(building_id)} # Do the request and verify that it has been deleted and that there is no empty body r = self.testApp.post('/buildings', params=post_params, status=200) assert Building.selectBy(id=building_id).getOne(default=None) is None assert r.body is not None
def runTest(self): """ Tests the Screen object. """ try: b = Building(name="test") s = Screen(name='News Réaumur', building=b) assert_not_equal(None, s) s.set(name="testNew") assert_equal(s.name, "testNew") t = Screen.delete(s.id) Building.delete((b.id)) assert_equal(t, None) except DuplicateEntryError: assert_true(False) return
def tearDown(self): try: Building.delete(self.building_id) Building.delete(self.building2_id) except SQLObjectNotFound: pass for email in [ self.user_nothing_email, self.user_contributor_email, self.user_channel_admin_email, self.user_administrator_email, self.user_super_administrator_email ]: User.deleteBy(email=email) self.channel.destroySelf() Plugin.deleteBy(name="dummy") super().tearDown()
def runTest(self): """ Tests that we can successfully create a building via the Building page """ # Verify that there is no building with this name in db assert Building.selectBy(name=self.new_building_name).getOne( default=None) is None # Do the post request to create the building r = self.testApp.post('/buildings', params=self.post_params, status=200) select_result = Building.selectBy(name=self.new_building_name) # verify that the building has been created (exactly one), with the correct name and that there is no empty body assert select_result.getOne().name == self.new_building_name assert select_result.getOne().city == self.new_building_city assert r.body is not None Building.deleteBy(name=self.new_building_name)
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 screens_config_1(self): # basic test Screen(name='A', building=Building(name='A')) r = self.testApp.get('/screens/1/config') print(r.body, r.headers) assert_equal(r.status, 200) assert_not_equal(r.body, None)
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)
def runTest(self): """ Tests that we can successfully delete a building via the Building page """ new_name = "BuildingNewName" new_city = "BuildingNewCity" post_params = { "action": "edit", "id": str(self.building_id), "name": new_name, "city": new_city } # Do the request r = self.testApp.post('/buildings', params=post_params, status=200) # check that the old name is not there anymore assert Building.selectBy(name=self.building_name).getOne( default=None) is None # check that the building still has the same id but the new name assert Building.get(self.building_id).name == new_name assert Building.get(self.building_id).city == new_city assert r.body is not None
def setUp(self): super().setUp() self.building_id = Building(name=self.building_name).id self.building2_id = Building(name=self.second_building_name).id self.channel = PluginChannel(name=self.channel_name, plugin=Plugin(name="dummy", activated="notfound"), 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) contributor = User(email=self.user_contributor_email, disabled=False) self.channel.give_permission_to_user(contributor) channel_admin = User(email=self.user_channel_admin_email, disabled=False) self.channel.give_permission_to_user( channel_admin, UserPermissions.channel_administrator)
def render_page(self): u = User.get(self.session['user']['id']) screen_status_validity = datetime.now() - timedelta(hours=1) return self.renderer.screens( screens=Screen.get_visible_screens_of(u), buildings=Building.select(), user=u, users=User.select(), channels=Channel.get_screens_channels_from(user=u), subscriptions=u.get_subscriptions_of_owned_screens(), screen_status_validity=screen_status_validity, max_inactivity_period=timedelta(weeks=4))
def runTest(self): """ Tests the Building object. """ try: Building(name="test") except DuplicateEntryError: assert_true(False) b = Building.selectBy(name="test").getOne() assert_not_equal(b, None) assert_equal(b.name, "test") try: b.set(name="newName") except DuplicateEntryError: assert_true(False) assert_equal(b.name, "newName") b.set(name="test") t = Building.delete(b.id) assert_equal(t, None) t = Building.selectBy(name="test").getOne(None) assert_equal(t, None)
def runTest(self): """ Checks that we cannot delete a building with screens attached to it, and checks that the error is correctly handler for the user """ building_to_test = Building.get(self.building2_id) s = Screen(name="mytestscreen", building=building_to_test, secret="secret") def f(): post_params = {"action": "delete", "id": building_to_test.id} # Use a wrong ID and see if nothing changed and the error has been handled properly for the user r = self.testApp.post('/buildings', params=post_params, status=200) assert r.body is not None check_table_did_not_change(f) s.destroySelf()
def runTest(self): """ Checks that a user cannot add a building with a name composed by only spaces and tabs""" for action in ["create", "edit"]: for name in ["", " ", " ", "\t", " \t "]: post_params = { "action": action, "id": self.building_id, "name": name } # Use a wrong name and see if nothing changed and the error has been handled properly for the user r = self.testApp.post('/buildings', params=post_params, status=200) assert r.body is not None # check that the name did not change assert Building.get( self.building_id).name == self.building_name
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 ]
def runTest(self): """ Tests the User object """ try: User(username='******', fullname='test test', email='*****@*****.**', super_admin=True, disabled=False) except DuplicateEntryError: assert_true(False) b = User.selectBy(username="******").getOne() assert_not_equal(b, None) assert_equal(b.username, "test") try: b.set(username="******") except DuplicateEntryError: assert_true(False) assert_equal(b.username, "newName") b.set(username="******") l = b.get_subscriptions_of_owned_screens() assert_true(l.count() >= 0) una = User(username='******', fullname='testnoadmin test', email='*****@*****.**', super_admin=False, disabled=False) l = una.get_subscriptions_of_owned_screens() assert_true(l.count() >= 0) User.delete(una.id) b.set(disabled=True) assert_equal(b.disabled, True) t = b.owns_screen() assert_equal(t, False) b = User.selectBy(username="******").getOne() b.addScreen(Screen(name='A', building=Building(name='A'))) t = b.owns_screen() assert_equal(t, True) b = User.selectBy(username="******").getOne() t = User.delete(b.id) assert_equal(t, None) b = User.selectBy(username="******").getOne(None) assert_true(None == b)
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
def render_page(self): u = User.get(self.session['user']['id']) screen_status_validity = datetime.now() - timedelta(hours=1) def get_data_edit_object(screen): object = screen.to_dictionary(['name', 'comment', 'location']) object['screenid'] = screen.id object['mac'] = screen.get_macs_string( ) if screen.macs is not None else '' object['building-name'] = screen.building.name return json.dumps(object) return self.renderer.screens( screens=Screen.get_visible_screens_of(u), buildings=Building.select(), user=u, highest_permission_level=u.highest_permission_level, users=User.select(), channels=Channel.get_screens_channels_from(user=u), subscriptions=u.get_subscriptions_of_owned_screens(), screen_status_validity=screen_status_validity, max_inactivity_period=timedelta(weeks=4), get_data_edit_object=get_data_edit_object)
def runTest(self): """ Tests the ScreenMac SQLObject """ building = Building(name='building') screen = Screen(name='Screen', building=building, secret='abcdef') mac = ScreenMac(screen=screen, mac='00b16b00b500') assert mac.get_pretty_mac() == '00:b1:6b:00:b5:00'
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 check_table_did_not_change(f): """ verifies that the table has not changes after executing f """ before = set(Building.select()) f() after = set(Building.select()) assert after == before
def runTest(self): """ Tests the screen routing based on encoded MAC addresses. """ sc = Screen(name='A', building=Building(name='A')) sm = ScreenMac(screen=sc, mac='000000000000') self.testApp.get('/screens/redirect/' + sm.mac, status=303)
def screen_view_1(self): sc = Screen(name='A', building=Building(name='A')) r = self.testApp.get('/screens/1/view/' + sc.secret, status=200) assert_not_equal(r.body, None)
def render_page(self): current_user = User.get(self.session['user']['id']) return self.renderer.buildings(buildings=Building.select(), current_user=current_user)
def screens_1(self): # basic test Screen(name='A', building=Building(name='A')) r = self.testApp.get('/screens/1', status=200) assert_not_equal(r.body, None)
def post(self): """ Handles screen creation, editing, deletion, channel subscriptions. """ form = self.form u = User.get(self.session['user']['id']) try: if form.action == 'delete': if not u.super_admin: resp.forbidden() try: screenid = int(form.screenid) except ValueError: raise ImmediateFeedback(form.action, 'invalid_id') try: Screen.delete(screenid) raise ImmediateFeedback(form.action, 'ok') except SQLObjectNotFound as e: raise ImmediateFeedback(form.action, 'no_id_matching') elif form.action == 'subscribe': if form.diff == 'diff': raise ImmediateFeedback(form.action, 'nothing_changed') try: diff = json.loads(form.diff) except (json.JSONDecodeError, ValueError): raise ImmediateFeedback(form.action, 'inconsistent_diff') try: screenid = int(form.screenid) except ValueError: raise ImmediateFeedback(form.action, 'invalid_channel/screen_id') for k, v in diff.items(): try: sub = bool(v) except ValueError: raise ImmediateFeedback(form.action, 'invalid_channel/screen_id') try: channelid = int(k) except ValueError: raise ImmediateFeedback(form.action, 'invalid_channel/screen_id') try: channel = Channel.get(channelid) screen = Screen.get(screenid) if UserPermissions.administrator not in u.highest_permission_level and not ( channel.can_subscribe(u) and u in screen.owners): resp.forbidden() if sub: if hasattr(channel, "plugin" ) and channel.plugin.activated != 'yes': resp.forbidden() screen.subscribe_to(user=u, channel=channel) else: screen.unsubscribe_from(user=u, channel=channel) except SQLObjectNotFound: raise ImmediateFeedback(form.action, 'invalid_channel/screen_id') except DuplicateEntryError: # if the user has checked + unchecked the same checkbox, it will appear on the diff, # we just need to do nothing. pass except SQLObjectIntegrityError: # when there id more than one subscription matching the pair channel/screen # TODO: log something pass elif form.action == 'chown': if form.diff == 'diff': raise ImmediateFeedback(form.action, 'nothing_changed') try: diff = json.loads(form.diff) except (json.JSONDecodeError, ValueError): raise ImmediateFeedback(form.action, 'inconsistent_diff') try: screenid = int(form.screenid) except ValueError: raise ImmediateFeedback(form.action, 'screenid') for k, v in diff.items(): try: own = bool(v) except ValueError: raise ImmediateFeedback(form.action, 'invalid_screen/user_id') try: userid = int(k) except ValueError: raise ImmediateFeedback(form.action, 'invalid_screen/user_id') try: screen = Screen.get(screenid) if UserPermissions.administrator not in u.highest_permission_level and u not in screen.owners: resp.forbidden() user = User.get(userid) if own: screen.safe_add_user(user) else: screen.removeUser(user) except SQLObjectNotFound: raise ImmediateFeedback(form.action, 'invalid_screen/user_id') except DuplicateEntryError: # if the user has checked + unchecked the same checkbox, it will appear on the diff, # we just need to do nothing. pass except SQLObjectIntegrityError: # when there is more than one subscription mathing the pair channel/screen # TODO: log something pass elif form.action == 'configure': screen = Screen.get(int(form.id)) screen.shuffle = form.get('shuffle') == 'on' screen.show_postit = form.get('postit') == 'on' screen.show_slide_number = form.get( 'show_slide_number') == 'on' else: building = Building.get(form.building.strip()) name = form.name.strip() form.building_name = None if not building: raise ImmediateFeedback(form.action, 'empty_building') form.building_name = building.name if not name: raise ImmediateFeedback(form.action, 'empty_name') if len(name) > Screen.sqlmeta.columns['name'].length: raise ImmediateFeedback(form.action, 'too_long_name') try: macs = { self.check_mac_address(mac) for mac in form.mac.strip().lower().split(';') if mac } except ValueError: raise ImmediateFeedback(form.action, 'invalid_mac_address') if form.action == 'create': if UserPermissions.administrator not in u.highest_permission_level: resp.forbidden() try: screen = Screen(name=form.name.strip(), building=form.building, location=form.location.strip(), comment=form.comment.strip(), orientation=form.orientation) except DuplicateEntryError: raise ImmediateFeedback(form.action, 'name_already_exists') elif form.action == 'edit': if UserPermissions.administrator not in u.highest_permission_level: resp.forbidden() try: screenid = int(form.screenid) except ValueError: raise ImmediateFeedback(form.action, 'invalid_id') screen = Screen.get(screenid) if screen is None: raise ImmediateFeedback(form.action, 'no_id_matching') try: screen.name = form.name.strip() screen.building = form.building screen.orientation = form.orientation except DuplicateEntryError: raise ImmediateFeedback(form.action, 'name_already_exists') screen.location = form.location.strip() screen.comment = form.comment.strip() else: raise NotImplementedError try: screen_macs = [ screen_mac.mac for screen_mac in screen.macs ] for mac in screen_macs: if mac not in macs: ScreenMac.selectBy(mac=mac).getOne().destroySelf() for mac in macs: if mac not in screen_macs: ScreenMac(screen=screen, mac=mac) except DuplicateEntryError: raise ImmediateFeedback(form.action, 'mac_already_exists') add_feedback(form.action, 'ok') except ImmediateFeedback: pass store_form(form) return self.render_page()
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
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
def runTest(self): """ Tests the User SQLObject """ user = User(username='******', fullname='fullname', email='email') fake_plugin = Plugin(name='fake_plugin', activated='notfound') plugin_channel = PluginChannel(plugin=fake_plugin, name='Channel', subscription_right='public') building = Building(name='building') screen = Screen(name='Screen', building=building) screen.subscribe_to(user, plugin_channel) # Miscellaneous test assert user.log_name == 'fullname (%d)' % user.id user.fullname = None assert user.log_name == 'email (%d)' % user.id user.fullname = 'fullname' assert user.log_name == 'fullname (%d)' % user.id assert user.readable_name == 'fullname' user.fullname = None assert user.readable_name == 'username' user.username = None assert user.readable_name == 'email' # Test permissions assert user.highest_permission_level == UserPermissions.no_permission assert list(user.get_subscriptions_of_owned_screens()) == [] assert list( user.get_channels_with_permission_level( UserPermissions.channel_contributor)) == [] assert list( user.get_channels_with_permission_level( UserPermissions.channel_administrator)) == [] plugin_channel.give_permission_to_user( user, UserPermissions.channel_contributor) assert user.highest_permission_level == UserPermissions.channel_contributor assert list( user.get_channels_with_permission_level( UserPermissions.channel_contributor)) == [plugin_channel] assert list( user.get_channels_with_permission_level( UserPermissions.channel_administrator)) == [] plugin_channel.give_permission_to_user( user, UserPermissions.channel_administrator) assert user.highest_permission_level == UserPermissions.channel_administrator assert list( user.get_channels_with_permission_level( UserPermissions.channel_contributor)) == [] assert list( user.get_channels_with_permission_level( UserPermissions.channel_administrator)) == [plugin_channel] plugin_channel.remove_permission_to_user(user) assert user.highest_permission_level == UserPermissions.no_permission assert list(user.get_subscriptions_of_owned_screens()) == [] user.admin = True assert user.highest_permission_level == UserPermissions.administrator assert list(user.get_subscriptions_of_owned_screens()) == list( screen.subscriptions) user.admin = False assert user.highest_permission_level == UserPermissions.no_permission assert list(user.get_subscriptions_of_owned_screens()) == [] user.super_admin = True assert user.highest_permission_level == UserPermissions.super_administrator assert list(user.get_subscriptions_of_owned_screens()) == list( screen.subscriptions) user.super_admin = False assert user.highest_permission_level == UserPermissions.no_permission assert list(user.get_subscriptions_of_owned_screens()) == [] screen.safe_add_user(user) assert user.highest_permission_level == UserPermissions.screen_administrator assert list(user.get_subscriptions_of_owned_screens()) == list( screen.subscriptions) screen.removeUser(user) assert list(user.get_subscriptions_of_owned_screens()) == [] assert user.highest_permission_level == UserPermissions.no_permission