forked from ansible/awx
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add gateway support to awxkit (ansible#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>
- Loading branch information
Showing
3 changed files
with
85 additions
and
7 deletions.
There are no files selected for viewing
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
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,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 |