File size: 6,637 Bytes
bf34e9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
测试用户收藏和播放历史功能
"""
import pytest


@pytest.mark.asyncio
async def test_add_favorite(client, user_token, db_session):
    from app.models.vod import Vod
    from app.models.category import Category

    cat = Category(name="电影", sort_order=1, is_active=True)
    db_session.add(cat)
    await db_session.flush()
    vod = Vod(name="收藏测试影片", category_id=cat.id, is_hidden=False)
    db_session.add(vod)
    await db_session.commit()

    resp = await client.post(
        "/user/favorite",
        json={"vod_id": vod.id},
        headers={"Authorization": f"Bearer {user_token}"},
    )
    assert resp.status_code == 200
    assert resp.json()["code"] == 0


@pytest.mark.asyncio
async def test_add_favorite_duplicate(client, user_token, db_session):
    from app.models.vod import Vod
    from app.models.category import Category

    cat = Category(name="剧集", sort_order=2, is_active=True)
    db_session.add(cat)
    await db_session.flush()
    vod = Vod(name="重复收藏影片", category_id=cat.id, is_hidden=False)
    db_session.add(vod)
    await db_session.commit()

    headers = {"Authorization": f"Bearer {user_token}"}
    await client.post("/user/favorite", json={"vod_id": vod.id}, headers=headers)
    resp = await client.post("/user/favorite", json={"vod_id": vod.id}, headers=headers)
    assert resp.json()["code"] == 40002


@pytest.mark.asyncio
async def test_list_favorites(client, user_token, db_session):
    from app.models.vod import Vod
    from app.models.category import Category

    cat = Category(name="综艺", sort_order=3, is_active=True)
    db_session.add(cat)
    await db_session.flush()
    vod = Vod(name="列表收藏影片", category_id=cat.id, is_hidden=False)
    db_session.add(vod)
    await db_session.commit()

    headers = {"Authorization": f"Bearer {user_token}"}
    await client.post("/user/favorite", json={"vod_id": vod.id}, headers=headers)

    resp = await client.get("/user/favorite", headers=headers)
    assert resp.status_code == 200
    body = resp.json()
    assert body["code"] == 0
    assert body["data"]["total"] >= 1


@pytest.mark.asyncio
async def test_remove_favorite(client, user_token, db_session):
    from app.models.vod import Vod
    from app.models.category import Category

    cat = Category(name="动漫", sort_order=4, is_active=True)
    db_session.add(cat)
    await db_session.flush()
    vod = Vod(name="删除收藏影片", category_id=cat.id, is_hidden=False)
    db_session.add(vod)
    await db_session.commit()

    headers = {"Authorization": f"Bearer {user_token}"}
    await client.post("/user/favorite", json={"vod_id": vod.id}, headers=headers)

    resp = await client.delete(f"/user/favorite/{vod.id}", headers=headers)
    assert resp.status_code == 200
    assert resp.json()["code"] == 0


@pytest.mark.asyncio
async def test_remove_favorite_not_found(client, user_token):
    resp = await client.delete(
        "/user/favorite/99999",
        headers={"Authorization": f"Bearer {user_token}"},
    )
    assert resp.json()["code"] == 40401


@pytest.mark.asyncio
async def test_record_history(client, user_token, db_session):
    from app.models.vod import Vod
    from app.models.category import Category
    from app.models.source_site import SourceSite

    cat = Category(name="纪录片", sort_order=5, is_active=True)
    db_session.add(cat)
    await db_session.flush()
    vod = Vod(name="历史记录影片", category_id=cat.id, is_hidden=False)
    db_session.add(vod)
    source = SourceSite(name="测试源", code="test_src", base_url="http://test.com", sync_interval=60, sort_order=0, last_sync_count=0, total_count=0)
    db_session.add(source)
    await db_session.commit()

    resp = await client.post(
        "/user/history",
        json={
            "vod_id": vod.id,
            "source_id": source.id,
            "episode": "第1集",
            "play_url": "http://test.com/play.m3u8",
            "progress": 120,
            "duration": 3600,
        },
        headers={"Authorization": f"Bearer {user_token}"},
    )
    assert resp.status_code == 200
    assert resp.json()["code"] == 0


@pytest.mark.asyncio
async def test_list_history(client, user_token, db_session):
    from app.models.vod import Vod
    from app.models.category import Category
    from app.models.source_site import SourceSite

    cat = Category(name="短片", sort_order=6, is_active=True)
    db_session.add(cat)
    await db_session.flush()
    vod = Vod(name="历史列表影片", category_id=cat.id, is_hidden=False)
    db_session.add(vod)
    source = SourceSite(name="测试源2", code="test_src2", base_url="http://test2.com", sync_interval=60, sort_order=0, last_sync_count=0, total_count=0)
    db_session.add(source)
    await db_session.commit()

    headers = {"Authorization": f"Bearer {user_token}"}
    await client.post("/user/history", json={
        "vod_id": vod.id, "source_id": source.id,
        "episode": "第1集", "play_url": "http://test.com/1.m3u8",
        "progress": 60, "duration": 1800,
    }, headers=headers)

    resp = await client.get("/user/history", headers=headers)
    assert resp.status_code == 200
    body = resp.json()
    assert body["code"] == 0
    assert body["data"]["total"] >= 1


@pytest.mark.asyncio
async def test_delete_history(client, user_token, db_session):
    from app.models.vod import Vod
    from app.models.category import Category
    from app.models.source_site import SourceSite
    from app.models.play_history import PlayHistory
    from app.models.user import User
    from sqlalchemy import select

    cat = Category(name="MV", sort_order=7, is_active=True)
    db_session.add(cat)
    await db_session.flush()
    vod = Vod(name="删除历史影片", category_id=cat.id, is_hidden=False)
    db_session.add(vod)
    source = SourceSite(name="测试源3", code="test_src3", base_url="http://test3.com", sync_interval=60, sort_order=0, last_sync_count=0, total_count=0)
    db_session.add(source)
    await db_session.commit()

    headers = {"Authorization": f"Bearer {user_token}"}
    await client.post("/user/history", json={
        "vod_id": vod.id, "source_id": source.id,
        "episode": "第1集", "play_url": "http://test.com/1.m3u8",
        "progress": 30, "duration": 900,
    }, headers=headers)

    result = await db_session.execute(select(PlayHistory).order_by(PlayHistory.id.desc()).limit(1))
    history = result.scalar_one()

    resp = await client.delete(f"/user/history/{history.id}", headers=headers)
    assert resp.status_code == 200
    assert resp.json()["code"] == 0