Spaces:
Running
Running
File size: 10,681 Bytes
9f69f35 71ca2eb 9f69f35 71ca2eb 9f69f35 71ca2eb 9f69f35 71ca2eb 9f69f35 71ca2eb 9f69f35 71ca2eb 9f69f35 71ca2eb 9f69f35 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
"""Unit tests for OAuth-related functions in app.py."""
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
# Mock gradio and its dependencies before importing app functions
import sys
from unittest.mock import MagicMock as Mock
# Create a comprehensive gradio mock
mock_gradio = Mock()
mock_gradio.OAuthToken = Mock
mock_gradio.OAuthProfile = Mock
mock_gradio.Request = Mock
mock_gradio.update = Mock(return_value={"choices": [], "value": ""})
mock_gradio.Blocks = Mock
mock_gradio.Markdown = Mock
mock_gradio.LoginButton = Mock
mock_gradio.Dropdown = Mock
mock_gradio.Textbox = Mock
mock_gradio.State = Mock
# Mock gradio.data_classes
mock_gradio.data_classes = Mock()
mock_gradio.data_classes.FileData = Mock()
sys.modules["gradio"] = mock_gradio
sys.modules["gradio.data_classes"] = mock_gradio.data_classes
# Mock other dependencies that might be imported
sys.modules["neo4j"] = Mock()
sys.modules["neo4j"].GraphDatabase = Mock()
from src.app import extract_oauth_info, update_model_provider_dropdowns
class TestExtractOAuthInfo:
"""Tests for extract_oauth_info function."""
def test_extract_from_oauth_token_attribute(self) -> None:
"""Should extract token from request.oauth_token.token."""
mock_request = MagicMock()
mock_oauth_token = MagicMock()
mock_oauth_token.token = "hf_test_token_123"
mock_request.oauth_token = mock_oauth_token
mock_request.username = "testuser"
token, username = extract_oauth_info(mock_request)
assert token == "hf_test_token_123"
assert username == "testuser"
def test_extract_from_string_oauth_token(self) -> None:
"""Should extract token when oauth_token is a string."""
mock_request = MagicMock()
mock_request.oauth_token = "hf_test_token_123"
mock_request.username = "testuser"
token, username = extract_oauth_info(mock_request)
assert token == "hf_test_token_123"
assert username == "testuser"
def test_extract_from_authorization_header(self) -> None:
"""Should extract token from Authorization header."""
mock_request = MagicMock()
mock_request.oauth_token = None
mock_request.headers = {"Authorization": "Bearer hf_test_token_123"}
mock_request.username = "testuser"
token, username = extract_oauth_info(mock_request)
assert token == "hf_test_token_123"
assert username == "testuser"
def test_extract_username_from_oauth_profile(self) -> None:
"""Should extract username from oauth_profile."""
mock_request = MagicMock()
mock_request.oauth_token = None
mock_request.username = None
mock_oauth_profile = MagicMock()
mock_oauth_profile.username = "testuser"
mock_request.oauth_profile = mock_oauth_profile
token, username = extract_oauth_info(mock_request)
assert username == "testuser"
def test_extract_name_from_oauth_profile(self) -> None:
"""Should extract name from oauth_profile when username not available."""
mock_request = MagicMock()
mock_request.oauth_token = None
# Ensure username attribute doesn't exist or is explicitly None
# Use delattr to remove it, then set oauth_profile
if hasattr(mock_request, "username"):
delattr(mock_request, "username")
mock_oauth_profile = MagicMock()
mock_oauth_profile.username = None
mock_oauth_profile.name = "Test User"
mock_request.oauth_profile = mock_oauth_profile
token, username = extract_oauth_info(mock_request)
assert username == "Test User"
def test_extract_none_request(self) -> None:
"""Should return None for both when request is None."""
token, username = extract_oauth_info(None)
assert token is None
assert username is None
def test_extract_no_oauth_info(self) -> None:
"""Should return None when no OAuth info is available."""
mock_request = MagicMock()
mock_request.oauth_token = None
mock_request.headers = {}
mock_request.username = None
mock_request.oauth_profile = None
token, username = extract_oauth_info(mock_request)
assert token is None
assert username is None
class TestUpdateModelProviderDropdowns:
"""Tests for update_model_provider_dropdowns function."""
@pytest.mark.asyncio
async def test_update_with_valid_token(self) -> None:
"""Should update dropdowns with available models and providers."""
mock_oauth_token = MagicMock()
mock_oauth_token.token = "hf_test_token_123"
mock_oauth_profile = MagicMock()
mock_validation_result = {
"is_valid": True,
"has_inference_api_scope": True,
"available_models": ["model1", "model2"],
"available_providers": ["auto", "nebius"],
"username": "testuser",
}
with patch("src.utils.hf_model_validator.validate_oauth_token", return_value=mock_validation_result) as mock_validate, \
patch("src.utils.hf_model_validator.get_available_models", new_callable=AsyncMock) as mock_get_models, \
patch("src.utils.hf_model_validator.get_available_providers", new_callable=AsyncMock) as mock_get_providers, \
patch("src.app.gr") as mock_gr, \
patch("src.app.logger"):
mock_get_models.return_value = ["model1", "model2"]
mock_get_providers.return_value = ["auto", "nebius"]
mock_gr.update.return_value = {"choices": [], "value": ""}
result = await update_model_provider_dropdowns(mock_oauth_token, mock_oauth_profile)
assert len(result) == 3 # model_update, provider_update, status_msg
assert "testuser" in result[2] # Status message should contain username
mock_validate.assert_called_once_with("hf_test_token_123")
@pytest.mark.asyncio
async def test_update_with_no_token(self) -> None:
"""Should return defaults when no token provided."""
with patch("src.app.gr") as mock_gr:
mock_gr.update.return_value = {"choices": [], "value": ""}
result = await update_model_provider_dropdowns(None, None)
assert len(result) == 3
assert "Not authenticated" in result[2] # Status message
@pytest.mark.asyncio
async def test_update_with_invalid_token(self) -> None:
"""Should return error message for invalid token."""
mock_oauth_token = MagicMock()
mock_oauth_token.token = "invalid_token"
mock_validation_result = {
"is_valid": False,
"error": "Invalid token format",
}
with patch("src.utils.hf_model_validator.validate_oauth_token", return_value=mock_validation_result), \
patch("src.app.gr") as mock_gr:
mock_gr.update.return_value = {"choices": [], "value": ""}
result = await update_model_provider_dropdowns(mock_oauth_token, None)
assert len(result) == 3
assert "Token validation failed" in result[2]
@pytest.mark.asyncio
async def test_update_without_inference_scope(self) -> None:
"""Should warn when token lacks inference-api scope."""
mock_oauth_token = MagicMock()
mock_oauth_token.token = "hf_token_without_scope"
mock_validation_result = {
"is_valid": True,
"has_inference_api_scope": False,
"available_models": [],
"available_providers": ["auto"],
"username": "testuser",
}
with patch("src.utils.hf_model_validator.validate_oauth_token", return_value=mock_validation_result), \
patch("src.utils.hf_model_validator.get_available_models", new_callable=AsyncMock) as mock_get_models, \
patch("src.utils.hf_model_validator.get_available_providers", new_callable=AsyncMock) as mock_get_providers, \
patch("src.app.gr") as mock_gr, \
patch("src.app.logger"):
mock_get_models.return_value = []
mock_get_providers.return_value = ["auto"]
mock_gr.update.return_value = {"choices": [], "value": ""}
result = await update_model_provider_dropdowns(mock_oauth_token, None)
assert len(result) == 3
assert "inference-api" in result[2] and "scope" in result[2]
@pytest.mark.asyncio
async def test_update_handles_exception(self) -> None:
"""Should handle exceptions gracefully."""
mock_oauth_token = MagicMock()
mock_oauth_token.token = "hf_test_token"
with patch("src.utils.hf_model_validator.validate_oauth_token", side_effect=Exception("API error")), \
patch("src.app.gr") as mock_gr, \
patch("src.app.logger"):
mock_gr.update.return_value = {"choices": [], "value": ""}
result = await update_model_provider_dropdowns(mock_oauth_token, None)
assert len(result) == 3
assert "Failed to load models" in result[2]
@pytest.mark.asyncio
async def test_update_with_string_token(self) -> None:
"""Should handle string token (edge case)."""
# Edge case: oauth_token is already a string
with patch("src.utils.hf_model_validator.validate_oauth_token") as mock_validate, \
patch("src.utils.hf_model_validator.get_available_models", new_callable=AsyncMock), \
patch("src.utils.hf_model_validator.get_available_providers", new_callable=AsyncMock), \
patch("src.app.gr") as mock_gr, \
patch("src.app.logger"):
mock_validation_result = {
"is_valid": True,
"has_inference_api_scope": True,
"available_models": ["model1"],
"available_providers": ["auto"],
"username": "testuser",
}
mock_validate.return_value = mock_validation_result
mock_gr.update.return_value = {"choices": [], "value": ""}
# Pass string directly (shouldn't happen but defensive)
result = await update_model_provider_dropdowns("hf_string_token", None)
# Should still work (extracts as string)
assert len(result) == 3
|