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