From 94565d52bb2883e09f16d1363170ac9c0dccb7a1 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 15 Apr 2019 15:26:15 +0200 Subject: Shared utils -> extra-utils Because they need dev dependencies --- shared/extra-utils/requests/activitypub.ts | 43 ++++++ shared/extra-utils/requests/check-api-params.ts | 40 ++++++ shared/extra-utils/requests/requests.ts | 184 ++++++++++++++++++++++++ 3 files changed, 267 insertions(+) create mode 100644 shared/extra-utils/requests/activitypub.ts create mode 100644 shared/extra-utils/requests/check-api-params.ts create mode 100644 shared/extra-utils/requests/requests.ts (limited to 'shared/extra-utils/requests') diff --git a/shared/extra-utils/requests/activitypub.ts b/shared/extra-utils/requests/activitypub.ts new file mode 100644 index 000000000..4762a8665 --- /dev/null +++ b/shared/extra-utils/requests/activitypub.ts @@ -0,0 +1,43 @@ +import { doRequest } from '../../../server/helpers/requests' +import { HTTP_SIGNATURE } from '../../../server/initializers/constants' +import { buildGlobalHeaders } from '../../../server/lib/job-queue/handlers/utils/activitypub-http-utils' +import { activityPubContextify } from '../../../server/helpers/activitypub' + +function makePOSTAPRequest (url: string, body: any, httpSignature: any, headers: any) { + const options = { + method: 'POST', + uri: url, + json: body, + httpSignature, + headers + } + + return doRequest(options) +} + +async function makeFollowRequest (to: { url: string }, by: { url: string, privateKey }) { + const follow = { + type: 'Follow', + id: by.url + '/toto', + actor: by.url, + object: to.url + } + + const body = activityPubContextify(follow) + + const httpSignature = { + algorithm: HTTP_SIGNATURE.ALGORITHM, + authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME, + keyId: by.url, + key: by.privateKey, + headers: HTTP_SIGNATURE.HEADERS_TO_SIGN + } + const headers = buildGlobalHeaders(body) + + return makePOSTAPRequest(to.url, body, httpSignature, headers) +} + +export { + makePOSTAPRequest, + makeFollowRequest +} diff --git a/shared/extra-utils/requests/check-api-params.ts b/shared/extra-utils/requests/check-api-params.ts new file mode 100644 index 000000000..a2a549682 --- /dev/null +++ b/shared/extra-utils/requests/check-api-params.ts @@ -0,0 +1,40 @@ +import { makeGetRequest } from './requests' +import { immutableAssign } from '../miscs/miscs' + +function checkBadStartPagination (url: string, path: string, token?: string, query = {}) { + return makeGetRequest({ + url, + path, + token, + query: immutableAssign(query, { start: 'hello' }), + statusCodeExpected: 400 + }) +} + +function checkBadCountPagination (url: string, path: string, token?: string, query = {}) { + return makeGetRequest({ + url, + path, + token, + query: immutableAssign(query, { count: 'hello' }), + statusCodeExpected: 400 + }) +} + +function checkBadSortPagination (url: string, path: string, token?: string, query = {}) { + return makeGetRequest({ + url, + path, + token, + query: immutableAssign(query, { sort: 'hello' }), + statusCodeExpected: 400 + }) +} + +// --------------------------------------------------------------------------- + +export { + checkBadStartPagination, + checkBadCountPagination, + checkBadSortPagination +} diff --git a/shared/extra-utils/requests/requests.ts b/shared/extra-utils/requests/requests.ts new file mode 100644 index 000000000..3532fb429 --- /dev/null +++ b/shared/extra-utils/requests/requests.ts @@ -0,0 +1,184 @@ +import * as request from 'supertest' +import { buildAbsoluteFixturePath, root } from '../miscs/miscs' +import { isAbsolute, join } from 'path' +import { parse } from 'url' + +function get4KFileUrl () { + return 'https://download.cpy.re/peertube/4k_file.txt' +} + +function makeRawRequest (url: string, statusCodeExpected?: number, range?: string) { + const { host, protocol, pathname } = parse(url) + + return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, statusCodeExpected, range }) +} + +function makeGetRequest (options: { + url: string, + path?: string, + query?: any, + token?: string, + statusCodeExpected?: number, + contentType?: string, + range?: string +}) { + if (!options.statusCodeExpected) options.statusCodeExpected = 400 + if (options.contentType === undefined) options.contentType = 'application/json' + + const req = request(options.url).get(options.path) + + if (options.contentType) req.set('Accept', options.contentType) + if (options.token) req.set('Authorization', 'Bearer ' + options.token) + if (options.query) req.query(options.query) + if (options.range) req.set('Range', options.range) + + return req.expect(options.statusCodeExpected) +} + +function makeDeleteRequest (options: { + url: string, + path: string, + token?: string, + statusCodeExpected?: number +}) { + if (!options.statusCodeExpected) options.statusCodeExpected = 400 + + const req = request(options.url) + .delete(options.path) + .set('Accept', 'application/json') + + if (options.token) req.set('Authorization', 'Bearer ' + options.token) + + return req.expect(options.statusCodeExpected) +} + +function makeUploadRequest (options: { + url: string, + method?: 'POST' | 'PUT', + path: string, + token?: string, + fields: { [ fieldName: string ]: any }, + attaches: { [ attachName: string ]: any | any[] }, + statusCodeExpected?: number +}) { + if (!options.statusCodeExpected) options.statusCodeExpected = 400 + + let req: request.Test + if (options.method === 'PUT') { + req = request(options.url).put(options.path) + } else { + req = request(options.url).post(options.path) + } + + req.set('Accept', 'application/json') + + if (options.token) req.set('Authorization', 'Bearer ' + options.token) + + Object.keys(options.fields).forEach(field => { + const value = options.fields[field] + + if (value === undefined) return + + if (Array.isArray(value)) { + for (let i = 0; i < value.length; i++) { + req.field(field + '[' + i + ']', value[i]) + } + } else { + req.field(field, value) + } + }) + + Object.keys(options.attaches).forEach(attach => { + const value = options.attaches[attach] + if (Array.isArray(value)) { + req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1]) + } else { + req.attach(attach, buildAbsoluteFixturePath(value)) + } + }) + + return req.expect(options.statusCodeExpected) +} + +function makePostBodyRequest (options: { + url: string, + path: string, + token?: string, + fields?: { [ fieldName: string ]: any }, + statusCodeExpected?: number +}) { + if (!options.fields) options.fields = {} + if (!options.statusCodeExpected) options.statusCodeExpected = 400 + + const req = request(options.url) + .post(options.path) + .set('Accept', 'application/json') + + if (options.token) req.set('Authorization', 'Bearer ' + options.token) + + return req.send(options.fields) + .expect(options.statusCodeExpected) +} + +function makePutBodyRequest (options: { + url: string, + path: string, + token?: string, + fields: { [ fieldName: string ]: any }, + statusCodeExpected?: number +}) { + if (!options.statusCodeExpected) options.statusCodeExpected = 400 + + const req = request(options.url) + .put(options.path) + .set('Accept', 'application/json') + + if (options.token) req.set('Authorization', 'Bearer ' + options.token) + + return req.send(options.fields) + .expect(options.statusCodeExpected) +} + +function makeHTMLRequest (url: string, path: string) { + return request(url) + .get(path) + .set('Accept', 'text/html') + .expect(200) +} + +function updateAvatarRequest (options: { + url: string, + path: string, + accessToken: string, + fixture: string +}) { + let filePath = '' + if (isAbsolute(options.fixture)) { + filePath = options.fixture + } else { + filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture) + } + + return makeUploadRequest({ + url: options.url, + path: options.path, + token: options.accessToken, + fields: {}, + attaches: { avatarfile: filePath }, + statusCodeExpected: 200 + }) +} + +// --------------------------------------------------------------------------- + +export { + get4KFileUrl, + makeHTMLRequest, + makeGetRequest, + makeUploadRequest, + makePostBodyRequest, + makePutBodyRequest, + makeDeleteRequest, + makeRawRequest, + updateAvatarRequest +} -- cgit v1.2.3