1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
8 getVideosListWithToken,
10 killallServers, reRunServer,
14 setAccessTokensToServers,
19 } from '../../../../shared/extra-utils'
20 import { Video, VideoDetails } from '../../../../shared/models/videos'
21 import { listMyVideosHistory, removeMyVideosHistory, userWatchVideo } from '../../../../shared/extra-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 () {
36 server = await flushAndRunServer(1)
38 await setAccessTokensToServers([ server ])
41 const res = await uploadVideo(server.url, server.accessToken, { name: 'video 1' })
42 video1UUID = res.body.video.uuid
46 const res = await uploadVideo(server.url, server.accessToken, { name: 'video 2' })
47 video2UUID = res.body.video.uuid
51 const res = await uploadVideo(server.url, server.accessToken, { name: 'video 3' })
52 video3UUID = res.body.video.uuid
57 password: 'super password'
59 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
60 userAccessToken = await userLogin(server, user)
63 it('Should get videos, without watching history', async function () {
64 const res = await getVideosListWithToken(server.url, server.accessToken)
65 const videos: Video[] = res.body.data
67 for (const video of videos) {
68 const resDetail = await getVideoWithToken(server.url, server.accessToken, video.id)
69 const videoDetails: VideoDetails = resDetail.body
71 expect(video.userHistory).to.be.undefined
72 expect(videoDetails.userHistory).to.be.undefined
76 it('Should watch the first and second video', async function () {
77 await userWatchVideo(server.url, server.accessToken, video2UUID, 8)
78 await userWatchVideo(server.url, server.accessToken, video1UUID, 3)
81 it('Should return the correct history when listing, searching and getting videos', async function () {
82 const videosOfVideos: Video[][] = []
85 const res = await getVideosListWithToken(server.url, server.accessToken)
86 videosOfVideos.push(res.body.data)
90 const res = await searchVideoWithToken(server.url, 'video', server.accessToken)
91 videosOfVideos.push(res.body.data)
94 for (const videos of videosOfVideos) {
95 const video1 = videos.find(v => v.uuid === video1UUID)
96 const video2 = videos.find(v => v.uuid === video2UUID)
97 const video3 = videos.find(v => v.uuid === video3UUID)
99 expect(video1.userHistory).to.not.be.undefined
100 expect(video1.userHistory.currentTime).to.equal(3)
102 expect(video2.userHistory).to.not.be.undefined
103 expect(video2.userHistory.currentTime).to.equal(8)
105 expect(video3.userHistory).to.be.undefined
109 const resDetail = await getVideoWithToken(server.url, server.accessToken, video1UUID)
110 const videoDetails: VideoDetails = resDetail.body
112 expect(videoDetails.userHistory).to.not.be.undefined
113 expect(videoDetails.userHistory.currentTime).to.equal(3)
117 const resDetail = await getVideoWithToken(server.url, server.accessToken, video2UUID)
118 const videoDetails: VideoDetails = resDetail.body
120 expect(videoDetails.userHistory).to.not.be.undefined
121 expect(videoDetails.userHistory.currentTime).to.equal(8)
125 const resDetail = await getVideoWithToken(server.url, server.accessToken, video3UUID)
126 const videoDetails: VideoDetails = resDetail.body
128 expect(videoDetails.userHistory).to.be.undefined
132 it('Should have these videos when listing my history', async function () {
133 video3WatchedDate = new Date()
134 await userWatchVideo(server.url, server.accessToken, video3UUID, 2)
136 const res = await listMyVideosHistory(server.url, server.accessToken)
138 expect(res.body.total).to.equal(3)
140 const videos: Video[] = res.body.data
141 expect(videos[0].name).to.equal('video 3')
142 expect(videos[1].name).to.equal('video 1')
143 expect(videos[2].name).to.equal('video 2')
146 it('Should not have videos history on another user', async function () {
147 const res = await listMyVideosHistory(server.url, userAccessToken)
149 expect(res.body.total).to.equal(0)
150 expect(res.body.data).to.have.lengthOf(0)
153 it('Should clear my history', async function () {
154 await removeMyVideosHistory(server.url, server.accessToken, video3WatchedDate.toISOString())
157 it('Should have my history cleared', async function () {
158 const res = await listMyVideosHistory(server.url, server.accessToken)
160 expect(res.body.total).to.equal(1)
162 const videos: Video[] = res.body.data
163 expect(videos[0].name).to.equal('video 3')
166 it('Should disable videos history', async function () {
169 accessToken: server.accessToken,
170 videosHistoryEnabled: false
173 await userWatchVideo(server.url, server.accessToken, video2UUID, 8, 409)
176 it('Should re-enable videos history', async function () {
179 accessToken: server.accessToken,
180 videosHistoryEnabled: true
183 await userWatchVideo(server.url, server.accessToken, video1UUID, 8)
185 const res = await listMyVideosHistory(server.url, server.accessToken)
187 expect(res.body.total).to.equal(2)
189 const videos: Video[] = res.body.data
190 expect(videos[0].name).to.equal('video 1')
191 expect(videos[1].name).to.equal('video 3')
194 it('Should not clean old history', async function () {
197 killallServers([ server ])
199 await reRunServer(server, { history: { videos: { max_age: '10 days' } } })
203 // Should still have history
205 const res = await listMyVideosHistory(server.url, server.accessToken)
207 expect(res.body.total).to.equal(2)
210 it('Should clean old history', async function () {
213 killallServers([ server ])
215 await reRunServer(server, { history: { videos: { max_age: '5 seconds' } } })
219 const res = await listMyVideosHistory(server.url, server.accessToken)
220 expect(res.body.total).to.equal(0)
224 killallServers([ server ])