Add ability to limit videos history size
[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   createUser,
7   flushTests,
8   getVideosListWithToken,
9   getVideoWithToken,
10   killallServers, reRunServer,
11   runServer,
12   searchVideoWithToken,
13   ServerInfo,
14   setAccessTokensToServers,
15   updateMyUser,
16   uploadVideo,
17   userLogin,
18   wait
19 } from '../../../../shared/utils'
20 import { Video, VideoDetails } from '../../../../shared/models/videos'
21 import { listMyVideosHistory, removeMyVideosHistory, userWatchVideo } from '../../../../shared/utils/videos/video-history'
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
33   before(async function () {
34     this.timeout(30000)
35
36     await flushTests()
37
38     server = await runServer(1)
39
40     await setAccessTokensToServers([ server ])
41
42     {
43       const res = await uploadVideo(server.url, server.accessToken, { name: 'video 1' })
44       video1UUID = res.body.video.uuid
45     }
46
47     {
48       const res = await uploadVideo(server.url, server.accessToken, { name: 'video 2' })
49       video2UUID = res.body.video.uuid
50     }
51
52     {
53       const res = await uploadVideo(server.url, server.accessToken, { name: 'video 3' })
54       video3UUID = res.body.video.uuid
55     }
56
57     const user = {
58       username: 'user_1',
59       password: 'super password'
60     }
61     await createUser(server.url, server.accessToken, user.username, user.password)
62     userAccessToken = await userLogin(server, user)
63   })
64
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
68
69     for (const video of videos) {
70       const resDetail = await getVideoWithToken(server.url, server.accessToken, video.id)
71       const videoDetails: VideoDetails = resDetail.body
72
73       expect(video.userHistory).to.be.undefined
74       expect(videoDetails.userHistory).to.be.undefined
75     }
76   })
77
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)
81   })
82
83   it('Should return the correct history when listing, searching and getting videos', async function () {
84     const videosOfVideos: Video[][] = []
85
86     {
87       const res = await getVideosListWithToken(server.url, server.accessToken)
88       videosOfVideos.push(res.body.data)
89     }
90
91     {
92       const res = await searchVideoWithToken(server.url, 'video', server.accessToken)
93       videosOfVideos.push(res.body.data)
94     }
95
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)
100
101       expect(video1.userHistory).to.not.be.undefined
102       expect(video1.userHistory.currentTime).to.equal(3)
103
104       expect(video2.userHistory).to.not.be.undefined
105       expect(video2.userHistory.currentTime).to.equal(8)
106
107       expect(video3.userHistory).to.be.undefined
108     }
109
110     {
111       const resDetail = await getVideoWithToken(server.url, server.accessToken, video1UUID)
112       const videoDetails: VideoDetails = resDetail.body
113
114       expect(videoDetails.userHistory).to.not.be.undefined
115       expect(videoDetails.userHistory.currentTime).to.equal(3)
116     }
117
118     {
119       const resDetail = await getVideoWithToken(server.url, server.accessToken, video2UUID)
120       const videoDetails: VideoDetails = resDetail.body
121
122       expect(videoDetails.userHistory).to.not.be.undefined
123       expect(videoDetails.userHistory.currentTime).to.equal(8)
124     }
125
126     {
127       const resDetail = await getVideoWithToken(server.url, server.accessToken, video3UUID)
128       const videoDetails: VideoDetails = resDetail.body
129
130       expect(videoDetails.userHistory).to.be.undefined
131     }
132   })
133
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)
137
138     const res = await listMyVideosHistory(server.url, server.accessToken)
139
140     expect(res.body.total).to.equal(3)
141
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')
146   })
147
148   it('Should not have videos history on another user', async function () {
149     const res = await listMyVideosHistory(server.url, userAccessToken)
150
151     expect(res.body.total).to.equal(0)
152     expect(res.body.data).to.have.lengthOf(0)
153   })
154
155   it('Should clear my history', async function () {
156     await removeMyVideosHistory(server.url, server.accessToken, video3WatchedDate.toISOString())
157   })
158
159   it('Should have my history cleared', async function () {
160     const res = await listMyVideosHistory(server.url, server.accessToken)
161
162     expect(res.body.total).to.equal(1)
163
164     const videos: Video[] = res.body.data
165     expect(videos[0].name).to.equal('video 3')
166   })
167
168   it('Should disable videos history', async function () {
169     await updateMyUser({
170       url: server.url,
171       accessToken: server.accessToken,
172       videosHistoryEnabled: false
173     })
174
175     await userWatchVideo(server.url, server.accessToken, video2UUID, 8, 409)
176   })
177
178   it('Should re-enable videos history', async function () {
179     await updateMyUser({
180       url: server.url,
181       accessToken: server.accessToken,
182       videosHistoryEnabled: true
183     })
184
185     await userWatchVideo(server.url, server.accessToken, video1UUID, 8)
186
187     const res = await listMyVideosHistory(server.url, server.accessToken)
188
189     expect(res.body.total).to.equal(2)
190
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')
194   })
195
196   it('Should not clean old history', async function () {
197     this.timeout(50000)
198
199     killallServers([ server ])
200
201     await reRunServer(server, { history: { videos: { max_age: '10 days' } } })
202
203     await wait(6000)
204
205     // Should still have history
206
207     const res = await listMyVideosHistory(server.url, server.accessToken)
208
209     expect(res.body.total).to.equal(2)
210   })
211
212   it('Should clean old history', async function () {
213     this.timeout(50000)
214
215     killallServers([ server ])
216
217     await reRunServer(server, { history: { videos: { max_age: '5 seconds' } } })
218
219     await wait(6000)
220
221     const res = await listMyVideosHistory(server.url, server.accessToken)
222     expect(res.body.total).to.equal(0)
223   })
224
225   after(async function () {
226     killallServers([ server ])
227
228     // Keep the logs if the test failed
229     if (this['ok']) {
230       await flushTests()
231     }
232   })
233 })