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