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