Skip to content

Commit

Permalink
add cli tests for auth config
Browse files Browse the repository at this point in the history
  • Loading branch information
xoxys committed Sep 4, 2023
1 parent c8c174c commit 26775b9
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 8 deletions.
13 changes: 6 additions & 7 deletions prometheuspvesd/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,13 @@ def _get_config(self):
if not value:
self.log.sysexit_with_message(f"Option '{name}' is required but not set")

if config.config["pve"]["token_name"] and not config.config["pve"]["token_value"]:
if (
not config.config["pve"]["password"]
and not (config.config["pve"]["token_name"] and config.config["pve"]["token_value"])
):
self.log.sysexit_with_message(
"Option 'pve.token_name' require 'pve.token_value' to be set"
)

if not config.config["pve"]["token_name"] and not config.config["pve"]["password"]:
self.log.sysexit_with_message(
"Neither password nor API token have been set for pve authentication"
"Either 'pve.password' or 'pve.token_name' and 'pve.token_value' "
"are required but not set"
)

self.logger.info(f"Using config file {config.config_file}")
Expand Down
2 changes: 2 additions & 0 deletions prometheuspvesd/test/fixtures/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ def defaults():
"password": "",
"server": "",
"user": "",
"token_name": "",
"token_value": "",
"verify_ssl": True
},
"service": True,
Expand Down
59 changes: 59 additions & 0 deletions prometheuspvesd/test/unit/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,65 @@ def test_cli_required_error(mocker, capsys):
assert e.value.code == 1


@pytest.mark.parametrize(
"testinput", [{
"pve.user": "dummy",
"pve.password": "",
"pve.token_name": "",
"pve.token_value": ""
}, {
"pve.user": "dummy",
"pve.password": "",
"pve.token_name": "dummy",
"pve.token_value": ""
}, {
"pve.user": "dummy",
"pve.password": "",
"pve.token_name": "",
"pve.token_value": "dummy"
}]
)
def test_cli_auth_required_error(mocker, capsys, builtins, testinput):
for key, value in testinput.items():
builtins[key]["default"] = value

mocker.patch.dict(Config.SETTINGS, builtins)
mocker.patch.object(ProxmoxClient, "_auth", return_value=mocker.create_autospec(ProxmoxAPI))
mocker.patch.object(PrometheusSD, "_fetch", return_value=True)

with pytest.raises(SystemExit) as e:
PrometheusSD()

stdout, stderr = capsys.readouterr()
assert "Either 'pve.password' or 'pve.token_name' and 'pve.token_value' are required but not set" in stderr
assert e.value.code == 1


@pytest.mark.parametrize(
"testinput", [{
"pve.password": "dummy",
"pve.token_name": "",
"pve.token_value": ""
}, {
"pve.password": "",
"pve.token_name": "dummy",
"pve.token_value": "dummy"
}]
)
def test_cli_auth_no_error(mocker, capsys, builtins, testinput):
for key, value in testinput.items():
builtins[key]["default"] = value

mocker.patch.dict(Config.SETTINGS, builtins)
mocker.patch.object(ProxmoxClient, "_auth", return_value=mocker.create_autospec(ProxmoxAPI))
mocker.patch.object(PrometheusSD, "_fetch", return_value=True)

psd = PrometheusSD()

for key, value in testinput.items():
assert psd.config.config["pve"][key.split(".")[1]] == value


def test_cli_config_error(mocker, capsys):
mocker.patch(
"prometheuspvesd.config.SingleConfig.__init__",
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ sections = ["FUTURE", "STDLIB", "THIRDPARTY", "FIRSTPARTY", "LOCALFOLDER"]
skip_glob = ["**/.env*", "**/env/*", "**/.venv/*", "**/docs/*"]

[tool.pytest.ini_options]
addopts = "prometheuspvesd --cov=prometheuspvesd --cov-report=xml:coverage.xml --cov-report=term --no-cov-on-fail"
addopts = "prometheuspvesd --cov=prometheuspvesd --cov-report=xml:coverage.xml --cov-report=term-missing --no-cov-on-fail --cov-fail-under=80"
filterwarnings = [
"ignore::FutureWarning",
"ignore::DeprecationWarning",
Expand Down

0 comments on commit 26775b9

Please sign in to comment.