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