Server: add ability to update a video
[oweals/peertube.git] / server / tests / utils / videos.js
1 'use strict'
2
3 const fs = require('fs')
4 const pathUtils = require('path')
5 const request = require('supertest')
6
7 const videosUtils = {
8   getAllVideosListBy,
9   getVideo,
10   getVideosList,
11   getVideosListPagination,
12   getVideosListSort,
13   removeVideo,
14   searchVideo,
15   searchVideoWithPagination,
16   searchVideoWithSort,
17   testVideoImage,
18   uploadVideo,
19   updateVideo
20 }
21
22 // ---------------------- Export functions --------------------
23
24 function getAllVideosListBy (url, end) {
25   const path = '/api/v1/videos'
26
27   request(url)
28     .get(path)
29     .query({ sort: 'createdAt' })
30     .query({ start: 0 })
31     .query({ count: 10000 })
32     .set('Accept', 'application/json')
33     .expect(200)
34     .expect('Content-Type', /json/)
35     .end(end)
36 }
37
38 function getVideo (url, id, end) {
39   const path = '/api/v1/videos/' + id
40
41   request(url)
42     .get(path)
43     .set('Accept', 'application/json')
44     .expect(200)
45     .expect('Content-Type', /json/)
46     .end(end)
47 }
48
49 function getVideosList (url, end) {
50   const path = '/api/v1/videos'
51
52   request(url)
53     .get(path)
54     .query({ sort: 'name' })
55     .set('Accept', 'application/json')
56     .expect(200)
57     .expect('Content-Type', /json/)
58     .end(end)
59 }
60
61 function getVideosListPagination (url, start, count, end) {
62   const path = '/api/v1/videos'
63
64   request(url)
65     .get(path)
66     .query({ start: start })
67     .query({ count: count })
68     .set('Accept', 'application/json')
69     .expect(200)
70     .expect('Content-Type', /json/)
71     .end(end)
72 }
73
74 function getVideosListSort (url, sort, end) {
75   const path = '/api/v1/videos'
76
77   request(url)
78     .get(path)
79     .query({ sort: sort })
80     .set('Accept', 'application/json')
81     .expect(200)
82     .expect('Content-Type', /json/)
83     .end(end)
84 }
85
86 function removeVideo (url, token, id, expectedStatus, end) {
87   if (!end) {
88     end = expectedStatus
89     expectedStatus = 204
90   }
91
92   const path = '/api/v1/videos'
93
94   request(url)
95     .delete(path + '/' + id)
96     .set('Accept', 'application/json')
97     .set('Authorization', 'Bearer ' + token)
98     .expect(expectedStatus)
99     .end(end)
100 }
101
102 function searchVideo (url, search, field, end) {
103   if (!end) {
104     end = field
105     field = null
106   }
107
108   const path = '/api/v1/videos'
109   const req = request(url)
110               .get(path + '/search/' + search)
111               .set('Accept', 'application/json')
112
113   if (field) req.query({ field: field })
114   req.expect(200)
115      .expect('Content-Type', /json/)
116      .end(end)
117 }
118
119 function searchVideoWithPagination (url, search, field, start, count, end) {
120   const path = '/api/v1/videos'
121
122   request(url)
123     .get(path + '/search/' + search)
124     .query({ start: start })
125     .query({ count: count })
126     .query({ field: field })
127     .set('Accept', 'application/json')
128     .expect(200)
129     .expect('Content-Type', /json/)
130     .end(end)
131 }
132
133 function searchVideoWithSort (url, search, sort, end) {
134   const path = '/api/v1/videos'
135
136   request(url)
137     .get(path + '/search/' + search)
138     .query({ sort: sort })
139     .set('Accept', 'application/json')
140     .expect(200)
141     .expect('Content-Type', /json/)
142     .end(end)
143 }
144
145 function testVideoImage (url, videoName, imagePath, callback) {
146   // Don't test images if the node env is not set
147   // Because we need a special ffmpeg version for this test
148   if (process.env.NODE_TEST_IMAGE) {
149     request(url)
150       .get(imagePath)
151       .expect(200)
152       .end(function (err, res) {
153         if (err) return callback(err)
154
155         fs.readFile(pathUtils.join(__dirname, '..', 'api', 'fixtures', videoName + '.jpg'), function (err, data) {
156           if (err) return callback(err)
157
158           callback(null, data.equals(res.body))
159         })
160       })
161   } else {
162     console.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.')
163     callback(null, true)
164   }
165 }
166
167 function uploadVideo (url, accessToken, name, description, tags, fixture, specialStatus, end) {
168   if (!end) {
169     end = specialStatus
170     specialStatus = 204
171   }
172
173   const path = '/api/v1/videos'
174
175   const req = request(url)
176               .post(path)
177               .set('Accept', 'application/json')
178               .set('Authorization', 'Bearer ' + accessToken)
179               .field('name', name)
180               .field('description', description)
181
182   for (let i = 0; i < tags.length; i++) {
183     req.field('tags[' + i + ']', tags[i])
184   }
185
186   let filepath = ''
187   if (pathUtils.isAbsolute(fixture)) {
188     filepath = fixture
189   } else {
190     filepath = pathUtils.join(__dirname, '..', 'api', 'fixtures', fixture)
191   }
192
193   req.attach('videofile', filepath)
194      .expect(specialStatus)
195      .end(end)
196 }
197
198 function updateVideo (url, accessToken, id, name, description, tags, specialStatus, end) {
199   if (!end) {
200     end = specialStatus
201     specialStatus = 204
202   }
203
204   const path = '/api/v1/videos/' + id
205
206   const req = request(url)
207               .put(path)
208               .set('Accept', 'application/json')
209               .set('Authorization', 'Bearer ' + accessToken)
210
211   if (name) req.field('name', name)
212   if (description) req.field('description', description)
213
214   if (tags) {
215     for (let i = 0; i < tags.length; i++) {
216       req.field('tags[' + i + ']', tags[i])
217     }
218   }
219
220   req.expect(specialStatus).end(end)
221 }
222
223 // ---------------------------------------------------------------------------
224
225 module.exports = videosUtils