def test_aggregate_data_with_list_dict(self): self.response.content = '["item1", "item2"]' self.attack = ExcaliburAttack(self.request, self.response) plugin_runner = PluginsRunner( self.excconf.acl_conf.configuration, self.excconf.sources_conf.configuration, self.excconf.ressource_conf.configuration, self.excconf.plugins_module, raw_yaml_content=True) newdata, errors = self.attack.make_and_run_query(plugin_runner) response = self.attack.aggregate_data(newdata) self.assertEqual(response.status_code, 200) # self.assertEqual( # json.loads(response.content.decode('utf-8')), # ['item1', 'item2', {'key1': 'val1'}, {'key3': 'val3'}, # {'key2': 'val2'}]) items = [ 'item1', 'item2', { 'key1': 'val1' }, { 'key3': 'val3' }, { 'key2': 'val2' } ] loaded_json = json.loads(response.content.decode('utf-8')) not_found = lambda x: x[0] not in x[1] not_found_list = [thing for thing in items \ if not_found([thing,loaded_json])] self.assertEqual([], not_found_list)
def test_manage_errors(self): plugin_runner = PluginsRunner( self.excconf.acl_conf.configuration, self.excconf.sources_conf.configuration, self.excconf.ressource_conf.configuration, self.excconf.plugins_module, raw_yaml_content=True) newdata, errors = self.attack.make_and_run_query(plugin_runner) self.assertEqual(newdata, {}) self.assertTrue(errors)
def test_aggregate_data_with_list_list(self): self.response.content = '["item99", "item98"]' self.attack = ExcaliburAttack(self.request, self.response) plugin_runner = PluginsRunner( self.excconf.acl_conf.configuration, self.excconf.sources_conf.configuration, self.excconf.ressource_conf.configuration, self.excconf.plugins_module, raw_yaml_content=True) newdata, errors = self.attack.make_and_run_query(plugin_runner) response = self.attack.aggregate_data(newdata) self.assertEqual(response.status_code, 200) self.assertEqual( json.loads(response.content.decode('utf-8')).sort(), [ "item99", "item98", "item1", "item2", "item5", "item6", "item3", "item4" ].sort())
def test_aggregate_data_with_404(self): self.response.status_code = 404 self.response.content = '{"error":"404 not found"}' self.attack = ExcaliburAttack(self.request, self.response) plugin_runner = PluginsRunner( self.excconf.acl_conf.configuration, self.excconf.sources_conf.configuration, self.excconf.ressource_conf.configuration, self.excconf.plugins_module, raw_yaml_content=True) newdata, errors = self.attack.make_and_run_query(plugin_runner) response = self.attack.aggregate_data(newdata) self.assertEqual(response.status_code, 200) self.assertEqual(json.loads(response.content.decode('utf-8')), { "key3": "val3", "key2": "val2", "key1": "val1" })
def test_aggregate_data_dict_dict(self): plugin_runner = PluginsRunner( self.excconf.acl_conf.configuration, self.excconf.sources_conf.configuration, self.excconf.ressource_conf.configuration, self.excconf.plugins_module, raw_yaml_content=True) newdata, errors = self.attack.make_and_run_query(plugin_runner) response = self.attack.aggregate_data(newdata) self.assertEqual(response.status_code, 200) self.assertEqual( json.loads(response.content.decode('utf-8')), { "key3": "val3", "key2": "val2", "age": 18, "name": "toto", "key1": "prioritary" })
def test_aggregate_data_with_dict_list(self): self.attack = ExcaliburAttack(self.request, self.response) plugin_runner = PluginsRunner( self.excconf.acl_conf.configuration, self.excconf.sources_conf.configuration, self.excconf.ressource_conf.configuration, self.excconf.plugins_module, raw_yaml_content=True) newdata, errors = self.attack.make_and_run_query(plugin_runner) response = self.attack.aggregate_data(newdata) self.assertEqual(response.status_code, 200) self.assertEqual( json.loads(response.content.decode('utf-8')), { 'name': 'toto', 'age': 18, 'Apogee': ['item3', 'item4'], 'Harpege': ['item1', 'item2'], 'Ldapuds': ['item5', 'item6'], 'key1': 'prioritary' })
def process_response(self, request, response): """ use excalibur when optional "project" and "source" params are in the url """ try: # get all params for query excalibur = ExcaliburAttack(request, response) # get user user = excalibur.get_request_user() if user: try: excconf = ExcaliburConf() except (AttributeError, ObjectDoesNotExist) as e: raise ConfigurationLoaderError(str(e)) # run plugins plugin_runner = PluginsRunner( excconf.acl_conf.configuration, excconf.sources_conf.configuration, excconf.ressource_conf.configuration, excconf.plugins_module, raw_yaml_content=True, check_signature=False if user.is_superuser else True) newdata, errors = excalibur.make_and_run_query(plugin_runner) if not errors: response = excalibur.aggregate_data(newdata) else: response = excalibur.manage_errors(errors) except ExcaliburError as e: logger.error(e.message) response = excalibur_exception_handler(e, response) return response
def test_make_and_run_query(self): plugin_runner = PluginsRunner( self.excconf.acl_conf.configuration, self.excconf.sources_conf.configuration, self.excconf.ressource_conf.configuration, self.excconf.plugins_module, raw_yaml_content=True) newdata, errors = self.attack.make_and_run_query(plugin_runner) self.assertEqual(errors, {}) self.assertEqual( newdata, { 'Harpege': { 'key1': 'val1' }, 'Ldapuds': { 'key3': 'val3' }, 'Apogee': { 'key2': 'val2' } })