Skip to content

Commit

Permalink
test_util: Add test for build_headers_with_authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
sonic2kk committed Aug 11, 2024
1 parent a6db734 commit ec3c7d7
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,42 @@
from pupgui2.datastructures import SteamApp, LutrisGame, HeroicGame, Launcher


def test_build_headers_with_authorization() -> None:

"""
Test whether the expected Authorization Tokens get inserted into the returned headers dict,
that existing Authorization is replaced properly, and that all other existing headers are preserved.
"""

user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'

# Simulate existing headers with old Authentiation to be replaced, and a User-Agent that should remain untouched
request_headers: dict[str, Any] = {
'Authorization': 'ABC123',
'User-Agent': user_agent
}

# Simulate auth tokens that would normally come from the environment or config file
authorization_tokens: dict[str, str] = {
'github': 'gha_abc123daf456',
'gitlab': 'glpat-zyx987wvu654',
}

github_token_call: dict[str, Any] = build_headers_with_authorization(request_headers, authorization_tokens, 'github')
gitlab_token_call: dict[str, Any] = build_headers_with_authorization(request_headers, authorization_tokens, 'gitlab')

unknown_token_call: dict[str, Any] = build_headers_with_authorization(request_headers, authorization_tokens, '')
call_with_no_tokens: dict[str, Any] = build_headers_with_authorization(request_headers, {}, 'github')

assert github_token_call.get('Authorization', '') == f'token {authorization_tokens["github"]}'
assert gitlab_token_call.get('Authorization', '') == f'Bearer {authorization_tokens["gitlab"]}'

assert unknown_token_call.get('Authorization', '') == ''
assert call_with_no_tokens.get('Authorization', '') == ''

assert github_token_call.get('User-Agent', '') == user_agent


def test_get_dict_key_from_value() -> None:

"""
Expand Down

0 comments on commit ec3c7d7

Please sign in to comment.