def test_create(self): """ Test the CreateGroup API function. """ data = APIClient.create_group(self.client, 'SOME GROUP') self.assertEqual(data['error'], False) self.assertIsNotNone(Database.get_group('SOME GROUP'))
def test_target_set_facts(self): """ This test will pass if the facts are correctly set. """ initial_facts = { 'some fact': 54, 'some other fact': 'Pi', 'A list fact': ['sdasd', 'asdasd'] } fact_update = { 'new fact': 'Wow. I am new!', 'A list fact': ['asdasd', 'sdasd'], 'some fact': 55 } final_facts = { 'new fact': 'Wow. I am new!', 'some other fact': 'Pi', 'A list fact': ['asdasd', 'sdasd'], 'some fact': 55 } target = Database.create_target('FACT TEST', ['AA:BB:CC:DD:EE:FF'], initial_facts) data = APIClient.set_target_facts(self.client, 'FACT TEST', fact_update) self.assertEqual(data['error'], False) target = Database.get_target('FACT TEST') self.assertIsNotNone(target) self.assertDictEqual(final_facts, target.facts)
def test_target_rename_association(self): """ Tests the RenameTarget API function, check to make sure Sessions, Targets, and Groups. """ target = Database.create_target() target_name = target.name session_id = Database.create_session(target_name).session_id orig_group = Database.create_group('some_group') orig_group.whitelist_member(target.name) action_id = Database.create_action(target_name).action_id data = APIClient.rename_target(self.client, target_name, 'TEST') self.assertEqual(data['error'], False) target = Database.get_target('TEST') self.assertIsNotNone(target) with self.assertRaises(DoesNotExist): Database.get_target(target_name) self.assertEqual(target.name, 'TEST') session = Database.get_session(session_id) self.assertEqual(session.target_name, 'TEST') action = Database.get_action(action_id) self.assertEqual(action.target_name, 'TEST') group = Database.get_group(orig_group.name) self.assertIn(target.name, group.member_names)
def test_target_rename(self): """ Tests the RenameTarget API function. """ target = Database.create_target('NOTTHIS') data = APIClient.rename_target(self.client, target.name, 'TEST') self.assertEqual(data['error'], False) target = Database.get_target('TEST') self.assertIsNotNone(target) self.assertEqual(target.name, 'TEST') with self.assertRaises(DoesNotExist): Database.get_target('NOTTHIS') target2 = Database.create_target() data = APIClient.rename_target(self.client, target2.name, 'TEST') self.assertEqual(data['error'], True) self.assertEqual(data['error_type'], 'cannot-rename-target') self.assertIsNotNone(Database.get_target(target2.name))
def test_delete(self): """ Test the DeleteGroup function. """ group = Database.create_group() data = APIClient.delete_group(self.client, group.name) self.assertEqual(data['error'], False) with self.assertRaises(DoesNotExist): Database.get_group(group.name)
def test_get(self): """ This test will pass if it finds the correct group action. """ group_action = Database.create_group_action() data = APIClient.get_group_action(self.client, group_action.group_action_id) self.assertEqual(data['error'], False) self.assertListEqual(sorted(data['group_action']['action_ids']), sorted(group_action.action_ids))
def test_create(self): """ Test the CreateLog API function. """ app = 'TEST APP {}'.format(uuid4()) data = APIClient.create_log(self.client, 'TEST APP', 'FATAL', 'Testing CreateLog API method') self.assertEqual(data['error'], False) for entry in Database.list_logs(app): self.assertEqual(entry.message, 'Testing CreateLog API method')
def test_get(self): """ This test will pass if it finds the correct target. """ target = Database.create_target('GET TEST') data = APIClient.get_target(self.client, 'GET TEST') self.assertEqual(data['error'], False) self.assertIsInstance(data['target'], dict) self.assertEqual(data['target']['name'], 'GET TEST') self.assertIsInstance(data['target']['mac_addrs'], list) self.assertListEqual(data['target']['mac_addrs'], target.mac_addrs)
def test_list(self): """ Test the ListLogs API function. """ Database.create_log(0, 'NOSEE', 'FATAL', 'NOSEE') Database.create_log(time.time(), 'WESEE', 'FATAL', 'WESEE') archived_log = Database.create_log(time.time(), 'ARCHIVED', 'FATAL', 'ARCHIVED') archived_log.archived = True archived_log.save() for entry in APIClient.list_logs(self.client, False, 10)['logs']: self.assertNotEqual(entry['application'], 'NOSEE') self.assertNotEqual(entry['application'], 'ARCHIVED') logs = [ entry['application'] for entry in APIClient.list_logs(self.client, True)['logs'] ] self.assertIn('NOSEE', logs) self.assertIn('ARCHIVED', logs)
def test_add_member(self): """ Test the GetGroup API function. """ Database.create_group('TEST GROUP') target = Database.create_target() data = APIClient.add_group_member(self.client, 'TEST GROUP', target.name) self.assertEqual(data['error'], False) group = Database.get_group('TEST GROUP') self.assertListEqual(group.whitelist_members, [target.name])
def test_get(self): """ This test will pass if it finds the correct action. """ target = Database.create_target() action_id = Database.create_action(target.name, 'exec ls')['action_id'] data = APIClient.get_action(self.client, action_id) action = Database.get_action(action_id) self.assertEqual(data['error'], False) self.assertIsNotNone(data['action']) self.assertEqual(action_id, action.action_id) self.assertEqual(target.name, action.target_name) self.assertEqual('ls', action.command)
def test_cancel(self): """ This test will pass if an action is successfully cancelled. """ target = Database.create_target() action_id = Database.create_action( target.name, 'exec echo hello world')['action_id'] action = Database.get_action(action_id) self.assertEqual(action.cancelled, False) data = APIClient.cancel_action(self.client, action_id) self.assertEqual(data['error'], False) action = Database.get_action(action_id) self.assertEqual(action.status, ACTION_STATUSES.get('cancelled')) self.assertEqual(action.cancelled, True)
def test_list(self): """ This test will create a few action objects through the API, and then test listing them. """ group_actions = [ Database.create_group_action().group_action_id, #Database.create_group_action().group_action_id, #Database.create_group_action().group_action_id, #Database.create_group_action().group_action_id, #Database.create_group_action().group_action_id, ] data = APIClient.list_group_actions(self.client) self.assertEqual(data['error'], False) self.assertListEqual(sorted(list(data['group_actions'].keys())), sorted(group_actions))
def test_create(self): """ This test will pass if the action is created and content matches. """ target = Database.create_target() data = APIClient.create_action(self.client, target.name, 'exec ls -al /dir') action_id = data['action_id'] self.assertEqual(False, data['error']) action = Database.get_action(action_id) self.assertIsNotNone(action) self.assertEqual(action.action_type, ACTION_TYPES.get('exec', 1)) self.assertEqual(action.command, 'ls') self.assertListEqual(action.args, ['-al', '/dir']) self.assertEqual(action.cancelled, False)
def test_list(self): """ Populates the database with sample groups, and calls the list API function to ensure that all are returned. """ groups = [ Database.create_group(), Database.create_group(), Database.create_group(), Database.create_group(), Database.create_group(), ] data = APIClient.list_groups(self.client) self.assertEqual(data['error'], False) self.assertEqual(sorted(list(data['groups'].keys())), sorted([group.name for group in groups]))
def test_cancel(self): """ This test will pass if an action is successfully cancelled. """ group_action = Database.create_group_action() self.assertEqual(group_action.cancelled, False) data = APIClient.cancel_group_action(self.client, group_action.group_action_id) self.assertEqual(data['error'], False) group_action = Database.get_group_action(group_action.group_action_id) self.assertEqual(group_action.cancelled, True) self.assertEqual(group_action.get_status(), GROUP_ACTION_STATUSES.get('cancelled', 'cancelled')) for action in group_action.actions: self.assertEqual(action.cancelled, True) self.assertEqual(action.status, ACTION_STATUSES.get('cancelled', 'cancelled'))
def test_target_list(self): """ Populates the database with sample targets, and calls the list API function to ensure that all are returned. """ targets = [ Database.create_target(), Database.create_target(), Database.create_target(), Database.create_target(), ] data = APIClient.list_targets(self.client) self.assertEqual(data['error'], False) self.assertListEqual(sorted(list(data['targets'].keys())), sorted([target.name for target in targets]))
def test_get(self): """ Test the GetGroup API function. """ targets = [ Database.create_target(), Database.create_target(), Database.create_target(), ] Database.create_group('TEST GROUP', [target.name for target in targets]) data = APIClient.get_group(self.client, 'TEST GROUP') self.assertEqual(data['error'], False) self.assertEqual(data['group']['name'], 'TEST GROUP') self.assertListEqual(sorted(data['group']['whitelist_members']), sorted([target.name for target in targets]))
def test_create(self): """ This test will pass if the group action is created and content matches. """ targets = [ Database.create_target().name, Database.create_target().name, Database.create_target().name, Database.create_target().name, ] group = Database.create_group('TEST', targets) data = APIClient.create_group_action(self.client, group.name, 'exec echo hello | wall') self.assertEqual(data['error'], False) group_action = Database.get_group_action(data['group_action_id']) self.assertIsNotNone(group_action) self.assertEqual(group_action.action_string, 'exec echo hello | wall') for action in group_action.actions: self.assertEqual(action.action_string, 'exec echo hello | wall')
def test_remove_member(self): """ Test the RemoveGroupMember function. """ targets = [ Database.create_target().name, Database.create_target().name, Database.create_target().name, ] Database.create_group('REMOVE GROUP', targets) data = APIClient.remove_group_member(self.client, 'REMOVE GROUP', targets[0]) self.assertEqual(data['error'], False) group = Database.get_group('REMOVE GROUP') self.assertListEqual(sorted(targets[1:]), sorted(group.member_names))
def test_create(self): """ This test will pass if the target is created. """ with self.assertRaises(DoesNotExist): Database.get_target('TEST Target') data = APIClient.create_target(self.client, 'TEST Target', ['AA:BB:CC:DD:EE:FF'], {'test_fact': 'hello'}) self.assertEqual(False, data['error']) target = Database.get_target('TEST Target') self.assertIsNotNone(target) self.assertEqual(target.name, 'TEST Target') self.assertListEqual(['AA:BB:CC:DD:EE:FF'], target.mac_addrs) self.assertDictEqual({'test_fact': 'hello'}, target.facts)
def test_get_params(self): """ This test will pass if get returns the correct parameters. """ target = Database.create_target('PARAMS TEST') action = Database.create_action(target.name) data = APIClient.get_target(self.client, 'PARAMS TEST', False, False, True) self.assertEqual(data['error'], False) self.assertIsInstance(data['target'], dict) self.assertEqual(data['target']['name'], 'PARAMS TEST') self.assertIsInstance(data['target']['mac_addrs'], list) self.assertListEqual(data['target']['mac_addrs'], target.mac_addrs) self.assertIsNotNone(data['target']['actions']) self.assertEqual(data['target']['actions'][0]['action_id'], action.action_id) with self.assertRaises(KeyError): data['target']['sessions'] #pylint: disable=pointless-statement with self.assertRaises(KeyError): data['target']['facts'] #pylint: disable=pointless-statement
def test_blacklist_member(self): """ Test the BlacklistGroupMember function. """ targets = [ Database.create_target().name, Database.create_target().name, Database.create_target().name, ] Database.create_group('BLACKLIST GROUP', targets) data = APIClient.blacklist_group_member(self.client, 'BLACKLIST GROUP', targets[0]) self.assertEqual(data['error'], False) group = Database.get_group('BLACKLIST GROUP') self.assertListEqual(sorted(targets[1:]), sorted(group.member_names)) self.assertListEqual([targets[0]], group.blacklist_members)
def test_list(self): """ This test will create a few action objects through the API, and then test listing them. """ target = Database.create_target() test_actions = [ Database.create_action(target.name, 'exec ls -al'), Database.create_action(target.name, 'config -i 20'), Database.create_action(target.name, 'exec --time 1345.12345 rm -rf tmp/'), Database.create_action(target.name, 'exec --spawn /bin/beloved'), Database.create_action(target.name, 'upload /some/file /another/file'), Database.create_action(target.name, 'download /lol/what /ha/nope'), Database.create_action(target.name, 'gather -s min') ] data = APIClient.list_actions(self.client) self.assertEqual(data['error'], False) for action in test_actions: self.assertIn(action['action_id'], data['actions'].keys())
def test_migrate_target(self): """ Tests the MigrateTarget API function. """ old_target = Database.create_target() old_sessions = [ Database.create_session(old_target.name), Database.create_session(old_target.name), Database.create_session(old_target.name), Database.create_session(old_target.name), Database.create_session(old_target.name), ] new_target = Database.create_target(None, None, {'updated': True}) new_sessions = [ Database.create_session(new_target.name), Database.create_session(new_target.name), Database.create_session(new_target.name), Database.create_session(new_target.name), Database.create_session(new_target.name), ] data = APIClient.migrate_target(self.client, old_target.name, new_target.name) self.assertEqual(data['error'], False) # Ensure new_target has old name, and still has facts target = Database.get_target(old_target.name) self.assertIsNotNone(target) self.assertEqual(target.facts['updated'], True) # Ensure new target name is gone with self.assertRaises(DoesNotExist): Database.get_target(new_target.name) # Ensure all sessions exist on new_target self.assertListEqual( sorted([ session.session_id for session in old_sessions + new_sessions ]), sorted([session.session_id for session in target.sessions]))