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