def test_render_dict_raises_expected_exception_on_missing_secret(monkeypatch): monkeypatch.delenv("ANOTHER_SECRET", raising=False) webhook = Webhook( build_request_kwargs={ "url": "https://content.dropboxapi.com/2/files?key=${ANOTHER_SECRET}" }, build_request_http_method="POST", get_flow_request_kwargs={ "url": "https://content.dropboxapi.com/2/files" }, get_flow_request_http_method="GET", ) with pytest.raises(RuntimeError, match="does not refer to an environment variable or"): _render_dict(webhook.build_request_kwargs)
def test_render_dict_raises_expected_exception_on_missing_env_var(monkeypatch): monkeypatch.delenv("SOME_CRED", raising=False) webhook = Webhook( build_request_kwargs={ "url": "https://content.dropboxapi.com/2/files", "headers": { "X-Api-Key": "Token ${SOME_CRED}" }, }, build_request_http_method="POST", get_flow_request_kwargs={ "url": "https://content.dropboxapi.com/2/files" }, get_flow_request_http_method="GET", ) with pytest.raises(RuntimeError, match="SOME_CRED"): _render_dict(webhook.build_request_kwargs)
def test_render_dict_gets_env_variables(monkeypatch): some_cred = str(uuid.uuid4()) another_secret = str(uuid.uuid4()) monkeypatch.setenv("SOME_CRED", some_cred) webhook = Webhook( build_request_kwargs={ "url": "https://content.dropboxapi.com/2/files", "headers": { "X-Api-Key": "${SOME_CRED}", "X-Custom-Key": "${ANOTHER_SECRET}", }, }, build_request_http_method="POST", get_flow_request_kwargs={ "url": "https://content.dropboxapi.com/2/files" }, get_flow_request_http_method="GET", ) # set a local secret context.setdefault("secrets", {}) context.secrets["ANOTHER_SECRET"] = another_secret new_kwargs = _render_dict(webhook.build_request_kwargs) # _render_dict should not have side effects assert webhook.build_request_kwargs == { "url": "https://content.dropboxapi.com/2/files", "headers": { "X-Api-Key": "${SOME_CRED}", "X-Custom-Key": "${ANOTHER_SECRET}" }, } # env variables and secrets should have been filled in assert new_kwargs["headers"]["X-Api-Key"] == some_cred assert new_kwargs["headers"]["X-Custom-Key"] == another_secret
def templating_works_with_embedded_json_strings(): x = {"headers": {"dropbox-args": '{"USER"}'}} out = _render_dict(x) assert out == x
def templating_works_if_nothing_to_template(): x = {"thing": 4, "stuff": [5, 6, 7], "big": {"if": True}} out = _render_dict(x) assert out == x assert x == {"thing": 4, "stuff": [5, 6, 7], "big": {"if": True}}
def test_templating_works_with_env_variable_recursively(monkeypatch): monkeypatch.setenv("USER", "leia") x = {"a": {"b": {"c": "Bearer: ${USER}"}}} out = _render_dict(x) assert out == {"a": {"b": {"c": "Bearer: leia"}}} assert x == {"a": {"b": {"c": "Bearer: ${USER}"}}}
def test_templating_works_with_env_variable_top_level(monkeypatch): monkeypatch.setenv("JAVA_HOME", "abc") x = {"a": "coffee_place: ${JAVA_HOME}"} out = _render_dict(x) assert out == {"a": "coffee_place: abc"} assert x == {"a": "coffee_place: ${JAVA_HOME}"}