diff options
author | Chocobozzz <me@florianbigard.com> | 2018-02-13 18:17:05 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-02-14 16:03:09 +0100 |
commit | ac81d1a06d57b9ae86663831e7f5edcef57b0fa4 (patch) | |
tree | da31775c9533d3e270f68f921e146f086bf7c0b8 /server/tests/utils | |
parent | e883399fa6caa56bb8519c9a2e22d88001f26661 (diff) | |
download | PeerTube-ac81d1a06d57b9ae86663831e7f5edcef57b0fa4.tar.gz PeerTube-ac81d1a06d57b9ae86663831e7f5edcef57b0fa4.tar.zst PeerTube-ac81d1a06d57b9ae86663831e7f5edcef57b0fa4.zip |
Add ability to set video thumbnail/preview
Diffstat (limited to 'server/tests/utils')
-rw-r--r-- | server/tests/utils/miscs/miscs.ts | 15 | ||||
-rw-r--r-- | server/tests/utils/requests/requests.ts | 19 | ||||
-rw-r--r-- | server/tests/utils/users/users.ts | 4 | ||||
-rw-r--r-- | server/tests/utils/videos/videos.ts | 83 |
4 files changed, 87 insertions, 34 deletions
diff --git a/server/tests/utils/miscs/miscs.ts b/server/tests/utils/miscs/miscs.ts index 99d109bfe..24cbf59ca 100644 --- a/server/tests/utils/miscs/miscs.ts +++ b/server/tests/utils/miscs/miscs.ts | |||
@@ -1,6 +1,6 @@ | |||
1 | /* tslint:disable:no-unused-expression */ | 1 | /* tslint:disable:no-unused-expression */ |
2 | 2 | ||
3 | import { join } from 'path' | 3 | import { isAbsolute, join } from 'path' |
4 | import * as request from 'supertest' | 4 | import * as request from 'supertest' |
5 | import * as WebTorrent from 'webtorrent' | 5 | import * as WebTorrent from 'webtorrent' |
6 | import { readFileBufferPromise } from '../../../helpers/core-utils' | 6 | import { readFileBufferPromise } from '../../../helpers/core-utils' |
@@ -45,8 +45,8 @@ async function testImage (url: string, imageName: string, imagePath: string, ext | |||
45 | const body = res.body | 45 | const body = res.body |
46 | 46 | ||
47 | const data = await readFileBufferPromise(join(__dirname, '..', '..', 'api', 'fixtures', imageName + extension)) | 47 | const data = await readFileBufferPromise(join(__dirname, '..', '..', 'api', 'fixtures', imageName + extension)) |
48 | const minLength = body.length - ((50 * body.length) / 100) | 48 | const minLength = body.length - ((20 * body.length) / 100) |
49 | const maxLength = body.length + ((50 * body.length) / 100) | 49 | const maxLength = body.length + ((20 * body.length) / 100) |
50 | 50 | ||
51 | return data.length > minLength && data.length < maxLength | 51 | return data.length > minLength && data.length < maxLength |
52 | } else { | 52 | } else { |
@@ -55,6 +55,14 @@ async function testImage (url: string, imageName: string, imagePath: string, ext | |||
55 | } | 55 | } |
56 | } | 56 | } |
57 | 57 | ||
58 | function buildAbsoluteFixturePath (path: string) { | ||
59 | if (isAbsolute(path)) { | ||
60 | return path | ||
61 | } | ||
62 | |||
63 | return join(__dirname, '..', '..', 'api', 'fixtures', path) | ||
64 | } | ||
65 | |||
58 | // --------------------------------------------------------------------------- | 66 | // --------------------------------------------------------------------------- |
59 | 67 | ||
60 | export { | 68 | export { |
@@ -63,5 +71,6 @@ export { | |||
63 | webtorrentAdd, | 71 | webtorrentAdd, |
64 | immutableAssign, | 72 | immutableAssign, |
65 | testImage, | 73 | testImage, |
74 | buildAbsoluteFixturePath, | ||
66 | root | 75 | root |
67 | } | 76 | } |
diff --git a/server/tests/utils/requests/requests.ts b/server/tests/utils/requests/requests.ts index 840072430..a9b1dff9a 100644 --- a/server/tests/utils/requests/requests.ts +++ b/server/tests/utils/requests/requests.ts | |||
@@ -1,4 +1,5 @@ | |||
1 | import * as request from 'supertest' | 1 | import * as request from 'supertest' |
2 | import { buildAbsoluteFixturePath } from '../' | ||
2 | 3 | ||
3 | function makeGetRequest (options: { | 4 | function makeGetRequest (options: { |
4 | url: string, | 5 | url: string, |
@@ -40,8 +41,9 @@ function makeDeleteRequest (options: { | |||
40 | .expect(options.statusCodeExpected) | 41 | .expect(options.statusCodeExpected) |
41 | } | 42 | } |
42 | 43 | ||
43 | function makePostUploadRequest (options: { | 44 | function makeUploadRequest (options: { |
44 | url: string, | 45 | url: string, |
46 | method?: 'POST' | 'PUT', | ||
45 | path: string, | 47 | path: string, |
46 | token: string, | 48 | token: string, |
47 | fields: { [ fieldName: string ]: any }, | 49 | fields: { [ fieldName: string ]: any }, |
@@ -50,9 +52,14 @@ function makePostUploadRequest (options: { | |||
50 | }) { | 52 | }) { |
51 | if (!options.statusCodeExpected) options.statusCodeExpected = 400 | 53 | if (!options.statusCodeExpected) options.statusCodeExpected = 400 |
52 | 54 | ||
53 | const req = request(options.url) | 55 | let req: request.Test |
54 | .post(options.path) | 56 | if (options.method === 'PUT') { |
55 | .set('Accept', 'application/json') | 57 | req = request(options.url).put(options.path) |
58 | } else { | ||
59 | req = request(options.url).post(options.path) | ||
60 | } | ||
61 | |||
62 | req.set('Accept', 'application/json') | ||
56 | 63 | ||
57 | if (options.token) req.set('Authorization', 'Bearer ' + options.token) | 64 | if (options.token) req.set('Authorization', 'Bearer ' + options.token) |
58 | 65 | ||
@@ -70,7 +77,7 @@ function makePostUploadRequest (options: { | |||
70 | 77 | ||
71 | Object.keys(options.attaches).forEach(attach => { | 78 | Object.keys(options.attaches).forEach(attach => { |
72 | const value = options.attaches[attach] | 79 | const value = options.attaches[attach] |
73 | req.attach(attach, value) | 80 | req.attach(attach, buildAbsoluteFixturePath(value)) |
74 | }) | 81 | }) |
75 | 82 | ||
76 | return req.expect(options.statusCodeExpected) | 83 | return req.expect(options.statusCodeExpected) |
@@ -119,7 +126,7 @@ function makePutBodyRequest (options: { | |||
119 | 126 | ||
120 | export { | 127 | export { |
121 | makeGetRequest, | 128 | makeGetRequest, |
122 | makePostUploadRequest, | 129 | makeUploadRequest, |
123 | makePostBodyRequest, | 130 | makePostBodyRequest, |
124 | makePutBodyRequest, | 131 | makePutBodyRequest, |
125 | makeDeleteRequest | 132 | makeDeleteRequest |
diff --git a/server/tests/utils/users/users.ts b/server/tests/utils/users/users.ts index 9e33e6796..3c9d46246 100644 --- a/server/tests/utils/users/users.ts +++ b/server/tests/utils/users/users.ts | |||
@@ -1,6 +1,6 @@ | |||
1 | import { isAbsolute, join } from 'path' | 1 | import { isAbsolute, join } from 'path' |
2 | import * as request from 'supertest' | 2 | import * as request from 'supertest' |
3 | import { makePostBodyRequest, makePostUploadRequest, makePutBodyRequest } from '../' | 3 | import { makePostBodyRequest, makeUploadRequest, makePutBodyRequest } from '../' |
4 | 4 | ||
5 | import { UserRole } from '../../../../shared/index' | 5 | import { UserRole } from '../../../../shared/index' |
6 | 6 | ||
@@ -162,7 +162,7 @@ function updateMyAvatar (options: { | |||
162 | filePath = join(__dirname, '..', '..', 'api', 'fixtures', options.fixture) | 162 | filePath = join(__dirname, '..', '..', 'api', 'fixtures', options.fixture) |
163 | } | 163 | } |
164 | 164 | ||
165 | return makePostUploadRequest({ | 165 | return makeUploadRequest({ |
166 | url: options.url, | 166 | url: options.url, |
167 | path, | 167 | path, |
168 | token: options.accessToken, | 168 | token: options.accessToken, |
diff --git a/server/tests/utils/videos/videos.ts b/server/tests/utils/videos/videos.ts index 9105b5f13..9d4267db8 100644 --- a/server/tests/utils/videos/videos.ts +++ b/server/tests/utils/videos/videos.ts | |||
@@ -5,7 +5,16 @@ import { existsSync, readFile } from 'fs' | |||
5 | import * as parseTorrent from 'parse-torrent' | 5 | import * as parseTorrent from 'parse-torrent' |
6 | import { extname, isAbsolute, join } from 'path' | 6 | import { extname, isAbsolute, join } from 'path' |
7 | import * as request from 'supertest' | 7 | import * as request from 'supertest' |
8 | import { getMyUserInformation, makeGetRequest, root, ServerInfo, testImage } from '../' | 8 | import { |
9 | buildAbsoluteFixturePath, | ||
10 | getMyUserInformation, | ||
11 | makeGetRequest, | ||
12 | makePutBodyRequest, | ||
13 | makeUploadRequest, | ||
14 | root, | ||
15 | ServerInfo, | ||
16 | testImage | ||
17 | } from '../' | ||
9 | import { VideoPrivacy } from '../../../../shared/models/videos' | 18 | import { VideoPrivacy } from '../../../../shared/models/videos' |
10 | import { readdirPromise } from '../../../helpers/core-utils' | 19 | import { readdirPromise } from '../../../helpers/core-utils' |
11 | import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../initializers' | 20 | import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../initializers' |
@@ -23,6 +32,8 @@ type VideoAttributes = { | |||
23 | channelId?: number | 32 | channelId?: number |
24 | privacy?: VideoPrivacy | 33 | privacy?: VideoPrivacy |
25 | fixture?: string | 34 | fixture?: string |
35 | thumbnailfile?: string | ||
36 | previewfile?: string | ||
26 | } | 37 | } |
27 | 38 | ||
28 | function getVideoCategories (url: string) { | 39 | function getVideoCategories (url: string) { |
@@ -228,8 +239,8 @@ async function uploadVideo (url: string, accessToken: string, videoAttributesArg | |||
228 | defaultChannelId = res.body.videoChannels[0].id | 239 | defaultChannelId = res.body.videoChannels[0].id |
229 | } catch (e) { /* empty */ } | 240 | } catch (e) { /* empty */ } |
230 | 241 | ||
231 | // Default attributes | 242 | // Override default attributes |
232 | let attributes = { | 243 | const attributes = Object.assign({ |
233 | name: 'my super video', | 244 | name: 'my super video', |
234 | category: 5, | 245 | category: 5, |
235 | licence: 4, | 246 | licence: 4, |
@@ -241,8 +252,7 @@ async function uploadVideo (url: string, accessToken: string, videoAttributesArg | |||
241 | privacy: VideoPrivacy.PUBLIC, | 252 | privacy: VideoPrivacy.PUBLIC, |
242 | commentsEnabled: true, | 253 | commentsEnabled: true, |
243 | fixture: 'video_short.webm' | 254 | fixture: 'video_short.webm' |
244 | } | 255 | }, videoAttributesArg) |
245 | attributes = Object.assign(attributes, videoAttributesArg) | ||
246 | 256 | ||
247 | const req = request(url) | 257 | const req = request(url) |
248 | .post(path) | 258 | .post(path) |
@@ -267,22 +277,22 @@ async function uploadVideo (url: string, accessToken: string, videoAttributesArg | |||
267 | req.field('licence', attributes.licence.toString()) | 277 | req.field('licence', attributes.licence.toString()) |
268 | } | 278 | } |
269 | 279 | ||
270 | for (let i = 0; i < attributes.tags.length; i++) { | 280 | if (attributes.thumbnailfile !== undefined) { |
271 | req.field('tags[' + i + ']', attributes.tags[i]) | 281 | req.attach('thumbnailfile', buildAbsoluteFixturePath(attributes.thumbnailfile)) |
282 | } | ||
283 | if (attributes.previewfile !== undefined) { | ||
284 | req.attach('previewfile', buildAbsoluteFixturePath(attributes.previewfile)) | ||
272 | } | 285 | } |
273 | 286 | ||
274 | let filePath = '' | 287 | for (let i = 0; i < attributes.tags.length; i++) { |
275 | if (isAbsolute(attributes.fixture)) { | 288 | req.field('tags[' + i + ']', attributes.tags[i]) |
276 | filePath = attributes.fixture | ||
277 | } else { | ||
278 | filePath = join(__dirname, '..', '..', 'api', 'fixtures', attributes.fixture) | ||
279 | } | 289 | } |
280 | 290 | ||
281 | return req.attach('videofile', filePath) | 291 | return req.attach('videofile', buildAbsoluteFixturePath(attributes.fixture)) |
282 | .expect(specialStatus) | 292 | .expect(specialStatus) |
283 | } | 293 | } |
284 | 294 | ||
285 | function updateVideo (url: string, accessToken: string, id: number | string, attributes: VideoAttributes, specialStatus = 204) { | 295 | function updateVideo (url: string, accessToken: string, id: number | string, attributes: VideoAttributes, statusCodeExpected = 204) { |
286 | const path = '/api/v1/videos/' + id | 296 | const path = '/api/v1/videos/' + id |
287 | const body = {} | 297 | const body = {} |
288 | 298 | ||
@@ -296,12 +306,30 @@ function updateVideo (url: string, accessToken: string, id: number | string, att | |||
296 | if (attributes.tags) body['tags'] = attributes.tags | 306 | if (attributes.tags) body['tags'] = attributes.tags |
297 | if (attributes.privacy) body['privacy'] = attributes.privacy | 307 | if (attributes.privacy) body['privacy'] = attributes.privacy |
298 | 308 | ||
299 | return request(url) | 309 | // Upload request |
300 | .put(path) | 310 | if (attributes.thumbnailfile || attributes.previewfile) { |
301 | .send(body) | 311 | const attaches: any = {} |
302 | .set('Accept', 'application/json') | 312 | if (attributes.thumbnailfile) attaches.thumbnailfile = attributes.thumbnailfile |
303 | .set('Authorization', 'Bearer ' + accessToken) | 313 | if (attributes.previewfile) attaches.previewfile = attributes.previewfile |
304 | .expect(specialStatus) | 314 | |
315 | return makeUploadRequest({ | ||
316 | url, | ||
317 | method: 'PUT', | ||
318 | path, | ||
319 | token: accessToken, | ||
320 | fields: body, | ||
321 | attaches, | ||
322 | statusCodeExpected | ||
323 | }) | ||
324 | } | ||
325 | |||
326 | return makePutBodyRequest({ | ||
327 | url, | ||
328 | path, | ||
329 | fields: body, | ||
330 | token: accessToken, | ||
331 | statusCodeExpected | ||
332 | }) | ||
305 | } | 333 | } |
306 | 334 | ||
307 | function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = 204) { | 335 | function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = 204) { |
@@ -355,7 +383,9 @@ async function completeVideoCheck ( | |||
355 | files: { | 383 | files: { |
356 | resolution: number | 384 | resolution: number |
357 | size: number | 385 | size: number |
358 | }[] | 386 | }[], |
387 | thumbnailfile?: string | ||
388 | previewfile?: string | ||
359 | } | 389 | } |
360 | ) { | 390 | ) { |
361 | if (!attributes.likes) attributes.likes = 0 | 391 | if (!attributes.likes) attributes.likes = 0 |
@@ -414,8 +444,15 @@ async function completeVideoCheck ( | |||
414 | const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100) | 444 | const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100) |
415 | expect(file.size).to.be.above(minSize).and.below(maxSize) | 445 | expect(file.size).to.be.above(minSize).and.below(maxSize) |
416 | 446 | ||
417 | const test = await testImage(url, attributes.fixture, videoDetails.thumbnailPath) | 447 | { |
418 | expect(test).to.equal(true) | 448 | const test = await testImage(url, attributes.thumbnailfile || attributes.fixture, videoDetails.thumbnailPath) |
449 | expect(test).to.equal(true) | ||
450 | } | ||
451 | |||
452 | if (attributes.previewfile) { | ||
453 | const test = await testImage(url, attributes.previewfile, videoDetails.previewPath) | ||
454 | expect(test).to.equal(true) | ||
455 | } | ||
419 | 456 | ||
420 | const torrent = await webtorrentAdd(magnetUri, true) | 457 | const torrent = await webtorrentAdd(magnetUri, true) |
421 | expect(torrent.files).to.be.an('array') | 458 | expect(torrent.files).to.be.an('array') |