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