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