]>
Commit | Line | Data |
---|---|---|
a1587156 | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
6e46de09 | 2 | |
6e46de09 | 3 | import 'mocha' |
af971e06 | 4 | import * as chai from 'chai' |
c55e3d72 | 5 | import { wait } from '@shared/core-utils' |
b2111066 C |
6 | import { Video } from '@shared/models' |
7 | import { cleanupTests, createSingleServer, killallServers, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands' | |
6e46de09 C |
8 | |
9 | const expect = chai.expect | |
10 | ||
11 | describe('Test videos history', function () { | |
254d3579 | 12 | let server: PeerTubeServer = null |
7177b46c | 13 | let video1Id: number |
6e46de09 C |
14 | let video1UUID: string |
15 | let video2UUID: string | |
16 | let video3UUID: string | |
8b9a525a C |
17 | let video3WatchedDate: Date |
18 | let userAccessToken: string | |
6e46de09 C |
19 | |
20 | before(async function () { | |
8d57e728 | 21 | this.timeout(120000) |
6e46de09 | 22 | |
254d3579 | 23 | server = await createSingleServer(1) |
6e46de09 C |
24 | |
25 | await setAccessTokensToServers([ server ]) | |
26 | ||
b2111066 | 27 | // 10 seconds long |
d74bb064 | 28 | const fixture = 'video_short1.webm' |
313228e9 | 29 | |
6e46de09 | 30 | { |
b2111066 | 31 | const { id, uuid } = await server.videos.upload({ attributes: { name: 'video 1', fixture } }) |
d23dd9fb | 32 | video1UUID = uuid |
7177b46c | 33 | video1Id = id |
6e46de09 C |
34 | } |
35 | ||
36 | { | |
b2111066 | 37 | const { uuid } = await server.videos.upload({ attributes: { name: 'video 2', fixture } }) |
d23dd9fb | 38 | video2UUID = uuid |
6e46de09 C |
39 | } |
40 | ||
41 | { | |
b2111066 | 42 | const { uuid } = await server.videos.upload({ attributes: { name: 'video 3', fixture } }) |
d23dd9fb | 43 | video3UUID = uuid |
6e46de09 | 44 | } |
8b9a525a | 45 | |
b2111066 | 46 | userAccessToken = await server.users.generateUserAndToken('user_1') |
6e46de09 C |
47 | }) |
48 | ||
49 | it('Should get videos, without watching history', async function () { | |
89d241a7 | 50 | const { data } = await server.videos.listWithToken() |
6e46de09 | 51 | |
d23dd9fb | 52 | for (const video of data) { |
89d241a7 | 53 | const videoDetails = await server.videos.getWithToken({ id: video.id }) |
6e46de09 C |
54 | |
55 | expect(video.userHistory).to.be.undefined | |
56 | expect(videoDetails.userHistory).to.be.undefined | |
57 | } | |
58 | }) | |
59 | ||
60 | it('Should watch the first and second video', async function () { | |
b2111066 C |
61 | await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 }) |
62 | await server.views.view({ id: video1UUID, token: server.accessToken, currentTime: 3 }) | |
6e46de09 C |
63 | }) |
64 | ||
65 | it('Should return the correct history when listing, searching and getting videos', async function () { | |
66 | const videosOfVideos: Video[][] = [] | |
67 | ||
68 | { | |
89d241a7 | 69 | const { data } = await server.videos.listWithToken() |
d23dd9fb | 70 | videosOfVideos.push(data) |
6e46de09 C |
71 | } |
72 | ||
73 | { | |
89d241a7 | 74 | const body = await server.search.searchVideos({ token: server.accessToken, search: 'video' }) |
af971e06 | 75 | videosOfVideos.push(body.data) |
6e46de09 C |
76 | } |
77 | ||
78 | for (const videos of videosOfVideos) { | |
79 | const video1 = videos.find(v => v.uuid === video1UUID) | |
80 | const video2 = videos.find(v => v.uuid === video2UUID) | |
81 | const video3 = videos.find(v => v.uuid === video3UUID) | |
82 | ||
83 | expect(video1.userHistory).to.not.be.undefined | |
84 | expect(video1.userHistory.currentTime).to.equal(3) | |
85 | ||
86 | expect(video2.userHistory).to.not.be.undefined | |
87 | expect(video2.userHistory.currentTime).to.equal(8) | |
88 | ||
89 | expect(video3.userHistory).to.be.undefined | |
90 | } | |
91 | ||
92 | { | |
89d241a7 | 93 | const videoDetails = await server.videos.getWithToken({ id: video1UUID }) |
6e46de09 C |
94 | |
95 | expect(videoDetails.userHistory).to.not.be.undefined | |
96 | expect(videoDetails.userHistory.currentTime).to.equal(3) | |
97 | } | |
98 | ||
99 | { | |
89d241a7 | 100 | const videoDetails = await server.videos.getWithToken({ id: video2UUID }) |
6e46de09 C |
101 | |
102 | expect(videoDetails.userHistory).to.not.be.undefined | |
103 | expect(videoDetails.userHistory.currentTime).to.equal(8) | |
104 | } | |
105 | ||
106 | { | |
89d241a7 | 107 | const videoDetails = await server.videos.getWithToken({ id: video3UUID }) |
6e46de09 C |
108 | |
109 | expect(videoDetails.userHistory).to.be.undefined | |
110 | } | |
111 | }) | |
112 | ||
8b9a525a C |
113 | it('Should have these videos when listing my history', async function () { |
114 | video3WatchedDate = new Date() | |
b2111066 | 115 | await server.views.view({ id: video3UUID, token: server.accessToken, currentTime: 2 }) |
8b9a525a | 116 | |
b2111066 | 117 | const body = await server.history.list() |
8b9a525a | 118 | |
313228e9 | 119 | expect(body.total).to.equal(3) |
8b9a525a | 120 | |
313228e9 | 121 | const videos = body.data |
8b9a525a C |
122 | expect(videos[0].name).to.equal('video 3') |
123 | expect(videos[1].name).to.equal('video 1') | |
124 | expect(videos[2].name).to.equal('video 2') | |
125 | }) | |
126 | ||
127 | it('Should not have videos history on another user', async function () { | |
b2111066 | 128 | const body = await server.history.list({ token: userAccessToken }) |
8b9a525a | 129 | |
313228e9 C |
130 | expect(body.total).to.equal(0) |
131 | expect(body.data).to.have.lengthOf(0) | |
8b9a525a C |
132 | }) |
133 | ||
d8b34ee5 | 134 | it('Should be able to search through videos in my history', async function () { |
b2111066 | 135 | const body = await server.history.list({ search: '2' }) |
313228e9 | 136 | expect(body.total).to.equal(1) |
d8b34ee5 | 137 | |
313228e9 | 138 | const videos = body.data |
d8b34ee5 RK |
139 | expect(videos[0].name).to.equal('video 2') |
140 | }) | |
141 | ||
8b9a525a | 142 | it('Should clear my history', async function () { |
b2111066 | 143 | await server.history.removeAll({ beforeDate: video3WatchedDate.toISOString() }) |
8b9a525a C |
144 | }) |
145 | ||
146 | it('Should have my history cleared', async function () { | |
b2111066 | 147 | const body = await server.history.list() |
313228e9 | 148 | expect(body.total).to.equal(1) |
8b9a525a | 149 | |
313228e9 | 150 | const videos = body.data |
8b9a525a C |
151 | expect(videos[0].name).to.equal('video 3') |
152 | }) | |
153 | ||
154 | it('Should disable videos history', async function () { | |
89d241a7 | 155 | await server.users.updateMe({ |
8b9a525a C |
156 | videosHistoryEnabled: false |
157 | }) | |
158 | ||
b2111066 C |
159 | await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 }) |
160 | ||
161 | const { data } = await server.history.list() | |
162 | expect(data[0].name).to.not.equal('video 2') | |
8b9a525a C |
163 | }) |
164 | ||
165 | it('Should re-enable videos history', async function () { | |
89d241a7 | 166 | await server.users.updateMe({ |
8b9a525a C |
167 | videosHistoryEnabled: true |
168 | }) | |
169 | ||
b2111066 | 170 | await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 }) |
8b9a525a | 171 | |
b2111066 C |
172 | const { data } = await server.history.list() |
173 | expect(data[0].name).to.equal('video 2') | |
8b9a525a C |
174 | }) |
175 | ||
8f0bc73d C |
176 | it('Should not clean old history', async function () { |
177 | this.timeout(50000) | |
178 | ||
9293139f | 179 | await killallServers([ server ]) |
8f0bc73d | 180 | |
254d3579 | 181 | await server.run({ history: { videos: { max_age: '10 days' } } }) |
8f0bc73d C |
182 | |
183 | await wait(6000) | |
184 | ||
185 | // Should still have history | |
186 | ||
b2111066 | 187 | const body = await server.history.list() |
313228e9 | 188 | expect(body.total).to.equal(2) |
8f0bc73d C |
189 | }) |
190 | ||
191 | it('Should clean old history', async function () { | |
192 | this.timeout(50000) | |
193 | ||
9293139f | 194 | await killallServers([ server ]) |
8f0bc73d | 195 | |
254d3579 | 196 | await server.run({ history: { videos: { max_age: '5 seconds' } } }) |
8f0bc73d C |
197 | |
198 | await wait(6000) | |
199 | ||
b2111066 | 200 | const body = await server.history.list() |
313228e9 | 201 | expect(body.total).to.equal(0) |
8f0bc73d C |
202 | }) |
203 | ||
7177b46c C |
204 | it('Should delete a specific history element', async function () { |
205 | { | |
b2111066 C |
206 | await server.views.view({ id: video1UUID, token: server.accessToken, currentTime: 4 }) |
207 | await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 }) | |
7177b46c C |
208 | } |
209 | ||
210 | { | |
b2111066 | 211 | const body = await server.history.list() |
7177b46c C |
212 | expect(body.total).to.equal(2) |
213 | } | |
214 | ||
215 | { | |
b2111066 | 216 | await server.history.removeElement({ videoId: video1Id }) |
7177b46c | 217 | |
b2111066 | 218 | const body = await server.history.list() |
7177b46c C |
219 | expect(body.total).to.equal(1) |
220 | expect(body.data[0].uuid).to.equal(video2UUID) | |
221 | } | |
222 | }) | |
223 | ||
7c3b7976 C |
224 | after(async function () { |
225 | await cleanupTests([ server ]) | |
6e46de09 C |
226 | }) |
227 | }) |