def test_filter_version(): params = _filter_kwargs_to_query_params({"version": "foo"}) assert params == {"match[version]": "foo"} dt = datetime.datetime(2010, 9, 8, 7, 6, 5) params = _filter_kwargs_to_query_params({"version": dt}) assert params == {"match[version]": "2010-09-08T07:06:05Z"} params = _filter_kwargs_to_query_params({"version": (dt, "bar")}) assert params == {"match[version]": "2010-09-08T07:06:05Z,bar"}
def test_filter_added_after(): params = _filter_kwargs_to_query_params({"added_after": "foo"}) assert params == {"added_after": "foo"} dt = datetime.datetime(2010, 9, 8, 7, 6, 5) params = _filter_kwargs_to_query_params({"added_after": dt}) assert params == {"added_after": "2010-09-08T07:06:05Z"} with pytest.raises(InvalidArgumentsError): _filter_kwargs_to_query_params({"added_after": (dt, "bar")})
def get_objects(self, **filter_kwargs): self._verify_can_read() query_params = _filter_kwargs_to_query_params(filter_kwargs) if not isinstance(query_params, dict): query_params = json.loads(query_params) full_filter = BasicFilter(query_params or {}) objs = full_filter.process_filter(self.objects, ("id", "type", "version"), []) if objs: return Bundle(objects=objs) else: resp = Response() resp.status_code = 404 resp.raise_for_status()
def test_filter_combo(): dt = datetime.datetime(2010, 9, 8, 7, 6, 5) params = _filter_kwargs_to_query_params({ "added_after": dt, "type": ("indicator", "malware"), "version": dt, "foo": ("bar", "baz") }) assert params == { "added_after": "2010-09-08T07:06:05Z", "match[type]": "indicator,malware", "match[version]": "2010-09-08T07:06:05Z", "match[foo]": "bar,baz" }
def get_object(self, id, version=None): self._verify_can_read() query_params = None if version: query_params = _filter_kwargs_to_query_params({"version": version}) if query_params: query_params = json.loads(query_params) full_filter = BasicFilter(query_params or {}) objs = full_filter.process_filter(self.objects, ("version", ), []) if objs: return Bundle(objects=objs) else: resp = Response() resp.status_code = 404 resp.raise_for_status()
def get_object(self, id, **filter_kwargs): self._verify_can_read() query_params = _filter_kwargs_to_query_params(filter_kwargs) assert isinstance(query_params, dict) full_filter = BasicFilter(query_params) # In this endpoint we must first filter objects by id beforehand. objects = [x for x in self.objects if x["id"] == id] if objects: filtered_objects = full_filter.process_filter( objects, ("version", ), [], ) else: filtered_objects = [] if filtered_objects: return stix2.v21.Bundle(objects=filtered_objects) else: resp = Response() resp.status_code = 404 resp.raise_for_status()
def test_params_filter_unknown(): params = _filter_kwargs_to_query_params({"foo": "bar"}) assert params == {"match[foo]": "bar"}
def test_filter_id(): params = _filter_kwargs_to_query_params({"id": "foo"}) assert params == {"match[id]": "foo"} params = _filter_kwargs_to_query_params({"id": ("foo", "bar")}) assert params == {"match[id]": "foo,bar"}
def test_url_filter_type(): params = _filter_kwargs_to_query_params({"type": "foo"}) assert params == {"match[type]": "foo"} params = _filter_kwargs_to_query_params({"type": ("foo", "bar")}) assert params == {"match[type]": "foo,bar"}