Files
archived-MoviePilot/tests/test_site_cookie_endpoint.py
jxxghp 16ada1a6c4 feat(site): add POST /site/cookie/{site_id} endpoint for updating site Cookie&UA via request body
- Introduce SiteCookieUpdate schema for structured cookie update requests
- Add POST endpoint to update site Cookie&UA using JSON body
- Refactor cookie update logic into shared _update_site_cookie function
- Update SKILL.md to document new endpoint and endpoint count
- Add tests for both POST and legacy GET cookie update endpoints
2026-05-31 09:11:09 +08:00

65 lines
1.9 KiB
Python

from types import SimpleNamespace
from unittest.mock import Mock, patch
from app import schemas
from app.api.endpoints import site as site_endpoint
def test_update_cookie_by_body_uses_request_body():
"""
POST 更新站点 Cookie 时应从请求体读取登录参数。
"""
fake_site = SimpleNamespace(id=1, name="TestSite")
fake_chain = Mock()
fake_chain.update_cookie.return_value = (True, "ok")
request = schemas.SiteCookieUpdate(username="user", password="password", code="123456")
with patch.object(site_endpoint.Site, "get", return_value=fake_site), patch.object(
site_endpoint, "SiteChain", return_value=fake_chain
):
response = site_endpoint.update_cookie_by_body(
site_id=1,
site_cookie_update=request,
db=Mock(),
_=Mock(),
)
assert response.success is True
assert response.message == "ok"
fake_chain.update_cookie.assert_called_once_with(
site_info=fake_site,
username="user",
password="password",
two_step_code="123456",
)
def test_update_cookie_legacy_get_keeps_query_params():
"""
旧 GET 入口仍应兼容查询参数更新站点 Cookie。
"""
fake_site = SimpleNamespace(id=1, name="TestSite")
fake_chain = Mock()
fake_chain.update_cookie.return_value = (False, "failed")
with patch.object(site_endpoint.Site, "get", return_value=fake_site), patch.object(
site_endpoint, "SiteChain", return_value=fake_chain
):
response = site_endpoint.update_cookie(
site_id=1,
username="user",
password="password",
code=None,
db=Mock(),
_=Mock(),
)
assert response.success is False
assert response.message == "failed"
fake_chain.update_cookie.assert_called_once_with(
site_info=fake_site,
username="user",
password="password",
two_step_code=None,
)