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