Skip to content

Commit

Permalink
Add tests for environmental variables interpolation support
Browse files Browse the repository at this point in the history
  • Loading branch information
dbagan13 committed Oct 24, 2023
1 parent bd5b063 commit 647d4ad
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions tests/unit/credentials/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ def creds_yaml_bad_url():
"""


@pytest.fixture
def creds_yaml_env_variables():
return """
secrets:
local_ftp: ftp://${USER}:${PASSWORD}@local.com
remote_db: postgresql://${USER_DB}:${PASSWORD_DB}@db.com/database
"""


@pytest.fixture
def creds_yaml_bad_env_variables():
return """
secrets:
local_ftp: ftp://${FAKE_USER}:${FAKE_PASSWORD}@local.com
remote_db: postgresql://${FAKE_USER_DB}:${FAKE_PASSWORD_DB}@db.com/database
"""


def test_bad_yaml():
with pytest.raises(TentaclioFileError):
data = io.StringIO("sadfsaf")
Expand Down Expand Up @@ -62,3 +80,29 @@ def test_credentials_bad_url(creds_yaml_bad_url):
data = io.StringIO(creds_yaml_bad_url)
with pytest.raises(Exception):
reader.add_credentials_from_reader(injection.CredentialsInjector(), data)


@pytest.mark.parametrize(
"url, expected",
[
("ftp://local.com/file.txt", "ftp://user:[email protected]/file.txt"),
("postgresql://db.com/database", "postgresql://user_db:[email protected]/database"),
],
)
def test_credentials_env_variable(url, expected, creds_yaml_env_variables, monkeypatch):
monkeypatch.setenv('USER', 'user')
monkeypatch.setenv('USER_DB', 'user_db')
monkeypatch.setenv('PASSWORD', 'password')
monkeypatch.setenv('PASSWORD_DB', 'password_db')

data = io.StringIO(creds_yaml_env_variables)
injector = reader.add_credentials_from_reader(injection.CredentialsInjector(), data)

result = injector.inject(urls.URL(url))
assert result == urls.URL(expected)


def test_credentials_bad_env_variable(creds_yaml_bad_env_variables):
data = io.StringIO(creds_yaml_bad_env_variables)
with pytest.raises(EnvironmentError):
reader.add_credentials_from_reader(injection.CredentialsInjector(), data)

0 comments on commit 647d4ad

Please sign in to comment.