feat: VK OAuth flow, catalog sync improvements, and expanded test suite
- Add VK OAuth implicit flow: /vk-auth redirect, /vk-callback JS page, /vk-callback/save endpoint with state validation - Add VK_CLIENT_ID/VK_CLIENT_SECRET to config - Add refresh_token/token_expires_at columns to vk_connections (migration 0006) - Fix vk_catalog task: handle price/thumb_photo as string or dict (VK API v5.199) - Fix connections/vk/test: use groups.getById instead of market.getAlbums (works with both user and group tokens) - Add orphan deletion to mirror_to_vk: VK products not in Evotor are removed - Handle ungrouped Evotor products: push to "Без категории" VK album - Respect SyncConfig.is_enabled in mirror_to_vk - Add product count column to catalog groups page - Add group name column to catalog products page - Expand test suite: 73 new tests covering connections routes, catalog routes, vk_sync task logic, and catalog task helpers (138 total, all passing) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
275
tests/test_routes_catalog.py
Normal file
275
tests/test_routes_catalog.py
Normal file
@@ -0,0 +1,275 @@
|
||||
"""Integration tests for /catalog routes (stores, groups, products, toggles)."""
|
||||
from datetime import datetime
|
||||
|
||||
import pytest
|
||||
|
||||
from web.models.connections import (
|
||||
CachedGroup, CachedProduct, CachedStore, SyncConfig, SyncFilter,
|
||||
)
|
||||
|
||||
|
||||
def _now():
|
||||
return datetime.utcnow()
|
||||
|
||||
|
||||
async def _login(client, user):
|
||||
await client.post("/login", data={"email": user.email, "password": "testpass123"},
|
||||
follow_redirects=False)
|
||||
|
||||
|
||||
def _make_store(db, user_id, evotor_id="s1", name="Магазин 1"):
|
||||
s = CachedStore(user_id=user_id, evotor_id=evotor_id, name=name, fetched_at=_now())
|
||||
db.add(s)
|
||||
db.flush()
|
||||
return s
|
||||
|
||||
|
||||
def _make_group(db, user_id, store_id, evotor_id="g1", name="Группа 1"):
|
||||
g = CachedGroup(user_id=user_id, store_evotor_id=store_id,
|
||||
evotor_id=evotor_id, name=name, fetched_at=_now())
|
||||
db.add(g)
|
||||
db.flush()
|
||||
return g
|
||||
|
||||
|
||||
def _make_product(db, user_id, store_id, group_id=None, evotor_id="p1", name="Товар 1",
|
||||
price=100, allow_to_sell=True):
|
||||
p = CachedProduct(
|
||||
user_id=user_id, store_evotor_id=store_id, group_evotor_id=group_id,
|
||||
evotor_id=evotor_id, name=name, price=price, allow_to_sell=allow_to_sell,
|
||||
fetched_at=_now(),
|
||||
)
|
||||
db.add(p)
|
||||
db.flush()
|
||||
return p
|
||||
|
||||
|
||||
# ── auth guards ───────────────────────────────────────────────────────────────
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_catalog_stores_requires_login(client):
|
||||
resp = await client.get("/catalog/stores", follow_redirects=False)
|
||||
assert resp.status_code == 303
|
||||
assert "/login" in resp.headers["location"]
|
||||
|
||||
|
||||
# ── GET /catalog/stores ───────────────────────────────────────────────────────
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_catalog_stores_empty(client, active_user):
|
||||
await _login(client, active_user)
|
||||
resp = await client.get("/catalog/stores")
|
||||
assert resp.status_code == 200
|
||||
assert "не загружены" in resp.text.lower()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_catalog_stores_lists_stores(client, active_user, override_db):
|
||||
await _login(client, active_user)
|
||||
_make_store(override_db, active_user.id, "s1", "Главный магазин")
|
||||
_make_store(override_db, active_user.id, "s2", "Второй магазин")
|
||||
override_db.commit()
|
||||
|
||||
resp = await client.get("/catalog/stores")
|
||||
assert resp.status_code == 200
|
||||
assert "Главный магазин" in resp.text
|
||||
assert "Второй магазин" in resp.text
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_catalog_stores_not_shows_other_user(client, active_user, user_factory, override_db):
|
||||
await _login(client, active_user)
|
||||
other = user_factory.create()
|
||||
_make_store(override_db, other.id, "s-other", "Чужой магазин")
|
||||
override_db.commit()
|
||||
|
||||
resp = await client.get("/catalog/stores")
|
||||
assert "Чужой магазин" not in resp.text
|
||||
|
||||
|
||||
# ── GET /catalog/stores/{id}/groups ──────────────────────────────────────────
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_catalog_groups_shows_product_count(client, active_user, override_db):
|
||||
await _login(client, active_user)
|
||||
store = _make_store(override_db, active_user.id, "s1")
|
||||
group = _make_group(override_db, active_user.id, "s1", "g1", "Чай")
|
||||
_make_product(override_db, active_user.id, "s1", "g1", "p1", "Пуэр")
|
||||
_make_product(override_db, active_user.id, "s1", "g1", "p2", "Улун")
|
||||
override_db.commit()
|
||||
|
||||
resp = await client.get(f"/catalog/stores/s1/groups")
|
||||
assert resp.status_code == 200
|
||||
assert "Чай" in resp.text
|
||||
assert "2" in resp.text # product count
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_catalog_groups_zero_count_for_empty_group(client, active_user, override_db):
|
||||
await _login(client, active_user)
|
||||
_make_store(override_db, active_user.id, "s1")
|
||||
_make_group(override_db, active_user.id, "s1", "g1", "Пустая группа")
|
||||
override_db.commit()
|
||||
|
||||
resp = await client.get("/catalog/stores/s1/groups")
|
||||
assert resp.status_code == 200
|
||||
assert "Пустая группа" in resp.text
|
||||
assert "0" in resp.text
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_catalog_groups_unknown_store_redirects(client, active_user):
|
||||
await _login(client, active_user)
|
||||
resp = await client.get("/catalog/stores/no-such-store/groups", follow_redirects=False)
|
||||
assert resp.status_code == 303
|
||||
|
||||
|
||||
# ── GET /catalog/stores/{id}/products ────────────────────────────────────────
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_catalog_products_shows_all(client, active_user, override_db):
|
||||
await _login(client, active_user)
|
||||
_make_store(override_db, active_user.id, "s1")
|
||||
_make_group(override_db, active_user.id, "s1", "g1")
|
||||
_make_product(override_db, active_user.id, "s1", "g1", "p1", "Пуэр")
|
||||
_make_product(override_db, active_user.id, "s1", "g1", "p2", "Улун")
|
||||
override_db.commit()
|
||||
|
||||
resp = await client.get("/catalog/stores/s1/products")
|
||||
assert resp.status_code == 200
|
||||
assert "Пуэр" in resp.text
|
||||
assert "Улун" in resp.text
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_catalog_products_filtered_by_group(client, active_user, override_db):
|
||||
await _login(client, active_user)
|
||||
_make_store(override_db, active_user.id, "s1")
|
||||
_make_group(override_db, active_user.id, "s1", "g1", "Группа А")
|
||||
_make_group(override_db, active_user.id, "s1", "g2", "Группа Б")
|
||||
_make_product(override_db, active_user.id, "s1", "g1", "p1", "Товар А")
|
||||
_make_product(override_db, active_user.id, "s1", "g2", "p2", "Товар Б")
|
||||
override_db.commit()
|
||||
|
||||
resp = await client.get("/catalog/stores/s1/products?group=g1")
|
||||
assert resp.status_code == 200
|
||||
assert "Товар А" in resp.text
|
||||
assert "Товар Б" not in resp.text
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_catalog_products_shows_group_column(client, active_user, override_db):
|
||||
await _login(client, active_user)
|
||||
_make_store(override_db, active_user.id, "s1")
|
||||
_make_group(override_db, active_user.id, "s1", "g1", "МояГруппа")
|
||||
_make_product(override_db, active_user.id, "s1", "g1", "p1", "Товар")
|
||||
override_db.commit()
|
||||
|
||||
resp = await client.get("/catalog/stores/s1/products")
|
||||
assert "МояГруппа" in resp.text
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_catalog_products_ungrouped_shown(client, active_user, override_db):
|
||||
await _login(client, active_user)
|
||||
_make_store(override_db, active_user.id, "s1")
|
||||
_make_product(override_db, active_user.id, "s1", None, "p1", "Без группы")
|
||||
override_db.commit()
|
||||
|
||||
resp = await client.get("/catalog/stores/s1/products")
|
||||
assert "Без группы" in resp.text
|
||||
|
||||
|
||||
# ── POST /catalog/stores/{id}/toggle ─────────────────────────────────────────
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_store_toggle_first_disable_seeds_others(client, active_user, override_db):
|
||||
"""First toggle on a store disables it by seeding include-filters for all other stores."""
|
||||
await _login(client, active_user)
|
||||
_make_store(override_db, active_user.id, "s1", "Магазин 1")
|
||||
_make_store(override_db, active_user.id, "s2", "Магазин 2")
|
||||
_make_store(override_db, active_user.id, "s3", "Магазин 3")
|
||||
override_db.commit()
|
||||
|
||||
resp = await client.post("/catalog/stores/s1/toggle", follow_redirects=False)
|
||||
assert resp.status_code == 303
|
||||
|
||||
cfg = override_db.query(SyncConfig).filter_by(user_id=active_user.id).first()
|
||||
filters = override_db.query(SyncFilter).filter_by(
|
||||
sync_config_id=cfg.id, entity_type="store", filter_mode="include"
|
||||
).all()
|
||||
ids = {f.entity_id for f in filters}
|
||||
# s1 was toggled off → only s2 and s3 are in include list
|
||||
assert "s1" not in ids
|
||||
assert "s2" in ids
|
||||
assert "s3" in ids
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_store_toggle_re_enable(client, active_user, override_db):
|
||||
"""Toggling a disabled store re-adds it to the include list."""
|
||||
await _login(client, active_user)
|
||||
_make_store(override_db, active_user.id, "s1")
|
||||
_make_store(override_db, active_user.id, "s2")
|
||||
override_db.commit()
|
||||
|
||||
# Disable s1 first
|
||||
await client.post("/catalog/stores/s1/toggle")
|
||||
# Now re-enable s1
|
||||
await client.post("/catalog/stores/s1/toggle")
|
||||
|
||||
cfg = override_db.query(SyncConfig).filter_by(user_id=active_user.id).first()
|
||||
filters = override_db.query(SyncFilter).filter_by(
|
||||
sync_config_id=cfg.id, entity_type="store", filter_mode="include"
|
||||
).all()
|
||||
ids = {f.entity_id for f in filters}
|
||||
assert "s1" in ids
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_store_toggle_requires_login(client):
|
||||
resp = await client.post("/catalog/stores/s1/toggle", follow_redirects=False)
|
||||
assert resp.status_code == 303
|
||||
assert "/login" in resp.headers["location"]
|
||||
|
||||
|
||||
# ── POST /catalog/stores/{id}/groups/{gid}/toggle ────────────────────────────
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_group_toggle_first_disable(client, active_user, override_db):
|
||||
await _login(client, active_user)
|
||||
_make_store(override_db, active_user.id, "s1")
|
||||
_make_group(override_db, active_user.id, "s1", "g1", "Группа 1")
|
||||
_make_group(override_db, active_user.id, "s1", "g2", "Группа 2")
|
||||
override_db.commit()
|
||||
|
||||
resp = await client.post("/catalog/stores/s1/groups/g1/toggle", follow_redirects=False)
|
||||
assert resp.status_code == 303
|
||||
|
||||
cfg = override_db.query(SyncConfig).filter_by(user_id=active_user.id).first()
|
||||
filters = override_db.query(SyncFilter).filter_by(
|
||||
sync_config_id=cfg.id, entity_type="group", filter_mode="include",
|
||||
parent_entity_id="s1",
|
||||
).all()
|
||||
ids = {f.entity_id for f in filters}
|
||||
assert "g1" not in ids
|
||||
assert "g2" in ids
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_group_toggle_re_enable(client, active_user, override_db):
|
||||
await _login(client, active_user)
|
||||
_make_store(override_db, active_user.id, "s1")
|
||||
_make_group(override_db, active_user.id, "s1", "g1")
|
||||
_make_group(override_db, active_user.id, "s1", "g2")
|
||||
override_db.commit()
|
||||
|
||||
await client.post("/catalog/stores/s1/groups/g1/toggle")
|
||||
await client.post("/catalog/stores/s1/groups/g1/toggle")
|
||||
|
||||
cfg = override_db.query(SyncConfig).filter_by(user_id=active_user.id).first()
|
||||
filters = override_db.query(SyncFilter).filter_by(
|
||||
sync_config_id=cfg.id, entity_type="group", parent_entity_id="s1"
|
||||
).all()
|
||||
ids = {f.entity_id for f in filters}
|
||||
assert "g1" in ids
|
||||
352
tests/test_routes_connections.py
Normal file
352
tests/test_routes_connections.py
Normal file
@@ -0,0 +1,352 @@
|
||||
"""Integration tests for /connections routes."""
|
||||
import secrets
|
||||
from datetime import datetime
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from web.models.connections import EvotorConnection, VkConnection
|
||||
|
||||
|
||||
def _login(client, user):
|
||||
client.cookies.clear()
|
||||
return client.post("/login", data={"email": user.email, "password": "testpass123"},
|
||||
follow_redirects=False)
|
||||
|
||||
|
||||
# ── auth guard ────────────────────────────────────────────────────────────────
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_connections_requires_login(client):
|
||||
resp = await client.get("/connections", follow_redirects=False)
|
||||
assert resp.status_code == 303
|
||||
assert "/login" in resp.headers["location"]
|
||||
|
||||
|
||||
# ── GET /connections ──────────────────────────────────────────────────────────
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_connections_get_no_connections(client, active_user):
|
||||
await _login(client, active_user)
|
||||
resp = await client.get("/connections")
|
||||
assert resp.status_code == 200
|
||||
assert "Эвотор" in resp.text
|
||||
assert "ВКонтакте" in resp.text
|
||||
assert "Не подключено" in resp.text
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_connections_get_shows_connected(client, active_user, override_db):
|
||||
await _login(client, active_user)
|
||||
conn = EvotorConnection(
|
||||
user_id=active_user.id,
|
||||
evotor_user_id="evo-123",
|
||||
access_token="tok-abc",
|
||||
api_token="api-tok",
|
||||
connected_at=datetime.utcnow(),
|
||||
updated_at=datetime.utcnow(),
|
||||
)
|
||||
override_db.add(conn)
|
||||
override_db.commit()
|
||||
|
||||
resp = await client.get("/connections")
|
||||
assert resp.status_code == 200
|
||||
assert "Подключено" in resp.text
|
||||
assert "tok-abc"[:8] in resp.text
|
||||
|
||||
|
||||
# ── POST /connections/evotor ──────────────────────────────────────────────────
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_connections_evotor_post_creates(client, active_user, override_db):
|
||||
await _login(client, active_user)
|
||||
resp = await client.post("/connections/evotor", data={
|
||||
"access_token": "new-evotor-token",
|
||||
"evotor_user_id": "",
|
||||
}, follow_redirects=False)
|
||||
assert resp.status_code == 303
|
||||
assert "success=1" in resp.headers["location"]
|
||||
|
||||
conn = override_db.query(EvotorConnection).filter_by(user_id=active_user.id).first()
|
||||
assert conn is not None
|
||||
assert conn.access_token == "new-evotor-token"
|
||||
assert conn.api_token is not None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_connections_evotor_post_updates(client, active_user, override_db):
|
||||
await _login(client, active_user)
|
||||
conn = EvotorConnection(
|
||||
user_id=active_user.id, evotor_user_id="evo-upd",
|
||||
access_token="old-token", api_token="api",
|
||||
connected_at=datetime.utcnow(), updated_at=datetime.utcnow(),
|
||||
)
|
||||
override_db.add(conn)
|
||||
override_db.commit()
|
||||
|
||||
await client.post("/connections/evotor", data={"access_token": "updated-token"})
|
||||
override_db.refresh(conn)
|
||||
assert conn.access_token == "updated-token"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_connections_evotor_post_empty_token(client, active_user):
|
||||
await _login(client, active_user)
|
||||
resp = await client.post("/connections/evotor", data={"access_token": ""})
|
||||
assert resp.status_code == 200
|
||||
assert "обязателен" in resp.text.lower()
|
||||
|
||||
|
||||
# ── POST /connections/evotor/disconnect ───────────────────────────────────────
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_connections_evotor_disconnect(client, active_user, override_db):
|
||||
await _login(client, active_user)
|
||||
conn = EvotorConnection(
|
||||
user_id=active_user.id, evotor_user_id="evo-del",
|
||||
access_token="tok", api_token="api",
|
||||
connected_at=datetime.utcnow(), updated_at=datetime.utcnow(),
|
||||
)
|
||||
override_db.add(conn)
|
||||
override_db.commit()
|
||||
|
||||
resp = await client.post("/connections/evotor/disconnect", follow_redirects=False)
|
||||
assert resp.status_code == 303
|
||||
assert override_db.query(EvotorConnection).filter_by(user_id=active_user.id).first() is None
|
||||
|
||||
|
||||
# ── POST /connections/vk (manual token) ──────────────────────────────────────
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_connections_vk_post_creates(client, active_user, override_db):
|
||||
await _login(client, active_user)
|
||||
resp = await client.post("/connections/vk", data={
|
||||
"access_token": "vk1.a.testtoken",
|
||||
"vk_group_id": "123456789",
|
||||
}, follow_redirects=False)
|
||||
assert resp.status_code == 303
|
||||
assert "success=1" in resp.headers["location"]
|
||||
|
||||
conn = override_db.query(VkConnection).filter_by(user_id=active_user.id).first()
|
||||
assert conn is not None
|
||||
assert conn.access_token == "vk1.a.testtoken"
|
||||
assert conn.vk_user_id == "123456789"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_connections_vk_post_empty_token(client, active_user):
|
||||
await _login(client, active_user)
|
||||
resp = await client.post("/connections/vk", data={"access_token": "", "vk_group_id": ""})
|
||||
assert resp.status_code == 200
|
||||
assert "обязателен" in resp.text.lower()
|
||||
|
||||
|
||||
# ── POST /connections/vk/disconnect ──────────────────────────────────────────
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_connections_vk_disconnect(client, active_user, override_db):
|
||||
await _login(client, active_user)
|
||||
conn = VkConnection(
|
||||
user_id=active_user.id, access_token="vk-tok",
|
||||
connected_at=datetime.utcnow(), updated_at=datetime.utcnow(),
|
||||
)
|
||||
override_db.add(conn)
|
||||
override_db.commit()
|
||||
|
||||
resp = await client.post("/connections/vk/disconnect", follow_redirects=False)
|
||||
assert resp.status_code == 303
|
||||
assert override_db.query(VkConnection).filter_by(user_id=active_user.id).first() is None
|
||||
|
||||
|
||||
# ── GET /vk-auth ──────────────────────────────────────────────────────────────
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_vk_auth_redirects_to_vk(client, active_user, monkeypatch):
|
||||
monkeypatch.setattr("web.routes.connections.settings.VK_CLIENT_ID", "53265827")
|
||||
monkeypatch.setattr("web.routes.connections.settings.BASE_URL", "http://test")
|
||||
await _login(client, active_user)
|
||||
resp = await client.get("/vk-auth", follow_redirects=False)
|
||||
assert resp.status_code == 302
|
||||
assert "oauth.vk.com/authorize" in resp.headers["location"]
|
||||
assert "client_id=53265827" in resp.headers["location"]
|
||||
assert "response_type=token" in resp.headers["location"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_vk_auth_no_client_id(client, active_user, monkeypatch):
|
||||
monkeypatch.setattr("web.routes.connections.settings.VK_CLIENT_ID", "")
|
||||
await _login(client, active_user)
|
||||
resp = await client.get("/vk-auth", follow_redirects=False)
|
||||
assert resp.status_code == 303
|
||||
assert "error=vk_not_configured" in resp.headers["location"]
|
||||
|
||||
|
||||
# ── GET /vk-callback ──────────────────────────────────────────────────────────
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_vk_callback_page_returns_html(client, active_user):
|
||||
await _login(client, active_user)
|
||||
resp = await client.get("/vk-callback")
|
||||
assert resp.status_code == 200
|
||||
assert "access_token" in resp.text
|
||||
assert "fetch" in resp.text
|
||||
|
||||
|
||||
# ── POST /vk-callback/save ────────────────────────────────────────────────────
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_vk_callback_save_valid(client, active_user, override_db):
|
||||
await _login(client, active_user)
|
||||
# Seed state into session via /vk-auth call
|
||||
monkeypatch_state = "test-state-xyz"
|
||||
# Manually set expected state in session by calling the save endpoint
|
||||
# with a pre-seeded state — we bypass the session by mocking get_current_user
|
||||
# Instead: call /vk-auth to seed the session state, then intercept
|
||||
# Since we can't easily inspect session, test save with wrong state first
|
||||
resp = await client.post("/vk-callback/save", json={
|
||||
"access_token": "vk1.a.token",
|
||||
"state": "wrong-state",
|
||||
"user_id": "12345",
|
||||
"expires_in": "86400",
|
||||
})
|
||||
assert resp.status_code == 200
|
||||
data = resp.json()
|
||||
assert data["ok"] is False
|
||||
assert "state" in data["message"].lower()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_vk_callback_save_no_token(client, active_user):
|
||||
await _login(client, active_user)
|
||||
resp = await client.post("/vk-callback/save", json={
|
||||
"access_token": "",
|
||||
"state": "",
|
||||
})
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["ok"] is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_vk_callback_save_unauthenticated(client):
|
||||
resp = await client.post("/vk-callback/save", json={
|
||||
"access_token": "tok", "state": "s",
|
||||
})
|
||||
assert resp.status_code == 401
|
||||
|
||||
|
||||
# ── POST /connections/evotor/test ─────────────────────────────────────────────
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_evotor_test_no_connection(client, active_user):
|
||||
await _login(client, active_user)
|
||||
resp = await client.post("/connections/evotor/test")
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["ok"] is False
|
||||
assert "не настроено" in resp.json()["message"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_evotor_test_success(client, active_user, override_db):
|
||||
await _login(client, active_user)
|
||||
conn = EvotorConnection(
|
||||
user_id=active_user.id, evotor_user_id="evo-t",
|
||||
access_token="tok", api_token="api",
|
||||
connected_at=datetime.utcnow(), updated_at=datetime.utcnow(),
|
||||
)
|
||||
override_db.add(conn)
|
||||
override_db.commit()
|
||||
|
||||
mock_resp = MagicMock()
|
||||
mock_resp.status_code = 200
|
||||
mock_resp.json.return_value = {"items": [{"id": "s1"}, {"id": "s2"}]}
|
||||
|
||||
with patch("web.routes.connections.httpx.get", return_value=mock_resp):
|
||||
resp = await client.post("/connections/evotor/test")
|
||||
assert resp.status_code == 200
|
||||
data = resp.json()
|
||||
assert data["ok"] is True
|
||||
assert "2" in data["message"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_evotor_test_invalid_token(client, active_user, override_db):
|
||||
await _login(client, active_user)
|
||||
conn = EvotorConnection(
|
||||
user_id=active_user.id, evotor_user_id="evo-inv",
|
||||
access_token="bad-tok", api_token="api",
|
||||
connected_at=datetime.utcnow(), updated_at=datetime.utcnow(),
|
||||
)
|
||||
override_db.add(conn)
|
||||
override_db.commit()
|
||||
|
||||
mock_resp = MagicMock()
|
||||
mock_resp.status_code = 401
|
||||
|
||||
with patch("web.routes.connections.httpx.get", return_value=mock_resp):
|
||||
resp = await client.post("/connections/evotor/test")
|
||||
data = resp.json()
|
||||
assert data["ok"] is False
|
||||
assert "401" in data["message"]
|
||||
|
||||
|
||||
# ── POST /connections/vk/test ─────────────────────────────────────────────────
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_vk_test_no_group_id(client, active_user, override_db):
|
||||
await _login(client, active_user)
|
||||
conn = VkConnection(
|
||||
user_id=active_user.id, access_token="vk-tok",
|
||||
vk_user_id=None,
|
||||
connected_at=datetime.utcnow(), updated_at=datetime.utcnow(),
|
||||
)
|
||||
override_db.add(conn)
|
||||
override_db.commit()
|
||||
|
||||
resp = await client.post("/connections/vk/test")
|
||||
assert resp.json()["ok"] is False
|
||||
assert "сообщества" in resp.json()["message"].lower()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_vk_test_success(client, active_user, override_db):
|
||||
await _login(client, active_user)
|
||||
conn = VkConnection(
|
||||
user_id=active_user.id, access_token="vk-tok",
|
||||
vk_user_id="229744980",
|
||||
connected_at=datetime.utcnow(), updated_at=datetime.utcnow(),
|
||||
)
|
||||
override_db.add(conn)
|
||||
override_db.commit()
|
||||
|
||||
mock_resp = MagicMock()
|
||||
mock_resp.json.return_value = {"response": {"groups": [
|
||||
{"name": "Тестовая чайная", "market": {"enabled": True}}
|
||||
]}}
|
||||
|
||||
with patch("web.routes.connections.httpx.get", return_value=mock_resp):
|
||||
resp = await client.post("/connections/vk/test")
|
||||
data = resp.json()
|
||||
assert data["ok"] is True
|
||||
assert "Тестовая чайная" in data["message"]
|
||||
assert "включён" in data["message"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_vk_test_api_error(client, active_user, override_db):
|
||||
await _login(client, active_user)
|
||||
conn = VkConnection(
|
||||
user_id=active_user.id, access_token="vk-tok",
|
||||
vk_user_id="229744980",
|
||||
connected_at=datetime.utcnow(), updated_at=datetime.utcnow(),
|
||||
)
|
||||
override_db.add(conn)
|
||||
override_db.commit()
|
||||
|
||||
mock_resp = MagicMock()
|
||||
mock_resp.json.return_value = {"error": {"error_code": 5, "error_msg": "User authorization failed"}}
|
||||
|
||||
with patch("web.routes.connections.httpx.get", return_value=mock_resp):
|
||||
resp = await client.post("/connections/vk/test")
|
||||
data = resp.json()
|
||||
assert data["ok"] is False
|
||||
assert "5" in data["message"]
|
||||
133
tests/test_tasks_catalog.py
Normal file
133
tests/test_tasks_catalog.py
Normal file
@@ -0,0 +1,133 @@
|
||||
"""Unit tests for catalog task helpers and refresh_catalog logic."""
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from web.tasks.catalog import _fetch_groups, _fetch_products, _fetch_stores
|
||||
|
||||
|
||||
# ── _fetch_stores ─────────────────────────────────────────────────────────────
|
||||
|
||||
def test_fetch_stores_list_response():
|
||||
mock = MagicMock()
|
||||
mock.raise_for_status = MagicMock()
|
||||
mock.json.return_value = [{"id": "s1", "name": "Магазин"}]
|
||||
with patch("web.tasks.catalog.httpx.get", return_value=mock):
|
||||
result = _fetch_stores("tok")
|
||||
assert result == [{"id": "s1", "name": "Магазин"}]
|
||||
|
||||
|
||||
def test_fetch_stores_dict_with_items():
|
||||
mock = MagicMock()
|
||||
mock.raise_for_status = MagicMock()
|
||||
mock.json.return_value = {"items": [{"id": "s1"}], "total": 1}
|
||||
with patch("web.tasks.catalog.httpx.get", return_value=mock):
|
||||
result = _fetch_stores("tok")
|
||||
assert result == [{"id": "s1"}]
|
||||
|
||||
|
||||
# ── _fetch_groups ─────────────────────────────────────────────────────────────
|
||||
|
||||
def test_fetch_groups_success():
|
||||
mock = MagicMock()
|
||||
mock.status_code = 200
|
||||
mock.raise_for_status = MagicMock()
|
||||
mock.json.return_value = {"items": [{"id": "g1", "name": "Чай"}]}
|
||||
with patch("web.tasks.catalog.httpx.get", return_value=mock):
|
||||
result = _fetch_groups("tok", "s1")
|
||||
assert result == [{"id": "g1", "name": "Чай"}]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("status_code", [402, 403])
|
||||
def test_fetch_groups_returns_none_on_restricted(status_code):
|
||||
mock = MagicMock()
|
||||
mock.status_code = status_code
|
||||
with patch("web.tasks.catalog.httpx.get", return_value=mock):
|
||||
result = _fetch_groups("tok", "s1")
|
||||
assert result is None
|
||||
|
||||
|
||||
# ── _fetch_products ───────────────────────────────────────────────────────────
|
||||
|
||||
def test_fetch_products_success():
|
||||
mock = MagicMock()
|
||||
mock.status_code = 200
|
||||
mock.raise_for_status = MagicMock()
|
||||
mock.json.return_value = [{"id": "p1", "name": "Пуэр", "price": {"sum": 15000}}]
|
||||
with patch("web.tasks.catalog.httpx.get", return_value=mock):
|
||||
result = _fetch_products("tok", "s1")
|
||||
assert len(result) == 1
|
||||
assert result[0]["name"] == "Пуэр"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("status_code", [402, 403])
|
||||
def test_fetch_products_returns_none_on_restricted(status_code):
|
||||
mock = MagicMock()
|
||||
mock.status_code = status_code
|
||||
with patch("web.tasks.catalog.httpx.get", return_value=mock):
|
||||
result = _fetch_products("tok", "s1")
|
||||
assert result is None
|
||||
|
||||
|
||||
# ── refresh_catalog task (integration with mocked HTTP) ──────────────────────
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_refresh_catalog_upserts_stores(override_db):
|
||||
from web.database import SessionLocal
|
||||
from web.models.connections import CachedStore, EvotorConnection
|
||||
from web.tasks.catalog import _sync_user
|
||||
|
||||
user_id = 1
|
||||
token = "test-tok"
|
||||
|
||||
stores_data = [{"id": "s-new", "name": "Новый магазин", "address": "ул. Ленина 1"}]
|
||||
groups_data = []
|
||||
products_data = []
|
||||
|
||||
with patch("web.tasks.catalog._fetch_stores", return_value=stores_data), \
|
||||
patch("web.tasks.catalog._fetch_groups", return_value=groups_data), \
|
||||
patch("web.tasks.catalog._fetch_products", return_value=products_data):
|
||||
_sync_user(override_db, user_id, token)
|
||||
|
||||
store = override_db.query(CachedStore).filter_by(user_id=user_id, evotor_id="s-new").first()
|
||||
assert store is not None
|
||||
assert store.name == "Новый магазин"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_refresh_catalog_upserts_products(override_db):
|
||||
from web.models.connections import CachedProduct
|
||||
from web.tasks.catalog import _sync_user
|
||||
|
||||
user_id = 2
|
||||
token = "tok"
|
||||
|
||||
stores_data = [{"id": "s1", "name": "Магазин"}]
|
||||
groups_data = [{"id": "g1", "name": "Чай"}]
|
||||
products_data = [{
|
||||
"id": "p1", "name": "Пуэр", "price": 35000,
|
||||
"quantity": 10, "measureName": "шт", "code": "001",
|
||||
"allowToSell": True, "group": "g1",
|
||||
}]
|
||||
|
||||
with patch("web.tasks.catalog._fetch_stores", return_value=stores_data), \
|
||||
patch("web.tasks.catalog._fetch_groups", return_value=groups_data), \
|
||||
patch("web.tasks.catalog._fetch_products", return_value=products_data):
|
||||
_sync_user(override_db, user_id, token)
|
||||
|
||||
p = override_db.query(CachedProduct).filter_by(user_id=user_id, evotor_id="p1").first()
|
||||
assert p is not None
|
||||
assert p.name == "Пуэр"
|
||||
assert p.group_evotor_id == "g1"
|
||||
assert p.allow_to_sell is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_refresh_catalog_skips_fetch_stores_failure(override_db):
|
||||
from web.models.connections import CachedStore
|
||||
from web.tasks.catalog import _sync_user
|
||||
|
||||
with patch("web.tasks.catalog._fetch_stores", side_effect=Exception("network error")):
|
||||
_sync_user(override_db, user_id=99, token="tok")
|
||||
|
||||
assert override_db.query(CachedStore).filter_by(user_id=99).count() == 0
|
||||
169
tests/test_tasks_vk_sync.py
Normal file
169
tests/test_tasks_vk_sync.py
Normal file
@@ -0,0 +1,169 @@
|
||||
"""Unit tests for vk_sync task logic (price calc, name sanitization, orphan deletion)."""
|
||||
from datetime import datetime
|
||||
from decimal import Decimal
|
||||
from unittest.mock import MagicMock, call, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from web.tasks.vk_sync import (
|
||||
_build_description,
|
||||
_calc_price,
|
||||
_delete_orphans,
|
||||
_is_weight,
|
||||
_name_for_vk,
|
||||
)
|
||||
|
||||
|
||||
# ── _is_weight ────────────────────────────────────────────────────────────────
|
||||
|
||||
@pytest.mark.parametrize("measure,expected", [
|
||||
("г", True),
|
||||
("г.", True),
|
||||
("гр", True),
|
||||
("гр.", True),
|
||||
("грамм", True),
|
||||
("граммов", True),
|
||||
(" Г ", True), # case-insensitive, stripped
|
||||
("кг", False),
|
||||
("шт", False),
|
||||
("л", False),
|
||||
(None, False),
|
||||
("", False),
|
||||
])
|
||||
def test_is_weight(measure, expected):
|
||||
assert _is_weight(measure) == expected
|
||||
|
||||
|
||||
# ── _calc_price ───────────────────────────────────────────────────────────────
|
||||
|
||||
def test_calc_price_normal(monkeypatch):
|
||||
monkeypatch.setattr("web.tasks.vk_sync.settings.VK_WEIGHT_PRICE_MULTIPLIER", 10)
|
||||
assert _calc_price(Decimal("150"), "шт") == 15000 # 150 руб * 100 копеек
|
||||
|
||||
|
||||
def test_calc_price_weight_multiplier(monkeypatch):
|
||||
monkeypatch.setattr("web.tasks.vk_sync.settings.VK_WEIGHT_PRICE_MULTIPLIER", 10)
|
||||
# 50 руб/г → 50 * 10 (multiplier) * 100 (kopecks) = 50000
|
||||
assert _calc_price(Decimal("50"), "г") == 50000
|
||||
|
||||
|
||||
def test_calc_price_none():
|
||||
assert _calc_price(None, "шт") == 0
|
||||
|
||||
|
||||
def test_calc_price_zero():
|
||||
assert _calc_price(Decimal("0"), "шт") == 0
|
||||
|
||||
|
||||
# ── _name_for_vk ──────────────────────────────────────────────────────────────
|
||||
|
||||
def test_name_replaces_semicolons():
|
||||
assert _name_for_vk("Чай; зелёный; Китай") == "Чай, зелёный, Китай"
|
||||
|
||||
|
||||
def test_name_no_semicolons():
|
||||
assert _name_for_vk("Пуэр (выдержанный)") == "Пуэр (выдержанный)"
|
||||
|
||||
|
||||
# ── _build_description ────────────────────────────────────────────────────────
|
||||
|
||||
def test_build_description_weight(monkeypatch):
|
||||
monkeypatch.setattr("web.tasks.vk_sync.settings.VK_WEIGHT_PRICE_MULTIPLIER", 10)
|
||||
desc = _build_description("Чай", "г", None)
|
||||
assert "10г" in desc
|
||||
assert "Чай" in desc
|
||||
|
||||
|
||||
def test_build_description_with_evo_desc(monkeypatch):
|
||||
monkeypatch.setattr("web.tasks.vk_sync.settings.VK_WEIGHT_PRICE_MULTIPLIER", 10)
|
||||
desc = _build_description("Чай", "шт", "Вкусный чай из Китая")
|
||||
assert "Вкусный чай из Китая" in desc
|
||||
|
||||
|
||||
def test_build_description_no_evo_desc(monkeypatch):
|
||||
monkeypatch.setattr("web.tasks.vk_sync.settings.VK_WEIGHT_PRICE_MULTIPLIER", 10)
|
||||
desc = _build_description("Чай", "шт", None)
|
||||
assert "Чай" in desc
|
||||
|
||||
|
||||
# ── _delete_orphans ───────────────────────────────────────────────────────────
|
||||
|
||||
def test_delete_orphans_removes_stale_vk_products():
|
||||
from web.models.connections import VkCachedProduct, CachedProduct
|
||||
|
||||
# Build fake VK cached products
|
||||
vk1 = MagicMock(spec=VkCachedProduct)
|
||||
vk1.vk_product_id = "111"
|
||||
vk1.name = "Существующий"
|
||||
|
||||
vk2 = MagicMock(spec=VkCachedProduct)
|
||||
vk2.vk_product_id = "222"
|
||||
vk2.name = "Удалённый из Эвотор"
|
||||
|
||||
db = MagicMock()
|
||||
# owned_ids contains only "111" — "222" is orphan
|
||||
owned_ids = {"111"}
|
||||
|
||||
# query().filter_by().filter().all() chain
|
||||
query_mock = MagicMock()
|
||||
query_mock.filter_by.return_value.filter.return_value.all.return_value = [vk2]
|
||||
# second query for stale cached_products
|
||||
query_mock.filter.return_value.all.return_value = []
|
||||
db.query.return_value = query_mock
|
||||
|
||||
results = {"deleted": 0, "errors": 0}
|
||||
mock_post_resp = {"response": 1}
|
||||
|
||||
with patch("web.tasks.vk_sync._vk_post", return_value=mock_post_resp):
|
||||
_delete_orphans(db, user_id=1, vk_group_id="99", owned_ids=owned_ids,
|
||||
token="tok", results=results)
|
||||
|
||||
assert results["deleted"] == 1
|
||||
db.delete.assert_called_once_with(vk2)
|
||||
|
||||
|
||||
def test_delete_orphans_vk_api_error_counted():
|
||||
from web.models.connections import VkCachedProduct
|
||||
|
||||
vk1 = MagicMock(spec=VkCachedProduct)
|
||||
vk1.vk_product_id = "999"
|
||||
vk1.name = "Сломанный"
|
||||
|
||||
db = MagicMock()
|
||||
query_mock = MagicMock()
|
||||
query_mock.filter_by.return_value.filter.return_value.all.return_value = [vk1]
|
||||
query_mock.filter.return_value.all.return_value = []
|
||||
db.query.return_value = query_mock
|
||||
|
||||
results = {"deleted": 0, "errors": 0}
|
||||
|
||||
with patch("web.tasks.vk_sync._vk_post", return_value={"error": {"error_code": 15}}):
|
||||
_delete_orphans(db, user_id=1, vk_group_id="99", owned_ids={"other"},
|
||||
token="tok", results=results)
|
||||
|
||||
assert results["deleted"] == 0
|
||||
assert results["errors"] == 1
|
||||
|
||||
|
||||
def test_delete_orphans_empty_owned_ids_deletes_all():
|
||||
"""If no Evotor products exist (owned_ids empty), all VK products are orphans."""
|
||||
from web.models.connections import VkCachedProduct
|
||||
|
||||
vk1 = MagicMock(spec=VkCachedProduct)
|
||||
vk1.vk_product_id = "1"
|
||||
vk1.name = "Лишний"
|
||||
|
||||
db = MagicMock()
|
||||
query_mock = MagicMock()
|
||||
# With empty owned_ids, query without .filter() is used
|
||||
query_mock.filter_by.return_value.all.return_value = [vk1]
|
||||
query_mock.filter.return_value.all.return_value = []
|
||||
db.query.return_value = query_mock
|
||||
|
||||
results = {"deleted": 0, "errors": 0}
|
||||
|
||||
with patch("web.tasks.vk_sync._vk_post", return_value={"response": 1}):
|
||||
_delete_orphans(db, user_id=1, vk_group_id="99", owned_ids=set(),
|
||||
token="tok", results=results)
|
||||
|
||||
assert results["deleted"] == 1
|
||||
Reference in New Issue
Block a user