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_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(): 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 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_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_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_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_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_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()
class TestBase(unittest.TestCase): 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 tearDown(self): print( "Finishing test: " + self.id() + ". Test took: " + str(time.time() - self.start_time) + " seconds." ) self.hallo.close() self.hallo_thread.join() print( "Finished test: " + self.id() + ". Test took: " + str(time.time() - self.start_time) + " seconds." ) @classmethod def tearDownClass(cls): del cls gc.collect()