def test_webhook_raises_error_on_get_flow_failure(sample_flow): webhook = Webhook( build_request_kwargs={"url": "https://content.dropboxapi.com/2/files"}, build_request_http_method="POST", get_flow_request_kwargs={ "url": "https://content.dropboxapi.com/2/files" }, get_flow_request_http_method="GET", ) def _mock_failed_get(*args, **kwargs): return _MockResponse(status_code=500, json={}) def _mock_successful_post(*args, **kwargs): return _MockResponse(status_code=200, json={"id": "abc"}) webhook._method_to_function = { "GET": _mock_failed_get, "POST": _mock_successful_post, } webhook.add_flow(sample_flow) webhook.build() with pytest.raises(HTTPError, match="test-error-message"): webhook.get_flow()
def test_webhook_build_works_with_no_arguments(sample_flow): webhook = Webhook( build_request_kwargs={"url": "https://content.dropboxapi.com/2/files"}, build_request_http_method="POST", get_flow_request_kwargs={ "url": "https://content.dropboxapi.com/2/files" }, get_flow_request_http_method="GET", ) def _mock_successful_get(*args, **kwargs): return _MockResponse(status_code=200, json={}, content=cloudpickle.dumps(sample_flow)) def _mock_successful_post(*args, **kwargs): return _MockResponse(status_code=200, json={"id": "abc"}) webhook._method_to_function = { "GET": _mock_successful_get, "POST": _mock_successful_post, } webhook.add_flow(sample_flow) res = webhook.build() assert isinstance(res, Webhook) res = webhook.get_flow() assert isinstance(res, Flow)
def test_webhook_works_with_file_storage(sample_flow, tmpdir): script_file = os.path.join(tmpdir, "{}.py".format(str(uuid.uuid4()))) script_contents = """from prefect import Flow\nf=Flow('test-flow')""" with open(script_file, "w") as f: f.write(script_contents) webhook = Webhook( build_request_kwargs={ "url": "https://content.dropboxapi.com/2/files", "headers": { "Content-Type": "application/octet-stream" }, }, build_request_http_method="POST", get_flow_request_kwargs={ "url": "https://content.dropboxapi.com/2/files", "headers": { "Accept": "application/octet-stream" }, }, get_flow_request_http_method="GET", stored_as_script=True, flow_script_path=script_file, ) def _mock_successful_get(*args, **kwargs): file_contents = """from prefect import Flow\nf=Flow('test-flow')""" return _MockResponse(status_code=200, json={}, content=file_contents.encode("utf-8")) def _mock_successful_post(*args, **kwargs): return _MockResponse(status_code=200, json={"id": "abc"}) webhook._method_to_function = { "GET": _mock_successful_get, "POST": _mock_successful_post, } webhook.add_flow(sample_flow) res = webhook.build() assert isinstance(res, Webhook) res = webhook.get_flow() assert isinstance(res, Flow) assert res.name == "test-flow"