Merge branch 'release/v1.0.0' into develop
[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   flushTests,
7   getVideosListWithToken,
8   getVideoWithToken,
9   killallServers, makePutBodyRequest,
10   runServer, searchVideoWithToken,
11   ServerInfo,
12   setAccessTokensToServers,
13   uploadVideo
14 } from '../../utils'
15 import { Video, VideoDetails } from '../../../../shared/models/videos'
16 import { userWatchVideo } from '../../utils/videos/video-history'
17
18 const expect = chai.expect
19
20 describe('Test videos history', function () {
21   let server: ServerInfo = null
22   let video1UUID: string
23   let video2UUID: string
24   let video3UUID: string
25
26   before(async function () {
27     this.timeout(30000)
28
29     await flushTests()
30
31     server = await runServer(1)
32
33     await setAccessTokensToServers([ server ])
34
35     {
36       const res = await uploadVideo(server.url, server.accessToken, { name: 'video 1' })
37       video1UUID = res.body.video.uuid
38     }
39
40     {
41       const res = await uploadVideo(server.url, server.accessToken, { name: 'video 2' })
42       video2UUID = res.body.video.uuid
43     }
44
45     {
46       const res = await uploadVideo(server.url, server.accessToken, { name: 'video 3' })
47       video3UUID = res.body.video.uuid
48     }
49   })
50
51   it('Should get videos, without watching history', async function () {
52     const res = await getVideosListWithToken(server.url, server.accessToken)
53     const videos: Video[] = res.body.data
54
55     for (const video of videos) {
56       const resDetail = await getVideoWithToken(server.url, server.accessToken, video.id)
57       const videoDetails: VideoDetails = resDetail.body
58
59       expect(video.userHistory).to.be.undefined
60       expect(videoDetails.userHistory).to.be.undefined
61     }
62   })
63
64   it('Should watch the first and second video', async function () {
65     await userWatchVideo(server.url, server.accessToken, video1UUID, 3)
66     await userWatchVideo(server.url, server.accessToken, video2UUID, 8)
67   })
68
69   it('Should return the correct history when listing, searching and getting videos', async function () {
70     const videosOfVideos: Video[][] = []
71
72     {
73       const res = await getVideosListWithToken(server.url, server.accessToken)
74       videosOfVideos.push(res.body.data)
75     }
76
77     {
78       const res = await searchVideoWithToken(server.url, 'video', server.accessToken)
79       videosOfVideos.push(res.body.data)
80     }
81
82     for (const videos of videosOfVideos) {
83       const video1 = videos.find(v => v.uuid === video1UUID)
84       const video2 = videos.find(v => v.uuid === video2UUID)
85       const video3 = videos.find(v => v.uuid === video3UUID)
86
87       expect(video1.userHistory).to.not.be.undefined
88       expect(video1.userHistory.currentTime).to.equal(3)
89
90       expect(video2.userHistory).to.not.be.undefined
91       expect(video2.userHistory.currentTime).to.equal(8)
92
93       expect(video3.userHistory).to.be.undefined
94     }
95
96     {
97       const resDetail = await getVideoWithToken(server.url, server.accessToken, video1UUID)
98       const videoDetails: VideoDetails = resDetail.body
99
100       expect(videoDetails.userHistory).to.not.be.undefined
101       expect(videoDetails.userHistory.currentTime).to.equal(3)
102     }
103
104     {
105       const resDetail = await getVideoWithToken(server.url, server.accessToken, video2UUID)
106       const videoDetails: VideoDetails = resDetail.body
107
108       expect(videoDetails.userHistory).to.not.be.undefined
109       expect(videoDetails.userHistory.currentTime).to.equal(8)
110     }
111
112     {
113       const resDetail = await getVideoWithToken(server.url, server.accessToken, video3UUID)
114       const videoDetails: VideoDetails = resDetail.body
115
116       expect(videoDetails.userHistory).to.be.undefined
117     }
118   })
119
120   after(async function () {
121     killallServers([ server ])
122
123     // Keep the logs if the test failed
124     if (this['ok']) {
125       await flushTests()
126     }
127   })
128 })