Skip to content

Commit

Permalink
fix logout and authentication tests
Browse files Browse the repository at this point in the history
  • Loading branch information
austinmroczek committed Feb 1, 2025
1 parent d36e220 commit 4d50bd6
Showing 1 changed file with 72 additions and 46 deletions.
118 changes: 72 additions & 46 deletions tests/test_client_authenticate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
from common import create_client
from const import HTTP_RESPONSE_BAD_USER_OR_PASSWORD

from total_connect_client.const import AUTH_TOKEN_ENDPOINT, _ResultCode
from total_connect_client.exceptions import AuthenticationError
from total_connect_client.const import AUTH_TOKEN_ENDPOINT,HTTP_API_LOGOUT, _ResultCode
from total_connect_client.exceptions import AuthenticationError, TotalConnectError
from total_connect_client.client import TotalConnectClient

RESPONSE_SUCCESS = {
"ResultCode": _ResultCode.SUCCESS.value,
Expand All @@ -17,59 +18,84 @@
}


def mock_log_out(client: TotalConnectClient, response)->None:
"""Logout client with given response."""
with requests_mock.Mocker(real_http=True) as rm:
rm.post(HTTP_API_LOGOUT,json=response)
client.log_out()


def tests_logout():
"""Test logout."""
client = create_client()
responses = [
RESPONSE_SUCCESS,
]
with patch(
"total_connect_client.client.TotalConnectClient.request",
side_effect=responses,
):
assert client.is_logged_in() is True
client.log_out()
assert client.is_logged_in() is False
assert client.is_logged_in() is True

# succeeds because we are logged out
client.log_out()
RESPONSE_LOGOUT_FAILURE = {
"ResultCode": 1,
"ResultData": "some error"
}

with pytest.raises(TotalConnectError):
mock_log_out(client, RESPONSE_LOGOUT_FAILURE)
assert client.is_logged_in() is True

mock_log_out(client, RESPONSE_SUCCESS)
assert client.is_logged_in() is False

# succeeds without API call because we are logged out
assert client.is_logged_in() is False


def tests_authenticate():
"""Test authenticate()."""
client = create_client()
responses = [
RESPONSE_SUCCESS,
RESPONSE_SUCCESS,
]
with patch(
"total_connect_client.client.TotalConnectClient.request",
side_effect=responses,
), patch(
"total_connect_client.client.TotalConnectClient._make_locations",
return_value=["fakelocations"],

# ensure logged out to start
mock_log_out(client, RESPONSE_SUCCESS)
assert client.is_logged_in() is False

client.authenticate()
assert client.is_logged_in() is True

mock_log_out(client, RESPONSE_SUCCESS)
assert client.is_logged_in() is False

# bad user or pass
with requests_mock.Mocker(real_http=True) as rm, pytest.raises(
AuthenticationError
):
# ensure we start logged out (first SUCCESS)
client.log_out()
assert client.is_logged_in() is False
rm.post(
AUTH_TOKEN_ENDPOINT,
json=HTTP_RESPONSE_BAD_USER_OR_PASSWORD,
status_code=403,
)
client.authenticate()
assert client.is_logged_in() is False

# success (second SUCCESS)
RESPONSE = {
"ResultCode": _ResultCode.AUTHENTICATION_FAILED,
"ResultData": "some error"
}

# authentication failed
with requests_mock.Mocker(real_http=True) as rm, pytest.raises(
AuthenticationError
):
rm.post(
AUTH_TOKEN_ENDPOINT,
json=RESPONSE,
)
client.authenticate()
assert client.is_logged_in() is False

# authentication locked
RESPONSE["ResultCode"] = _ResultCode.ACCOUNT_LOCKED
with requests_mock.Mocker(real_http=True) as rm, pytest.raises(
AuthenticationError
):
rm.post(
AUTH_TOKEN_ENDPOINT,
json=RESPONSE,
)
client.authenticate()
assert client.is_logged_in() is True

# bad user or pass
with requests_mock.Mocker(real_http=True) as rm, pytest.raises(
AuthenticationError
):
rm.post(
AUTH_TOKEN_ENDPOINT,
json=HTTP_RESPONSE_BAD_USER_OR_PASSWORD,
status_code=403,
)
client.authenticate()
assert client.is_logged_in() is False

# authentication failed
with pytest.raises(AuthenticationError):
client.authenticate()
assert client.is_logged_in() is False
assert client.is_logged_in() is False

0 comments on commit 4d50bd6

Please sign in to comment.