Skip to content

Commit

Permalink
added test refresh failure
Browse files Browse the repository at this point in the history
  • Loading branch information
Moasib-Arif committed Jul 1, 2024
1 parent 4010579 commit 26642c9
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions tests/http/test_token_auth.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from datetime import datetime, timedelta
from unittest.mock import MagicMock, patch

import pytest
Expand Down Expand Up @@ -97,3 +98,31 @@ def test_refresh_user_token():
assert token_auth.auth_token == "new_auth_token"
assert token_auth.id_token == "new_id_token"
assert mock_put.call_count == 1


def test_refresh_user_token_failure():
"""
Ensures that refresh_user_token() raises an exception when the put request is unsuccessful.
"""
os.environ["FLORENCE_USER"] = "test_user"
os.environ["FLORENCE_PASSWORD"] = "test_password"
os.environ["IDENTITY_API_URL"] = "http://test_url"
mock_response = MagicMock()
mock_response.status_code = 400
with patch.object(
TokenAuth, "put", return_value=mock_response
) as mock_put, patch.object(
TokenAuth, "post", return_value=mock_response
), patch.object(
TokenAuth, "set_user_tokens"
):
token_auth = TokenAuth()
# Manually set the necessary attributes
token_auth.refresh_token = "test_refresh_token"
token_auth.id_token = "test_id_token"
token_auth.identity_api_url = os.environ["IDENTITY_API_URL"]
# Mock token_creation_time to be more than 10 minutes in the past
token_auth.token_creation_time = datetime.now() - timedelta(minutes=11)
with pytest.raises(Exception) as e_info:
token_auth.refresh_user_token()
assert str(e_info.value) == "Refreshing token failed, returned a 400 error"

0 comments on commit 26642c9

Please sign in to comment.