0e3311df8e
- Enhance .gitignore with comprehensive Python patterns - Improve README with better setup and testing instructions - Add test infrastructure: * Test requirements file * Environment setup script * Test runner script * Comprehensive test suite * Coverage configuration
89 lines
3.4 KiB
Python
89 lines
3.4 KiB
Python
import pytest
|
|
import requests
|
|
from unittest.mock import patch, MagicMock
|
|
from main import (
|
|
get_user_info_from_endpoint,
|
|
get_userinfo_endpoint,
|
|
get_token_endpoint
|
|
)
|
|
|
|
@patch('main.requests.get')
|
|
def test_get_user_info_from_endpoint_success(mock_get):
|
|
"""Test successful retrieval of user info from endpoint."""
|
|
# Mock successful response
|
|
mock_response = MagicMock()
|
|
mock_response.json.return_value = {
|
|
"email": "test@example.com",
|
|
"given_name": "Test",
|
|
"family_name": "User"
|
|
}
|
|
mock_response.text = '{"email": "test@example.com"}'
|
|
mock_response.raise_for_status.return_value = None
|
|
mock_get.return_value = mock_response
|
|
|
|
with patch('main.get_userinfo_endpoint') as mock_endpoint:
|
|
mock_endpoint.return_value = "https://login.infomaniak.com/userinfo"
|
|
|
|
user_info = get_user_info_from_endpoint("valid-access-token")
|
|
assert "email" in user_info
|
|
assert user_info["email"] == "test@example.com"
|
|
|
|
@patch('main.requests.get')
|
|
def test_get_user_info_from_endpoint_failure(mock_get):
|
|
"""Test failed retrieval of user info from endpoint."""
|
|
# Mock failed response with requests exception
|
|
mock_get.side_effect = requests.exceptions.RequestException("Network error")
|
|
|
|
with patch('main.get_userinfo_endpoint') as mock_endpoint:
|
|
mock_endpoint.return_value = "https://login.infomaniak.com/userinfo"
|
|
|
|
with pytest.raises(ValueError, match="Failed to fetch user info"):
|
|
get_user_info_from_endpoint("invalid-access-token")
|
|
|
|
@patch('main.requests.get')
|
|
def test_get_userinfo_endpoint_success(mock_get):
|
|
"""Test successful retrieval of userinfo endpoint."""
|
|
# Mock successful response for well-known config
|
|
with patch('main.get_well_known_config') as mock_config:
|
|
mock_config.return_value = {
|
|
"userinfo_endpoint": "https://login.infomaniak.com/userinfo"
|
|
}
|
|
|
|
endpoint = get_userinfo_endpoint()
|
|
assert endpoint == "https://login.infomaniak.com/userinfo"
|
|
|
|
def test_get_userinfo_endpoint_failure():
|
|
"""Test failed retrieval of userinfo endpoint."""
|
|
# Clear cache first
|
|
get_userinfo_endpoint.cache_clear()
|
|
|
|
# Mock well-known config without userinfo endpoint
|
|
with patch('main.get_well_known_config') as mock_config:
|
|
mock_config.return_value = {}
|
|
|
|
with pytest.raises(ValueError, match="Userinfo endpoint not found in well-known configuration"):
|
|
get_userinfo_endpoint()
|
|
|
|
@patch('main.requests.get')
|
|
def test_get_token_endpoint_success(mock_get):
|
|
"""Test successful retrieval of token endpoint."""
|
|
# Mock successful response for well-known config
|
|
with patch('main.get_well_known_config') as mock_config:
|
|
mock_config.return_value = {
|
|
"token_endpoint": "https://login.infomaniak.com/token"
|
|
}
|
|
|
|
endpoint = get_token_endpoint()
|
|
assert endpoint == "https://login.infomaniak.com/token"
|
|
|
|
def test_get_token_endpoint_failure():
|
|
"""Test failed retrieval of token endpoint."""
|
|
# Clear cache first
|
|
get_token_endpoint.cache_clear()
|
|
|
|
# Mock well-known config without token endpoint
|
|
with patch('main.get_well_known_config') as mock_config:
|
|
mock_config.return_value = {}
|
|
|
|
with pytest.raises(ValueError, match="Token endpoint not found in well-known configuration"):
|
|
get_token_endpoint() |