1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
8 getVideosListWithToken,
10 killallServers, reRunServer,
14 setAccessTokensToServers,
19 } from '../../../../shared/utils'
20 import { Video, VideoDetails } from '../../../../shared/models/videos'
21 import { listMyVideosHistory, removeMyVideosHistory, userWatchVideo } from '../../../../shared/utils/videos/video-history'
23 const expect = chai.expect
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
33 before(async function () {
38 server = await runServer(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(server.url, server.accessToken, user.username, 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 clear my history', async function () {
156 await removeMyVideosHistory(server.url, server.accessToken, video3WatchedDate.toISOString())
159 it('Should have my history cleared', async function () {
160 const res = await listMyVideosHistory(server.url, server.accessToken)
162 expect(res.body.total).to.equal(1)
164 const videos: Video[] = res.body.data
165 expect(videos[0].name).to.equal('video 3')
168 it('Should disable videos history', async function () {
171 accessToken: server.accessToken,
172 videosHistoryEnabled: false
175 await userWatchVideo(server.url, server.accessToken, video2UUID, 8, 409)
178 it('Should re-enable videos history', async function () {
181 accessToken: server.accessToken,
182 videosHistoryEnabled: true
185 await userWatchVideo(server.url, server.accessToken, video1UUID, 8)
187 const res = await listMyVideosHistory(server.url, server.accessToken)
189 expect(res.body.total).to.equal(2)
191 const videos: Video[] = res.body.data
192 expect(videos[0].name).to.equal('video 1')
193 expect(videos[1].name).to.equal('video 3')
196 it('Should not clean old history', async function () {
199 killallServers([ server ])
201 await reRunServer(server, { history: { videos: { max_age: '10 days' } } })
205 // Should still have history
207 const res = await listMyVideosHistory(server.url, server.accessToken)
209 expect(res.body.total).to.equal(2)
212 it('Should clean old history', async function () {
215 killallServers([ server ])
217 await reRunServer(server, { history: { videos: { max_age: '5 seconds' } } })
221 const res = await listMyVideosHistory(server.url, server.accessToken)
222 expect(res.body.total).to.equal(0)
225 after(async function () {
226 killallServers([ server ])
228 // Keep the logs if the test failed