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