import pytest from http import HTTPStatus as http_status from server.db.models import Account, Favorites from server.db.session import db_session as session_scope from tests.utils import check_object, create_accounts from tests.factories import FavoritesFactory @pytest.mark.parametrize( "count,is_active,data,headers,status,is_created", ( (1, True, {}, {}, http_status.BAD_REQUEST, None), (1, True, {"user_id": "user2"}, {"X-App-Slug": "app7"}, http_status.NOT_FOUND, None), (1, True, {"user_id": "user2", "get_or_create": "false"}, {"X-App-Slug": "app1"}, http_status.NOT_FOUND, None), (1, True, {"user_id": "user2", "get_or_create": "true"}, {"X-App-Slug": "app1"}, http_status.OK, True), (1, True, {"user_id": "user1", "get_or_create": "true"}, {"X-App-Slug": "app1"}, http_status.OK, False), (1, False, {"user_id": "user1"}, {"X-App-Slug": "app1"}, http_status.OK, False), (2, False, {"user_id": "user1"}, {"X-App-Slug": "app1"}, http_status.OK, False), ) ) async def test_account_get_or_create( count: int, is_active: bool, data: dict, headers: dict, status: int, is_created: bool, db_session, client, auth ): async with session_scope() as session: await create_accounts(session, count=1, apps_count=2, is_active=is_active) if count > 1: await create_accounts(session, count=count, is_active=True, with_apps=False) headers.update(auth) response = await client.get("/api/v2/accounts/", headers=headers, params=data) assert response.status == status if status != http_status.OK: return result = await response.json() assert "id" in result assert result["app_slug"] == headers["X-App-Slug"] assert result["user_id"] == data["user_id"] assert result["is_created"] == is_created get_id = result["id"] async with session_scope(): account = await Account.get(id=get_id) check_object(account, result, exclude=("is_created",), is_active=True) @pytest.mark.parametrize( "data,headers,status", ( ({}, {}, http_status.BAD_REQUEST), ( { "account_id": 1, "entity_type": "unknown", "entity_id": "1" }, {"X-App-Slug": "app1"}, http_status.BAD_REQUEST, ), ( { "account_id": 1, "entity_type": "genre", "entity_id": "1" }, {"X-App-Slug": "app7"}, http_status.NOT_FOUND, ), ( { "account_id": 2, "entity_type": "genre", "entity_id": "1" }, {"X-App-Slug": "app1"}, http_status.NOT_FOUND, ), ( { "account_id": 1, "entity_type": "country", "entity_id": "us" }, {"X-App-Slug": "app1"}, http_status.BAD_REQUEST, ), ( { "account_id": 1, "entity_type": "genre", "entity_id": "rock", "data": {"type": "music genre"} }, {"X-App-Slug": "app1"}, http_status.CREATED, ) ) ) async def test_add_account_favorites(data, headers, status, db_session, client, auth): async with session_scope() as session: await create_accounts(session, count=1, apps_count=2) existing_favorite = { "account_id": 1, "entity_type": "country", "entity_id": "us", "data": {"code": "us"} } session.add(FavoritesFactory.create(**existing_favorite)) headers.update(auth) response = await client.post( "/api/v2/accounts/favorites/", headers=headers, json=data) assert response.status == status if status == http_status.CREATED: result = await response.json() assert "id" in result for k in ("account_id", "entity_type", "entity_id"): assert result[k] == data[k] assert data.get("data") == result["data"] _id = result["id"] async with session_scope(): favorite = await Favorites.get(id=_id) check_object(favorite, data) elif status != http_status.BAD_REQUEST: data.pop("data", None) async with session_scope(): favorite = await Favorites.get(**data) assert favorite is None @pytest.mark.parametrize( "data,headers,status", ( ({}, {}, http_status.BAD_REQUEST), ( { "account_id": 1, "entity_type": "unknown", "entity_id": "1" }, {"X-App-Slug": "app1"}, http_status.BAD_REQUEST, ), ( { "account_id": 1, "entity_type": "genre", "entity_id": "1" }, {"X-App-Slug": "app7"}, http_status.NOT_FOUND, ), ( { "account_id": 2, "entity_type": "genre", "entity_id": "1" }, {"X-App-Slug": "app1"}, http_status.NOT_FOUND, ), ( { "account_id": 1, "entity_type": "country", "entity_id": "us" }, {"X-App-Slug": "app1"}, http_status.NO_CONTENT, ), ) ) async def test_remove_account_favorites(data, headers, status, db_session, client, auth): async with session_scope() as session: await create_accounts(session, count=1, apps_count=2) existing_favorite = { "account_id": 1, "entity_type": "country", "entity_id": "us", "data": {"code": "us"} } session.add(FavoritesFactory.create(**existing_favorite)) headers.update(auth) response = await client.delete( "/api/v2/accounts/favorites/", headers=headers, json=data) assert response.status == status favorites_count = await Favorites.count() assert (status == http_status.NO_CONTENT) ^ (favorites_count > 0)