1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai from 'chai'
9 getVideosListWithToken,
15 setAccessTokensToServers,
20 } from '../../../../shared/extra-utils'
21 import { Video, VideoDetails } from '../../../../shared/models/videos'
22 import { listMyVideosHistory, removeMyVideosHistory, userWatchVideo } from '../../../../shared/extra-utils/videos/video-history'
23 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
25 const expect = chai.expect
27 describe('Test videos history', function () {
28 let server: ServerInfo = null
29 let video1UUID: string
30 let video2UUID: string
31 let video3UUID: string
32 let video3WatchedDate: Date
33 let userAccessToken: string
35 before(async function () {
38 server = await flushAndRunServer(1)
40 await setAccessTokensToServers([ server ])
43 const res = await uploadVideo(server.url, server.accessToken, { name: 'video 1' })
44 video1UUID = res.body.video.uuid
48 const res = await uploadVideo(server.url, server.accessToken, { name: 'video 2' })
49 video2UUID = res.body.video.uuid
53 const res = await uploadVideo(server.url, server.accessToken, { name: 'video 3' })
54 video3UUID = res.body.video.uuid
59 password: 'super password'
61 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
62 userAccessToken = await userLogin(server, user)
65 it('Should get videos, without watching history', async function () {
66 const res = await getVideosListWithToken(server.url, server.accessToken)
67 const videos: Video[] = res.body.data
69 for (const video of videos) {
70 const resDetail = await getVideoWithToken(server.url, server.accessToken, video.id)
71 const videoDetails: VideoDetails = resDetail.body
73 expect(video.userHistory).to.be.undefined
74 expect(videoDetails.userHistory).to.be.undefined
78 it('Should watch the first and second video', async function () {
79 await userWatchVideo(server.url, server.accessToken, video2UUID, 8)
80 await userWatchVideo(server.url, server.accessToken, video1UUID, 3)
83 it('Should return the correct history when listing, searching and getting videos', async function () {
84 const videosOfVideos: Video[][] = []
87 const res = await getVideosListWithToken(server.url, server.accessToken)
88 videosOfVideos.push(res.body.data)
92 const res = await searchVideoWithToken(server.url, 'video', server.accessToken)
93 videosOfVideos.push(res.body.data)
96 for (const videos of videosOfVideos) {
97 const video1 = videos.find(v => v.uuid === video1UUID)
98 const video2 = videos.find(v => v.uuid === video2UUID)
99 const video3 = videos.find(v => v.uuid === video3UUID)
101 expect(video1.userHistory).to.not.be.undefined
102 expect(video1.userHistory.currentTime).to.equal(3)
104 expect(video2.userHistory).to.not.be.undefined
105 expect(video2.userHistory.currentTime).to.equal(8)
107 expect(video3.userHistory).to.be.undefined
111 const resDetail = await getVideoWithToken(server.url, server.accessToken, video1UUID)
112 const videoDetails: VideoDetails = resDetail.body
114 expect(videoDetails.userHistory).to.not.be.undefined
115 expect(videoDetails.userHistory.currentTime).to.equal(3)
119 const resDetail = await getVideoWithToken(server.url, server.accessToken, video2UUID)
120 const videoDetails: VideoDetails = resDetail.body
122 expect(videoDetails.userHistory).to.not.be.undefined
123 expect(videoDetails.userHistory.currentTime).to.equal(8)
127 const resDetail = await getVideoWithToken(server.url, server.accessToken, video3UUID)
128 const videoDetails: VideoDetails = resDetail.body
130 expect(videoDetails.userHistory).to.be.undefined
134 it('Should have these videos when listing my history', async function () {
135 video3WatchedDate = new Date()
136 await userWatchVideo(server.url, server.accessToken, video3UUID, 2)
138 const res = await listMyVideosHistory(server.url, server.accessToken)
140 expect(res.body.total).to.equal(3)
142 const videos: Video[] = res.body.data
143 expect(videos[0].name).to.equal('video 3')
144 expect(videos[1].name).to.equal('video 1')
145 expect(videos[2].name).to.equal('video 2')
148 it('Should not have videos history on another user', async function () {
149 const res = await listMyVideosHistory(server.url, userAccessToken)
151 expect(res.body.total).to.equal(0)
152 expect(res.body.data).to.have.lengthOf(0)
155 it('Should be able to search through videos in my history', async function () {
156 const res = await listMyVideosHistory(server.url, server.accessToken, '2')
158 expect(res.body.total).to.equal(1)
160 const videos: Video[] = res.body.data
161 expect(videos[0].name).to.equal('video 2')
164 it('Should clear my history', async function () {
165 await removeMyVideosHistory(server.url, server.accessToken, video3WatchedDate.toISOString())
168 it('Should have my history cleared', async function () {
169 const res = await listMyVideosHistory(server.url, server.accessToken)
171 expect(res.body.total).to.equal(1)
173 const videos: Video[] = res.body.data
174 expect(videos[0].name).to.equal('video 3')
177 it('Should disable videos history', async function () {
180 accessToken: server.accessToken,
181 videosHistoryEnabled: false
184 await userWatchVideo(server.url, server.accessToken, video2UUID, 8, HttpStatusCode.CONFLICT_409)
187 it('Should re-enable videos history', async function () {
190 accessToken: server.accessToken,
191 videosHistoryEnabled: true
194 await userWatchVideo(server.url, server.accessToken, video1UUID, 8)
196 const res = await listMyVideosHistory(server.url, server.accessToken)
198 expect(res.body.total).to.equal(2)
200 const videos: Video[] = res.body.data
201 expect(videos[0].name).to.equal('video 1')
202 expect(videos[1].name).to.equal('video 3')
205 it('Should not clean old history', async function () {
208 killallServers([ server ])
210 await reRunServer(server, { history: { videos: { max_age: '10 days' } } })
214 // Should still have history
216 const res = await listMyVideosHistory(server.url, server.accessToken)
218 expect(res.body.total).to.equal(2)
221 it('Should clean old history', async function () {
224 killallServers([ server ])
226 await reRunServer(server, { history: { videos: { max_age: '5 seconds' } } })
230 const res = await listMyVideosHistory(server.url, server.accessToken)
231 expect(res.body.total).to.equal(0)
234 after(async function () {
235 await cleanupTests([ server ])