]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
6e46de09 2
6e46de09 3import 'mocha'
af971e06 4import * as chai from 'chai'
c55e3d72
C
5import { wait } from '@shared/core-utils'
6import { HttpStatusCode, Video } from '@shared/models'
6e46de09 7import {
7c3b7976 8 cleanupTests,
254d3579 9 createSingleServer,
313228e9 10 HistoryCommand,
7c3b7976 11 killallServers,
254d3579 12 PeerTubeServer,
c55e3d72 13 setAccessTokensToServers
bf54587a 14} from '@shared/server-commands'
6e46de09
C
15
16const expect = chai.expect
17
18describe('Test videos history', function () {
254d3579 19 let server: PeerTubeServer = null
7177b46c 20 let video1Id: number
6e46de09
C
21 let video1UUID: string
22 let video2UUID: string
23 let video3UUID: string
8b9a525a
C
24 let video3WatchedDate: Date
25 let userAccessToken: string
313228e9 26 let command: HistoryCommand
6e46de09
C
27
28 before(async function () {
29 this.timeout(30000)
30
254d3579 31 server = await createSingleServer(1)
6e46de09
C
32
33 await setAccessTokensToServers([ server ])
34
89d241a7 35 command = server.history
313228e9 36
6e46de09 37 {
7177b46c 38 const { id, uuid } = await server.videos.upload({ attributes: { name: 'video 1' } })
d23dd9fb 39 video1UUID = uuid
7177b46c 40 video1Id = id
6e46de09
C
41 }
42
43 {
89d241a7 44 const { uuid } = await server.videos.upload({ attributes: { name: 'video 2' } })
d23dd9fb 45 video2UUID = uuid
6e46de09
C
46 }
47
48 {
89d241a7 49 const { uuid } = await server.videos.upload({ attributes: { name: 'video 3' } })
d23dd9fb 50 video3UUID = uuid
6e46de09 51 }
8b9a525a
C
52
53 const user = {
54 username: 'user_1',
55 password: 'super password'
56 }
89d241a7
C
57 await server.users.create({ username: user.username, password: user.password })
58 userAccessToken = await server.login.getAccessToken(user)
6e46de09
C
59 })
60
61 it('Should get videos, without watching history', async function () {
89d241a7 62 const { data } = await server.videos.listWithToken()
6e46de09 63
d23dd9fb 64 for (const video of data) {
89d241a7 65 const videoDetails = await server.videos.getWithToken({ id: video.id })
6e46de09
C
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 () {
7177b46c
C
73 await command.watchVideo({ videoId: video2UUID, currentTime: 8 })
74 await command.watchVideo({ videoId: video1UUID, currentTime: 3 })
6e46de09
C
75 })
76
77 it('Should return the correct history when listing, searching and getting videos', async function () {
78 const videosOfVideos: Video[][] = []
79
80 {
89d241a7 81 const { data } = await server.videos.listWithToken()
d23dd9fb 82 videosOfVideos.push(data)
6e46de09
C
83 }
84
85 {
89d241a7 86 const body = await server.search.searchVideos({ token: server.accessToken, search: 'video' })
af971e06 87 videosOfVideos.push(body.data)
6e46de09
C
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 {
89d241a7 105 const videoDetails = await server.videos.getWithToken({ id: video1UUID })
6e46de09
C
106
107 expect(videoDetails.userHistory).to.not.be.undefined
108 expect(videoDetails.userHistory.currentTime).to.equal(3)
109 }
110
111 {
89d241a7 112 const videoDetails = await server.videos.getWithToken({ id: video2UUID })
6e46de09
C
113
114 expect(videoDetails.userHistory).to.not.be.undefined
115 expect(videoDetails.userHistory.currentTime).to.equal(8)
116 }
117
118 {
89d241a7 119 const videoDetails = await server.videos.getWithToken({ id: video3UUID })
6e46de09
C
120
121 expect(videoDetails.userHistory).to.be.undefined
122 }
123 })
124
8b9a525a
C
125 it('Should have these videos when listing my history', async function () {
126 video3WatchedDate = new Date()
7177b46c 127 await command.watchVideo({ videoId: video3UUID, currentTime: 2 })
8b9a525a 128
313228e9 129 const body = await command.list()
8b9a525a 130
313228e9 131 expect(body.total).to.equal(3)
8b9a525a 132
313228e9 133 const videos = body.data
8b9a525a
C
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 () {
313228e9 140 const body = await command.list({ token: userAccessToken })
8b9a525a 141
313228e9
C
142 expect(body.total).to.equal(0)
143 expect(body.data).to.have.lengthOf(0)
8b9a525a
C
144 })
145
d8b34ee5 146 it('Should be able to search through videos in my history', async function () {
313228e9
C
147 const body = await command.list({ search: '2' })
148 expect(body.total).to.equal(1)
d8b34ee5 149
313228e9 150 const videos = body.data
d8b34ee5
RK
151 expect(videos[0].name).to.equal('video 2')
152 })
153
8b9a525a 154 it('Should clear my history', async function () {
7177b46c 155 await command.removeAll({ beforeDate: video3WatchedDate.toISOString() })
8b9a525a
C
156 })
157
158 it('Should have my history cleared', async function () {
313228e9
C
159 const body = await command.list()
160 expect(body.total).to.equal(1)
8b9a525a 161
313228e9 162 const videos = body.data
8b9a525a
C
163 expect(videos[0].name).to.equal('video 3')
164 })
165
166 it('Should disable videos history', async function () {
89d241a7 167 await server.users.updateMe({
8b9a525a
C
168 videosHistoryEnabled: false
169 })
170
7177b46c 171 await command.watchVideo({ videoId: video2UUID, currentTime: 8, expectedStatus: HttpStatusCode.CONFLICT_409 })
8b9a525a
C
172 })
173
174 it('Should re-enable videos history', async function () {
89d241a7 175 await server.users.updateMe({
8b9a525a
C
176 videosHistoryEnabled: true
177 })
178
7177b46c 179 await command.watchVideo({ videoId: video1UUID, currentTime: 8 })
8b9a525a 180
313228e9
C
181 const body = await command.list()
182 expect(body.total).to.equal(2)
8b9a525a 183
313228e9 184 const videos = body.data
8b9a525a
C
185 expect(videos[0].name).to.equal('video 1')
186 expect(videos[1].name).to.equal('video 3')
187 })
188
8f0bc73d
C
189 it('Should not clean old history', async function () {
190 this.timeout(50000)
191
9293139f 192 await killallServers([ server ])
8f0bc73d 193
254d3579 194 await server.run({ history: { videos: { max_age: '10 days' } } })
8f0bc73d
C
195
196 await wait(6000)
197
198 // Should still have history
199
313228e9
C
200 const body = await command.list()
201 expect(body.total).to.equal(2)
8f0bc73d
C
202 })
203
204 it('Should clean old history', async function () {
205 this.timeout(50000)
206
9293139f 207 await killallServers([ server ])
8f0bc73d 208
254d3579 209 await server.run({ history: { videos: { max_age: '5 seconds' } } })
8f0bc73d
C
210
211 await wait(6000)
212
313228e9
C
213 const body = await command.list()
214 expect(body.total).to.equal(0)
8f0bc73d
C
215 })
216
7177b46c
C
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
7c3b7976
C
237 after(async function () {
238 await cleanupTests([ server ])
6e46de09
C
239 })
240})