From c0e8b12e7fd554ba4d2ceb0c4900804c6a4c63ea Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 16 Jul 2021 10:42:24 +0200 Subject: Refactor requests --- shared/extra-utils/bulk/bulk-command.ts | 4 +- .../custom-pages/custom-pages-command.ts | 2 +- shared/extra-utils/feeds/feeds-command.ts | 2 +- shared/extra-utils/logs/logs-command.ts | 2 +- shared/extra-utils/miscs/checks.ts | 4 +- shared/extra-utils/moderation/abuses-command.ts | 2 +- shared/extra-utils/overviews/overviews-command.ts | 2 +- shared/extra-utils/requests/check-api-params.ts | 10 +- shared/extra-utils/requests/requests.ts | 256 ++++++++------------- shared/extra-utils/search/search-command.ts | 2 +- shared/extra-utils/server/config-command.ts | 4 +- shared/extra-utils/server/contact-form-command.ts | 2 +- shared/extra-utils/server/debug-command.ts | 2 +- shared/extra-utils/server/follows-command.ts | 2 +- shared/extra-utils/server/jobs-command.ts | 2 +- shared/extra-utils/server/plugins-command.ts | 2 +- shared/extra-utils/server/redundancy-command.ts | 2 +- shared/extra-utils/server/server.ts | 2 +- shared/extra-utils/server/servers-command.ts | 2 +- shared/extra-utils/server/stats-command.ts | 2 +- shared/extra-utils/shared/abstract-command.ts | 66 +++--- shared/extra-utils/users/accounts-command.ts | 2 +- shared/extra-utils/users/blocklist-command.ts | 2 +- shared/extra-utils/users/index.ts | 1 - shared/extra-utils/users/login-command.ts | 10 +- shared/extra-utils/users/notifications-command.ts | 2 +- shared/extra-utils/users/subscriptions-command.ts | 2 +- shared/extra-utils/users/users-command.ts | 2 +- shared/extra-utils/users/users.ts | 20 -- shared/extra-utils/videos/blacklist-command.ts | 2 +- shared/extra-utils/videos/captions-command.ts | 2 +- shared/extra-utils/videos/captions.ts | 2 +- .../extra-utils/videos/change-ownership-command.ts | 2 +- shared/extra-utils/videos/channels-command.ts | 2 +- shared/extra-utils/videos/comments-command.ts | 2 +- shared/extra-utils/videos/history-command.ts | 2 +- shared/extra-utils/videos/imports-command.ts | 2 +- shared/extra-utils/videos/live-command.ts | 2 +- shared/extra-utils/videos/playlists-command.ts | 2 +- shared/extra-utils/videos/services-command.ts | 2 +- .../videos/streaming-playlists-command.ts | 3 +- shared/extra-utils/videos/streaming-playlists.ts | 2 +- shared/extra-utils/videos/videos-command.ts | 36 +-- shared/extra-utils/videos/videos.ts | 2 +- 44 files changed, 200 insertions(+), 280 deletions(-) delete mode 100644 shared/extra-utils/users/users.ts (limited to 'shared/extra-utils') diff --git a/shared/extra-utils/bulk/bulk-command.ts b/shared/extra-utils/bulk/bulk-command.ts index 6dac6034f..b5c5673ce 100644 --- a/shared/extra-utils/bulk/bulk-command.ts +++ b/shared/extra-utils/bulk/bulk-command.ts @@ -1,6 +1,4 @@ - -import { BulkRemoveCommentsOfBody } from '@shared/models/bulk/bulk-remove-comments-of-body.model' -import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' +import { BulkRemoveCommentsOfBody, HttpStatusCode } from '@shared/models' import { AbstractCommand, OverrideCommandOptions } from '../shared' export class BulkCommand extends AbstractCommand { diff --git a/shared/extra-utils/custom-pages/custom-pages-command.ts b/shared/extra-utils/custom-pages/custom-pages-command.ts index 0dd77503e..6042233d4 100644 --- a/shared/extra-utils/custom-pages/custom-pages-command.ts +++ b/shared/extra-utils/custom-pages/custom-pages-command.ts @@ -1,5 +1,5 @@ import { CustomPage } from '@shared/models' -import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' +import { HttpStatusCode } from '@shared/models' import { AbstractCommand, OverrideCommandOptions } from '../shared' export class CustomPagesCommand extends AbstractCommand { diff --git a/shared/extra-utils/feeds/feeds-command.ts b/shared/extra-utils/feeds/feeds-command.ts index 2da4110a7..3c95f9536 100644 --- a/shared/extra-utils/feeds/feeds-command.ts +++ b/shared/extra-utils/feeds/feeds-command.ts @@ -1,5 +1,5 @@ -import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' +import { HttpStatusCode } from '@shared/models' import { AbstractCommand, OverrideCommandOptions } from '../shared' type FeedType = 'videos' | 'video-comments' | 'subscriptions' diff --git a/shared/extra-utils/logs/logs-command.ts b/shared/extra-utils/logs/logs-command.ts index f34c58c47..5912e814f 100644 --- a/shared/extra-utils/logs/logs-command.ts +++ b/shared/extra-utils/logs/logs-command.ts @@ -1,4 +1,4 @@ -import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' +import { HttpStatusCode } from '@shared/models' import { LogLevel } from '../../models/server/log-level.type' import { AbstractCommand, OverrideCommandOptions } from '../shared' diff --git a/shared/extra-utils/miscs/checks.ts b/shared/extra-utils/miscs/checks.ts index c81460330..7fc92f804 100644 --- a/shared/extra-utils/miscs/checks.ts +++ b/shared/extra-utils/miscs/checks.ts @@ -4,7 +4,7 @@ import { expect } from 'chai' import { pathExists, readFile } from 'fs-extra' import { join } from 'path' import { root } from '@server/helpers/core-utils' -import { HttpStatusCode } from '@shared/core-utils' +import { HttpStatusCode } from '@shared/models' import { makeGetRequest } from '../requests' import { PeerTubeServer } from '../server' @@ -20,7 +20,7 @@ async function testImage (url: string, imageName: string, imagePath: string, ext const res = await makeGetRequest({ url, path: imagePath, - statusCodeExpected: HttpStatusCode.OK_200 + expectedStatus: HttpStatusCode.OK_200 }) const body = res.body diff --git a/shared/extra-utils/moderation/abuses-command.ts b/shared/extra-utils/moderation/abuses-command.ts index 03da0c85a..72f2c9951 100644 --- a/shared/extra-utils/moderation/abuses-command.ts +++ b/shared/extra-utils/moderation/abuses-command.ts @@ -10,7 +10,7 @@ import { ResultList, UserAbuse } from '@shared/models' -import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' +import { HttpStatusCode } from '@shared/models' import { AbstractCommand, OverrideCommandOptions } from '../shared' import { unwrapBody } from '../requests/requests' diff --git a/shared/extra-utils/overviews/overviews-command.ts b/shared/extra-utils/overviews/overviews-command.ts index e15644d94..d4a2ac254 100644 --- a/shared/extra-utils/overviews/overviews-command.ts +++ b/shared/extra-utils/overviews/overviews-command.ts @@ -1,4 +1,4 @@ -import { HttpStatusCode } from '@shared/core-utils' +import { HttpStatusCode } from '@shared/models' import { VideosOverview } from '@shared/models' import { AbstractCommand, OverrideCommandOptions } from '../shared' diff --git a/shared/extra-utils/requests/check-api-params.ts b/shared/extra-utils/requests/check-api-params.ts index 7df63b004..26ba1e913 100644 --- a/shared/extra-utils/requests/check-api-params.ts +++ b/shared/extra-utils/requests/check-api-params.ts @@ -1,4 +1,4 @@ -import { HttpStatusCode } from '@shared/core-utils' +import { HttpStatusCode } from '@shared/models' import { makeGetRequest } from './requests' function checkBadStartPagination (url: string, path: string, token?: string, query = {}) { @@ -7,7 +7,7 @@ function checkBadStartPagination (url: string, path: string, token?: string, que path, token, query: { ...query, start: 'hello' }, - statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 + expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) } @@ -17,7 +17,7 @@ async function checkBadCountPagination (url: string, path: string, token?: strin path, token, query: { ...query, count: 'hello' }, - statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 + expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) await makeGetRequest({ @@ -25,7 +25,7 @@ async function checkBadCountPagination (url: string, path: string, token?: strin path, token, query: { ...query, count: 2000 }, - statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 + expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) } @@ -35,7 +35,7 @@ function checkBadSortPagination (url: string, path: string, token?: string, quer path, token, query: { ...query, sort: 'hello' }, - statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 + expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) } diff --git a/shared/extra-utils/requests/requests.ts b/shared/extra-utils/requests/requests.ts index 60c9b938b..70f790222 100644 --- a/shared/extra-utils/requests/requests.ts +++ b/shared/extra-utils/requests/requests.ts @@ -1,111 +1,82 @@ -/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */ +/* eslint-disable @typescript-eslint/no-floating-promises */ -import { isAbsolute, join } from 'path' import { decode } from 'querystring' import * as request from 'supertest' import { URL } from 'url' -import { HttpStatusCode } from '@shared/core-utils' -import { buildAbsoluteFixturePath, root } from '../miscs/tests' +import { HttpStatusCode } from '@shared/models' +import { buildAbsoluteFixturePath } from '../miscs/tests' -function makeRawRequest (url: string, statusCodeExpected?: HttpStatusCode, range?: string) { - const { host, protocol, pathname } = new URL(url) - - return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, statusCodeExpected, range }) -} - -function makeGetRequest (options: { +export type CommonRequestParams = { url: string path?: string - query?: any - token?: string - statusCodeExpected?: HttpStatusCode contentType?: string range?: string redirects?: number accept?: string host?: string -}) { - if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 - if (options.contentType === undefined) options.contentType = 'application/json' - - const req = request(options.url).get(options.path) + token?: string + headers?: { [ name: string ]: string } + type?: string + xForwardedFor?: string + expectedStatus?: HttpStatusCode +} - 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) - if (options.accept) req.set('Accept', options.accept) - if (options.host) req.set('Host', options.host) - if (options.redirects) req.redirects(options.redirects) +function makeRawRequest (url: string, expectedStatus?: HttpStatusCode, range?: string) { + const { host, protocol, pathname } = new URL(url) - return req.expect(options.statusCodeExpected) + return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, expectedStatus, range }) } -function makeDeleteRequest (options: { - url: string - path: string - token?: string - statusCodeExpected?: HttpStatusCode +function makeGetRequest (options: CommonRequestParams & { + query?: any }) { - if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 + const req = request(options.url).get(options.path) + .query(options.query) - const req = request(options.url) - .delete(options.path) - .set('Accept', 'application/json') + return buildRequest(req, { contentType: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options }) +} - if (options.token) req.set('Authorization', 'Bearer ' + options.token) +function makeHTMLRequest (url: string, path: string) { + return makeGetRequest({ + url, + path, + accept: 'text/html', + expectedStatus: HttpStatusCode.OK_200 + }) +} - return req.expect(options.statusCodeExpected) +function makeActivityPubGetRequest (url: string, path: string, expectedStatus = HttpStatusCode.OK_200) { + return makeGetRequest({ + url, + path, + expectedStatus: expectedStatus, + accept: 'application/activity+json,text/html;q=0.9,\\*/\\*;q=0.8' + }) } -function makeUploadRequest (options: { - url: string +function makeDeleteRequest (options: CommonRequestParams) { + const req = request(options.url).delete(options.path) + + return buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options }) +} + +function makeUploadRequest (options: CommonRequestParams & { method?: 'POST' | 'PUT' - path: string - token?: string fields: { [ fieldName: string ]: any } attaches?: { [ attachName: string ]: any | any[] } - - headers?: { [ name: string ]: string } - - statusCodeExpected?: HttpStatusCode }) { - if (options.statusCodeExpected === undefined) { - options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 - } + let req = options.method === 'PUT' + ? request(options.url).put(options.path) + : request(options.url).post(options.path) - let req: request.Test - if (options.method === 'PUT') { - req = request(options.url).put(options.path) - } else { - req = request(options.url).post(options.path) - } + req = buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options }) - req.set('Accept', 'application/json') - - if (options.token) req.set('Authorization', 'Bearer ' + options.token) - - Object.keys(options.headers || {}).forEach(name => { - req.set(name, options.headers[name]) - }) - - 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) - } - }) + buildFields(req, options.fields) Object.keys(options.attaches || {}).forEach(attach => { const value = options.attaches[attach] + if (Array.isArray(value)) { req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1]) } else { @@ -113,40 +84,16 @@ function makeUploadRequest (options: { } }) - if (options.statusCodeExpected) { - req.expect(options.statusCodeExpected) - } - return req } -function makePostBodyRequest (options: { - url: string - path: string - token?: string +function makePostBodyRequest (options: CommonRequestParams & { fields?: { [ fieldName: string ]: any } - headers?: { [ name: string ]: string } - type?: string - xForwardedFor?: string - statusCodeExpected?: HttpStatusCode }) { - if (!options.fields) options.fields = {} - if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 - - const req = request(options.url) - .post(options.path) - .set('Accept', 'application/json') + const req = request(options.url).post(options.path) + .send(options.fields) - if (options.token) req.set('Authorization', 'Bearer ' + options.token) - if (options.xForwardedFor) req.set('X-Forwarded-For', options.xForwardedFor) - if (options.type) req.type(options.type) - - Object.keys(options.headers || {}).forEach(name => { - req.set(name, options.headers[name]) - }) - - return req.send(options.fields) - .expect(options.statusCodeExpected) + return buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options }) } function makePutBodyRequest (options: { @@ -154,58 +101,12 @@ function makePutBodyRequest (options: { path: string token?: string fields: { [ fieldName: string ]: any } - statusCodeExpected?: HttpStatusCode -}) { - if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_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(HttpStatusCode.OK_200) -} - -function makeActivityPubGetRequest (url: string, path: string, expectedStatus = HttpStatusCode.OK_200) { - return makeGetRequest({ - url, - path, - statusCodeExpected: expectedStatus, - accept: 'application/activity+json,text/html;q=0.9,\\*/\\*;q=0.8' - }) -} - -function updateImageRequest (options: { - url: string - path: string - accessToken: string - fixture: string - fieldname: string + expectedStatus?: HttpStatusCode }) { - let filePath = '' - if (isAbsolute(options.fixture)) { - filePath = options.fixture - } else { - filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture) - } + const req = request(options.url).put(options.path) + .send(options.fields) - return makeUploadRequest({ - url: options.url, - path: options.path, - token: options.accessToken, - fields: {}, - attaches: { [options.fieldname]: filePath }, - statusCodeExpected: HttpStatusCode.OK_200 - }) + return buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options }) } function decodeQueryString (path: string) { @@ -233,6 +134,49 @@ export { makeRawRequest, makeActivityPubGetRequest, unwrapBody, - unwrapText, - updateImageRequest + unwrapText +} + +// --------------------------------------------------------------------------- + +function buildRequest (req: request.Test, options: CommonRequestParams) { + if (options.contentType) req.set('Accept', options.contentType) + if (options.token) req.set('Authorization', 'Bearer ' + options.token) + if (options.range) req.set('Range', options.range) + if (options.accept) req.set('Accept', options.accept) + if (options.host) req.set('Host', options.host) + if (options.redirects) req.redirects(options.redirects) + if (options.expectedStatus) req.expect(options.expectedStatus) + if (options.xForwardedFor) req.set('X-Forwarded-For', options.xForwardedFor) + if (options.type) req.type(options.type) + + Object.keys(options.headers || {}).forEach(name => { + req.set(name, options.headers[name]) + }) + + return req +} + +function buildFields (req: request.Test, fields: { [ fieldName: string ]: any }, namespace?: string) { + if (!fields) return + + let formKey: string + + for (const key of Object.keys(fields)) { + if (namespace) formKey = `${namespace}[${key}]` + else formKey = key + + if (fields[key] === undefined) continue + + if (Array.isArray(fields[key]) && fields[key].length === 0) { + req.field(key, null) + continue + } + + if (fields[key] !== null && typeof fields[key] === 'object') { + buildFields(req, fields[key], formKey) + } else { + req.field(formKey, fields[key]) + } + } } diff --git a/shared/extra-utils/search/search-command.ts b/shared/extra-utils/search/search-command.ts index 7539a21ec..09f5d3f1d 100644 --- a/shared/extra-utils/search/search-command.ts +++ b/shared/extra-utils/search/search-command.ts @@ -7,7 +7,7 @@ import { VideoPlaylistsSearchQuery, VideosSearchQuery } from '@shared/models' -import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' +import { HttpStatusCode } from '@shared/models' import { AbstractCommand, OverrideCommandOptions } from '../shared' export class SearchCommand extends AbstractCommand { diff --git a/shared/extra-utils/server/config-command.ts b/shared/extra-utils/server/config-command.ts index f5d7fc5e3..6e875fdf6 100644 --- a/shared/extra-utils/server/config-command.ts +++ b/shared/extra-utils/server/config-command.ts @@ -1,6 +1,6 @@ import { merge } from 'lodash' -import { DeepPartial, HttpStatusCode } from '@shared/core-utils' -import { About, ServerConfig } from '@shared/models' +import { DeepPartial } from '@shared/core-utils' +import { About, ServerConfig, HttpStatusCode } from '@shared/models' import { CustomConfig } from '../../models/server/custom-config.model' import { AbstractCommand, OverrideCommandOptions } from '../shared' diff --git a/shared/extra-utils/server/contact-form-command.ts b/shared/extra-utils/server/contact-form-command.ts index 8d034552b..0e8fd6d84 100644 --- a/shared/extra-utils/server/contact-form-command.ts +++ b/shared/extra-utils/server/contact-form-command.ts @@ -1,4 +1,4 @@ -import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' +import { HttpStatusCode } from '@shared/models' import { ContactForm } from '../../models/server' import { AbstractCommand, OverrideCommandOptions } from '../shared' diff --git a/shared/extra-utils/server/debug-command.ts b/shared/extra-utils/server/debug-command.ts index 8b24b3067..36704836d 100644 --- a/shared/extra-utils/server/debug-command.ts +++ b/shared/extra-utils/server/debug-command.ts @@ -1,5 +1,5 @@ import { Debug, SendDebugCommand } from '@shared/models' -import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' +import { HttpStatusCode } from '@shared/models' import { AbstractCommand, OverrideCommandOptions } from '../shared' export class DebugCommand extends AbstractCommand { diff --git a/shared/extra-utils/server/follows-command.ts b/shared/extra-utils/server/follows-command.ts index 4e9ed9494..694f5ea24 100644 --- a/shared/extra-utils/server/follows-command.ts +++ b/shared/extra-utils/server/follows-command.ts @@ -1,6 +1,6 @@ import { pick } from 'lodash' import { ActivityPubActorType, ActorFollow, FollowState, ResultList } from '@shared/models' -import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' +import { HttpStatusCode } from '@shared/models' import { AbstractCommand, OverrideCommandOptions } from '../shared' import { PeerTubeServer } from './server' diff --git a/shared/extra-utils/server/jobs-command.ts b/shared/extra-utils/server/jobs-command.ts index 392b868c1..09a299e5b 100644 --- a/shared/extra-utils/server/jobs-command.ts +++ b/shared/extra-utils/server/jobs-command.ts @@ -1,5 +1,5 @@ import { pick } from 'lodash' -import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' +import { HttpStatusCode } from '@shared/models' import { Job, JobState, JobType, ResultList } from '../../models' import { AbstractCommand, OverrideCommandOptions } from '../shared' diff --git a/shared/extra-utils/server/plugins-command.ts b/shared/extra-utils/server/plugins-command.ts index 98049ce19..59bc79b3d 100644 --- a/shared/extra-utils/server/plugins-command.ts +++ b/shared/extra-utils/server/plugins-command.ts @@ -3,7 +3,7 @@ import { readJSON, writeJSON } from 'fs-extra' import { join } from 'path' import { root } from '@server/helpers/core-utils' -import { HttpStatusCode } from '@shared/core-utils' +import { HttpStatusCode } from '@shared/models' import { PeerTubePlugin, PeerTubePluginIndex, diff --git a/shared/extra-utils/server/redundancy-command.ts b/shared/extra-utils/server/redundancy-command.ts index 728332fdd..137d7f01c 100644 --- a/shared/extra-utils/server/redundancy-command.ts +++ b/shared/extra-utils/server/redundancy-command.ts @@ -1,5 +1,5 @@ import { ResultList, VideoRedundanciesTarget, VideoRedundancy } from '@shared/models' -import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' +import { HttpStatusCode } from '@shared/models' import { AbstractCommand, OverrideCommandOptions } from '../shared' export class RedundancyCommand extends AbstractCommand { diff --git a/shared/extra-utils/server/server.ts b/shared/extra-utils/server/server.ts index cc6df2efe..b33bb9d1e 100644 --- a/shared/extra-utils/server/server.ts +++ b/shared/extra-utils/server/server.ts @@ -254,7 +254,7 @@ export class PeerTubeServer { process.on('exit', () => { try { - process.kill(self.server.app.pid) + process.kill(self.app.pid) } catch { /* empty */ } }) diff --git a/shared/extra-utils/server/servers-command.ts b/shared/extra-utils/server/servers-command.ts index 1a7b2aade..107e2b4ad 100644 --- a/shared/extra-utils/server/servers-command.ts +++ b/shared/extra-utils/server/servers-command.ts @@ -2,7 +2,7 @@ import { exec } from 'child_process' import { copy, ensureDir, readFile, remove } from 'fs-extra' import { join } from 'path' import { root } from '@server/helpers/core-utils' -import { HttpStatusCode } from '@shared/core-utils' +import { HttpStatusCode } from '@shared/models' import { getFileSize } from '@uploadx/core' import { isGithubCI, wait } from '../miscs' import { AbstractCommand, OverrideCommandOptions } from '../shared' diff --git a/shared/extra-utils/server/stats-command.ts b/shared/extra-utils/server/stats-command.ts index f0f02ca08..6db473588 100644 --- a/shared/extra-utils/server/stats-command.ts +++ b/shared/extra-utils/server/stats-command.ts @@ -1,5 +1,5 @@ import { ServerStats } from '@shared/models' -import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' +import { HttpStatusCode } from '@shared/models' import { AbstractCommand, OverrideCommandOptions } from '../shared' export class StatsCommand extends AbstractCommand { diff --git a/shared/extra-utils/shared/abstract-command.ts b/shared/extra-utils/shared/abstract-command.ts index 967f8f2ac..021045e49 100644 --- a/shared/extra-utils/shared/abstract-command.ts +++ b/shared/extra-utils/shared/abstract-command.ts @@ -24,15 +24,20 @@ interface InternalCommonCommandOptions extends OverrideCommandOptions { // If we automatically send the server token if the token is not provided implicitToken: boolean defaultExpectedStatus: number -} -interface InternalGetCommandOptions extends InternalCommonCommandOptions { - query?: { [ id: string ]: any } + // Common optional request parameters contentType?: string accept?: string redirects?: number range?: string host?: string + headers?: { [ name: string ]: string } + requestType?: string + xForwardedFor?: string +} + +interface InternalGetCommandOptions extends InternalCommonCommandOptions { + query?: { [ id: string ]: any } } abstract class AbstractCommand { @@ -59,7 +64,7 @@ abstract class AbstractCommand { ...options, token: this.buildCommonRequestToken(options), - defaultExpectedStatus: this.buildStatusCodeExpected(options), + defaultExpectedStatus: this.buildExpectedStatus(options), url: `${protocol}//${host}`, path: pathname, @@ -68,17 +73,12 @@ abstract class AbstractCommand { } protected getRequest (options: InternalGetCommandOptions) { - const { redirects, query, contentType, accept, range, host } = options + const { query } = options return makeGetRequest({ ...this.buildCommonRequestOptions(options), - redirects, - query, - contentType, - range, - host, - accept + query }) } @@ -100,51 +100,41 @@ abstract class AbstractCommand { protected postBodyRequest (options: InternalCommonCommandOptions & { fields?: { [ fieldName: string ]: any } - headers?: { [ name: string ]: string } - type?: string - xForwardedFor?: string }) { - const { type, fields, xForwardedFor, headers } = options + const { fields } = options return makePostBodyRequest({ ...this.buildCommonRequestOptions(options), - fields, - xForwardedFor, - type, - headers + fields }) } protected postUploadRequest (options: InternalCommonCommandOptions & { fields?: { [ fieldName: string ]: any } attaches?: { [ fieldName: string ]: any } - headers?: { [ name: string ]: string } }) { - const { fields, attaches, headers } = options + const { fields, attaches } = options return makeUploadRequest({ ...this.buildCommonRequestOptions(options), method: 'POST', fields, - attaches, - headers + attaches }) } protected putUploadRequest (options: InternalCommonCommandOptions & { fields?: { [ fieldName: string ]: any } attaches?: { [ fieldName: string ]: any } - headers?: { [ name: string ]: string } }) { - const { fields, attaches, headers } = options + const { fields, attaches } = options return makeUploadRequest({ ...this.buildCommonRequestOptions(options), method: 'PUT', - headers, fields, attaches }) @@ -154,12 +144,9 @@ abstract class AbstractCommand { fixture: string fieldname: string }) { - let filePath = '' - if (isAbsolute(options.fixture)) { - filePath = options.fixture - } else { - filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture) - } + const filePath = isAbsolute(options.fixture) + ? options.fixture + : join(root(), 'server', 'tests', 'fixtures', options.fixture) return this.postUploadRequest({ ...options, @@ -170,14 +157,23 @@ abstract class AbstractCommand { } protected buildCommonRequestOptions (options: InternalCommonCommandOptions) { - const { url, path } = options + const { url, path, redirects, contentType, accept, range, host, headers, requestType, xForwardedFor } = options return { url: url ?? this.server.url, path, token: this.buildCommonRequestToken(options), - statusCodeExpected: this.buildStatusCodeExpected(options) + expectedStatus: this.buildExpectedStatus(options), + + redirects, + contentType, + range, + host, + accept, + headers, + type: requestType, + xForwardedFor } } @@ -191,7 +187,7 @@ abstract class AbstractCommand { return token !== undefined ? token : fallbackToken } - protected buildStatusCodeExpected (options: Pick) { + protected buildExpectedStatus (options: Pick) { const { expectedStatus, defaultExpectedStatus } = options return expectedStatus !== undefined ? expectedStatus : defaultExpectedStatus diff --git a/shared/extra-utils/users/accounts-command.ts b/shared/extra-utils/users/accounts-command.ts index 4cd1d2158..08977e58b 100644 --- a/shared/extra-utils/users/accounts-command.ts +++ b/shared/extra-utils/users/accounts-command.ts @@ -1,5 +1,5 @@ import { ResultList } from '@shared/models' -import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' +import { HttpStatusCode } from '@shared/models' import { Account } from '../../models/actors' import { AccountVideoRate, VideoRateType } from '../../models/videos' import { AbstractCommand, OverrideCommandOptions } from '../shared' diff --git a/shared/extra-utils/users/blocklist-command.ts b/shared/extra-utils/users/blocklist-command.ts index 089b5a579..a9431acf3 100644 --- a/shared/extra-utils/users/blocklist-command.ts +++ b/shared/extra-utils/users/blocklist-command.ts @@ -1,6 +1,6 @@ /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ -import { HttpStatusCode } from '@shared/core-utils' +import { HttpStatusCode } from '@shared/models' import { AccountBlock, ResultList, ServerBlock } from '@shared/models' import { AbstractCommand, OverrideCommandOptions } from '../shared' diff --git a/shared/extra-utils/users/index.ts b/shared/extra-utils/users/index.ts index e6107afa5..fbb454e8f 100644 --- a/shared/extra-utils/users/index.ts +++ b/shared/extra-utils/users/index.ts @@ -7,4 +7,3 @@ export * from './notifications' export * from './notifications-command' export * from './subscriptions-command' export * from './users-command' -export * from './users' diff --git a/shared/extra-utils/users/login-command.ts b/shared/extra-utils/users/login-command.ts index 10c3db851..b39577260 100644 --- a/shared/extra-utils/users/login-command.ts +++ b/shared/extra-utils/users/login-command.ts @@ -1,4 +1,4 @@ -import { HttpStatusCode } from '@shared/core-utils' +import { HttpStatusCode } from '@shared/models' import { PeerTubeProblemDocument } from '@shared/models' import { unwrapBody } from '../requests' import { AbstractCommand, OverrideCommandOptions } from '../shared' @@ -26,7 +26,7 @@ export class LoginCommand extends AbstractCommand { ...options, path, - type: 'form', + requestType: 'form', fields: body, implicitToken: false, defaultExpectedStatus: HttpStatusCode.OK_200 @@ -72,7 +72,7 @@ export class LoginCommand extends AbstractCommand { ...options, path, - type: 'form', + requestType: 'form', fields: body, implicitToken: false, defaultExpectedStatus: HttpStatusCode.OK_200 @@ -88,7 +88,7 @@ export class LoginCommand extends AbstractCommand { ...options, path, - type: 'form', + requestType: 'form', implicitToken: false, defaultExpectedStatus: HttpStatusCode.OK_200 })) @@ -111,7 +111,7 @@ export class LoginCommand extends AbstractCommand { ...options, path, - type: 'form', + requestType: 'form', fields: body, implicitToken: false, defaultExpectedStatus: HttpStatusCode.OK_200 diff --git a/shared/extra-utils/users/notifications-command.ts b/shared/extra-utils/users/notifications-command.ts index dfe574ca1..a51fcc3af 100644 --- a/shared/extra-utils/users/notifications-command.ts +++ b/shared/extra-utils/users/notifications-command.ts @@ -1,5 +1,5 @@ import { ResultList } from '@shared/models' -import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' +import { HttpStatusCode } from '@shared/models' import { UserNotification, UserNotificationSetting } from '../../models/users' import { AbstractCommand, OverrideCommandOptions } from '../shared' diff --git a/shared/extra-utils/users/subscriptions-command.ts b/shared/extra-utils/users/subscriptions-command.ts index e998eb426..a69d2a194 100644 --- a/shared/extra-utils/users/subscriptions-command.ts +++ b/shared/extra-utils/users/subscriptions-command.ts @@ -1,5 +1,5 @@ import { ResultList, Video, VideoChannel } from '@shared/models' -import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' +import { HttpStatusCode } from '@shared/models' import { AbstractCommand, OverrideCommandOptions } from '../shared' export class SubscriptionsCommand extends AbstractCommand { diff --git a/shared/extra-utils/users/users-command.ts b/shared/extra-utils/users/users-command.ts index 59dc6d018..f3a251e65 100644 --- a/shared/extra-utils/users/users-command.ts +++ b/shared/extra-utils/users/users-command.ts @@ -1,5 +1,5 @@ import { omit, pick } from 'lodash' -import { HttpStatusCode } from '@shared/core-utils' +import { HttpStatusCode } from '@shared/models' import { MyUser, ResultList, diff --git a/shared/extra-utils/users/users.ts b/shared/extra-utils/users/users.ts deleted file mode 100644 index 6cf61d60e..000000000 --- a/shared/extra-utils/users/users.ts +++ /dev/null @@ -1,20 +0,0 @@ -import * as request from 'supertest' -import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' - -// FIXME: delete once videos does not use it anymore -function xxxgetMyUserInformation (url: string, accessToken: string, specialStatus = HttpStatusCode.OK_200) { - const path = '/api/v1/users/me' - - return request(url) - .get(path) - .set('Accept', 'application/json') - .set('Authorization', 'Bearer ' + accessToken) - .expect(specialStatus) - .expect('Content-Type', /json/) -} - -// --------------------------------------------------------------------------- - -export { - xxxgetMyUserInformation -} diff --git a/shared/extra-utils/videos/blacklist-command.ts b/shared/extra-utils/videos/blacklist-command.ts index fdae6b469..9404d4c08 100644 --- a/shared/extra-utils/videos/blacklist-command.ts +++ b/shared/extra-utils/videos/blacklist-command.ts @@ -1,6 +1,6 @@ import { ResultList } from '@shared/models' -import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' +import { HttpStatusCode } from '@shared/models' import { VideoBlacklist, VideoBlacklistType } from '../../models/videos' import { AbstractCommand, OverrideCommandOptions } from '../shared' diff --git a/shared/extra-utils/videos/captions-command.ts b/shared/extra-utils/videos/captions-command.ts index ac3bde7a9..04dd32f84 100644 --- a/shared/extra-utils/videos/captions-command.ts +++ b/shared/extra-utils/videos/captions-command.ts @@ -1,4 +1,4 @@ -import { HttpStatusCode } from '@shared/core-utils' +import { HttpStatusCode } from '@shared/models' import { ResultList, VideoCaption } from '@shared/models' import { buildAbsoluteFixturePath } from '../miscs' import { AbstractCommand, OverrideCommandOptions } from '../shared' diff --git a/shared/extra-utils/videos/captions.ts b/shared/extra-utils/videos/captions.ts index 2246bd133..ff8a43366 100644 --- a/shared/extra-utils/videos/captions.ts +++ b/shared/extra-utils/videos/captions.ts @@ -1,6 +1,6 @@ import { expect } from 'chai' import * as request from 'supertest' -import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' +import { HttpStatusCode } from '@shared/models' async function testCaptionFile (url: string, captionPath: string, containsString: string) { const res = await request(url) diff --git a/shared/extra-utils/videos/change-ownership-command.ts b/shared/extra-utils/videos/change-ownership-command.ts index 03f77a95f..ef6f07536 100644 --- a/shared/extra-utils/videos/change-ownership-command.ts +++ b/shared/extra-utils/videos/change-ownership-command.ts @@ -1,6 +1,6 @@ import { ResultList, VideoChangeOwnership } from '@shared/models' -import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' +import { HttpStatusCode } from '@shared/models' import { AbstractCommand, OverrideCommandOptions } from '../shared' export class ChangeOwnershipCommand extends AbstractCommand { diff --git a/shared/extra-utils/videos/channels-command.ts b/shared/extra-utils/videos/channels-command.ts index a98c5cc93..e5393ff56 100644 --- a/shared/extra-utils/videos/channels-command.ts +++ b/shared/extra-utils/videos/channels-command.ts @@ -1,6 +1,6 @@ import { pick } from 'lodash' import { ResultList, VideoChannel, VideoChannelCreateResult } from '@shared/models' -import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' +import { HttpStatusCode } from '@shared/models' import { VideoChannelCreate } from '../../models/videos/channel/video-channel-create.model' import { VideoChannelUpdate } from '../../models/videos/channel/video-channel-update.model' import { unwrapBody } from '../requests' diff --git a/shared/extra-utils/videos/comments-command.ts b/shared/extra-utils/videos/comments-command.ts index b31f3e2dd..7368f3ea2 100644 --- a/shared/extra-utils/videos/comments-command.ts +++ b/shared/extra-utils/videos/comments-command.ts @@ -1,6 +1,6 @@ import { pick } from 'lodash' +import { HttpStatusCode } from '@shared/models' import { ResultList, VideoComment, VideoCommentThreads, VideoCommentThreadTree } from '@shared/models' -import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' import { unwrapBody } from '../requests' import { AbstractCommand, OverrideCommandOptions } from '../shared' diff --git a/shared/extra-utils/videos/history-command.ts b/shared/extra-utils/videos/history-command.ts index 8a144a312..41afc6bc6 100644 --- a/shared/extra-utils/videos/history-command.ts +++ b/shared/extra-utils/videos/history-command.ts @@ -1,5 +1,5 @@ +import { HttpStatusCode } from '@shared/models' import { ResultList, Video } from '@shared/models' -import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' import { AbstractCommand, OverrideCommandOptions } from '../shared' export class HistoryCommand extends AbstractCommand { diff --git a/shared/extra-utils/videos/imports-command.ts b/shared/extra-utils/videos/imports-command.ts index de8b65829..d30f9745b 100644 --- a/shared/extra-utils/videos/imports-command.ts +++ b/shared/extra-utils/videos/imports-command.ts @@ -1,6 +1,6 @@ +import { HttpStatusCode } from '@shared/models' import { ResultList } from '@shared/models' -import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' import { VideoImport, VideoImportCreate } from '../../models/videos' import { unwrapBody } from '../requests' import { AbstractCommand, OverrideCommandOptions } from '../shared' diff --git a/shared/extra-utils/videos/live-command.ts b/shared/extra-utils/videos/live-command.ts index fd66c9924..9dfe3087e 100644 --- a/shared/extra-utils/videos/live-command.ts +++ b/shared/extra-utils/videos/live-command.ts @@ -3,8 +3,8 @@ import { readdir } from 'fs-extra' import { omit } from 'lodash' import { join } from 'path' +import { HttpStatusCode } from '@shared/models' import { LiveVideo, LiveVideoCreate, LiveVideoUpdate, VideoCreateResult, VideoDetails, VideoState } from '@shared/models' -import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' import { wait } from '../miscs' import { unwrapBody } from '../requests' import { AbstractCommand, OverrideCommandOptions } from '../shared' diff --git a/shared/extra-utils/videos/playlists-command.ts b/shared/extra-utils/videos/playlists-command.ts index cbfc7e10f..40162c30d 100644 --- a/shared/extra-utils/videos/playlists-command.ts +++ b/shared/extra-utils/videos/playlists-command.ts @@ -1,5 +1,5 @@ import { omit, pick } from 'lodash' -import { HttpStatusCode } from '@shared/core-utils' +import { HttpStatusCode } from '@shared/models' import { BooleanBothQuery, ResultList, diff --git a/shared/extra-utils/videos/services-command.ts b/shared/extra-utils/videos/services-command.ts index 313b7878c..06760df42 100644 --- a/shared/extra-utils/videos/services-command.ts +++ b/shared/extra-utils/videos/services-command.ts @@ -1,4 +1,4 @@ -import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' +import { HttpStatusCode } from '@shared/models' import { AbstractCommand, OverrideCommandOptions } from '../shared' export class ServicesCommand extends AbstractCommand { diff --git a/shared/extra-utils/videos/streaming-playlists-command.ts b/shared/extra-utils/videos/streaming-playlists-command.ts index fab3eb556..9662685da 100644 --- a/shared/extra-utils/videos/streaming-playlists-command.ts +++ b/shared/extra-utils/videos/streaming-playlists-command.ts @@ -1,5 +1,4 @@ - -import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' +import { HttpStatusCode } from '@shared/models' import { unwrapBody, unwrapText } from '../requests' import { AbstractCommand, OverrideCommandOptions } from '../shared' diff --git a/shared/extra-utils/videos/streaming-playlists.ts b/shared/extra-utils/videos/streaming-playlists.ts index 002ae08b2..007d3d98d 100644 --- a/shared/extra-utils/videos/streaming-playlists.ts +++ b/shared/extra-utils/videos/streaming-playlists.ts @@ -1,6 +1,6 @@ import { expect } from 'chai' import { sha256 } from '@server/helpers/core-utils' -import { HttpStatusCode } from '@shared/core-utils' +import { HttpStatusCode } from '@shared/models' import { VideoStreamingPlaylist } from '@shared/models' import { PeerTubeServer } from '../server' diff --git a/shared/extra-utils/videos/videos-command.ts b/shared/extra-utils/videos/videos-command.ts index feef5a771..f46d386f4 100644 --- a/shared/extra-utils/videos/videos-command.ts +++ b/shared/extra-utils/videos/videos-command.ts @@ -7,7 +7,7 @@ import { omit, pick } from 'lodash' import validator from 'validator' import { buildUUID } from '@server/helpers/uuid' import { loadLanguages } from '@server/initializers/constants' -import { HttpStatusCode } from '@shared/core-utils' +import { HttpStatusCode } from '@shared/models' import { ResultList, UserVideoRateType, @@ -234,10 +234,10 @@ export class VideosCommand extends AbstractCommand { } listByAccount (options: OverrideCommandOptions & VideosWithSearchCommonQuery & { - accountName: string + handle: string }) { - const { accountName, search } = options - const path = '/api/v1/accounts/' + accountName + '/videos' + const { handle, search } = options + const path = '/api/v1/accounts/' + handle + '/videos' return this.getRequestBody>({ ...options, @@ -250,10 +250,10 @@ export class VideosCommand extends AbstractCommand { } listByChannel (options: OverrideCommandOptions & VideosWithSearchCommonQuery & { - videoChannelName: string + handle: string }) { - const { videoChannelName } = options - const path = '/api/v1/video-channels/' + videoChannelName + '/videos' + const { handle } = options + const path = '/api/v1/video-channels/' + handle + '/videos' return this.getRequestBody>({ ...options, @@ -309,13 +309,13 @@ export class VideosCommand extends AbstractCommand { }) { const path = '/api/v1/videos/' + options.id - return this.deleteRequest({ + return unwrapBody(this.deleteRequest({ ...options, path, implicitToken: true, defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 - }) + })) } async removeAll () { @@ -396,7 +396,7 @@ export class VideosCommand extends AbstractCommand { async buildResumeUpload (options: OverrideCommandOptions & { attributes: VideoEdit - }) { + }): Promise { const { attributes, expectedStatus } = options let size = 0 @@ -414,7 +414,8 @@ export class VideosCommand extends AbstractCommand { } } - const initializeSessionRes = await this.prepareResumableUpload({ ...options, attributes, size, mimetype }) + // Do not check status automatically, we'll check it manually + const initializeSessionRes = await this.prepareResumableUpload({ ...options, expectedStatus: null, attributes, size, mimetype }) const initStatus = initializeSessionRes.status if (videoFilePath && initStatus === HttpStatusCode.CREATED_201) { @@ -425,7 +426,7 @@ export class VideosCommand extends AbstractCommand { const result = await this.sendResumableChunks({ ...options, pathUploadId, videoFilePath, size }) - return result.body.video + return result.body?.video || result.body as any } const expectedInitStatus = expectedStatus === HttpStatusCode.OK_200 @@ -434,7 +435,7 @@ export class VideosCommand extends AbstractCommand { expect(initStatus).to.equal(expectedInitStatus) - return initializeSessionRes.body.video as VideoCreateResult + return initializeSessionRes.body.video || initializeSessionRes.body } async prepareResumableUpload (options: OverrideCommandOptions & { @@ -455,7 +456,10 @@ export class VideosCommand extends AbstractCommand { 'X-Upload-Content-Length': size.toString() }, fields: { filename: attributes.fixture, ...this.buildUploadFields(options.attributes) }, + // Fixture will be sent later + attaches: this.buildUploadAttaches(omit(options.attributes, 'fixture')), implicitToken: true, + defaultExpectedStatus: null }) } @@ -539,10 +543,10 @@ export class VideosCommand extends AbstractCommand { const attributes = { name, additionalParams } - if (wait) await waitJobs([ this.server ]) - const result = await this.upload({ ...options, attributes }) + if (wait) await waitJobs([ this.server ]) + return { ...result, name } } @@ -566,7 +570,7 @@ export class VideosCommand extends AbstractCommand { } private buildUploadFields (attributes: VideoEdit) { - return omit(attributes, [ 'thumbnailfile', 'previewfile' ]) + return omit(attributes, [ 'fixture', 'thumbnailfile', 'previewfile' ]) } private buildUploadAttaches (attributes: VideoEdit) { diff --git a/shared/extra-utils/videos/videos.ts b/shared/extra-utils/videos/videos.ts index b41533808..a96073c56 100644 --- a/shared/extra-utils/videos/videos.ts +++ b/shared/extra-utils/videos/videos.ts @@ -4,7 +4,7 @@ import { expect } from 'chai' import { pathExists, readdir } from 'fs-extra' import { join } from 'path' import { getLowercaseExtension } from '@server/helpers/core-utils' -import { HttpStatusCode } from '@shared/core-utils' +import { HttpStatusCode } from '@shared/models' import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../server/initializers/constants' import { dateIsValid, testImage, webtorrentAdd } from '../miscs' import { makeRawRequest } from '../requests/requests' -- cgit v1.2.3