Skip to content

Commit

Permalink
fix: require username and password to be specified
Browse files Browse the repository at this point in the history
We didn't require username and password to be specified when one was
provided but not the other. Such as username being provided but without
a password, or vice versa. Due to that convert2rhel fails much later
in the process than other warnings.

This addresses that with warning messages and aligns with other cases
like activation_key and org
  • Loading branch information
Venefilyn committed May 6, 2024
1 parent e618783 commit 2ec5a8c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
12 changes: 12 additions & 0 deletions convert2rhel/toolopts.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,18 @@ def _process_cli_options(self):
"Either the --org or the --activationkey option is missing. You can't use one without the other."
)

if (parsed_opts.password or config_opts.password) and not (parsed_opts.username or config_opts.username):
loggerinst.warning(
"You have passed the RHSM password without an associated username. Please provide a username together"
" with the password."
)

if (parsed_opts.username or config_opts.username) and not (parsed_opts.password or config_opts.password):
loggerinst.warning(
"You have passed the RHSM username without an associated password. Please provide a password together"
" with the username."
)


def _log_command_used():
"""We want to log the command used for convert2rhel to make it easier to know what command was used
Expand Down
26 changes: 26 additions & 0 deletions convert2rhel/unit_tests/toolopts_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,3 +668,29 @@ def test_setting_no_rpm_va(argv, env_var, expected, message, monkeypatch, global
assert global_tool_opts.no_rpm_va == expected
if message:
assert caplog.records[-1].message == message


@pytest.mark.parametrize(
("argv", "message"),
(
# The message is a log of used command
(mock_cli_arguments(["-u", "user", "-p", "pass"]), "-u ***** -p *****"),
(
mock_cli_arguments(["-p", "pass"]),
"You have passed the RHSM password without an associated username. Please provide a username together with the password",
),
(
mock_cli_arguments(["-u", "user"]),
"You have passed the RHSM username without an associated password. Please provide a password together with the username",
),
),
)
def test_cli_userpass_specified(argv, message, monkeypatch, caplog, global_tool_opts):
monkeypatch.setattr(sys, "argv", argv)

try:
convert2rhel.toolopts.CLI()
except SystemExit:
# Don't care about the exception, focus on output message
pass
assert message in caplog.text

0 comments on commit 2ec5a8c

Please sign in to comment.