aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/utils
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-02-13 18:17:05 +0100
committerChocobozzz <me@florianbigard.com>2018-02-14 16:03:09 +0100
commitac81d1a06d57b9ae86663831e7f5edcef57b0fa4 (patch)
treeda31775c9533d3e270f68f921e146f086bf7c0b8 /server/tests/utils
parente883399fa6caa56bb8519c9a2e22d88001f26661 (diff)
downloadPeerTube-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.ts15
-rw-r--r--server/tests/utils/requests/requests.ts19
-rw-r--r--server/tests/utils/users/users.ts4
-rw-r--r--server/tests/utils/videos/videos.ts83
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
3import { join } from 'path' 3import { isAbsolute, join } from 'path'
4import * as request from 'supertest' 4import * as request from 'supertest'
5import * as WebTorrent from 'webtorrent' 5import * as WebTorrent from 'webtorrent'
6import { readFileBufferPromise } from '../../../helpers/core-utils' 6import { 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
58function 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
60export { 68export {
@@ -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 @@
1import * as request from 'supertest' 1import * as request from 'supertest'
2import { buildAbsoluteFixturePath } from '../'
2 3
3function makeGetRequest (options: { 4function 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
43function makePostUploadRequest (options: { 44function 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
120export { 127export {
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 @@
1import { isAbsolute, join } from 'path' 1import { isAbsolute, join } from 'path'
2import * as request from 'supertest' 2import * as request from 'supertest'
3import { makePostBodyRequest, makePostUploadRequest, makePutBodyRequest } from '../' 3import { makePostBodyRequest, makeUploadRequest, makePutBodyRequest } from '../'
4 4
5import { UserRole } from '../../../../shared/index' 5import { 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'
5import * as parseTorrent from 'parse-torrent' 5import * as parseTorrent from 'parse-torrent'
6import { extname, isAbsolute, join } from 'path' 6import { extname, isAbsolute, join } from 'path'
7import * as request from 'supertest' 7import * as request from 'supertest'
8import { getMyUserInformation, makeGetRequest, root, ServerInfo, testImage } from '../' 8import {
9 buildAbsoluteFixturePath,
10 getMyUserInformation,
11 makeGetRequest,
12 makePutBodyRequest,
13 makeUploadRequest,
14 root,
15 ServerInfo,
16 testImage
17} from '../'
9import { VideoPrivacy } from '../../../../shared/models/videos' 18import { VideoPrivacy } from '../../../../shared/models/videos'
10import { readdirPromise } from '../../../helpers/core-utils' 19import { readdirPromise } from '../../../helpers/core-utils'
11import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../initializers' 20import { 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
28function getVideoCategories (url: string) { 39function 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
285function updateVideo (url: string, accessToken: string, id: number | string, attributes: VideoAttributes, specialStatus = 204) { 295function 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
307function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = 204) { 335function 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')