Skip to content

Commit

Permalink
test(insights): Preferring to patch.object sys.argv
Browse files Browse the repository at this point in the history
- Preferring to do a patch.object on sys.argv instead of overwriting
  the argument list.
- Using get defaults instead of or for expires_in and interval.
- Rebasing with main/updated rst manpage generation
  • Loading branch information
abellotti committed Nov 28, 2023
1 parent 15281c6 commit 9b29ecb
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 87 deletions.
24 changes: 6 additions & 18 deletions docs/_build/QPC_VAR_PROGRAM_NAME.1
Original file line number Diff line number Diff line change
Expand Up @@ -877,25 +877,13 @@ Optional. Sets the port to use to connect to Insights. The default port is \fB44
Optional. Determines whether to use HTTP instead of HTTPS. The default value is \fBFalse\fP\&.
.UNINDENT
.UNINDENT
.SS Adding Insights credentials information
.SS Login to Insights
.sp
To configure Insights credentials, simply provide the appropriate username and password associated with your Insights account.
To be able to publish reports to Insights, one must be authorized and successfully logged into Insights.
.sp
\fBQPC_VAR_PROGRAM_NAME insights add_login [\-\-username=\fP \fIusername\fP \fB] [\-\-password=\fP \fIpassword\fP \fB]\fP
\fBQPC_VAR_PROGRAM_NAME insights login\fP
.sp
\fB\-\-username=username\fP
.INDENT 0.0
.INDENT 3.5
Required. Sets the username that is used to log in to Insights.
.UNINDENT
.UNINDENT
.sp
\fB\-\-password=password\fP
.INDENT 0.0
.INDENT 3.5
Required. Prompts for the password for the \fB\-\-username\fP identity.
.UNINDENT
.UNINDENT
This command requests the authorization of the user to Insights. A user code and associated authorization URL is displayed that the user can access in a separate browser window to login to Insights and be authorized to use {{QPC_VAR_PROGRAM_NAME}} to publish reports.
.SS Publishing to Insights
.sp
The \fBQPC_VAR_PROGRAM_NAME insights publish\fP command allows you to publish an Insights report to Red Hat Insights and its services. You have two options for publishing a report: use the associated report identifier from the generating scan, or provide a previously downloaded report as an input file.
Expand Down Expand Up @@ -1095,9 +1083,9 @@ Configuring Insights
.sp
\fBQPC_VAR_PROGRAM_NAME insights config \-\-host stage.console.redhat.com \-\-port 8080\fP
.IP \(bu 2
Adding Insights credentials
Login to Insights
.sp
\fBQPC_VAR_PROGRAM_NAME insights add_login \-\-username insights\-user \-\-password\fP
\fBQPC_VAR_PROGRAM_NAME insights login\fP
.IP \(bu 2
Publishing to Insights using a report id
.sp
Expand Down
2 changes: 1 addition & 1 deletion docs/_build/man-qpc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ To be able to publish reports to Insights, one must be authorized and successful

**qpc insights login**

This command requests the authorization of the user to Insights. A user code and associated authorization URL is displayed that the user can access in a separate browser window to login to Insights and be authorized to use qpc to publish reports.
This command requests the authorization of the user to Insights. A user code and associated authorization URL is displayed that the user can access in a separate browser window to login to Insights and be authorized to use {{qpc}} to publish reports.


Publishing to Insights
Expand Down
26 changes: 7 additions & 19 deletions docs/_build/qpc.1
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ level margin: \\n[rst2man-indent\\n[rst2man-indent-level]]
.\" new: \\n[rst2man-indent\\n[rst2man-indent-level]]
.in \\n[rst2man-indent\\n[rst2man-indent-level]]u
..
.TH "qpc" "1" "November 27, 2023" "" "qpc"
.TH "qpc" "1" "November 28, 2023" "" "qpc"
.SH NAME
.sp
qpc \- Inspect and report on product entitlement metadata from various sources, including networks and systems management solutions.
Expand Down Expand Up @@ -877,25 +877,13 @@ Optional. Sets the port to use to connect to Insights. The default port is \fB44
Optional. Determines whether to use HTTP instead of HTTPS. The default value is \fBFalse\fP\&.
.UNINDENT
.UNINDENT
.SS Adding Insights credentials information
.SS Login to Insights
.sp
To configure Insights credentials, simply provide the appropriate username and password associated with your Insights account.
To be able to publish reports to Insights, one must be authorized and successfully logged into Insights.
.sp
\fBqpc insights add_login [\-\-username=\fP \fIusername\fP \fB] [\-\-password=\fP \fIpassword\fP \fB]\fP
\fBqpc insights login\fP
.sp
\fB\-\-username=username\fP
.INDENT 0.0
.INDENT 3.5
Required. Sets the username that is used to log in to Insights.
.UNINDENT
.UNINDENT
.sp
\fB\-\-password=password\fP
.INDENT 0.0
.INDENT 3.5
Required. Prompts for the password for the \fB\-\-username\fP identity.
.UNINDENT
.UNINDENT
This command requests the authorization of the user to Insights. A user code and associated authorization URL is displayed that the user can access in a separate browser window to login to Insights and be authorized to use {{qpc}} to publish reports.
.SS Publishing to Insights
.sp
The \fBqpc insights publish\fP command allows you to publish an Insights report to Red Hat Insights and its services. You have two options for publishing a report: use the associated report identifier from the generating scan, or provide a previously downloaded report as an input file.
Expand Down Expand Up @@ -1095,9 +1083,9 @@ Configuring Insights
.sp
\fBqpc insights config \-\-host stage.console.redhat.com \-\-port 8080\fP
.IP \(bu 2
Adding Insights credentials
Login to Insights
.sp
\fBqpc insights add_login \-\-username insights\-user \-\-password\fP
\fBqpc insights login\fP
.IP \(bu 2
Publishing to Insights using a report id
.sp
Expand Down
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions qpc/insights/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ def wait_for_authorization(self): # noqa: C901 PLR0912
"""
if self.auth_request:
device_code = self.auth_request["device_code"]
interval = self.auth_request.get("interval") or 5 # SSO default
expires_in = self.auth_request.get("expires_in") or 600 # SSO default
interval = self.auth_request.get("interval", 5) # SSO default
expires_in = self.auth_request.get("expires_in", 600) # SSO default

elapsed_time = 0
self.auth_token = None
Expand Down
48 changes: 30 additions & 18 deletions qpc/insights/test_insights_configure.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Test the CLI module."""
import sys
from unittest import mock

import pytest

Expand All @@ -21,26 +22,26 @@ class TestInsightsConfigure:

def test_insights_config_bad_port(self):
"""Testing insights configure when receiving bad port."""
sys.argv = ["/bin/qpc", "insights", "config", "--port", "abc"]
with pytest.raises(SystemExit):
test_argv = ["/bin/qpc", "insights", "config", "--port", "abc"]
with pytest.raises(SystemExit), mock.patch.object(sys, "argv", test_argv):
CLI().main()

def test_insights_config_empty_host(self):
"""Testing insights configure when receiving empty host."""
sys.argv = ["/bin/qpc", "insights", "config", "--host", ""]
with pytest.raises(SystemExit):
test_argv = ["/bin/qpc", "insights", "config", "--host", ""]
with pytest.raises(SystemExit), mock.patch.object(sys, "argv", test_argv):
CLI().main()

def test_insights_config_bad_host(self):
"""Testing insights configure when receiving bad host."""
sys.argv = ["/bin/qpc", "insights", "config", "--host", None]
with pytest.raises(SystemExit):
test_argv = ["/bin/qpc", "insights", "config", "--host", None]
with pytest.raises(SystemExit), mock.patch.object(sys, "argv", test_argv):
CLI().main()

def test_insights_config_bad_sso_host(self):
"""Testing insights configure when receiving bad sso host."""
sys.argv = ["/bin/qpc", "insights", "config", "--sso-host", None]
with pytest.raises(SystemExit):
test_argv = ["/bin/qpc", "insights", "config", "--sso-host", None]
with pytest.raises(SystemExit), mock.patch.object(sys, "argv", test_argv):
CLI().main()

def test_success_default_config_no_args(self):
Expand All @@ -51,7 +52,7 @@ def test_success_default_config_no_args(self):

def test_success_config_insights(self):
"""Testing insights configure green path."""
sys.argv = [
test_argv = [
"/bin/qpc",
"insights",
"config",
Expand All @@ -61,15 +62,16 @@ def test_success_config_insights(self):
"200",
"--use-http",
]
CLI().main()
with mock.patch.object(sys, "argv", test_argv):
CLI().main()
config = read_insights_config()
assert config["host"] == "console.insights.test"
assert config["port"] == 200
assert config["use_http"]

def test_success_config_insights_with_sso_host(self):
"""Testing insights configure green path with sso host."""
sys.argv = [
test_argv = [
"/bin/qpc",
"insights",
"config",
Expand All @@ -81,7 +83,8 @@ def test_success_config_insights_with_sso_host(self):
"--sso-host",
"sso.insights.test",
]
CLI().main()
with mock.patch.object(sys, "argv", test_argv):
CLI().main()
config = read_insights_config()
assert config["host"] == "console.insights.test"
assert config["port"] == 200
Expand All @@ -90,25 +93,33 @@ def test_success_config_insights_with_sso_host(self):

def test_insights_config_default_host(self):
"""Testing insights configure default host."""
sys.argv = ["/bin/qpc", "insights", "config", "--port", "200"]
CLI().main()
test_argv = ["/bin/qpc", "insights", "config", "--port", "200"]
with mock.patch.object(sys, "argv", test_argv):
CLI().main()
config = read_insights_config()
assert config["host"] == DEFAULT_HOST_INSIGHTS_CONFIG
assert config["port"] == 200
assert config["use_http"] == DEFAULT_USE_HTTP_INSIGHTS_CONFIG

def test_insights_config_default_port(self):
"""Testing insights configure default port."""
sys.argv = ["/bin/qpc", "insights", "config", "--host", "console.insights.test"]
CLI().main()
test_argv = [
"/bin/qpc",
"insights",
"config",
"--host",
"console.insights.test",
]
with mock.patch.object(sys, "argv", test_argv):
CLI().main()
config = read_insights_config()
assert config["host"] == "console.insights.test"
assert config["port"] == DEFAULT_PORT_INSIGHTS_CONFIG
assert config["use_http"] == DEFAULT_USE_HTTP_INSIGHTS_CONFIG

def test_insights_config_default_sso_host(self):
"""Testing insights configure default sso host."""
sys.argv = [
test_argv = [
"/bin/qpc",
"insights",
"config",
Expand All @@ -117,7 +128,8 @@ def test_insights_config_default_sso_host(self):
"--port",
"200",
]
CLI().main()
with mock.patch.object(sys, "argv", test_argv):
CLI().main()
config = read_insights_config()
assert config["host"] == "console.insights.test"
assert config["port"] == 200
Expand Down
19 changes: 11 additions & 8 deletions qpc/insights/test_insights_login.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Test the CLI module's Insights Login command."""

import sys
from unittest import mock
from unittest.mock import MagicMock

import pytest
Expand All @@ -16,17 +17,17 @@ class TestInsightsLogin:

def test_insights_login_invalid_username_args_err(self, capsys):
"""Testing that insights login rejects older username args."""
sys.argv = ["/bin/qpc", "insights", "login", "--username", "invalid-user"]
with pytest.raises(SystemExit):
test_argv = ["/bin/qpc", "insights", "login", "--username", "invalid-user"]
with pytest.raises(SystemExit), mock.patch.object(sys, "argv", test_argv):
CLI().main()
out, err = capsys.readouterr()
assert out == ""
assert "error: unrecognized arguments: --username invalid-user" in err

def test_insights_login_invalid_password_args_err(self, capsys):
"""Testing that insights login rejects older password args."""
sys.argv = ["/bin/qpc", "insights", "login", "--password"]
with pytest.raises(SystemExit):
test_argv = ["/bin/qpc", "insights", "login", "--password"]
with pytest.raises(SystemExit), mock.patch.object(sys, "argv", test_argv):
CLI().main()
out, err = capsys.readouterr()
assert out == ""
Expand All @@ -44,8 +45,9 @@ def test_insights_login_normal_behavior(self, faker, mocker, capsys):
}
insights_auth.wait_for_authorization.return_value = auth_token
mocker.patch.object(InsightsAuth, "request_auth", return_value=insights_auth)
sys.argv = ["/bin/qpc", "insights", "login"]
CLI().main()
test_argv = ["/bin/qpc", "insights", "login"]
with mock.patch.object(sys, "argv", test_argv):
CLI().main()
out, err = capsys.readouterr()
stdout_lines = out.splitlines()
assert "Insights login authorization requested" in stdout_lines[0]
Expand All @@ -60,7 +62,8 @@ def test_insights_login_auth_error(self, faker, mocker, capsys):
mocker.patch.object(
InsightsAuth, "request_auth", side_effect=InsightsAuthError(err_message)
)
sys.argv = ["/bin/qpc", "insights", "login"]
CLI().main()
test_argv = ["/bin/qpc", "insights", "login"]
with mock.patch.object(sys, "argv", test_argv):
CLI().main()
out, err = capsys.readouterr()
assert err_message in err
Loading

0 comments on commit 9b29ecb

Please sign in to comment.