diff options
Diffstat (limited to 'server/tests/utils/videos.ts')
-rw-r--r-- | server/tests/utils/videos.ts | 254 |
1 files changed, 254 insertions, 0 deletions
diff --git a/server/tests/utils/videos.ts b/server/tests/utils/videos.ts new file mode 100644 index 000000000..42b7dd05a --- /dev/null +++ b/server/tests/utils/videos.ts | |||
@@ -0,0 +1,254 @@ | |||
1 | import * as request from 'supertest' | ||
2 | import { join, isAbsolute } from 'path' | ||
3 | |||
4 | import { makeGetRequest } from './requests' | ||
5 | import { readFilePromise } from './miscs' | ||
6 | |||
7 | type VideoAttributes = { | ||
8 | name?: string | ||
9 | category?: number | ||
10 | licence?: number | ||
11 | language?: number | ||
12 | nsfw?: boolean | ||
13 | description?: string | ||
14 | tags?: string[] | ||
15 | fixture?: string | ||
16 | } | ||
17 | |||
18 | function getVideoCategories (url: string) { | ||
19 | const path = '/api/v1/videos/categories' | ||
20 | |||
21 | return makeGetRequest(url, path) | ||
22 | } | ||
23 | |||
24 | function getVideoLicences (url: string) { | ||
25 | const path = '/api/v1/videos/licences' | ||
26 | |||
27 | return makeGetRequest(url, path) | ||
28 | } | ||
29 | |||
30 | function getVideoLanguages (url: string) { | ||
31 | const path = '/api/v1/videos/languages' | ||
32 | |||
33 | return makeGetRequest(url, path) | ||
34 | } | ||
35 | |||
36 | function getAllVideosListBy (url: string) { | ||
37 | const path = '/api/v1/videos' | ||
38 | |||
39 | return request(url) | ||
40 | .get(path) | ||
41 | .query({ sort: 'createdAt' }) | ||
42 | .query({ start: 0 }) | ||
43 | .query({ count: 10000 }) | ||
44 | .set('Accept', 'application/json') | ||
45 | .expect(200) | ||
46 | .expect('Content-Type', /json/) | ||
47 | } | ||
48 | |||
49 | function getVideo (url: string, id: number | string) { | ||
50 | const path = '/api/v1/videos/' + id | ||
51 | |||
52 | return request(url) | ||
53 | .get(path) | ||
54 | .set('Accept', 'application/json') | ||
55 | .expect(200) | ||
56 | .expect('Content-Type', /json/) | ||
57 | } | ||
58 | |||
59 | function getVideosList (url: string) { | ||
60 | const path = '/api/v1/videos' | ||
61 | |||
62 | return request(url) | ||
63 | .get(path) | ||
64 | .query({ sort: 'name' }) | ||
65 | .set('Accept', 'application/json') | ||
66 | .expect(200) | ||
67 | .expect('Content-Type', /json/) | ||
68 | } | ||
69 | |||
70 | function getVideosListPagination (url: string, start: number, count: number, sort?: string) { | ||
71 | const path = '/api/v1/videos' | ||
72 | |||
73 | const req = request(url) | ||
74 | .get(path) | ||
75 | .query({ start: start }) | ||
76 | .query({ count: count }) | ||
77 | |||
78 | if (sort) req.query({ sort }) | ||
79 | |||
80 | return req.set('Accept', 'application/json') | ||
81 | .expect(200) | ||
82 | .expect('Content-Type', /json/) | ||
83 | } | ||
84 | |||
85 | function getVideosListSort (url: string, sort: string) { | ||
86 | const path = '/api/v1/videos' | ||
87 | |||
88 | return request(url) | ||
89 | .get(path) | ||
90 | .query({ sort: sort }) | ||
91 | .set('Accept', 'application/json') | ||
92 | .expect(200) | ||
93 | .expect('Content-Type', /json/) | ||
94 | } | ||
95 | |||
96 | function removeVideo (url: string, token: string, id: number, expectedStatus = 204) { | ||
97 | const path = '/api/v1/videos' | ||
98 | |||
99 | return request(url) | ||
100 | .delete(path + '/' + id) | ||
101 | .set('Accept', 'application/json') | ||
102 | .set('Authorization', 'Bearer ' + token) | ||
103 | .expect(expectedStatus) | ||
104 | } | ||
105 | |||
106 | function searchVideo (url: string, search: string, field?: string) { | ||
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 }) | ||
113 | |||
114 | return req.expect(200) | ||
115 | .expect('Content-Type', /json/) | ||
116 | } | ||
117 | |||
118 | function searchVideoWithPagination (url: string, search: string, field: string, start: number, count: number, sort?: string) { | ||
119 | const path = '/api/v1/videos' | ||
120 | |||
121 | const req = request(url) | ||
122 | .get(path + '/search/' + search) | ||
123 | .query({ start }) | ||
124 | .query({ count }) | ||
125 | .query({ field }) | ||
126 | |||
127 | if (sort) req.query({ sort }) | ||
128 | |||
129 | return req.set('Accept', 'application/json') | ||
130 | .expect(200) | ||
131 | .expect('Content-Type', /json/) | ||
132 | } | ||
133 | |||
134 | function searchVideoWithSort (url: string, search: string, sort: string) { | ||
135 | const path = '/api/v1/videos' | ||
136 | |||
137 | return request(url) | ||
138 | .get(path + '/search/' + search) | ||
139 | .query({ sort }) | ||
140 | .set('Accept', 'application/json') | ||
141 | .expect(200) | ||
142 | .expect('Content-Type', /json/) | ||
143 | } | ||
144 | |||
145 | async function testVideoImage (url: string, imageName: string, imagePath: string) { | ||
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 | const res = await request(url) | ||
150 | .get(imagePath) | ||
151 | .expect(200) | ||
152 | |||
153 | const data = await readFilePromise(join(__dirname, '..', 'api', 'fixtures', imageName + '.jpg')) | ||
154 | |||
155 | return data.equals(res.body) | ||
156 | } else { | ||
157 | console.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.') | ||
158 | return true | ||
159 | } | ||
160 | } | ||
161 | |||
162 | function uploadVideo (url: string, accessToken: string, videoAttributesArg: VideoAttributes, specialStatus = 204) { | ||
163 | const path = '/api/v1/videos' | ||
164 | |||
165 | // Default attributes | ||
166 | let attributes = { | ||
167 | name: 'my super video', | ||
168 | category: 5, | ||
169 | licence: 4, | ||
170 | language: 3, | ||
171 | nsfw: true, | ||
172 | description: 'my super description', | ||
173 | tags: [ 'tag' ], | ||
174 | fixture: 'video_short.webm' | ||
175 | } | ||
176 | attributes = Object.assign(attributes, videoAttributesArg) | ||
177 | |||
178 | const req = request(url) | ||
179 | .post(path) | ||
180 | .set('Accept', 'application/json') | ||
181 | .set('Authorization', 'Bearer ' + accessToken) | ||
182 | .field('name', attributes.name) | ||
183 | .field('category', attributes.category.toString()) | ||
184 | .field('licence', attributes.licence.toString()) | ||
185 | .field('language', attributes.language.toString()) | ||
186 | .field('nsfw', JSON.stringify(attributes.nsfw)) | ||
187 | .field('description', attributes.description) | ||
188 | |||
189 | for (let i = 0; i < attributes.tags.length; i++) { | ||
190 | req.field('tags[' + i + ']', attributes.tags[i]) | ||
191 | } | ||
192 | |||
193 | let filepath = '' | ||
194 | if (isAbsolute(attributes.fixture)) { | ||
195 | filepath = attributes.fixture | ||
196 | } else { | ||
197 | filepath = join(__dirname, '..', 'api', 'fixtures', attributes.fixture) | ||
198 | } | ||
199 | |||
200 | return req.attach('videofile', filepath) | ||
201 | .expect(specialStatus) | ||
202 | } | ||
203 | |||
204 | function updateVideo (url: string, accessToken: string, id: number, attributes: VideoAttributes, specialStatus = 204) { | ||
205 | const path = '/api/v1/videos/' + id | ||
206 | const body = {} | ||
207 | |||
208 | if (attributes.name) body['name'] = attributes.name | ||
209 | if (attributes.category) body['category'] = attributes.category | ||
210 | if (attributes.licence) body['licence'] = attributes.licence | ||
211 | if (attributes.language) body['language'] = attributes.language | ||
212 | if (attributes.nsfw) body['nsfw'] = attributes.nsfw | ||
213 | if (attributes.description) body['description'] = attributes.description | ||
214 | if (attributes.tags) body['tags'] = attributes.tags | ||
215 | |||
216 | return request(url) | ||
217 | .put(path) | ||
218 | .send(body) | ||
219 | .set('Accept', 'application/json') | ||
220 | .set('Authorization', 'Bearer ' + accessToken) | ||
221 | .expect(specialStatus) | ||
222 | } | ||
223 | |||
224 | function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = 204) { | ||
225 | const path = '/api/v1/videos/' + id + '/rate' | ||
226 | |||
227 | return request(url) | ||
228 | .put(path) | ||
229 | .set('Accept', 'application/json') | ||
230 | .set('Authorization', 'Bearer ' + accessToken) | ||
231 | .send({ rating }) | ||
232 | .expect(specialStatus) | ||
233 | } | ||
234 | |||
235 | // --------------------------------------------------------------------------- | ||
236 | |||
237 | export { | ||
238 | getVideoCategories, | ||
239 | getVideoLicences, | ||
240 | getVideoLanguages, | ||
241 | getAllVideosListBy, | ||
242 | getVideo, | ||
243 | getVideosList, | ||
244 | getVideosListPagination, | ||
245 | getVideosListSort, | ||
246 | removeVideo, | ||
247 | searchVideo, | ||
248 | searchVideoWithPagination, | ||
249 | searchVideoWithSort, | ||
250 | testVideoImage, | ||
251 | uploadVideo, | ||
252 | updateVideo, | ||
253 | rateVideo | ||
254 | } | ||