Coverage for tests/test_config.py: 100%
116 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-02-09 12:09 +0100
« prev ^ index » next coverage.py v6.5.0, created at 2023-02-09 12:09 +0100
1from __future__ import annotations
3from pathlib import Path
5import pytest
6import tomli
7from hypothesis import given
8from hypothesis import strategies as st
9from pydantic import ValidationError
11from harbor_cli.config import HarborCLIConfig
12from harbor_cli.config import HarborSettings
13from harbor_cli.config import load_config
14from harbor_cli.config import load_toml_file
15from harbor_cli.config import sample_config
16from harbor_cli.config import save_config
17from harbor_cli.config import TableSettings
20def test_save_config(tmp_path: Path, config: HarborCLIConfig) -> None:
21 conf_path = tmp_path / "config.toml"
22 save_config(config, conf_path)
23 assert conf_path.exists()
26def test_load_config(tmp_path: Path, config: HarborCLIConfig) -> None:
27 conf_path = tmp_path / "config.toml"
28 save_config(config, conf_path)
29 loaded = load_config(conf_path)
30 assert loaded.toml() == config.toml()
33def test_load_toml_file(tmp_path: Path, config: HarborCLIConfig) -> None:
34 contents = """\
35[harbor]
36url = "https://harbor.example.com"
38[output]
39format = "table"
40"""
41 toml_file = tmp_path / "config.toml"
42 toml_file.write_text(contents)
43 loaded = load_toml_file(toml_file)
44 assert loaded == {
45 "harbor": {"url": "https://harbor.example.com"},
46 "output": {"format": "table"},
47 }
50@given(st.builds(HarborCLIConfig))
51def test_harbor_cli_config_fuzz(config: HarborCLIConfig) -> None:
52 """Fuzzing with hypothesis."""
53 assert config is not None # not a lot to specifically test here
56def test_harbor_cli_config_from_file(config_file: Path) -> None:
57 assert config_file.exists()
58 config = HarborCLIConfig.from_file(config_file)
59 assert config is not None
62def test_harbor_cli_config_from_file_not_exists(tmp_path: Path) -> None:
63 f = tmp_path / "config.toml"
64 assert not f.exists()
65 with pytest.raises(FileNotFoundError):
66 HarborCLIConfig.from_file(f, create=False)
69def test_harbor_cli_config_from_file_create(tmp_path: Path) -> None:
70 f = tmp_path / "config.toml"
71 assert not f.exists()
72 conf = HarborCLIConfig.from_file(f, create=True)
73 assert conf is not None
74 assert f.exists()
77@given(st.builds(HarborSettings))
78def test_harbor_settings(settings: HarborSettings) -> None:
79 assert settings is not None
80 assert settings.credentials is not None
83def test_harbor_settings_credentials_file_empty_string() -> None:
84 h = HarborSettings(credentials_file="")
85 assert h.credentials_file is None
88def test_harbor_settings_credentials_file_not_exists(tmp_path: Path) -> None:
89 f = tmp_path / "credentials.json"
90 with pytest.raises(ValidationError) as exc_info:
91 HarborSettings(credentials_file=f)
92 e = exc_info.exconly()
93 assert "exist" in e.casefold()
96def test_harbor_settings_credentials_file_is_dir(tmp_path: Path) -> None:
97 f = tmp_path / "testdir"
98 f.mkdir()
99 with pytest.raises(ValidationError) as exc_info:
100 HarborSettings(credentials_file=f)
101 e = exc_info.exconly()
102 assert "file" in e.casefold()
105def test_harbor_is_authable_not() -> None:
106 h = HarborSettings()
107 assert not h.has_auth_method
110def test_harbor_is_authable_username() -> None:
111 h = HarborSettings(username="admin", secret="password")
112 assert h.has_auth_method
113 assert h.credentials["username"] == "admin"
114 assert h.credentials["secret"] == "password"
115 assert all(c == "*" for c in str(h.secret)) # secret string
116 h.secret = "" # both username and password is required
117 assert not h.has_auth_method
120def test_harbor_is_authable_basicauth() -> None:
121 h = HarborSettings(basicauth="dXNlcm5hbWU6cGFzc3dvcmQK")
122 assert h.has_auth_method
123 # TODO: rename to basicauth when harborapi supports it
124 assert h.credentials["credentials"] == "dXNlcm5hbWU6cGFzc3dvcmQK"
127def test_harbor_is_authable_credentials_file(tmp_path: Path) -> None:
128 f = tmp_path / "credentials.json"
129 f.write_text("test") # no validation of the file contents
130 h = HarborSettings(credentials_file=f)
131 assert h.has_auth_method
132 assert h.credentials["credentials_file"] == f
135@given(st.builds(TableSettings))
136def test_table_settings(table: TableSettings) -> None:
137 """Fuzzing with hypothesis."""
138 assert table is not None
139 # Max depth is either None or a non-negative integer
140 assert table.max_depth is None or table.max_depth >= 0
143def test_table_settings_max_depth_negative() -> None:
144 t = TableSettings(max_depth=-1)
145 assert t.max_depth == 0
148def test_table_settings_max_depth_none() -> None:
149 t = TableSettings(max_depth=None)
150 assert t.max_depth == 0
153def test_table_settings_max_depth_nonnegative() -> None:
154 t = TableSettings(max_depth=123)
155 assert t.max_depth == 123
158def test_sample_config() -> None:
159 s = sample_config()
160 config_dict = tomli.loads(s)
161 assert config_dict # not empty, not None
164@pytest.mark.parametrize("expose_secrets", [True, False])
165def test_harbor_cli_config_toml_expose_secrets(
166 config: HarborCLIConfig, expose_secrets: bool, tmp_path: Path
167) -> None:
168 """Test that the toml() expose_secrets parameter works as expected."""
169 creds_file = tmp_path / "somefile"
170 creds_file.touch()
172 config.harbor.username = "someuser"
173 config.harbor.secret = "somepassword"
174 config.harbor.basicauth = "somebasicauth"
175 config.harbor.credentials_file = creds_file
177 toml_str = config.toml(expose_secrets=expose_secrets)
178 if expose_secrets:
179 assert "somepassword" in toml_str
180 assert "somebasicauth" in toml_str
181 assert "somefile" in toml_str
182 else:
183 assert "somepassword" not in toml_str
184 assert "somebasicauth" not in toml_str
185 assert "somefile" not in toml_str
186 assert "***" in toml_str # don't be too specific about the number of stars