mirror of
https://github.com/willmiao/ComfyUI-Lora-Manager.git
synced 2026-05-06 08:26:45 -03:00
fix(civitai): use red-only api host (#897)
This commit is contained in:
@@ -94,7 +94,7 @@ class DummyDoctorScanner:
|
||||
|
||||
class DummyCivitaiClient:
|
||||
def __init__(self, *, success=True, result=None):
|
||||
self.base_url = 'https://civitai.com/api/v1'
|
||||
self.base_url = 'https://civitai.red/api/v1'
|
||||
self._success = success
|
||||
self._result = result if result is not None else {'items': []}
|
||||
|
||||
|
||||
@@ -62,6 +62,12 @@ async def test_download_file_uses_downloader(tmp_path, downloader):
|
||||
assert downloader.download_calls[0]["use_auth"] is True
|
||||
|
||||
|
||||
async def test_client_defaults_to_red_api_host(downloader):
|
||||
client = await CivitaiClient.get_instance()
|
||||
|
||||
assert client.base_url == "https://civitai.red/api/v1"
|
||||
|
||||
|
||||
async def test_get_model_by_hash_enriches_metadata(monkeypatch, downloader):
|
||||
version_payload = {
|
||||
"modelId": 123,
|
||||
@@ -551,36 +557,12 @@ async def test_get_image_info_prefers_red_host_for_red_source(monkeypatch, downl
|
||||
]
|
||||
|
||||
|
||||
async def test_get_image_info_falls_back_from_com_to_red(monkeypatch, downloader):
|
||||
async def test_get_image_info_uses_red_host_even_for_red_source(monkeypatch, downloader):
|
||||
requested_urls = []
|
||||
|
||||
async def fake_make_request(method, url, use_auth=True, **kwargs):
|
||||
requested_urls.append(url)
|
||||
if url.startswith("https://civitai.com/"):
|
||||
return True, {"items": []}
|
||||
return True, {"items": [{"id": 124950237, "name": "fallback"}]}
|
||||
|
||||
downloader.make_request = fake_make_request
|
||||
|
||||
client = await CivitaiClient.get_instance()
|
||||
|
||||
result = await client.get_image_info("124950237")
|
||||
|
||||
assert result == {"id": 124950237, "name": "fallback"}
|
||||
assert requested_urls == [
|
||||
"https://civitai.com/api/v1/images?imageId=124950237&nsfw=X",
|
||||
"https://civitai.red/api/v1/images?imageId=124950237&nsfw=X",
|
||||
]
|
||||
|
||||
|
||||
async def test_get_image_info_falls_back_from_red_to_com(monkeypatch, downloader):
|
||||
requested_urls = []
|
||||
|
||||
async def fake_make_request(method, url, use_auth=True, **kwargs):
|
||||
requested_urls.append(url)
|
||||
if url.startswith("https://civitai.red/"):
|
||||
return True, {"items": []}
|
||||
return True, {"items": [{"id": 124950237, "name": "fallback"}]}
|
||||
return True, {"items": [{"id": 124950237, "name": "target"}]}
|
||||
|
||||
downloader.make_request = fake_make_request
|
||||
|
||||
@@ -590,21 +572,18 @@ async def test_get_image_info_falls_back_from_red_to_com(monkeypatch, downloader
|
||||
"124950237", source_url="https://civitai.red/images/124950237"
|
||||
)
|
||||
|
||||
assert result == {"id": 124950237, "name": "fallback"}
|
||||
assert result == {"id": 124950237, "name": "target"}
|
||||
assert requested_urls == [
|
||||
"https://civitai.red/api/v1/images?imageId=124950237&nsfw=X",
|
||||
"https://civitai.com/api/v1/images?imageId=124950237&nsfw=X",
|
||||
]
|
||||
|
||||
|
||||
async def test_get_image_info_falls_back_after_request_failure(monkeypatch, downloader):
|
||||
async def test_get_image_info_does_not_fall_back_after_request_failure(monkeypatch, downloader):
|
||||
requested_urls = []
|
||||
|
||||
async def fake_make_request(method, url, use_auth=True, **kwargs):
|
||||
requested_urls.append(url)
|
||||
if url.startswith("https://civitai.red/"):
|
||||
return False, "403 forbidden"
|
||||
return True, {"items": [{"id": 124950237, "name": "fallback"}]}
|
||||
return False, "403 forbidden"
|
||||
|
||||
downloader.make_request = fake_make_request
|
||||
|
||||
@@ -614,10 +593,9 @@ async def test_get_image_info_falls_back_after_request_failure(monkeypatch, down
|
||||
"124950237", source_url="https://civitai.red/images/124950237"
|
||||
)
|
||||
|
||||
assert result == {"id": 124950237, "name": "fallback"}
|
||||
assert result is None
|
||||
assert requested_urls == [
|
||||
"https://civitai.red/api/v1/images?imageId=124950237&nsfw=X",
|
||||
"https://civitai.com/api/v1/images?imageId=124950237&nsfw=X",
|
||||
]
|
||||
|
||||
|
||||
|
||||
@@ -7,7 +7,10 @@ from unittest.mock import AsyncMock
|
||||
|
||||
import pytest
|
||||
|
||||
from py.services.download_manager import DownloadManager
|
||||
from py.services.download_manager import (
|
||||
CIVITAI_DOWNLOAD_URL_PREFIXES,
|
||||
DownloadManager,
|
||||
)
|
||||
from py.services import download_manager
|
||||
from py.services.service_registry import ServiceRegistry
|
||||
from py.services.settings_manager import SettingsManager, get_settings_manager
|
||||
@@ -309,6 +312,67 @@ async def test_execute_download_respects_blur_setting(monkeypatch, tmp_path):
|
||||
assert stored_preview and stored_preview.endswith(".jpeg")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_execute_download_uses_auth_for_red_civitai_downloads(monkeypatch, tmp_path):
|
||||
manager = DownloadManager()
|
||||
save_dir = tmp_path / "downloads"
|
||||
save_dir.mkdir()
|
||||
target_path = save_dir / "file.safetensors"
|
||||
|
||||
class DummyMetadata:
|
||||
def __init__(self, path: Path):
|
||||
self.file_path = str(path)
|
||||
self.sha256 = "sha256"
|
||||
self.file_name = path.stem
|
||||
self.preview_url = None
|
||||
self.preview_nsfw_level = None
|
||||
|
||||
def generate_unique_filename(self, *_args, **_kwargs):
|
||||
return os.path.basename(self.file_path)
|
||||
|
||||
def update_file_info(self, _path):
|
||||
return None
|
||||
|
||||
def to_dict(self):
|
||||
return {"file_path": self.file_path}
|
||||
|
||||
metadata = DummyMetadata(target_path)
|
||||
recorded_use_auth = []
|
||||
|
||||
class DummyDownloader:
|
||||
stall_timeout = None
|
||||
|
||||
async def download_file(self, url, path, progress_callback=None, use_auth=None, **_kwargs):
|
||||
recorded_use_auth.append((url, use_auth))
|
||||
Path(path).write_bytes(b"model")
|
||||
return True, None
|
||||
|
||||
monkeypatch.setattr(
|
||||
download_manager, "get_downloader", AsyncMock(return_value=DummyDownloader())
|
||||
)
|
||||
monkeypatch.setattr(MetadataManager, "save_metadata", AsyncMock(return_value=True))
|
||||
|
||||
dummy_scanner = SimpleNamespace(add_model_to_cache=AsyncMock(return_value=None))
|
||||
monkeypatch.setattr(
|
||||
DownloadManager, "_get_lora_scanner", AsyncMock(return_value=dummy_scanner)
|
||||
)
|
||||
|
||||
result = await manager._execute_download(
|
||||
download_urls=["https://civitai.red/api/download/models/119514"],
|
||||
save_dir=str(save_dir),
|
||||
metadata=metadata,
|
||||
version_info={"images": []},
|
||||
relative_path="",
|
||||
progress_callback=None,
|
||||
model_type="lora",
|
||||
download_id=None,
|
||||
)
|
||||
|
||||
assert result == {"success": True}
|
||||
assert recorded_use_auth == [("https://civitai.red/api/download/models/119514", True)]
|
||||
assert "https://civitai.red/api/download/".startswith(CIVITAI_DOWNLOAD_URL_PREFIXES)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_civarchive_source_uses_civarchive_provider(
|
||||
monkeypatch, scanners, tmp_path
|
||||
|
||||
Reference in New Issue
Block a user