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