Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pcmxgti committed Nov 16, 2023
1 parent b11ec12 commit cedcb86
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion tests/unit/test_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,41 @@ def client():
return HTTPClient()


@pytest.mark.parametrize(
"base_os, expected",
[
("Darwin", "Macintosh"),
("Linux", "X11"),
("Windows", "Windows"),
("Unknown", "compatible"),
],
)
def test_generate_user_agent(mocker, base_os, expected):
"""Test the generate_user_agent function."""
import platform

from tokendito.http_client import generate_user_agent

mocker.patch("platform.uname", return_value=(base_os, "", "pytest", "", "", ""))
python_version = platform.python_version()

user_agent = generate_user_agent()
assert user_agent == (
f"{__title__}/{__version__} "
f"({expected}; {base_os}/pytest) "
f"Python/{python_version}; "
f"requests/{requests.__version__})"
)


def test_init(client):
"""Test initialization of HTTPClient instance."""
# Check if the session property of the client is an instance of requests.Session
assert isinstance(client.session, requests.Session)

# Check if the User-Agent header was set correctly during initialization
expected_user_agent = f"{__title__}/{__version__}"
assert expected_user_agent in client.session.headers["User-Agent"]
assert str(expected_user_agent) in str(client.session.headers["User-Agent"])


def test_set_cookies(client):
Expand Down Expand Up @@ -166,6 +193,10 @@ def test_get_device_token(client):
# Check if the device token is set correctly in the session
assert client.get_device_token() == device_token

# Check no device token when the cookie is not set
client.session.cookies.clear()
assert client.get_device_token() is None


def test_set_device_token(client):
"""Test setting device token in the session."""
Expand All @@ -174,3 +205,7 @@ def test_set_device_token(client):

# Check if the device token is set correctly in the session
assert client.session.cookies.get("DT") == device_token

# Check no device token set when the cookie is not set
client.session.cookies.clear()
assert client.set_device_token("http://test.com", None) is None

0 comments on commit cedcb86

Please sign in to comment.