]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/videos/videos-history.ts
Merge branch 'release/v1.2.0'
[github/Chocobozzz/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,
11 runServer,
12 searchVideoWithToken,
13 ServerInfo,
14 setAccessTokensToServers,
15 updateMyUser,
16 uploadVideo,
17 userLogin
18 } from '../../../../shared/utils'
19 import { Video, VideoDetails } from '../../../../shared/models/videos'
20 import { listMyVideosHistory, removeMyVideosHistory, userWatchVideo } from '../../../../shared/utils/videos/video-history'
21
22 const expect = chai.expect
23
24 describe('Test videos history', function () {
25 let server: ServerInfo = null
26 let video1UUID: string
27 let video2UUID: string
28 let video3UUID: string
29 let video3WatchedDate: Date
30 let userAccessToken: string
31
32 before(async function () {
33 this.timeout(30000)
34
35 await flushTests()
36
37 server = await runServer(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(server.url, server.accessToken, user.username, 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 after(async function () {
196 killallServers([ server ])
197
198 // Keep the logs if the test failed
199 if (this['ok']) {
200 await flushTests()
201 }
202 })
203 })