-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vdk-control-api-auth: add get_authenticated_username
Add to Authentication library a method to get_authenticated_username. The purpose of this method is ot be used during debugging , in logging and in telemetry use cases. Testing Done: unit tests
- Loading branch information
1 parent
dbfc8e5
commit 7978c00
Showing
4 changed files
with
92 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
pytest | ||
pytest-httpserver | ||
requests | ||
requests_oauthlib | ||
vdk-test-utils |
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
40 changes: 40 additions & 0 deletions
40
projects/vdk-plugins/vdk-control-api-auth/tests/test_get_auth_user.py
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,40 @@ | ||
# Copyright 2021-2023 VMware, Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
from vdk.plugin.control_api_auth.authentication import Authentication | ||
|
||
|
||
def test_get_authenticated_username_with_username(): | ||
auth = Authentication(username="testuser") | ||
assert auth.get_authenticated_username() == "testuser" | ||
|
||
|
||
@patch("jwt.decode") | ||
def test_get_authenticated_username_with_token_username(mock_jwt_decode): | ||
mock_jwt_decode.return_value = {"username": "testuser"} | ||
auth = Authentication(token="testtoken") | ||
assert auth.get_authenticated_username() == "testuser" | ||
|
||
|
||
@pytest.mark.parametrize("user_id_field", ["acct", "email", "preferred_username"]) | ||
@patch("jwt.decode") | ||
def test_get_authenticated_username_with_token_other_fields( | ||
mock_jwt_decode, user_id_field | ||
): | ||
mock_jwt_decode.return_value = {user_id_field: "testuser"} | ||
auth = Authentication(token="testtoken") | ||
assert auth.get_authenticated_username() == "testuser" | ||
|
||
|
||
@patch("jwt.decode") | ||
def test_get_authenticated_username_with_token_no_user_id(mock_jwt_decode): | ||
mock_jwt_decode.return_value = {} | ||
auth = Authentication(token="testtoken") | ||
assert auth.get_authenticated_username() is None | ||
|
||
|
||
def test_get_authenticated_username_without_username_or_token(): | ||
auth = Authentication() | ||
assert auth.get_authenticated_username() is None |