def test_requestor_fails_without_publish_api_key_on_post(self): with mock_response(): cased.publish_key = None with pytest.raises(Exception): requestor = Requestor.publish_requestor() requestor.request("post", "/publish").status_code
def test_requestor_has_policy_api_key_on_404(self): with mock_response(status_code=404): cased.policy_key = "cs_test_001" requestor = Requestor.policy_requestor() assert requestor.api_key == "cs_test_001" assert requestor.request("get", "/policies").status_code == 404
def test_requestor_has_publish_api_key(self): with mock_response(): cased.publish_key = "cs_test_001" requestor = Requestor.publish_requestor() assert requestor.api_key == "cs_test_001" assert requestor.request("post", "/publish").status_code == 200
def fetch(cls, resource_id, **params): """ Fetch the resource represented by resource_id. Any APIResource has a fetch function. """ requestor = Requestor.policy_requestor(**params) url = cls.class_url() + "/" + resource_id response = requestor.request("get", url) return response
def list(cls, **params): url = cls.class_url() + "/" data = {} limit = params.get("limit") or 25 page = params.get("page", None) policy = params.get("policy", None) search = params.get("search", None) variables = params.get("variables", None) if page: data["page"] = page if search: data["phrase"] = search data["per_page"] = limit if variables: if not isinstance(variables, dict): raise TypeError("`variables` parameter must of type `dict`") for key, value in variables.items(): param_key = "variables[{}]".format(key) data[param_key] = value if params.get("api_key"): # Respect any api_key given on a per-request basis pass elif policy: # A policy was given; so use that key params["api_key"] = cased.policy_keys.get(policy) else: # Nothing to do, we'll use the default policy key pass requestor = Requestor.policy_requestor(**params) response = requestor.request("get", url, data=data) return ResultsList(response, cls.resource_class())
def publish(cls, data, **params): if cased.disable_publishing: return None requestor = Requestor.publish_requestor(**params) publish_data = data.copy() # Using the plugin system, add any data from plugins that have been configured data_plugins = getattr(cased, "data_plugins", []) for plugin in data_plugins: instance = plugin(publish_data) additions = instance.additions() publish_data.update(additions) # Include context data, overriding it with any local publish data current_context = cased.context.current() always_merger.merge(current_context, publish_data) # Update the .cased with any PII ranges processor = SensitiveDataProcessor(current_context) final_publish_data = processor.process() # Add the item to a ReliabilityEngine backend. The backend can be configured # with a "backend" keyword arg passed to publish(), or with the global config. # By default, we try/except just in case the user misconfigured their ReliabilityEngine: # we'd rather still send the audit data then fail here. But we do log a message. # Additionally, if a ReliabilityBackend is not set, we will (optionally) log a warning. try: if "backend" in params: engine = ReliabilityEngine(backend=params["backend"]) engine.add(publish_data) elif cased.reliability_backend: engine = ReliabilityEngine(backend=cased.reliability_backend) engine.add(publish_data) else: if cased.warn_if_no_reliability_backend: log_info("No reliability backend has been set") except ReliabilityEngineError as e: log_info("""Reliability backend is misconfigured. Please check configuration. Client will still send audit data to Cased. Error: {}""".format(e)) try: # Send to Cased response = requestor.request("post", "/", final_publish_data) log_info("Sent to Cased: " + str(final_publish_data)) # Publish the item to any additional publishers additional_publishers = cased.additional_publishers for publisher in additional_publishers: try: publisher.publish(final_publish_data) except AttributeError: raise PublisherException( "Publisher must implement publish()") except Exception as e: log_error(e) if cased.raise_on_publish_errors: raise finally: if cased.clear_context_after_publishing: cased.context.clear() return response
def download(cls, export_id, **params): requestor = Requestor.policy_requestor(**params) url = cls.class_url() + "/{}/download".format(export_id) return requestor.request("get", url)
def delete(cls, resource_id, **params): requestor = Requestor.policy_requestor(**params) url = cls.class_url() + "/" + resource_id response = requestor.request("delete", url) return response
def test_event_publish_uses_publish_base(self): with mock_response(): cased.publish_key = "cs_test_001" assert Requestor.publish_requestor().api_base == cased.publish_base
def update(cls, resource_id, **params): requestor = Requestor.policy_requestor(**params) data = params url = cls.class_url() + "/" + resource_id response = requestor.request("put", url, data) return response