]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/videos/videos-history.ts
Add ability to filter my imports by target URL
[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 { wait } from '@shared/core-utils'
6 import { HttpStatusCode, Video } from '@shared/models'
7 import {
8 cleanupTests,
9 createSingleServer,
10 HistoryCommand,
11 killallServers,
12 PeerTubeServer,
13 setAccessTokensToServers
14 } from '@shared/server-commands'
15
16 const expect = chai.expect
17
18 describe('Test videos history', function () {
19 let server: PeerTubeServer = null
20 let video1Id: number
21 let video1UUID: string
22 let video2UUID: string
23 let video3UUID: string
24 let video3WatchedDate: Date
25 let userAccessToken: string
26 let command: HistoryCommand
27
28 before(async function () {
29 this.timeout(30000)
30
31 server = await createSingleServer(1)
32
33 await setAccessTokensToServers([ server ])
34
35 command = server.history
36
37 {
38 const { id, uuid } = await server.videos.upload({ attributes: { name: 'video 1' } })
39 video1UUID = uuid
40 video1Id = id
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.watchVideo({ videoId: video2UUID, currentTime: 8 })
74 await command.watchVideo({ 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.watchVideo({ 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.removeAll({ 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.watchVideo({ 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.watchVideo({ 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 server.run({ 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 server.run({ 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 it('Should delete a specific history element', async function () {
218 {
219 await command.watchVideo({ videoId: video1UUID, currentTime: 4 })
220 await command.watchVideo({ videoId: video2UUID, currentTime: 8 })
221 }
222
223 {
224 const body = await command.list()
225 expect(body.total).to.equal(2)
226 }
227
228 {
229 await command.removeElement({ videoId: video1Id })
230
231 const body = await command.list()
232 expect(body.total).to.equal(1)
233 expect(body.data[0].uuid).to.equal(video2UUID)
234 }
235 })
236
237 after(async function () {
238 await cleanupTests([ server ])
239 })
240 })