aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/utils/videos.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-10-02 15:39:09 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-10-02 15:39:09 +0200
commita6375e69668ea42e19531c6bc68dcd37f3f7cbd7 (patch)
tree03204a408d56311692c3528bedcf95d2455e94f2 /server/tests/utils/videos.js
parent052937db8a8d282eccdbdf38d487ed8d85d3c0a7 (diff)
parentc4403b29ad4db097af528a7f04eea07e0ed320d0 (diff)
downloadPeerTube-a6375e69668ea42e19531c6bc68dcd37f3f7cbd7.tar.gz
PeerTube-a6375e69668ea42e19531c6bc68dcd37f3f7cbd7.tar.zst
PeerTube-a6375e69668ea42e19531c6bc68dcd37f3f7cbd7.zip
Merge branch 'master' into webseed-merged
Diffstat (limited to 'server/tests/utils/videos.js')
-rw-r--r--server/tests/utils/videos.js199
1 files changed, 199 insertions, 0 deletions
diff --git a/server/tests/utils/videos.js b/server/tests/utils/videos.js
new file mode 100644
index 000000000..536093db1
--- /dev/null
+++ b/server/tests/utils/videos.js
@@ -0,0 +1,199 @@
1'use strict'
2
3const fs = require('fs')
4const pathUtils = require('path')
5const request = require('supertest')
6
7const 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
23function getAllVideosListBy (url, end) {
24 const path = '/api/v1/videos'
25
26 request(url)
27 .get(path)
28 .query({ sort: 'createdDate' })
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
37function 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
48function 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
60function 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
73function 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
85function 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
101function 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
118function 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
132function 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
144function 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
166function 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
199module.exports = videosUtils