diff options
author | Mike Graves <mgraves@redhat.com> | 2024-10-16 18:01:30 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-16 18:01:30 +0200 |
commit | 764dcbf94b51f9b55b467824208e4c8b9dc15786 (patch) | |
tree | 33d5874799bb7b377e3938a2c5b9f89da6a9f68b /awxkit/test/api/pages/test_base.py | |
parent | remove oauth use (diff) | |
download | awx-764dcbf94b51f9b55b467824208e4c8b9dc15786.tar.xz awx-764dcbf94b51f9b55b467824208e4c8b9dc15786.zip |
Add gateway support to awxkit (#15576)
* Add gateway support to awxkit
This updates awxkit to add support for gateway when fetching oauth
tokens, which is used during the `login` subcommand. awxkit will first
try fetching a token from gateway and if that fails, fallback to
existing behavior. This change is backwards compatible.
Signed-off-by: Mike Graves <mgraves@redhat.com>
* Address review feedback
This:
* adds coverage for the get_oauth2_token() method
* changes AuthUrls to a TypedDict
* changes the url used for personal token access in gateway
* Address review feedback
This is just minor stylistic changes.
---------
Signed-off-by: Mike Graves <mgraves@redhat.com>
Diffstat (limited to '')
-rw-r--r-- | awxkit/test/api/pages/test_base.py | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/awxkit/test/api/pages/test_base.py b/awxkit/test/api/pages/test_base.py new file mode 100644 index 0000000000..6706d950d4 --- /dev/null +++ b/awxkit/test/api/pages/test_base.py @@ -0,0 +1,59 @@ +from http.client import NOT_FOUND +import pytest +from pytest_mock import MockerFixture +from requests import Response + +from awxkit.api.pages import Base +from awxkit.config import config + + +@pytest.fixture(autouse=True) +def setup_config(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr(config, "credentials", {"default": {"username": "foo", "password": "bar"}}, raising=False) + monkeypatch.setattr(config, "base_url", "", raising=False) + + +@pytest.fixture +def response(mocker): + r = mocker.Mock() + r.status_code = NOT_FOUND + r.json.return_value = { + "token": "my_personal_token", + "access_token": "my_token", + } + return r + + +@pytest.mark.parametrize( + ("auth_creds", "url", "token"), + [ + ({"client_id": "foo", "client_secret": "bar"}, "/o/token/", "my_token"), + ({"client_id": "foo"}, "/o/token/", "my_token"), + ({}, "/api/gateway/v1/tokens/", "my_personal_token"), + ], +) +def test_get_oauth2_token_from_gateway(mocker: MockerFixture, response: Response, auth_creds, url, token): + post = mocker.patch("requests.Session.post", return_value=response) + base = Base() + ret = base.get_oauth2_token(**auth_creds) + assert post.call_count == 1 + assert post.call_args.args[0] == url + assert ret == token + + +@pytest.mark.parametrize( + ("auth_creds", "url", "token"), + [ + ({"client_id": "foo", "client_secret": "bar"}, "/api/o/token/", "my_token"), + ({"client_id": "foo"}, "/api/o/token/", "my_token"), + ({}, "/api/v2/users/foo/personal_tokens/", "my_personal_token"), + ], +) +def test_get_oauth2_token_from_controller(mocker: MockerFixture, response: Response, auth_creds, url, token): + type(response).ok = mocker.PropertyMock(side_effect=[False, True]) + post = mocker.patch("requests.Session.post", return_value=response) + base = Base() + ret = base.get_oauth2_token(**auth_creds) + assert post.call_count == 2 + assert post.call_args.args[0] == url + assert ret == token |