def test_run_add_off(hallo_getter): hallo, test_server, test_channel, test_user = hallo_getter( {"permission_control"}) # Set up a test hallo and server and channel and user hallo1 = Hallo() perm0 = PermissionMask() hallo1.permission_mask = perm0 serv1 = ServerMock(hallo1) serv1.name = "test_serv1" perm1 = PermissionMask() serv1.permission_mask = perm1 hallo1.add_server(serv1) chan1 = serv1.get_channel_by_address("test_chan1".lower(), "test_chan1") perm2 = PermissionMask() chan1.permission_mask = perm2 user1 = serv1.get_user_by_address("test_user1", "test_user1") perm3 = PermissionMask() user1.permission_mask = perm3 # Get permission mask of given channel test_right = "test_right" hallo.function_dispatcher.dispatch( EventMessage( serv1, chan1, user1, "permissions server=test_serv1 channel=test_chan1 " + test_right + " off", )) data = serv1.get_send_data(1, chan1, EventMessage) assert "error" not in data[0].text.lower() assert "set " + test_right + " to false" in data[0].text.lower() assert test_right in perm2.rights_map assert not perm2.rights_map[test_right]
def test_1_user_just_name_privmsg(): perm_cont = Permissions() # Set up a test server and channel and user hallo1 = Hallo() perm3 = PermissionMask() hallo1.permission_mask = perm3 serv1 = ServerMock(hallo1) serv1.name = "test_serv1" perm0 = PermissionMask() serv1.permission_mask = perm0 hallo1.add_server(serv1) chan1 = serv1.get_channel_by_address("test_chan1".lower(), "test_chan1") perm1 = PermissionMask() chan1.permission_mask = perm1 user1 = serv1.get_user_by_address("test_user1".lower(), "test_user1") perm2 = PermissionMask() user1.permission_mask = perm2 chan1.add_user(user1) user2 = serv1.get_user_by_address("test_user2".lower(), "test_user2") perm4 = PermissionMask() user2.permission_mask = perm4 # Get permissions of specified user group try: perm_cont.find_permission_mask(["test_user2"], user1, None) assert False, "Find permission mask should have failed." except hallo.modules.permission_control.PermissionControlException as e: assert "error" in str(e).lower() assert "i can't find that permission mask" in str(e).lower()
def test_run_fail_not_bool(hallo_getter): hallo, test_server, test_channel, test_user = hallo_getter( {"permission_control"}) # Set up a test hallo and server and channel and user hallo1 = Hallo() perm0 = PermissionMask() hallo1.permission_mask = perm0 serv1 = ServerMock(hallo1) serv1.name = "test_serv1" perm1 = PermissionMask() serv1.permission_mask = perm1 hallo1.add_server(serv1) chan1 = serv1.get_channel_by_address("test_chan1".lower(), "test_chan1") perm2 = PermissionMask() chan1.permission_mask = perm2 user1 = serv1.get_user_by_address("test_user1", "test_user1") perm3 = PermissionMask() user1.permission_mask = perm3 # Get permission mask of given channel test_right = "test_right" perm1.set_right(test_right, True) hallo.function_dispatcher.dispatch( EventMessage( serv1, chan1, user1, "permissions server=test_serv1 " + test_right + " yellow", )) data = serv1.get_send_data(1, chan1, EventMessage) assert "error" in data[0].text.lower() assert "don't understand your boolean value" in data[0].text.lower() assert test_right in perm1.rights_map assert perm1.rights_map[test_right]
def test_1_channel_privmsg(): perm_cont = Permissions() # Set up a test server and channel and user hallo1 = Hallo() perm3 = PermissionMask() hallo1.permission_mask = perm3 serv1 = ServerMock(hallo1) serv1.name = "test_serv1" perm0 = PermissionMask() serv1.permission_mask = perm0 hallo1.add_server(serv1) chan1 = serv1.get_channel_by_address("test_chan1".lower(), "test_chan1") perm1 = PermissionMask() chan1.permission_mask = perm1 user1 = serv1.get_user_by_address("test_user1".lower(), "test_user1") perm2 = PermissionMask() user1.permission_mask = perm2 # Try to get permissions of current channel from a privmsg try: perm_cont.find_permission_mask(["channel"], user1, None) assert False, "Should not have managed to get permission mask." except hallo.modules.permission_control.PermissionControlException as e: assert "error" in str(e).lower() assert "can't set generic channel permissions in a privmsg" in str( e).lower()
def test_1_user_just_name(): perm_cont = Permissions() # Set up a test server and channel and user hallo1 = Hallo() perm3 = PermissionMask() hallo1.permission_mask = perm3 serv1 = ServerMock(hallo1) serv1.name = "test_serv1" perm0 = PermissionMask() serv1.permission_mask = perm0 hallo1.add_server(serv1) chan1 = serv1.get_channel_by_address("test_chan1".lower(), "test_chan1") perm1 = PermissionMask() chan1.permission_mask = perm1 user1 = serv1.get_user_by_address("test_user1".lower(), "test_user1") perm2 = PermissionMask() user1.permission_mask = perm2 chan1.add_user(user1) user2 = serv1.get_user_by_address("test_user2".lower(), "test_user2") perm4 = PermissionMask() user2.permission_mask = perm4 chan1.add_user(user2) # Get permissions of specified user in channel data = perm_cont.find_permission_mask(["test_user2"], user1, chan1) assert data == perm4, "Did not find the correct permission mask."
def hallo_getter(): # Create a Hallo hallo = Hallo() hallo_thread = Thread(target=hallo.start,) def function(modules=None): if modules is None: modules = { "ascii_art", "bio", "channel_control", "convert", "euler", "hallo_control", "math", "permission_control", "random", "server_control", "silly", "subscriptions", } function_dispatcher = FunctionDispatcher(modules, hallo) hallo.function_dispatcher = function_dispatcher # Create server server = ServerMock(hallo) server.name = "mock-server" hallo.add_server(server) # Start hallo thread hallo_thread.start() # Create test users and channel, and configure them hallo_user = server.get_user_by_address( server.get_nick().lower(), server.get_nick() ) test_user = server.get_user_by_address("test", "test") test_user.online = True test_chan = server.get_channel_by_address("#test", "#test") test_chan.in_channel = True test_chan.add_user(hallo_user) test_chan.add_user(test_user) # Wait until hallo is open count = 0 while not hallo.open: time.sleep(0.01) count += 1 assert count < 1000, "Hallo took too long to start." if count > 1000: break # Clear any data in the server server.get_send_data() return hallo, server, test_chan, test_user yield function hallo.close() hallo_thread.join()
def setUp(self): print("Starting test: " + self.id()) self.start_time = time.time() # Create a Hallo self.hallo = Hallo() # Only the required modules, only 1 (mock) server # Todo: specify modules by test? self.function_dispatcher = FunctionDispatcher( {"convert", "random", "server_control", "subscriptions"}, self.hallo ) self.hallo.function_dispatcher = self.function_dispatcher print( "Running test: " + self.id() + ". Init took: " + str(time.time() - self.start_time) + " seconds." ) self.server = ServerMock(self.hallo) self.server.name = "mock-server" self.hallo.add_server(self.server) # Start hallo thread self.hallo_thread = Thread(target=self.hallo.start,) self.hallo_thread.start() # Create test users and channel, and configure them self.hallo_user = self.server.get_user_by_address( self.server.get_nick().lower(), self.server.get_nick() ) self.test_user = self.server.get_user_by_address("test", "test") self.test_user.online = True self.test_chan = self.server.get_channel_by_address("#test", "#test") self.test_chan.in_channel = True self.test_chan.add_user(self.hallo_user) self.test_chan.add_user(self.test_user) # Wait until hallo is open count = 0 while not self.hallo.open: time.sleep(0.01) count += 1 assert count < 1000, "Hallo took too long to start." if count > 1000: break # Clear any data in the server self.server.get_send_data() # Print test startup time print( "Running test: " + self.id() + ". Startup took: " + str(time.time() - self.start_time) + " seconds." ) self.start_time = time.time()
def test_1_user_group_name(): perm_cont = Permissions() # Set up a test server and channel and user hallo1 = Hallo() perm3 = PermissionMask() hallo1.permission_mask = perm3 serv1 = ServerMock(hallo1) serv1.name = "test_serv1" perm0 = PermissionMask() serv1.permission_mask = perm0 hallo1.add_server(serv1) chan1 = serv1.get_channel_by_address("test_chan1".lower(), "test_chan1") perm1 = PermissionMask() chan1.permission_mask = perm1 user1 = serv1.get_user_by_address("test_user1".lower(), "test_user1") perm2 = PermissionMask() user1.permission_mask = perm2 group1 = UserGroup("test_group1", hallo1) perm4 = PermissionMask() group1.permission_mask = perm4 hallo1.add_user_group(group1) # Get permissions of specified user group data = perm_cont.find_permission_mask(["user_group=test_group1"], user1, chan1) assert data == perm4, "Did not find the correct permission mask."
def test_1_user_group_no_name(): perm_cont = Permissions() # Set up a test server and channel and user hallo1 = Hallo() perm3 = PermissionMask() hallo1.permission_mask = perm3 serv1 = ServerMock(hallo1) serv1.name = "test_serv1" perm0 = PermissionMask() serv1.permission_mask = perm0 hallo1.add_server(serv1) chan1 = serv1.get_channel_by_address("test_chan1".lower(), "test_chan1") perm1 = PermissionMask() chan1.permission_mask = perm1 user1 = serv1.get_user_by_address("test_user1".lower(), "test_user1") perm2 = PermissionMask() user1.permission_mask = perm2 group1 = UserGroup("test_group1", hallo1) perm4 = PermissionMask() group1.permission_mask = perm4 hallo1.add_user_group(group1) # Try to get permissions of non-existent user group try: perm_cont.find_permission_mask(["user_group=test_group2"], user1, chan1) assert False, "Find permission mask should have failed." except hallo.modules.permission_control.PermissionControlException as e: assert "error" in str(e).lower() assert "no user group exists by that name" in str(e).lower()
def test_help_mock_func_disp(): # Set up mock objects mock_hallo = Hallo() mock_func_disp = FunctionDispatcher(set(), mock_hallo) mock_hallo.function_dispatcher = mock_func_disp mock_func_disp.load_function(None, FunctionMock) mock_func_disp.load_function(None, FunctionMockNoDoc) mock_func_disp.load_function(None, Help) mock_server = ServerMock(mock_hallo) mock_server.name = "test_serv1" mock_user = mock_server.get_user_by_address("test_user1".lower(), "test_user1") # Test things mock_func_disp.dispatch(EventMessage(mock_server, None, mock_user, "help")) data = mock_server.get_send_data(1, mock_user, EventMessage) assert "error" not in data[0].text.lower() assert "list of available functions:" in data[0].text.lower() assert "function mock" in data[0].text.lower() assert "function no doc" in data[0].text.lower()
def test_1_channel(): perm_cont = Permissions() # Set up a test server and channel and user hallo1 = Hallo() perm3 = PermissionMask() hallo1.permission_mask = perm3 serv1 = ServerMock(hallo1) serv1.name = "test_serv1" perm0 = PermissionMask() serv1.permission_mask = perm0 hallo1.add_server(serv1) chan1 = serv1.get_channel_by_address("test_chan1".lower(), "test_chan1") perm1 = PermissionMask() chan1.permission_mask = perm1 user1 = serv1.get_user_by_address("test_user1".lower(), "test_user1") perm2 = PermissionMask() user1.permission_mask = perm2 # Get permissions of current channel data = perm_cont.find_permission_mask(["channel"], user1, chan1) assert data == perm1, "Did not find the correct permission mask."
def test_init(): # Create some basic stuff test_modules = {"euler"} test_hallo = Hallo() # Create function dispatcher fd = FunctionDispatcher(test_modules, test_hallo) test_hallo.function_dispatcher = fd try: # Check basic class variable setting assert (fd.hallo == test_hallo ), "Hallo object was not set correctly in FunctionDispatcher." assert ( fd.module_list == test_modules ), "Module list was not imported correctly by FunctionDispatcher." # Check that module reloading has done things assert len(fd.function_dict) == len( test_modules), "Modules were not loaded to function dictionary." assert len(fd.function_names ) != 0, "Functions were not added to function_names" finally: fd.close() test_hallo.close()
def test_1_server_name(hallo_getter): hallo_obj, test_server, test_channel, test_user = hallo_getter( {"permission_control"}) perm_cont = Permissions() # Set up a test server and channel and user hallo1 = Hallo() perm3 = PermissionMask() hallo1.permission_mask = perm3 serv1 = ServerMock(hallo1) serv1.name = "test_serv1" perm0 = PermissionMask() serv1.permission_mask = perm0 hallo1.add_server(serv1) chan1 = serv1.get_channel_by_address("test_chan1".lower(), "test_chan1") perm1 = PermissionMask() chan1.permission_mask = perm1 user1 = serv1.get_user_by_address("test_user1".lower(), "test_user1") perm2 = PermissionMask() user1.permission_mask = perm2 # Get permissions of current server data = perm_cont.find_permission_mask(["server=test_serv1"], user1, chan1) assert data == perm0, "Did not find correct permission mask"
def test_1_server_no_name(): perm_cont = Permissions() # Set up a test server and channel and user hallo1 = Hallo() perm3 = PermissionMask() hallo1.permission_mask = perm3 serv1 = ServerMock(hallo1) serv1.name = "test_serv1" perm0 = PermissionMask() serv1.permission_mask = perm0 hallo1.add_server(serv1) chan1 = serv1.get_channel_by_address("test_chan1".lower(), "test_chan1") perm1 = PermissionMask() chan1.permission_mask = perm1 user1 = serv1.get_user_by_address("test_user1".lower(), "test_user1") perm2 = PermissionMask() user1.permission_mask = perm2 # Get permissions of current server try: perm_cont.find_permission_mask(["server=test_serv2"], user1, chan1) assert False, "Find permission mask should have failed." except hallo.modules.permission_control.PermissionControlException as e: assert "error" in str(e).lower() assert "no server exists by that name" in str(e).lower()
def from_json(cls, json_data: Dict, hallo_obj: Hallo, sub_repo) -> 'Subscription': server = hallo_obj.get_server_by_name(json_data["server_name"]) if server is None: raise hallo.modules.subscriptions.subscription_exception.SubscriptionException( 'Could not find server with name "{}"'.format( json_data["server_name"])) # Load channel or user if "channel_address" in json_data: destination = server.get_channel_by_address( json_data["channel_address"]) else: if "user_address" in json_data: destination = server.get_user_by_address( json_data["user_address"]) else: raise hallo.modules.subscriptions.subscription_exception.SubscriptionException( "Channel or user must be defined.") if destination is None: raise hallo.modules.subscriptions.subscription_exception.SubscriptionException( "Could not find channel or user.") # Load update frequency period = isodate.parse_duration(json_data["period"]) # Load last check last_check = None if "last_check" in json_data: last_check = dateutil.parser.parse(json_data["last_check"]) # Load last update last_update = None if "last_update" in json_data: last_update = dateutil.parser.parse(json_data["last_update"]) # Load source source = hallo.modules.subscriptions.subscription_factory.SubscriptionFactory.source_from_json( json_data["source"], destination, sub_repo) subscription = Subscription(server, destination, source, period, last_check, last_update) return subscription