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