-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests(insights): Adding new insights device auth workflow tests
- Tests the new device auth workflow with insights login - Tests for Insights Auth request_auth
- Loading branch information
Showing
9 changed files
with
196 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
"""Test the CLI module's Insights Auth support.""" | ||
|
||
import pytest | ||
|
||
from qpc.insights.auth import DEVICE_AUTH_ENDPOINT, InsightsAuth | ||
from qpc.insights.exceptions import InsightsAuthError | ||
from qpc.utils import CONFIG_SSO_HOST_KEY, read_insights_config, write_insights_config | ||
|
||
|
||
class TestInsightsAuth: | ||
"""Class for testing Insights Device Auth support.""" | ||
|
||
def test_insights_request_auth(self, faker, requests_mock): | ||
"""Testing that insights auth targets the right endpoint.""" | ||
auth_response = {"device_code": faker.slug()} | ||
sso_host = read_insights_config().get(CONFIG_SSO_HOST_KEY) | ||
sso_url = f"https://{sso_host}{DEVICE_AUTH_ENDPOINT}" | ||
requests_mock.post(sso_url, json=auth_response) | ||
assert InsightsAuth().request_auth() == auth_response | ||
|
||
def test_insights_request_auth_alt_sso_host(self, faker, requests_mock): | ||
"""Testing that insights auth targets alternate sso hosts.""" | ||
alt_sso_host = faker.slug() | ||
write_insights_config({CONFIG_SSO_HOST_KEY: alt_sso_host}) | ||
auth_response = {"device_code": faker.slug()} | ||
sso_url = f"https://{alt_sso_host}{DEVICE_AUTH_ENDPOINT}" | ||
requests_mock.post(sso_url, json=auth_response) | ||
assert InsightsAuth().request_auth() == auth_response | ||
|
||
def test_insights_request_auth_connection_error(self, faker): | ||
"""Testing that insights auth handles connection errors.""" | ||
alt_sso_host = faker.slug() | ||
write_insights_config({CONFIG_SSO_HOST_KEY: alt_sso_host}) | ||
with pytest.raises(InsightsAuthError) as err: | ||
InsightsAuth().request_auth() | ||
assert "Failed to request login authorization" in err.value | ||
|
||
def test_insights_wait_for_authorization(self): | ||
"""Testing that insights wait for authorization returns the auth token.""" | ||
|
||
def test_insights_wait_for_authorization_connection_error(self): | ||
"""Testing that insights wait for authorization handles connection errors.""" | ||
|
||
def test_insights_wait_for_authorization_http_error(self): | ||
"""Testing that insights wait for authorization handles http errors.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
"""Test the CLI module's Insights Login command.""" | ||
|
||
import sys | ||
from unittest.mock import MagicMock | ||
|
||
import pytest | ||
|
||
from qpc import messages | ||
from qpc.cli import CLI | ||
from qpc.insights.auth import InsightsAuth | ||
from qpc.insights.exceptions import InsightsAuthError | ||
|
||
|
||
class TestInsightsLogin: | ||
"""Class for testing insights login command.""" | ||
|
||
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): | ||
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): | ||
CLI().main() | ||
out, err = capsys.readouterr() | ||
assert out == "" | ||
assert "error: unrecognized arguments: --password" in err | ||
|
||
def test_insights_login_normal_behavior(self, faker, mocker, capsys): | ||
"""Testing that insights login displays the expected messages.""" | ||
auth_token = faker.md5() | ||
user_code = faker.slug() | ||
verification_uri_complete = faker.url() | ||
insights_auth = MagicMock() | ||
insights_auth.request_auth.return_value = { | ||
"user_code": user_code, | ||
"verification_uri_complete": verification_uri_complete, | ||
} | ||
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() | ||
out, err = capsys.readouterr() | ||
stdout_lines = out.splitlines() | ||
assert "Insights login authorization requested" in stdout_lines[0] | ||
assert "User Code: " in stdout_lines[1] | ||
assert "Authorization URL: " in stdout_lines[2] | ||
assert "Waiting for login authorization ..." in stdout_lines[3] | ||
assert "Login authorization successful." in stdout_lines[4] | ||
|
||
def test_insights_login_auth_error(self, faker, mocker, capsys): | ||
"""Testing that insights login catches auth errors.""" | ||
err_message = messages.INSIGHTS_LOGIN_REQUEST_FAILED % "Network Error" | ||
mocker.patch.object( | ||
InsightsAuth, "request_auth", side_effect=InsightsAuthError(err_message) | ||
) | ||
sys.argv = ["/bin/qpc", "insights", "login"] | ||
CLI().main() | ||
out, err = capsys.readouterr() | ||
assert err_message in err |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters