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