]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/videos/videos-history.ts
Fix CLI tools
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / videos-history.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
6e46de09 2
6e46de09 3import 'mocha'
af971e06
C
4import * as chai from 'chai'
5import { HttpStatusCode } from '@shared/core-utils'
6e46de09 6import {
7c3b7976 7 cleanupTests,
8b9a525a 8 createUser,
7c3b7976 9 flushAndRunServer,
6e46de09
C
10 getVideosListWithToken,
11 getVideoWithToken,
313228e9 12 HistoryCommand,
7c3b7976
C
13 killallServers,
14 reRunServer,
6e46de09
C
15 ServerInfo,
16 setAccessTokensToServers,
8b9a525a
C
17 updateMyUser,
18 uploadVideo,
8f0bc73d
C
19 userLogin,
20 wait
af971e06 21} from '@shared/extra-utils'
af971e06 22import { Video, VideoDetails } from '@shared/models'
6e46de09
C
23
24const expect = chai.expect
25
26describe('Test videos history', function () {
27 let server: ServerInfo = null
28 let video1UUID: string
29 let video2UUID: string
30 let video3UUID: string
8b9a525a
C
31 let video3WatchedDate: Date
32 let userAccessToken: string
313228e9 33 let command: HistoryCommand
6e46de09
C
34
35 before(async function () {
36 this.timeout(30000)
37
210feb6c 38 server = await flushAndRunServer(1)
6e46de09
C
39
40 await setAccessTokensToServers([ server ])
41
313228e9
C
42 command = server.historyCommand
43
6e46de09
C
44 {
45 const res = await uploadVideo(server.url, server.accessToken, { name: 'video 1' })
46 video1UUID = res.body.video.uuid
47 }
48
49 {
50 const res = await uploadVideo(server.url, server.accessToken, { name: 'video 2' })
51 video2UUID = res.body.video.uuid
52 }
53
54 {
55 const res = await uploadVideo(server.url, server.accessToken, { name: 'video 3' })
56 video3UUID = res.body.video.uuid
57 }
8b9a525a
C
58
59 const user = {
60 username: 'user_1',
61 password: 'super password'
62 }
1eddc9a7 63 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
8b9a525a 64 userAccessToken = await userLogin(server, user)
6e46de09
C
65 })
66
67 it('Should get videos, without watching history', async function () {
68 const res = await getVideosListWithToken(server.url, server.accessToken)
69 const videos: Video[] = res.body.data
70
71 for (const video of videos) {
72 const resDetail = await getVideoWithToken(server.url, server.accessToken, video.id)
73 const videoDetails: VideoDetails = resDetail.body
74
75 expect(video.userHistory).to.be.undefined
76 expect(videoDetails.userHistory).to.be.undefined
77 }
78 })
79
80 it('Should watch the first and second video', async function () {
313228e9
C
81 await command.wathVideo({ videoId: video2UUID, currentTime: 8 })
82 await command.wathVideo({ videoId: video1UUID, currentTime: 3 })
6e46de09
C
83 })
84
85 it('Should return the correct history when listing, searching and getting videos', async function () {
86 const videosOfVideos: Video[][] = []
87
88 {
89 const res = await getVideosListWithToken(server.url, server.accessToken)
90 videosOfVideos.push(res.body.data)
91 }
92
93 {
af971e06
C
94 const body = await server.searchCommand.searchVideos({ token: server.accessToken, search: 'video' })
95 videosOfVideos.push(body.data)
6e46de09
C
96 }
97
98 for (const videos of videosOfVideos) {
99 const video1 = videos.find(v => v.uuid === video1UUID)
100 const video2 = videos.find(v => v.uuid === video2UUID)
101 const video3 = videos.find(v => v.uuid === video3UUID)
102
103 expect(video1.userHistory).to.not.be.undefined
104 expect(video1.userHistory.currentTime).to.equal(3)
105
106 expect(video2.userHistory).to.not.be.undefined
107 expect(video2.userHistory.currentTime).to.equal(8)
108
109 expect(video3.userHistory).to.be.undefined
110 }
111
112 {
113 const resDetail = await getVideoWithToken(server.url, server.accessToken, video1UUID)
114 const videoDetails: VideoDetails = resDetail.body
115
116 expect(videoDetails.userHistory).to.not.be.undefined
117 expect(videoDetails.userHistory.currentTime).to.equal(3)
118 }
119
120 {
121 const resDetail = await getVideoWithToken(server.url, server.accessToken, video2UUID)
122 const videoDetails: VideoDetails = resDetail.body
123
124 expect(videoDetails.userHistory).to.not.be.undefined
125 expect(videoDetails.userHistory.currentTime).to.equal(8)
126 }
127
128 {
129 const resDetail = await getVideoWithToken(server.url, server.accessToken, video3UUID)
130 const videoDetails: VideoDetails = resDetail.body
131
132 expect(videoDetails.userHistory).to.be.undefined
133 }
134 })
135
8b9a525a
C
136 it('Should have these videos when listing my history', async function () {
137 video3WatchedDate = new Date()
313228e9 138 await command.wathVideo({ videoId: video3UUID, currentTime: 2 })
8b9a525a 139
313228e9 140 const body = await command.list()
8b9a525a 141
313228e9 142 expect(body.total).to.equal(3)
8b9a525a 143
313228e9 144 const videos = body.data
8b9a525a
C
145 expect(videos[0].name).to.equal('video 3')
146 expect(videos[1].name).to.equal('video 1')
147 expect(videos[2].name).to.equal('video 2')
148 })
149
150 it('Should not have videos history on another user', async function () {
313228e9 151 const body = await command.list({ token: userAccessToken })
8b9a525a 152
313228e9
C
153 expect(body.total).to.equal(0)
154 expect(body.data).to.have.lengthOf(0)
8b9a525a
C
155 })
156
d8b34ee5 157 it('Should be able to search through videos in my history', async function () {
313228e9
C
158 const body = await command.list({ search: '2' })
159 expect(body.total).to.equal(1)
d8b34ee5 160
313228e9 161 const videos = body.data
d8b34ee5
RK
162 expect(videos[0].name).to.equal('video 2')
163 })
164
8b9a525a 165 it('Should clear my history', async function () {
313228e9 166 await command.remove({ beforeDate: video3WatchedDate.toISOString() })
8b9a525a
C
167 })
168
169 it('Should have my history cleared', async function () {
313228e9
C
170 const body = await command.list()
171 expect(body.total).to.equal(1)
8b9a525a 172
313228e9 173 const videos = body.data
8b9a525a
C
174 expect(videos[0].name).to.equal('video 3')
175 })
176
177 it('Should disable videos history', async function () {
178 await updateMyUser({
179 url: server.url,
180 accessToken: server.accessToken,
181 videosHistoryEnabled: false
182 })
183
313228e9 184 await command.wathVideo({ videoId: video2UUID, currentTime: 8, expectedStatus: HttpStatusCode.CONFLICT_409 })
8b9a525a
C
185 })
186
187 it('Should re-enable videos history', async function () {
188 await updateMyUser({
189 url: server.url,
190 accessToken: server.accessToken,
191 videosHistoryEnabled: true
192 })
193
313228e9 194 await command.wathVideo({ videoId: video1UUID, currentTime: 8 })
8b9a525a 195
313228e9
C
196 const body = await command.list()
197 expect(body.total).to.equal(2)
8b9a525a 198
313228e9 199 const videos = body.data
8b9a525a
C
200 expect(videos[0].name).to.equal('video 1')
201 expect(videos[1].name).to.equal('video 3')
202 })
203
8f0bc73d
C
204 it('Should not clean old history', async function () {
205 this.timeout(50000)
206
207 killallServers([ server ])
208
209 await reRunServer(server, { history: { videos: { max_age: '10 days' } } })
210
211 await wait(6000)
212
213 // Should still have history
214
313228e9
C
215 const body = await command.list()
216 expect(body.total).to.equal(2)
8f0bc73d
C
217 })
218
219 it('Should clean old history', async function () {
220 this.timeout(50000)
221
222 killallServers([ server ])
223
224 await reRunServer(server, { history: { videos: { max_age: '5 seconds' } } })
225
226 await wait(6000)
227
313228e9
C
228 const body = await command.list()
229 expect(body.total).to.equal(0)
8f0bc73d
C
230 })
231
7c3b7976
C
232 after(async function () {
233 await cleanupTests([ server ])
6e46de09
C
234 })
235})