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