From 2d53be0267acc49cda46707b885096193a1f4e9c Mon Sep 17 00:00:00 2001 From: Rigel Kent Date: Mon, 7 Dec 2020 14:32:36 +0100 Subject: replace numbers with typed http status codes (#3409) --- shared/extra-utils/bulk/bulk.ts | 3 +- shared/extra-utils/feeds/feeds.ts | 5 +- shared/extra-utils/logs/logs.ts | 5 +- shared/extra-utils/miscs/miscs.ts | 3 +- shared/extra-utils/moderation/abuses.ts | 24 +++++--- shared/extra-utils/overviews/overviews.ts | 5 +- shared/extra-utils/requests/check-api-params.ts | 9 +-- shared/extra-utils/requests/requests.ts | 27 ++++----- shared/extra-utils/search/video-channels.ts | 5 +- shared/extra-utils/search/videos.ts | 9 +-- shared/extra-utils/server/clients.ts | 3 +- shared/extra-utils/server/config.ts | 12 ++-- shared/extra-utils/server/contact-form.ts | 3 +- shared/extra-utils/server/follows.ts | 15 ++--- shared/extra-utils/server/jobs.ts | 5 +- shared/extra-utils/server/plugins.ts | 57 +++++++++++-------- shared/extra-utils/server/redundancy.ts | 9 +-- shared/extra-utils/server/stats.ts | 3 +- shared/extra-utils/users/accounts.ts | 13 ++++- shared/extra-utils/users/blocklist.ts | 65 ++++++++++++++++++---- shared/extra-utils/users/user-notifications.ts | 14 +++-- shared/extra-utils/users/user-subscriptions.ts | 13 +++-- shared/extra-utils/users/users.ts | 63 +++++++++++++-------- shared/extra-utils/videos/live.ts | 13 ++++- shared/extra-utils/videos/services.ts | 3 +- shared/extra-utils/videos/video-blacklist.ts | 17 ++++-- shared/extra-utils/videos/video-captions.ts | 9 +-- .../extra-utils/videos/video-change-ownership.ts | 26 +++++++-- shared/extra-utils/videos/video-channels.ts | 24 +++++--- shared/extra-utils/videos/video-comments.ts | 19 +++++-- shared/extra-utils/videos/video-history.ts | 5 +- shared/extra-utils/videos/video-imports.ts | 10 +++- shared/extra-utils/videos/video-playlists.ts | 31 ++++++----- .../videos/video-streaming-playlists.ts | 9 +-- shared/extra-utils/videos/videos.ts | 49 +++++++++------- 35 files changed, 379 insertions(+), 206 deletions(-) (limited to 'shared') diff --git a/shared/extra-utils/bulk/bulk.ts b/shared/extra-utils/bulk/bulk.ts index d6798ceb7..b6f437b8b 100644 --- a/shared/extra-utils/bulk/bulk.ts +++ b/shared/extra-utils/bulk/bulk.ts @@ -1,5 +1,6 @@ import { BulkRemoveCommentsOfBody } from "@shared/models/bulk/bulk-remove-comments-of-body.model" import { makePostBodyRequest } from "../requests/requests" +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' function bulkRemoveCommentsOf (options: { url: string @@ -15,7 +16,7 @@ function bulkRemoveCommentsOf (options: { path, token, fields: attributes, - statusCodeExpected: expectedStatus || 204 + statusCodeExpected: expectedStatus || HttpStatusCode.NO_CONTENT_204 }) } diff --git a/shared/extra-utils/feeds/feeds.ts b/shared/extra-utils/feeds/feeds.ts index 957d4499c..ce0a98c6d 100644 --- a/shared/extra-utils/feeds/feeds.ts +++ b/shared/extra-utils/feeds/feeds.ts @@ -1,4 +1,5 @@ import * as request from 'supertest' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' type FeedType = 'videos' | 'video-comments' | 'subscriptions' @@ -9,11 +10,11 @@ function getXMLfeed (url: string, feed: FeedType, format?: string) { .get(path) .query((format) ? { format: format } : {}) .set('Accept', 'application/xml') - .expect(200) + .expect(HttpStatusCode.OK_200) .expect('Content-Type', /xml/) } -function getJSONfeed (url: string, feed: FeedType, query: any = {}, statusCodeExpected = 200) { +function getJSONfeed (url: string, feed: FeedType, query: any = {}, statusCodeExpected = HttpStatusCode.OK_200) { const path = '/feeds/' + feed + '.json' return request(url) diff --git a/shared/extra-utils/logs/logs.ts b/shared/extra-utils/logs/logs.ts index c494c1f1e..8d741276c 100644 --- a/shared/extra-utils/logs/logs.ts +++ b/shared/extra-utils/logs/logs.ts @@ -1,5 +1,6 @@ import { makeGetRequest } from '../requests/requests' import { LogLevel } from '../../models/server/log-level.type' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' function getLogs (url: string, accessToken: string, startDate: Date, endDate?: Date, level?: LogLevel) { const path = '/api/v1/server/logs' @@ -9,7 +10,7 @@ function getLogs (url: string, accessToken: string, startDate: Date, endDate?: D path, token: accessToken, query: { startDate, endDate, level }, - statusCodeExpected: 200 + statusCodeExpected: HttpStatusCode.OK_200 }) } @@ -21,7 +22,7 @@ function getAuditLogs (url: string, accessToken: string, startDate: Date, endDat path, token: accessToken, query: { startDate, endDate }, - statusCodeExpected: 200 + statusCodeExpected: HttpStatusCode.OK_200 }) } diff --git a/shared/extra-utils/miscs/miscs.ts b/shared/extra-utils/miscs/miscs.ts index aea9563cf..764b74bda 100644 --- a/shared/extra-utils/miscs/miscs.ts +++ b/shared/extra-utils/miscs/miscs.ts @@ -6,6 +6,7 @@ import { ensureDir, pathExists, readFile, stat } from 'fs-extra' import { basename, dirname, isAbsolute, join, resolve } from 'path' import * as request from 'supertest' import * as WebTorrent from 'webtorrent' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' const expect = chai.expect let webtorrent: WebTorrent.Instance @@ -51,7 +52,7 @@ function buildServerDirectory (server: { internalServerNumber: number }, directo async function testImage (url: string, imageName: string, imagePath: string, extension = '.jpg') { const res = await request(url) .get(imagePath) - .expect(200) + .expect(HttpStatusCode.OK_200) const body = res.body diff --git a/shared/extra-utils/moderation/abuses.ts b/shared/extra-utils/moderation/abuses.ts index 7db75cebb..cb6ca46ef 100644 --- a/shared/extra-utils/moderation/abuses.ts +++ b/shared/extra-utils/moderation/abuses.ts @@ -1,6 +1,6 @@ - import { AbuseFilter, AbusePredefinedReasonsString, AbuseState, AbuseUpdate, AbuseVideoIs } from '@shared/models' import { makeDeleteRequest, makeGetRequest, makePostBodyRequest, makePutBodyRequest } from '../requests/requests' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' function reportAbuse (options: { url: string @@ -50,7 +50,7 @@ function reportAbuse (options: { token: options.token, fields: body, - statusCodeExpected: options.statusCodeExpected || 200 + statusCodeExpected: options.statusCodeExpected || HttpStatusCode.OK_200 }) } @@ -113,7 +113,7 @@ function getAdminAbusesList (options: { path, token, query, - statusCodeExpected: 200 + statusCodeExpected: HttpStatusCode.OK_200 }) } @@ -155,7 +155,7 @@ function getUserAbusesList (options: { path, token, query, - statusCodeExpected: 200 + statusCodeExpected: HttpStatusCode.OK_200 }) } @@ -164,7 +164,7 @@ function updateAbuse ( token: string, abuseId: number, body: AbuseUpdate, - statusCodeExpected = 204 + statusCodeExpected = HttpStatusCode.NO_CONTENT_204 ) { const path = '/api/v1/abuses/' + abuseId @@ -177,7 +177,7 @@ function updateAbuse ( }) } -function deleteAbuse (url: string, token: string, abuseId: number, statusCodeExpected = 204) { +function deleteAbuse (url: string, token: string, abuseId: number, statusCodeExpected = HttpStatusCode.NO_CONTENT_204) { const path = '/api/v1/abuses/' + abuseId return makeDeleteRequest({ @@ -188,7 +188,7 @@ function deleteAbuse (url: string, token: string, abuseId: number, statusCodeExp }) } -function listAbuseMessages (url: string, token: string, abuseId: number, statusCodeExpected = 200) { +function listAbuseMessages (url: string, token: string, abuseId: number, statusCodeExpected = HttpStatusCode.OK_200) { const path = '/api/v1/abuses/' + abuseId + '/messages' return makeGetRequest({ @@ -199,7 +199,13 @@ function listAbuseMessages (url: string, token: string, abuseId: number, statusC }) } -function deleteAbuseMessage (url: string, token: string, abuseId: number, messageId: number, statusCodeExpected = 204) { +function deleteAbuseMessage ( + url: string, + token: string, + abuseId: number, + messageId: number, + statusCodeExpected = HttpStatusCode.NO_CONTENT_204 +) { const path = '/api/v1/abuses/' + abuseId + '/messages/' + messageId return makeDeleteRequest({ @@ -210,7 +216,7 @@ function deleteAbuseMessage (url: string, token: string, abuseId: number, messag }) } -function addAbuseMessage (url: string, token: string, abuseId: number, message: string, statusCodeExpected = 200) { +function addAbuseMessage (url: string, token: string, abuseId: number, message: string, statusCodeExpected = HttpStatusCode.OK_200) { const path = '/api/v1/abuses/' + abuseId + '/messages' return makePostBodyRequest({ diff --git a/shared/extra-utils/overviews/overviews.ts b/shared/extra-utils/overviews/overviews.ts index ae4d31aa3..5e1a13e5e 100644 --- a/shared/extra-utils/overviews/overviews.ts +++ b/shared/extra-utils/overviews/overviews.ts @@ -1,6 +1,7 @@ import { makeGetRequest } from '../requests/requests' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' -function getVideosOverview (url: string, page: number, statusCodeExpected = 200) { +function getVideosOverview (url: string, page: number, statusCodeExpected = HttpStatusCode.OK_200) { const path = '/api/v1/overviews/videos' const query = { page } @@ -13,7 +14,7 @@ function getVideosOverview (url: string, page: number, statusCodeExpected = 200) }) } -function getVideosOverviewWithToken (url: string, page: number, token: string, statusCodeExpected = 200) { +function getVideosOverviewWithToken (url: string, page: number, token: string, statusCodeExpected = HttpStatusCode.OK_200) { const path = '/api/v1/overviews/videos' const query = { page } diff --git a/shared/extra-utils/requests/check-api-params.ts b/shared/extra-utils/requests/check-api-params.ts index c34c7c216..7f5ff775c 100644 --- a/shared/extra-utils/requests/check-api-params.ts +++ b/shared/extra-utils/requests/check-api-params.ts @@ -1,5 +1,6 @@ import { makeGetRequest } from './requests' import { immutableAssign } from '../miscs/miscs' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' function checkBadStartPagination (url: string, path: string, token?: string, query = {}) { return makeGetRequest({ @@ -7,7 +8,7 @@ function checkBadStartPagination (url: string, path: string, token?: string, que path, token, query: immutableAssign(query, { start: 'hello' }), - statusCodeExpected: 400 + statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 }) } @@ -17,7 +18,7 @@ async function checkBadCountPagination (url: string, path: string, token?: strin path, token, query: immutableAssign(query, { count: 'hello' }), - statusCodeExpected: 400 + statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 }) await makeGetRequest({ @@ -25,7 +26,7 @@ async function checkBadCountPagination (url: string, path: string, token?: strin path, token, query: immutableAssign(query, { count: 2000 }), - statusCodeExpected: 400 + statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 }) } @@ -35,7 +36,7 @@ function checkBadSortPagination (url: string, path: string, token?: string, quer path, token, query: immutableAssign(query, { sort: 'hello' }), - statusCodeExpected: 400 + statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 }) } diff --git a/shared/extra-utils/requests/requests.ts b/shared/extra-utils/requests/requests.ts index 6b00871e0..3e773ee03 100644 --- a/shared/extra-utils/requests/requests.ts +++ b/shared/extra-utils/requests/requests.ts @@ -5,12 +5,13 @@ import { buildAbsoluteFixturePath, root } from '../miscs/miscs' import { isAbsolute, join } from 'path' import { URL } from 'url' import { decode } from 'querystring' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' function get4KFileUrl () { return 'https://download.cpy.re/peertube/4k_file.txt' } -function makeRawRequest (url: string, statusCodeExpected?: number, range?: string) { +function makeRawRequest (url: string, statusCodeExpected?: HttpStatusCode, range?: string) { const { host, protocol, pathname } = new URL(url) return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, statusCodeExpected, range }) @@ -21,12 +22,12 @@ function makeGetRequest (options: { path?: string query?: any token?: string - statusCodeExpected?: number + statusCodeExpected?: HttpStatusCode contentType?: string range?: string redirects?: number }) { - if (!options.statusCodeExpected) options.statusCodeExpected = 400 + 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) @@ -44,9 +45,9 @@ function makeDeleteRequest (options: { url: string path: string token?: string - statusCodeExpected?: number + statusCodeExpected?: HttpStatusCode }) { - if (!options.statusCodeExpected) options.statusCodeExpected = 400 + if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 const req = request(options.url) .delete(options.path) @@ -64,9 +65,9 @@ function makeUploadRequest (options: { token?: string fields: { [ fieldName: string ]: any } attaches?: { [ attachName: string ]: any | any[] } - statusCodeExpected?: number + statusCodeExpected?: HttpStatusCode }) { - if (!options.statusCodeExpected) options.statusCodeExpected = 400 + if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 let req: request.Test if (options.method === 'PUT') { @@ -110,10 +111,10 @@ function makePostBodyRequest (options: { path: string token?: string fields?: { [ fieldName: string ]: any } - statusCodeExpected?: number + statusCodeExpected?: HttpStatusCode }) { if (!options.fields) options.fields = {} - if (!options.statusCodeExpected) options.statusCodeExpected = 400 + if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 const req = request(options.url) .post(options.path) @@ -130,9 +131,9 @@ function makePutBodyRequest (options: { path: string token?: string fields: { [ fieldName: string ]: any } - statusCodeExpected?: number + statusCodeExpected?: HttpStatusCode }) { - if (!options.statusCodeExpected) options.statusCodeExpected = 400 + if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 const req = request(options.url) .put(options.path) @@ -148,7 +149,7 @@ function makeHTMLRequest (url: string, path: string) { return request(url) .get(path) .set('Accept', 'text/html') - .expect(200) + .expect(HttpStatusCode.OK_200) } function updateAvatarRequest (options: { @@ -170,7 +171,7 @@ function updateAvatarRequest (options: { token: options.accessToken, fields: {}, attaches: { avatarfile: filePath }, - statusCodeExpected: 200 + statusCodeExpected: HttpStatusCode.OK_200 }) } diff --git a/shared/extra-utils/search/video-channels.ts b/shared/extra-utils/search/video-channels.ts index d16210530..8e0f42578 100644 --- a/shared/extra-utils/search/video-channels.ts +++ b/shared/extra-utils/search/video-channels.ts @@ -1,7 +1,8 @@ import { VideoChannelsSearchQuery } from '@shared/models' import { makeGetRequest } from '../requests/requests' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' -function searchVideoChannel (url: string, search: string, token?: string, statusCodeExpected = 200) { +function searchVideoChannel (url: string, search: string, token?: string, statusCodeExpected = HttpStatusCode.OK_200) { const path = '/api/v1/search/video-channels' return makeGetRequest({ @@ -23,7 +24,7 @@ function advancedVideoChannelSearch (url: string, search: VideoChannelsSearchQue url, path, query: search, - statusCodeExpected: 200 + statusCodeExpected: HttpStatusCode.OK_200 }) } diff --git a/shared/extra-utils/search/videos.ts b/shared/extra-utils/search/videos.ts index 4c52ea11c..ac65357e3 100644 --- a/shared/extra-utils/search/videos.ts +++ b/shared/extra-utils/search/videos.ts @@ -3,6 +3,7 @@ import * as request from 'supertest' import { VideosSearchQuery } from '../../models/search' import { immutableAssign } from '../miscs/miscs' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' function searchVideo (url: string, search: string) { const path = '/api/v1/search/videos' @@ -13,7 +14,7 @@ function searchVideo (url: string, search: string) { .query(query) .set('Accept', 'application/json') - return req.expect(200) + return req.expect(HttpStatusCode.OK_200) .expect('Content-Type', /json/) } @@ -25,7 +26,7 @@ function searchVideoWithToken (url: string, search: string, token: string, query .query(immutableAssign(query, { sort: '-publishedAt', search })) .set('Accept', 'application/json') - return req.expect(200) + return req.expect(HttpStatusCode.OK_200) .expect('Content-Type', /json/) } @@ -38,7 +39,7 @@ function searchVideoWithSort (url: string, search: string, sort: string) { .get(path) .query(query) .set('Accept', 'application/json') - .expect(200) + .expect(HttpStatusCode.OK_200) .expect('Content-Type', /json/) } @@ -49,7 +50,7 @@ function advancedVideosSearch (url: string, options: VideosSearchQuery) { .get(path) .query(options) .set('Accept', 'application/json') - .expect(200) + .expect(HttpStatusCode.OK_200) .expect('Content-Type', /json/) } diff --git a/shared/extra-utils/server/clients.ts b/shared/extra-utils/server/clients.ts index dc631e823..894fe4911 100644 --- a/shared/extra-utils/server/clients.ts +++ b/shared/extra-utils/server/clients.ts @@ -1,5 +1,6 @@ import * as request from 'supertest' import { URL } from 'url' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' function getClient (url: string) { const path = '/api/v1/oauth-clients/local' @@ -8,7 +9,7 @@ function getClient (url: string) { .get(path) .set('Host', new URL(url).host) .set('Accept', 'application/json') - .expect(200) + .expect(HttpStatusCode.OK_200) .expect('Content-Type', /json/) } diff --git a/shared/extra-utils/server/config.ts b/shared/extra-utils/server/config.ts index 7c1ad0a75..3b6afe9ff 100644 --- a/shared/extra-utils/server/config.ts +++ b/shared/extra-utils/server/config.ts @@ -1,6 +1,6 @@ import { makeDeleteRequest, makeGetRequest, makePutBodyRequest } from '../requests/requests' import { CustomConfig } from '../../models/server/custom-config.model' -import { DeepPartial } from '@shared/core-utils' +import { DeepPartial, HttpStatusCode } from '@shared/core-utils' import { merge } from 'lodash' function getConfig (url: string) { @@ -9,7 +9,7 @@ function getConfig (url: string) { return makeGetRequest({ url, path, - statusCodeExpected: 200 + statusCodeExpected: HttpStatusCode.OK_200 }) } @@ -19,11 +19,11 @@ function getAbout (url: string) { return makeGetRequest({ url, path, - statusCodeExpected: 200 + statusCodeExpected: HttpStatusCode.OK_200 }) } -function getCustomConfig (url: string, token: string, statusCodeExpected = 200) { +function getCustomConfig (url: string, token: string, statusCodeExpected = HttpStatusCode.OK_200) { const path = '/api/v1/config/custom' return makeGetRequest({ @@ -34,7 +34,7 @@ function getCustomConfig (url: string, token: string, statusCodeExpected = 200) }) } -function updateCustomConfig (url: string, token: string, newCustomConfig: CustomConfig, statusCodeExpected = 200) { +function updateCustomConfig (url: string, token: string, newCustomConfig: CustomConfig, statusCodeExpected = HttpStatusCode.OK_200) { const path = '/api/v1/config/custom' return makePutBodyRequest({ @@ -204,7 +204,7 @@ function updateCustomSubConfig (url: string, token: string, newConfig: DeepParti return updateCustomConfig(url, token, updateParams) } -function deleteCustomConfig (url: string, token: string, statusCodeExpected = 200) { +function deleteCustomConfig (url: string, token: string, statusCodeExpected = HttpStatusCode.OK_200) { const path = '/api/v1/config/custom' return makeDeleteRequest({ diff --git a/shared/extra-utils/server/contact-form.ts b/shared/extra-utils/server/contact-form.ts index d50f83241..6c9232cc6 100644 --- a/shared/extra-utils/server/contact-form.ts +++ b/shared/extra-utils/server/contact-form.ts @@ -1,5 +1,6 @@ import * as request from 'supertest' import { ContactForm } from '../../models/server' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' function sendContactForm (options: { url: string @@ -20,7 +21,7 @@ function sendContactForm (options: { return request(options.url) .post(path) .send(body) - .expect(options.expectedStatus || 204) + .expect(options.expectedStatus || HttpStatusCode.NO_CONTENT_204) } // --------------------------------------------------------------------------- diff --git a/shared/extra-utils/server/follows.ts b/shared/extra-utils/server/follows.ts index 006d59199..6aae4a31d 100644 --- a/shared/extra-utils/server/follows.ts +++ b/shared/extra-utils/server/follows.ts @@ -3,6 +3,7 @@ import { ServerInfo } from './servers' import { waitJobs } from './jobs' import { makePostBodyRequest } from '../requests/requests' import { ActivityPubActorType, FollowState } from '@shared/models' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' function getFollowersListPaginationAndSort (options: { url: string @@ -29,11 +30,11 @@ function getFollowersListPaginationAndSort (options: { .get(path) .query(query) .set('Accept', 'application/json') - .expect(200) + .expect(HttpStatusCode.OK_200) .expect('Content-Type', /json/) } -function acceptFollower (url: string, token: string, follower: string, statusCodeExpected = 204) { +function acceptFollower (url: string, token: string, follower: string, statusCodeExpected = HttpStatusCode.NO_CONTENT_204) { const path = '/api/v1/server/followers/' + follower + '/accept' return makePostBodyRequest({ @@ -44,7 +45,7 @@ function acceptFollower (url: string, token: string, follower: string, statusCod }) } -function rejectFollower (url: string, token: string, follower: string, statusCodeExpected = 204) { +function rejectFollower (url: string, token: string, follower: string, statusCodeExpected = HttpStatusCode.NO_CONTENT_204) { const path = '/api/v1/server/followers/' + follower + '/reject' return makePostBodyRequest({ @@ -80,11 +81,11 @@ function getFollowingListPaginationAndSort (options: { .get(path) .query(query) .set('Accept', 'application/json') - .expect(200) + .expect(HttpStatusCode.OK_200) .expect('Content-Type', /json/) } -function follow (follower: string, following: string[], accessToken: string, expectedStatus = 204) { +function follow (follower: string, following: string[], accessToken: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) { const path = '/api/v1/server/following' const followingHosts = following.map(f => f.replace(/^http:\/\//, '')) @@ -96,7 +97,7 @@ function follow (follower: string, following: string[], accessToken: string, exp .expect(expectedStatus) } -async function unfollow (url: string, accessToken: string, target: ServerInfo, expectedStatus = 204) { +async function unfollow (url: string, accessToken: string, target: ServerInfo, expectedStatus = HttpStatusCode.NO_CONTENT_204) { const path = '/api/v1/server/following/' + target.host return request(url) @@ -106,7 +107,7 @@ async function unfollow (url: string, accessToken: string, target: ServerInfo, e .expect(expectedStatus) } -function removeFollower (url: string, accessToken: string, follower: ServerInfo, expectedStatus = 204) { +function removeFollower (url: string, accessToken: string, follower: ServerInfo, expectedStatus = HttpStatusCode.NO_CONTENT_204) { const path = '/api/v1/server/followers/peertube@' + follower.host return request(url) diff --git a/shared/extra-utils/server/jobs.ts b/shared/extra-utils/server/jobs.ts index d984b3d1e..cac00e9ab 100644 --- a/shared/extra-utils/server/jobs.ts +++ b/shared/extra-utils/server/jobs.ts @@ -3,6 +3,7 @@ import { Job, JobState, JobType } from '../../models' import { wait } from '../miscs/miscs' import { ServerInfo } from './servers' import { makeGetRequest } from '../../../shared/extra-utils' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' function getJobsList (url: string, accessToken: string, state: JobState) { const path = '/api/v1/jobs/' + state @@ -11,7 +12,7 @@ function getJobsList (url: string, accessToken: string, state: JobState) { .get(path) .set('Accept', 'application/json') .set('Authorization', 'Bearer ' + accessToken) - .expect(200) + .expect(HttpStatusCode.OK_200) .expect('Content-Type', /json/) } @@ -38,7 +39,7 @@ function getJobsListPaginationAndSort (options: { url, path, token: accessToken, - statusCodeExpected: 200, + statusCodeExpected: HttpStatusCode.OK_200, query }) } diff --git a/shared/extra-utils/server/plugins.ts b/shared/extra-utils/server/plugins.ts index 83db2f6b8..864954ee7 100644 --- a/shared/extra-utils/server/plugins.ts +++ b/shared/extra-utils/server/plugins.ts @@ -9,6 +9,7 @@ import { PluginType } from '../../models/plugins/plugin.type' import { buildServerDirectory, root } from '../miscs/miscs' import { makeGetRequest, makePostBodyRequest, makePutBodyRequest } from '../requests/requests' import { ServerInfo } from './servers' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' function listPlugins (parameters: { url: string @@ -18,9 +19,9 @@ function listPlugins (parameters: { sort?: string pluginType?: PluginType uninstalled?: boolean - expectedStatus?: number + expectedStatus?: HttpStatusCode }) { - const { url, accessToken, start, count, sort, pluginType, uninstalled, expectedStatus = 200 } = parameters + const { url, accessToken, start, count, sort, pluginType, uninstalled, expectedStatus = HttpStatusCode.OK_200 } = parameters const path = '/api/v1/plugins' return makeGetRequest({ @@ -47,9 +48,19 @@ function listAvailablePlugins (parameters: { pluginType?: PluginType currentPeerTubeEngine?: string search?: string - expectedStatus?: number + expectedStatus?: HttpStatusCode }) { - const { url, accessToken, start, count, sort, pluginType, search, currentPeerTubeEngine, expectedStatus = 200 } = parameters + const { + url, + accessToken, + start, + count, + sort, + pluginType, + search, + currentPeerTubeEngine, + expectedStatus = HttpStatusCode.OK_200 + } = parameters const path = '/api/v1/plugins/available' const query: PeertubePluginIndexList = { @@ -74,9 +85,9 @@ function getPlugin (parameters: { url: string accessToken: string npmName: string - expectedStatus?: number + expectedStatus?: HttpStatusCode }) { - const { url, accessToken, npmName, expectedStatus = 200 } = parameters + const { url, accessToken, npmName, expectedStatus = HttpStatusCode.OK_200 } = parameters const path = '/api/v1/plugins/' + npmName return makeGetRequest({ @@ -92,9 +103,9 @@ function updatePluginSettings (parameters: { accessToken: string npmName: string settings: any - expectedStatus?: number + expectedStatus?: HttpStatusCode }) { - const { url, accessToken, npmName, settings, expectedStatus = 204 } = parameters + const { url, accessToken, npmName, settings, expectedStatus = HttpStatusCode.NO_CONTENT_204 } = parameters const path = '/api/v1/plugins/' + npmName + '/settings' return makePutBodyRequest({ @@ -110,9 +121,9 @@ function getPluginRegisteredSettings (parameters: { url: string accessToken: string npmName: string - expectedStatus?: number + expectedStatus?: HttpStatusCode }) { - const { url, accessToken, npmName, expectedStatus = 200 } = parameters + const { url, accessToken, npmName, expectedStatus = HttpStatusCode.OK_200 } = parameters const path = '/api/v1/plugins/' + npmName + '/registered-settings' return makeGetRequest({ @@ -141,9 +152,9 @@ async function testHelloWorldRegisteredSettings (server: ServerInfo) { function getPublicSettings (parameters: { url: string npmName: string - expectedStatus?: number + expectedStatus?: HttpStatusCode }) { - const { url, npmName, expectedStatus = 200 } = parameters + const { url, npmName, expectedStatus = HttpStatusCode.OK_200 } = parameters const path = '/api/v1/plugins/' + npmName + '/public-settings' return makeGetRequest({ @@ -156,9 +167,9 @@ function getPublicSettings (parameters: { function getPluginTranslations (parameters: { url: string locale: string - expectedStatus?: number + expectedStatus?: HttpStatusCode }) { - const { url, locale, expectedStatus = 200 } = parameters + const { url, locale, expectedStatus = HttpStatusCode.OK_200 } = parameters const path = '/plugins/translations/' + locale + '.json' return makeGetRequest({ @@ -173,9 +184,9 @@ function installPlugin (parameters: { accessToken: string path?: string npmName?: string - expectedStatus?: number + expectedStatus?: HttpStatusCode }) { - const { url, accessToken, npmName, path, expectedStatus = 200 } = parameters + const { url, accessToken, npmName, path, expectedStatus = HttpStatusCode.OK_200 } = parameters const apiPath = '/api/v1/plugins/install' return makePostBodyRequest({ @@ -192,9 +203,9 @@ function updatePlugin (parameters: { accessToken: string path?: string npmName?: string - expectedStatus?: number + expectedStatus?: HttpStatusCode }) { - const { url, accessToken, npmName, path, expectedStatus = 200 } = parameters + const { url, accessToken, npmName, path, expectedStatus = HttpStatusCode.OK_200 } = parameters const apiPath = '/api/v1/plugins/update' return makePostBodyRequest({ @@ -210,9 +221,9 @@ function uninstallPlugin (parameters: { url: string accessToken: string npmName: string - expectedStatus?: number + expectedStatus?: HttpStatusCode }) { - const { url, accessToken, npmName, expectedStatus = 204 } = parameters + const { url, accessToken, npmName, expectedStatus = HttpStatusCode.NO_CONTENT_204 } = parameters const apiPath = '/api/v1/plugins/uninstall' return makePostBodyRequest({ @@ -230,7 +241,7 @@ function getPluginsCSS (url: string) { return makeGetRequest({ url, path, - statusCodeExpected: 200 + statusCodeExpected: HttpStatusCode.OK_200 }) } @@ -260,7 +271,7 @@ function getExternalAuth (options: { npmVersion: string authName: string query?: any - statusCodeExpected?: number + statusCodeExpected?: HttpStatusCode }) { const { url, npmName, npmVersion, authName, statusCodeExpected, query } = options @@ -270,7 +281,7 @@ function getExternalAuth (options: { url, path, query, - statusCodeExpected: statusCodeExpected || 200, + statusCodeExpected: statusCodeExpected || HttpStatusCode.OK_200, redirects: 0 }) } diff --git a/shared/extra-utils/server/redundancy.ts b/shared/extra-utils/server/redundancy.ts index 08467e4c0..3aca4ebfd 100644 --- a/shared/extra-utils/server/redundancy.ts +++ b/shared/extra-utils/server/redundancy.ts @@ -1,5 +1,6 @@ import { makeDeleteRequest, makeGetRequest, makePostBodyRequest, makePutBodyRequest } from '../requests/requests' import { VideoRedundanciesTarget } from '@shared/models' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' function updateRedundancy (url: string, accessToken: string, host: string, redundancyAllowed: boolean, expectedStatus = 204) { const path = '/api/v1/server/redundancy/' + host @@ -20,7 +21,7 @@ function listVideoRedundancies (options: { start?: number count?: number sort?: string - statusCodeExpected?: number + statusCodeExpected?: HttpStatusCode }) { const path = '/api/v1/server/redundancy/videos' @@ -36,7 +37,7 @@ function listVideoRedundancies (options: { sort: sort ?? 'name', target }, - statusCodeExpected: statusCodeExpected || 200 + statusCodeExpected: statusCodeExpected || HttpStatusCode.OK_200 }) } @@ -53,7 +54,7 @@ function addVideoRedundancy (options: { token: accessToken, path, fields: { videoId }, - statusCodeExpected: 204 + statusCodeExpected: HttpStatusCode.NO_CONTENT_204 }) } @@ -69,7 +70,7 @@ function removeVideoRedundancy (options: { url, token: accessToken, path, - statusCodeExpected: 204 + statusCodeExpected: HttpStatusCode.NO_CONTENT_204 }) } diff --git a/shared/extra-utils/server/stats.ts b/shared/extra-utils/server/stats.ts index 6f079ad18..b9dae24e2 100644 --- a/shared/extra-utils/server/stats.ts +++ b/shared/extra-utils/server/stats.ts @@ -1,4 +1,5 @@ import { makeGetRequest } from '../requests/requests' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' function getStats (url: string, useCache = false) { const path = '/api/v1/server/stats' @@ -11,7 +12,7 @@ function getStats (url: string, useCache = false) { url, path, query, - statusCodeExpected: 200 + statusCodeExpected: HttpStatusCode.OK_200 }) } diff --git a/shared/extra-utils/users/accounts.ts b/shared/extra-utils/users/accounts.ts index f87706f6a..4ea7f1402 100644 --- a/shared/extra-utils/users/accounts.ts +++ b/shared/extra-utils/users/accounts.ts @@ -8,8 +8,9 @@ import { Account } from '../../models/actors' import { root } from '../miscs/miscs' import { makeGetRequest } from '../requests/requests' import { VideoRateType } from '../../models/videos' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' -function getAccountsList (url: string, sort = '-createdAt', statusCodeExpected = 200) { +function getAccountsList (url: string, sort = '-createdAt', statusCodeExpected = HttpStatusCode.OK_200) { const path = '/api/v1/accounts' return makeGetRequest({ @@ -20,7 +21,7 @@ function getAccountsList (url: string, sort = '-createdAt', statusCodeExpected = }) } -function getAccount (url: string, accountName: string, statusCodeExpected = 200) { +function getAccount (url: string, accountName: string, statusCodeExpected = HttpStatusCode.OK_200) { const path = '/api/v1/accounts/' + accountName return makeGetRequest({ @@ -55,7 +56,13 @@ async function checkActorFilesWereRemoved (filename: string, serverNumber: numbe } } -function getAccountRatings (url: string, accountName: string, accessToken: string, rating?: VideoRateType, statusCodeExpected = 200) { +function getAccountRatings ( + url: string, + accountName: string, + accessToken: string, + rating?: VideoRateType, + statusCodeExpected = HttpStatusCode.OK_200 +) { const path = '/api/v1/accounts/' + accountName + '/ratings' const query = rating ? { rating } : {} diff --git a/shared/extra-utils/users/blocklist.ts b/shared/extra-utils/users/blocklist.ts index 39e720b42..bdf7ee58a 100644 --- a/shared/extra-utils/users/blocklist.ts +++ b/shared/extra-utils/users/blocklist.ts @@ -1,6 +1,7 @@ /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ import { makeGetRequest, makeDeleteRequest, makePostBodyRequest } from '../requests/requests' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' function getAccountBlocklistByAccount ( url: string, @@ -8,7 +9,7 @@ function getAccountBlocklistByAccount ( start: number, count: number, sort = '-createdAt', - statusCodeExpected = 200 + statusCodeExpected = HttpStatusCode.OK_200 ) { const path = '/api/v1/users/me/blocklist/accounts' @@ -21,7 +22,12 @@ function getAccountBlocklistByAccount ( }) } -function addAccountToAccountBlocklist (url: string, token: string, accountToBlock: string, statusCodeExpected = 204) { +function addAccountToAccountBlocklist ( + url: string, + token: string, + accountToBlock: string, + statusCodeExpected = HttpStatusCode.NO_CONTENT_204 +) { const path = '/api/v1/users/me/blocklist/accounts' return makePostBodyRequest({ @@ -35,7 +41,12 @@ function addAccountToAccountBlocklist (url: string, token: string, accountToBloc }) } -function removeAccountFromAccountBlocklist (url: string, token: string, accountToUnblock: string, statusCodeExpected = 204) { +function removeAccountFromAccountBlocklist ( + url: string, + token: string, + accountToUnblock: string, + statusCodeExpected = HttpStatusCode.NO_CONTENT_204 +) { const path = '/api/v1/users/me/blocklist/accounts/' + accountToUnblock return makeDeleteRequest({ @@ -52,7 +63,7 @@ function getServerBlocklistByAccount ( start: number, count: number, sort = '-createdAt', - statusCodeExpected = 200 + statusCodeExpected = HttpStatusCode.OK_200 ) { const path = '/api/v1/users/me/blocklist/servers' @@ -65,7 +76,12 @@ function getServerBlocklistByAccount ( }) } -function addServerToAccountBlocklist (url: string, token: string, serverToBlock: string, statusCodeExpected = 204) { +function addServerToAccountBlocklist ( + url: string, + token: string, + serverToBlock: string, + statusCodeExpected = HttpStatusCode.NO_CONTENT_204 +) { const path = '/api/v1/users/me/blocklist/servers' return makePostBodyRequest({ @@ -79,7 +95,12 @@ function addServerToAccountBlocklist (url: string, token: string, serverToBlock: }) } -function removeServerFromAccountBlocklist (url: string, token: string, serverToBlock: string, statusCodeExpected = 204) { +function removeServerFromAccountBlocklist ( + url: string, + token: string, + serverToBlock: string, + statusCodeExpected = HttpStatusCode.NO_CONTENT_204 +) { const path = '/api/v1/users/me/blocklist/servers/' + serverToBlock return makeDeleteRequest({ @@ -96,7 +117,7 @@ function getAccountBlocklistByServer ( start: number, count: number, sort = '-createdAt', - statusCodeExpected = 200 + statusCodeExpected = HttpStatusCode.OK_200 ) { const path = '/api/v1/server/blocklist/accounts' @@ -109,7 +130,12 @@ function getAccountBlocklistByServer ( }) } -function addAccountToServerBlocklist (url: string, token: string, accountToBlock: string, statusCodeExpected = 204) { +function addAccountToServerBlocklist ( + url: string, + token: string, + accountToBlock: string, + statusCodeExpected = HttpStatusCode.NO_CONTENT_204 +) { const path = '/api/v1/server/blocklist/accounts' return makePostBodyRequest({ @@ -123,7 +149,12 @@ function addAccountToServerBlocklist (url: string, token: string, accountToBlock }) } -function removeAccountFromServerBlocklist (url: string, token: string, accountToUnblock: string, statusCodeExpected = 204) { +function removeAccountFromServerBlocklist ( + url: string, + token: string, + accountToUnblock: string, + statusCodeExpected = HttpStatusCode.NO_CONTENT_204 +) { const path = '/api/v1/server/blocklist/accounts/' + accountToUnblock return makeDeleteRequest({ @@ -140,7 +171,7 @@ function getServerBlocklistByServer ( start: number, count: number, sort = '-createdAt', - statusCodeExpected = 200 + statusCodeExpected = HttpStatusCode.OK_200 ) { const path = '/api/v1/server/blocklist/servers' @@ -153,7 +184,12 @@ function getServerBlocklistByServer ( }) } -function addServerToServerBlocklist (url: string, token: string, serverToBlock: string, statusCodeExpected = 204) { +function addServerToServerBlocklist ( + url: string, + token: string, + serverToBlock: string, + statusCodeExpected = HttpStatusCode.NO_CONTENT_204 +) { const path = '/api/v1/server/blocklist/servers' return makePostBodyRequest({ @@ -167,7 +203,12 @@ function addServerToServerBlocklist (url: string, token: string, serverToBlock: }) } -function removeServerFromServerBlocklist (url: string, token: string, serverToBlock: string, statusCodeExpected = 204) { +function removeServerFromServerBlocklist ( + url: string, + token: string, + serverToBlock: string, + statusCodeExpected = HttpStatusCode.NO_CONTENT_204 +) { const path = '/api/v1/server/blocklist/servers/' + serverToBlock return makeDeleteRequest({ diff --git a/shared/extra-utils/users/user-notifications.ts b/shared/extra-utils/users/user-notifications.ts index 98d222e1d..467a3d959 100644 --- a/shared/extra-utils/users/user-notifications.ts +++ b/shared/extra-utils/users/user-notifications.ts @@ -11,8 +11,14 @@ import { flushAndRunMultipleServers, ServerInfo } from '../server/servers' import { getUserNotificationSocket } from '../socket/socket-io' import { setAccessTokensToServers, userLogin } from './login' import { createUser, getMyUserInformation } from './users' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' -function updateMyNotificationSettings (url: string, token: string, settings: UserNotificationSetting, statusCodeExpected = 204) { +function updateMyNotificationSettings ( + url: string, + token: string, + settings: UserNotificationSetting, + statusCodeExpected = HttpStatusCode.NO_CONTENT_204 +) { const path = '/api/v1/users/me/notification-settings' return makePutBodyRequest({ @@ -31,7 +37,7 @@ async function getUserNotifications ( count: number, unread?: boolean, sort = '-createdAt', - statusCodeExpected = 200 + statusCodeExpected = HttpStatusCode.OK_200 ) { const path = '/api/v1/users/me/notifications' @@ -49,7 +55,7 @@ async function getUserNotifications ( }) } -function markAsReadNotifications (url: string, token: string, ids: number[], statusCodeExpected = 204) { +function markAsReadNotifications (url: string, token: string, ids: number[], statusCodeExpected = HttpStatusCode.NO_CONTENT_204) { const path = '/api/v1/users/me/notifications/read' return makePostBodyRequest({ @@ -61,7 +67,7 @@ function markAsReadNotifications (url: string, token: string, ids: number[], sta }) } -function markAsReadAllNotifications (url: string, token: string, statusCodeExpected = 204) { +function markAsReadAllNotifications (url: string, token: string, statusCodeExpected = HttpStatusCode.NO_CONTENT_204) { const path = '/api/v1/users/me/notifications/read-all' return makePostBodyRequest({ diff --git a/shared/extra-utils/users/user-subscriptions.ts b/shared/extra-utils/users/user-subscriptions.ts index 6d402c073..edc7a3562 100644 --- a/shared/extra-utils/users/user-subscriptions.ts +++ b/shared/extra-utils/users/user-subscriptions.ts @@ -1,6 +1,7 @@ import { makeDeleteRequest, makeGetRequest, makePostBodyRequest } from '../requests/requests' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' -function addUserSubscription (url: string, token: string, targetUri: string, statusCodeExpected = 204) { +function addUserSubscription (url: string, token: string, targetUri: string, statusCodeExpected = HttpStatusCode.NO_CONTENT_204) { const path = '/api/v1/users/me/subscriptions' return makePostBodyRequest({ @@ -19,7 +20,7 @@ function listUserSubscriptions (parameters: { search?: string statusCodeExpected?: number }) { - const { url, token, sort = '-createdAt', search, statusCodeExpected = 200 } = parameters + const { url, token, sort = '-createdAt', search, statusCodeExpected = HttpStatusCode.OK_200 } = parameters const path = '/api/v1/users/me/subscriptions' return makeGetRequest({ @@ -34,7 +35,7 @@ function listUserSubscriptions (parameters: { }) } -function listUserSubscriptionVideos (url: string, token: string, sort = '-createdAt', statusCodeExpected = 200) { +function listUserSubscriptionVideos (url: string, token: string, sort = '-createdAt', statusCodeExpected = HttpStatusCode.OK_200) { const path = '/api/v1/users/me/subscriptions/videos' return makeGetRequest({ @@ -46,7 +47,7 @@ function listUserSubscriptionVideos (url: string, token: string, sort = '-create }) } -function getUserSubscription (url: string, token: string, uri: string, statusCodeExpected = 200) { +function getUserSubscription (url: string, token: string, uri: string, statusCodeExpected = HttpStatusCode.OK_200) { const path = '/api/v1/users/me/subscriptions/' + uri return makeGetRequest({ @@ -57,7 +58,7 @@ function getUserSubscription (url: string, token: string, uri: string, statusCod }) } -function removeUserSubscription (url: string, token: string, uri: string, statusCodeExpected = 204) { +function removeUserSubscription (url: string, token: string, uri: string, statusCodeExpected = HttpStatusCode.NO_CONTENT_204) { const path = '/api/v1/users/me/subscriptions/' + uri return makeDeleteRequest({ @@ -68,7 +69,7 @@ function removeUserSubscription (url: string, token: string, uri: string, status }) } -function areSubscriptionsExist (url: string, token: string, uris: string[], statusCodeExpected = 200) { +function areSubscriptionsExist (url: string, token: string, uris: string[], statusCodeExpected = HttpStatusCode.OK_200) { const path = '/api/v1/users/me/subscriptions/exist' return makeGetRequest({ diff --git a/shared/extra-utils/users/users.ts b/shared/extra-utils/users/users.ts index ebb8bc257..c683dcdd1 100644 --- a/shared/extra-utils/users/users.ts +++ b/shared/extra-utils/users/users.ts @@ -7,6 +7,7 @@ import { UserRole } from '../../models/users/user-role' import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../requests/requests' import { ServerInfo } from '../server/servers' import { userLogin } from './login' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' type CreateUserArgs = { url: string @@ -29,7 +30,7 @@ function createUser (parameters: CreateUserArgs) { videoQuota = 1000000, videoQuotaDaily = -1, role = UserRole.USER, - specialStatus = 200 + specialStatus = HttpStatusCode.OK_200 } = parameters const path = '/api/v1/users' @@ -58,7 +59,7 @@ async function generateUserAccessToken (server: ServerInfo, username: string) { return userLogin(server, { username, password }) } -function registerUser (url: string, username: string, password: string, specialStatus = 204) { +function registerUser (url: string, username: string, password: string, specialStatus = HttpStatusCode.NO_CONTENT_204) { const path = '/api/v1/users/register' const body = { username, @@ -94,11 +95,11 @@ function registerUserWithChannel (options: { url: options.url, path, fields: body, - statusCodeExpected: 204 + statusCodeExpected: HttpStatusCode.NO_CONTENT_204 }) } -function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) { +function getMyUserInformation (url: string, accessToken: string, specialStatus = HttpStatusCode.OK_200) { const path = '/api/v1/users/me' return request(url) @@ -109,7 +110,7 @@ function getMyUserInformation (url: string, accessToken: string, specialStatus = .expect('Content-Type', /json/) } -function getUserScopedTokens (url: string, token: string, statusCodeExpected = 200) { +function getUserScopedTokens (url: string, token: string, statusCodeExpected = HttpStatusCode.OK_200) { const path = '/api/v1/users/scoped-tokens' return makeGetRequest({ @@ -120,7 +121,7 @@ function getUserScopedTokens (url: string, token: string, statusCodeExpected = 2 }) } -function renewUserScopedTokens (url: string, token: string, statusCodeExpected = 200) { +function renewUserScopedTokens (url: string, token: string, statusCodeExpected = HttpStatusCode.OK_200) { const path = '/api/v1/users/scoped-tokens' return makePostBodyRequest({ @@ -131,7 +132,7 @@ function renewUserScopedTokens (url: string, token: string, statusCodeExpected = }) } -function deleteMe (url: string, accessToken: string, specialStatus = 204) { +function deleteMe (url: string, accessToken: string, specialStatus = HttpStatusCode.NO_CONTENT_204) { const path = '/api/v1/users/me' return request(url) @@ -141,7 +142,7 @@ function deleteMe (url: string, accessToken: string, specialStatus = 204) { .expect(specialStatus) } -function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) { +function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = HttpStatusCode.OK_200) { const path = '/api/v1/users/me/video-quota-used' return request(url) @@ -160,11 +161,11 @@ function getUserInformation (url: string, accessToken: string, userId: number, w .query({ withStats }) .set('Accept', 'application/json') .set('Authorization', 'Bearer ' + accessToken) - .expect(200) + .expect(HttpStatusCode.OK_200) .expect('Content-Type', /json/) } -function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) { +function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = HttpStatusCode.OK_200) { const path = '/api/v1/users/me/videos/' + videoId + '/rating' return request(url) @@ -182,7 +183,7 @@ function getUsersList (url: string, accessToken: string) { .get(path) .set('Accept', 'application/json') .set('Authorization', 'Bearer ' + accessToken) - .expect(200) + .expect(HttpStatusCode.OK_200) .expect('Content-Type', /json/) } @@ -210,11 +211,11 @@ function getUsersListPaginationAndSort ( .query(query) .set('Accept', 'application/json') .set('Authorization', 'Bearer ' + accessToken) - .expect(200) + .expect(HttpStatusCode.OK_200) .expect('Content-Type', /json/) } -function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) { +function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) { const path = '/api/v1/users' return request(url) @@ -224,7 +225,13 @@ function removeUser (url: string, userId: number | string, accessToken: string, .expect(expectedStatus) } -function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) { +function blockUser ( + url: string, + userId: number | string, + accessToken: string, + expectedStatus = HttpStatusCode.NO_CONTENT_204, + reason?: string +) { const path = '/api/v1/users' let body: any if (reason) body = { reason } @@ -237,7 +244,7 @@ function blockUser (url: string, userId: number | string, accessToken: string, e .expect(expectedStatus) } -function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) { +function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) { const path = '/api/v1/users' return request(url) @@ -247,7 +254,7 @@ function unblockUser (url: string, userId: number | string, accessToken: string, .expect(expectedStatus) } -function updateMyUser (options: { url: string, accessToken: string, statusCodeExpected?: number } & UserUpdateMe) { +function updateMyUser (options: { url: string, accessToken: string, statusCodeExpected?: HttpStatusCode } & UserUpdateMe) { const path = '/api/v1/users/me' const toSend: UserUpdateMe = omit(options, 'url', 'accessToken') @@ -257,7 +264,7 @@ function updateMyUser (options: { url: string, accessToken: string, statusCodeEx path, token: options.accessToken, fields: toSend, - statusCodeExpected: options.statusCodeExpected || 204 + statusCodeExpected: options.statusCodeExpected || HttpStatusCode.NO_CONTENT_204 }) } @@ -299,7 +306,7 @@ function updateUser (options: { path, token: options.accessToken, fields: toSend, - statusCodeExpected: 204 + statusCodeExpected: HttpStatusCode.NO_CONTENT_204 }) } @@ -310,11 +317,17 @@ function askResetPassword (url: string, email: string) { url, path, fields: { email }, - statusCodeExpected: 204 + statusCodeExpected: HttpStatusCode.NO_CONTENT_204 }) } -function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) { +function resetPassword ( + url: string, + userId: number, + verificationString: string, + password: string, + statusCodeExpected = HttpStatusCode.NO_CONTENT_204 +) { const path = '/api/v1/users/' + userId + '/reset-password' return makePostBodyRequest({ @@ -332,11 +345,17 @@ function askSendVerifyEmail (url: string, email: string) { url, path, fields: { email }, - statusCodeExpected: 204 + statusCodeExpected: HttpStatusCode.NO_CONTENT_204 }) } -function verifyEmail (url: string, userId: number, verificationString: string, isPendingEmail = false, statusCodeExpected = 204) { +function verifyEmail ( + url: string, + userId: number, + verificationString: string, + isPendingEmail = false, + statusCodeExpected = HttpStatusCode.NO_CONTENT_204 +) { const path = '/api/v1/users/' + userId + '/verify-email' return makePostBodyRequest({ diff --git a/shared/extra-utils/videos/live.ts b/shared/extra-utils/videos/live.ts index 522beb8bc..4aa66622b 100644 --- a/shared/extra-utils/videos/live.ts +++ b/shared/extra-utils/videos/live.ts @@ -10,8 +10,9 @@ import { buildAbsoluteFixturePath, buildServerDirectory, wait } from '../miscs/m import { makeGetRequest, makePutBodyRequest, makeUploadRequest } from '../requests/requests' import { ServerInfo } from '../server/servers' import { getVideoWithToken } from './videos' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' -function getLive (url: string, token: string, videoId: number | string, statusCodeExpected = 200) { +function getLive (url: string, token: string, videoId: number | string, statusCodeExpected = HttpStatusCode.OK_200) { const path = '/api/v1/videos/live' return makeGetRequest({ @@ -22,7 +23,13 @@ function getLive (url: string, token: string, videoId: number | string, statusCo }) } -function updateLive (url: string, token: string, videoId: number | string, fields: LiveVideoUpdate, statusCodeExpected = 204) { +function updateLive ( + url: string, + token: string, + videoId: number | string, + fields: LiveVideoUpdate, + statusCodeExpected = HttpStatusCode.NO_CONTENT_204 +) { const path = '/api/v1/videos/live' return makePutBodyRequest({ @@ -34,7 +41,7 @@ function updateLive (url: string, token: string, videoId: number | string, field }) } -function createLive (url: string, token: string, fields: LiveVideoCreate, statusCodeExpected = 200) { +function createLive (url: string, token: string, fields: LiveVideoCreate, statusCodeExpected = HttpStatusCode.OK_200) { const path = '/api/v1/videos/live' const attaches: any = {} diff --git a/shared/extra-utils/videos/services.ts b/shared/extra-utils/videos/services.ts index 1a53dd4cf..e13a788bd 100644 --- a/shared/extra-utils/videos/services.ts +++ b/shared/extra-utils/videos/services.ts @@ -1,4 +1,5 @@ import * as request from 'supertest' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' function getOEmbed (url: string, oembedUrl: string, format?: string, maxHeight?: number, maxWidth?: number) { const path = '/services/oembed' @@ -13,7 +14,7 @@ function getOEmbed (url: string, oembedUrl: string, format?: string, maxHeight?: .get(path) .query(query) .set('Accept', 'application/json') - .expect(200) + .expect(HttpStatusCode.OK_200) } // --------------------------------------------------------------------------- diff --git a/shared/extra-utils/videos/video-blacklist.ts b/shared/extra-utils/videos/video-blacklist.ts index ba139ef95..aa1548537 100644 --- a/shared/extra-utils/videos/video-blacklist.ts +++ b/shared/extra-utils/videos/video-blacklist.ts @@ -1,6 +1,7 @@ import * as request from 'supertest' import { VideoBlacklistType } from '../../models/videos' import { makeGetRequest } from '..' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' function addVideoToBlacklist ( url: string, @@ -8,7 +9,7 @@ function addVideoToBlacklist ( videoId: number | string, reason?: string, unfederate?: boolean, - specialStatus = 204 + specialStatus = HttpStatusCode.NO_CONTENT_204 ) { const path = '/api/v1/videos/' + videoId + '/blacklist' @@ -20,7 +21,13 @@ function addVideoToBlacklist ( .expect(specialStatus) } -function updateVideoBlacklist (url: string, token: string, videoId: number, reason?: string, specialStatus = 204) { +function updateVideoBlacklist ( + url: string, + token: string, + videoId: number, + reason?: string, + specialStatus = HttpStatusCode.NO_CONTENT_204 +) { const path = '/api/v1/videos/' + videoId + '/blacklist' return request(url) @@ -31,7 +38,7 @@ function updateVideoBlacklist (url: string, token: string, videoId: number, reas .expect(specialStatus) } -function removeVideoFromBlacklist (url: string, token: string, videoId: number | string, specialStatus = 204) { +function removeVideoFromBlacklist (url: string, token: string, videoId: number | string, specialStatus = HttpStatusCode.NO_CONTENT_204) { const path = '/api/v1/videos/' + videoId + '/blacklist' return request(url) @@ -46,9 +53,9 @@ function getBlacklistedVideosList (parameters: { token: string sort?: string type?: VideoBlacklistType - specialStatus?: number + specialStatus?: HttpStatusCode }) { - const { url, token, sort, type, specialStatus = 200 } = parameters + const { url, token, sort, type, specialStatus = HttpStatusCode.OK_200 } = parameters const path = '/api/v1/videos/blacklist/' const query = { sort, type } diff --git a/shared/extra-utils/videos/video-captions.ts b/shared/extra-utils/videos/video-captions.ts index 5bd533bba..62eec7b90 100644 --- a/shared/extra-utils/videos/video-captions.ts +++ b/shared/extra-utils/videos/video-captions.ts @@ -2,6 +2,7 @@ import { makeDeleteRequest, makeGetRequest, makeUploadRequest } from '../request import * as request from 'supertest' import * as chai from 'chai' import { buildAbsoluteFixturePath } from '../miscs/miscs' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' const expect = chai.expect @@ -28,7 +29,7 @@ function createVideoCaption (args: { attaches: { captionfile: captionfileAttach }, - statusCodeExpected: args.statusCodeExpected || 204 + statusCodeExpected: args.statusCodeExpected || HttpStatusCode.NO_CONTENT_204 }) } @@ -38,7 +39,7 @@ function listVideoCaptions (url: string, videoId: string | number) { return makeGetRequest({ url, path, - statusCodeExpected: 200 + statusCodeExpected: HttpStatusCode.OK_200 }) } @@ -49,14 +50,14 @@ function deleteVideoCaption (url: string, token: string, videoId: string | numbe url, token, path, - statusCodeExpected: 204 + statusCodeExpected: HttpStatusCode.NO_CONTENT_204 }) } async function testCaptionFile (url: string, captionPath: string, containsString: string) { const res = await request(url) .get(captionPath) - .expect(200) + .expect(HttpStatusCode.OK_200) expect(res.text).to.contain(containsString) } diff --git a/shared/extra-utils/videos/video-change-ownership.ts b/shared/extra-utils/videos/video-change-ownership.ts index 371d02000..ef82a7636 100644 --- a/shared/extra-utils/videos/video-change-ownership.ts +++ b/shared/extra-utils/videos/video-change-ownership.ts @@ -1,6 +1,13 @@ import * as request from 'supertest' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' -function changeVideoOwnership (url: string, token: string, videoId: number | string, username, expectedStatus = 204) { +function changeVideoOwnership ( + url: string, + token: string, + videoId: number | string, + username, + expectedStatus = HttpStatusCode.NO_CONTENT_204 +) { const path = '/api/v1/videos/' + videoId + '/give-ownership' return request(url) @@ -19,11 +26,17 @@ function getVideoChangeOwnershipList (url: string, token: string) { .query({ sort: '-createdAt' }) .set('Accept', 'application/json') .set('Authorization', 'Bearer ' + token) - .expect(200) + .expect(HttpStatusCode.OK_200) .expect('Content-Type', /json/) } -function acceptChangeOwnership (url: string, token: string, ownershipId: string, channelId: number, expectedStatus = 204) { +function acceptChangeOwnership ( + url: string, + token: string, + ownershipId: string, + channelId: number, + expectedStatus = HttpStatusCode.NO_CONTENT_204 +) { const path = '/api/v1/videos/ownership/' + ownershipId + '/accept' return request(url) @@ -34,7 +47,12 @@ function acceptChangeOwnership (url: string, token: string, ownershipId: string, .expect(expectedStatus) } -function refuseChangeOwnership (url: string, token: string, ownershipId: string, expectedStatus = 204) { +function refuseChangeOwnership ( + url: string, + token: string, + ownershipId: string, + expectedStatus = HttpStatusCode.NO_CONTENT_204 +) { const path = '/api/v1/videos/ownership/' + ownershipId + '/refuse' return request(url) diff --git a/shared/extra-utils/videos/video-channels.ts b/shared/extra-utils/videos/video-channels.ts index 97b68178f..3ff445c2a 100644 --- a/shared/extra-utils/videos/video-channels.ts +++ b/shared/extra-utils/videos/video-channels.ts @@ -7,6 +7,7 @@ import { makeGetRequest, updateAvatarRequest } from '../requests/requests' import { ServerInfo } from '../server/servers' import { User } from '../../models/users/user.model' import { getMyUserInformation } from '../users/users' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' function getVideoChannelsList (url: string, start: number, count: number, sort?: string, withStats?: boolean) { const path = '/api/v1/video-channels' @@ -20,7 +21,7 @@ function getVideoChannelsList (url: string, start: number, count: number, sort?: if (withStats) req.query({ withStats }) return req.set('Accept', 'application/json') - .expect(200) + .expect(HttpStatusCode.OK_200) .expect('Content-Type', /json/) } @@ -30,11 +31,20 @@ function getAccountVideoChannelsList (parameters: { start?: number count?: number sort?: string - specialStatus?: number + specialStatus?: HttpStatusCode withStats?: boolean search?: string }) { - const { url, accountName, start, count, sort = 'createdAt', specialStatus = 200, withStats = false, search } = parameters + const { + url, + accountName, + start, + count, + sort = 'createdAt', + specialStatus = HttpStatusCode.OK_200, + withStats = false, + search + } = parameters const path = '/api/v1/accounts/' + accountName + '/video-channels' @@ -56,7 +66,7 @@ function addVideoChannel ( url: string, token: string, videoChannelAttributesArg: VideoChannelCreate, - expectedStatus = 200 + expectedStatus = HttpStatusCode.OK_200 ) { const path = '/api/v1/video-channels/' @@ -81,7 +91,7 @@ function updateVideoChannel ( token: string, channelName: string, attributes: VideoChannelUpdate, - expectedStatus = 204 + expectedStatus = HttpStatusCode.NO_CONTENT_204 ) { const body: any = {} const path = '/api/v1/video-channels/' + channelName @@ -99,7 +109,7 @@ function updateVideoChannel ( .expect(expectedStatus) } -function deleteVideoChannel (url: string, token: string, channelName: string, expectedStatus = 204) { +function deleteVideoChannel (url: string, token: string, channelName: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) { const path = '/api/v1/video-channels/' + channelName return request(url) @@ -115,7 +125,7 @@ function getVideoChannel (url: string, channelName: string) { return request(url) .get(path) .set('Accept', 'application/json') - .expect(200) + .expect(HttpStatusCode.OK_200) .expect('Content-Type', /json/) } diff --git a/shared/extra-utils/videos/video-comments.ts b/shared/extra-utils/videos/video-comments.ts index 0b0df81dc..71b9f875a 100644 --- a/shared/extra-utils/videos/video-comments.ts +++ b/shared/extra-utils/videos/video-comments.ts @@ -2,6 +2,7 @@ import * as request from 'supertest' import { makeDeleteRequest, makeGetRequest } from '../requests/requests' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' function getAdminVideoComments (options: { url: string @@ -33,7 +34,7 @@ function getAdminVideoComments (options: { path, token, query, - statusCodeExpected: 200 + statusCodeExpected: HttpStatusCode.OK_200 }) } @@ -49,7 +50,7 @@ function getVideoCommentThreads (url: string, videoId: number | string, start: n if (token) req.set('Authorization', 'Bearer ' + token) return req.set('Accept', 'application/json') - .expect(200) + .expect(HttpStatusCode.OK_200) .expect('Content-Type', /json/) } @@ -62,11 +63,17 @@ function getVideoThreadComments (url: string, videoId: number | string, threadId if (token) req.set('Authorization', 'Bearer ' + token) - return req.expect(200) + return req.expect(HttpStatusCode.OK_200) .expect('Content-Type', /json/) } -function addVideoCommentThread (url: string, token: string, videoId: number | string, text: string, expectedStatus = 200) { +function addVideoCommentThread ( + url: string, + token: string, + videoId: number | string, + text: string, + expectedStatus = HttpStatusCode.OK_200 +) { const path = '/api/v1/videos/' + videoId + '/comment-threads' return request(url) @@ -83,7 +90,7 @@ function addVideoCommentReply ( videoId: number | string, inReplyToCommentId: number, text: string, - expectedStatus = 200 + expectedStatus = HttpStatusCode.OK_200 ) { const path = '/api/v1/videos/' + videoId + '/comments/' + inReplyToCommentId @@ -106,7 +113,7 @@ function deleteVideoComment ( token: string, videoId: number | string, commentId: number, - statusCodeExpected = 204 + statusCodeExpected = HttpStatusCode.NO_CONTENT_204 ) { const path = '/api/v1/videos/' + videoId + '/comments/' + commentId diff --git a/shared/extra-utils/videos/video-history.ts b/shared/extra-utils/videos/video-history.ts index dc7095b4d..2d751cf14 100644 --- a/shared/extra-utils/videos/video-history.ts +++ b/shared/extra-utils/videos/video-history.ts @@ -1,4 +1,5 @@ import { makeGetRequest, makePostBodyRequest, makePutBodyRequest } from '../requests/requests' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' function userWatchVideo (url: string, token: string, videoId: number | string, currentTime: number, statusCodeExpected = 204) { const path = '/api/v1/videos/' + videoId + '/watching' @@ -14,7 +15,7 @@ function listMyVideosHistory (url: string, token: string) { url, path, token, - statusCodeExpected: 200 + statusCodeExpected: HttpStatusCode.OK_200 }) } @@ -26,7 +27,7 @@ function removeMyVideosHistory (url: string, token: string, beforeDate?: string) path, token, fields: beforeDate ? { beforeDate } : {}, - statusCodeExpected: 204 + statusCodeExpected: HttpStatusCode.NO_CONTENT_204 }) } diff --git a/shared/extra-utils/videos/video-imports.ts b/shared/extra-utils/videos/video-imports.ts index 6249e8a94..52e0075fb 100644 --- a/shared/extra-utils/videos/video-imports.ts +++ b/shared/extra-utils/videos/video-imports.ts @@ -1,6 +1,7 @@ import { VideoImportCreate } from '../../models/videos' import { makeGetRequest, makeUploadRequest } from '../requests/requests' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' function getYoutubeVideoUrl () { return 'http://www.youtube.com/watch?v=msX3jv1XdvM' @@ -19,7 +20,12 @@ function getGoodVideoUrl () { return 'https://download.cpy.re/peertube/good_video.mp4' } -function importVideo (url: string, token: string, attributes: VideoImportCreate & { torrentfile?: string }, statusCodeExpected = 200) { +function importVideo ( + url: string, + token: string, + attributes: VideoImportCreate & { torrentfile?: string }, + statusCodeExpected = HttpStatusCode.OK_200 +) { const path = '/api/v1/videos/imports' let attaches: any = {} @@ -46,7 +52,7 @@ function getMyVideoImports (url: string, token: string, sort?: string) { query, path, token, - statusCodeExpected: 200 + statusCodeExpected: HttpStatusCode.OK_200 }) } diff --git a/shared/extra-utils/videos/video-playlists.ts b/shared/extra-utils/videos/video-playlists.ts index 5bcc02570..c6f799e5d 100644 --- a/shared/extra-utils/videos/video-playlists.ts +++ b/shared/extra-utils/videos/video-playlists.ts @@ -10,6 +10,7 @@ import { root } from '..' import { readdir } from 'fs-extra' import { expect } from 'chai' import { VideoPlaylistType } from '../../models/videos/playlist/video-playlist-type.model' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' function getVideoPlaylistsList (url: string, start: number, count: number, sort?: string) { const path = '/api/v1/video-playlists' @@ -24,7 +25,7 @@ function getVideoPlaylistsList (url: string, start: number, count: number, sort? url, path, query, - statusCodeExpected: 200 + statusCodeExpected: HttpStatusCode.OK_200 }) } @@ -41,7 +42,7 @@ function getVideoChannelPlaylistsList (url: string, videoChannelName: string, st url, path, query, - statusCodeExpected: 200 + statusCodeExpected: HttpStatusCode.OK_200 }) } @@ -59,7 +60,7 @@ function getAccountPlaylistsList (url: string, accountName: string, start: numbe url, path, query, - statusCodeExpected: 200 + statusCodeExpected: HttpStatusCode.OK_200 }) } @@ -86,11 +87,11 @@ function getAccountPlaylistsListWithToken ( token, path, query, - statusCodeExpected: 200 + statusCodeExpected: HttpStatusCode.OK_200 }) } -function getVideoPlaylist (url: string, playlistId: number | string, statusCodeExpected = 200) { +function getVideoPlaylist (url: string, playlistId: number | string, statusCodeExpected = HttpStatusCode.OK_200) { const path = '/api/v1/video-playlists/' + playlistId return makeGetRequest({ @@ -100,7 +101,7 @@ function getVideoPlaylist (url: string, playlistId: number | string, statusCodeE }) } -function getVideoPlaylistWithToken (url: string, token: string, playlistId: number | string, statusCodeExpected = 200) { +function getVideoPlaylistWithToken (url: string, token: string, playlistId: number | string, statusCodeExpected = HttpStatusCode.OK_200) { const path = '/api/v1/video-playlists/' + playlistId return makeGetRequest({ @@ -111,7 +112,7 @@ function getVideoPlaylistWithToken (url: string, token: string, playlistId: numb }) } -function deleteVideoPlaylist (url: string, token: string, playlistId: number | string, statusCodeExpected = 204) { +function deleteVideoPlaylist (url: string, token: string, playlistId: number | string, statusCodeExpected = HttpStatusCode.NO_CONTENT_204) { const path = '/api/v1/video-playlists/' + playlistId return makeDeleteRequest({ @@ -143,7 +144,7 @@ function createVideoPlaylist (options: { token: options.token, fields, attaches, - statusCodeExpected: options.expectedStatus || 200 + statusCodeExpected: options.expectedStatus || HttpStatusCode.OK_200 }) } @@ -169,7 +170,7 @@ function updateVideoPlaylist (options: { token: options.token, fields, attaches, - statusCodeExpected: options.expectedStatus || 204 + statusCodeExpected: options.expectedStatus || HttpStatusCode.NO_CONTENT_204 }) } @@ -189,7 +190,7 @@ async function addVideoInPlaylist (options: { path, token: options.token, fields: options.elementAttrs, - statusCodeExpected: options.expectedStatus || 200 + statusCodeExpected: options.expectedStatus || HttpStatusCode.OK_200 }) } @@ -208,7 +209,7 @@ function updateVideoPlaylistElement (options: { path, token: options.token, fields: options.elementAttrs, - statusCodeExpected: options.expectedStatus || 204 + statusCodeExpected: options.expectedStatus || HttpStatusCode.NO_CONTENT_204 }) } @@ -225,7 +226,7 @@ function removeVideoFromPlaylist (options: { url: options.url, path, token: options.token, - statusCodeExpected: options.expectedStatus || 204 + statusCodeExpected: options.expectedStatus || HttpStatusCode.NO_CONTENT_204 }) } @@ -247,7 +248,7 @@ function reorderVideosPlaylist (options: { path, token: options.token, fields: options.elementAttrs, - statusCodeExpected: options.expectedStatus || 204 + statusCodeExpected: options.expectedStatus || HttpStatusCode.NO_CONTENT_204 }) } @@ -274,7 +275,7 @@ function getVideoPlaylistPrivacies (url: string) { return makeGetRequest({ url, path, - statusCodeExpected: 200 + statusCodeExpected: HttpStatusCode.OK_200 }) } @@ -286,7 +287,7 @@ function doVideosExistInMyPlaylist (url: string, token: string, videoIds: number token, path, query: { videoIds }, - statusCodeExpected: 200 + statusCodeExpected: HttpStatusCode.OK_200 }) } diff --git a/shared/extra-utils/videos/video-streaming-playlists.ts b/shared/extra-utils/videos/video-streaming-playlists.ts index b386e77c3..99c2e1880 100644 --- a/shared/extra-utils/videos/video-streaming-playlists.ts +++ b/shared/extra-utils/videos/video-streaming-playlists.ts @@ -2,16 +2,17 @@ import { makeRawRequest } from '../requests/requests' import { sha256 } from '../../../server/helpers/core-utils' import { VideoStreamingPlaylist } from '../../models/videos/video-streaming-playlist.model' import { expect } from 'chai' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' -function getPlaylist (url: string, statusCodeExpected = 200) { +function getPlaylist (url: string, statusCodeExpected = HttpStatusCode.OK_200) { return makeRawRequest(url, statusCodeExpected) } -function getSegment (url: string, statusCodeExpected = 200, range?: string) { +function getSegment (url: string, statusCodeExpected = HttpStatusCode.OK_200, range?: string) { return makeRawRequest(url, statusCodeExpected, range) } -function getSegmentSha256 (url: string, statusCodeExpected = 200) { +function getSegmentSha256 (url: string, statusCodeExpected = HttpStatusCode.OK_200) { return makeRawRequest(url, statusCodeExpected) } @@ -33,7 +34,7 @@ async function checkSegmentHash ( const offset = parseInt(matches[2], 10) const range = `${offset}-${offset + length - 1}` - const res2 = await getSegment(`${baseUrlSegment}/${videoUUID}/${videoName}`, 206, `bytes=${range}`) + const res2 = await getSegment(`${baseUrlSegment}/${videoUUID}/${videoName}`, HttpStatusCode.PARTIAL_CONTENT_206, `bytes=${range}`) const resSha = await getSegmentSha256(hlsPlaylist.segmentsSha256Url) diff --git a/shared/extra-utils/videos/videos.ts b/shared/extra-utils/videos/videos.ts index 2b8c55acb..a4b9d688e 100644 --- a/shared/extra-utils/videos/videos.ts +++ b/shared/extra-utils/videos/videos.ts @@ -1,5 +1,6 @@ /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */ +import { HttpStatusCode } from '@shared/core-utils' import { expect } from 'chai' import { pathExists, readdir, readFile } from 'fs-extra' import * as parseTorrent from 'parse-torrent' @@ -46,7 +47,7 @@ function getVideoCategories (url: string) { return makeGetRequest({ url, path, - statusCodeExpected: 200 + statusCodeExpected: HttpStatusCode.OK_200 }) } @@ -56,7 +57,7 @@ function getVideoLicences (url: string) { return makeGetRequest({ url, path, - statusCodeExpected: 200 + statusCodeExpected: HttpStatusCode.OK_200 }) } @@ -66,7 +67,7 @@ function getVideoLanguages (url: string) { return makeGetRequest({ url, path, - statusCodeExpected: 200 + statusCodeExpected: HttpStatusCode.OK_200 }) } @@ -76,11 +77,11 @@ function getVideoPrivacies (url: string) { return makeGetRequest({ url, path, - statusCodeExpected: 200 + statusCodeExpected: HttpStatusCode.OK_200 }) } -function getVideo (url: string, id: number | string, expectedStatus = 200) { +function getVideo (url: string, id: number | string, expectedStatus = HttpStatusCode.OK_200) { const path = '/api/v1/videos/' + id return request(url) @@ -99,11 +100,11 @@ function getVideoFileMetadataUrl (url: string) { return request(url) .get('/') .set('Accept', 'application/json') - .expect(200) + .expect(HttpStatusCode.OK_200) .expect('Content-Type', /json/) } -function viewVideo (url: string, id: number | string, expectedStatus = 204, xForwardedFor?: string) { +function viewVideo (url: string, id: number | string, expectedStatus = HttpStatusCode.NO_CONTENT_204, xForwardedFor?: string) { const path = '/api/v1/videos/' + id + '/views' const req = request(url) @@ -117,7 +118,7 @@ function viewVideo (url: string, id: number | string, expectedStatus = 204, xFor return req.expect(expectedStatus) } -function getVideoWithToken (url: string, token: string, id: number | string, expectedStatus = 200) { +function getVideoWithToken (url: string, token: string, id: number | string, expectedStatus = HttpStatusCode.OK_200) { const path = '/api/v1/videos/' + id return request(url) @@ -131,7 +132,7 @@ function getVideoDescription (url: string, descriptionPath: string) { return request(url) .get(descriptionPath) .set('Accept', 'application/json') - .expect(200) + .expect(HttpStatusCode.OK_200) .expect('Content-Type', /json/) } @@ -142,7 +143,7 @@ function getVideosList (url: string) { .get(path) .query({ sort: 'name' }) .set('Accept', 'application/json') - .expect(200) + .expect(HttpStatusCode.OK_200) .expect('Content-Type', /json/) } @@ -182,7 +183,7 @@ function getMyVideos (url: string, accessToken: string, start: number, count: nu return req.set('Accept', 'application/json') .set('Authorization', 'Bearer ' + accessToken) - .expect(200) + .expect(HttpStatusCode.OK_200) .expect('Content-Type', /json/) } @@ -206,7 +207,7 @@ function getAccountVideos ( sort }), token: accessToken, - statusCodeExpected: 200 + statusCodeExpected: HttpStatusCode.OK_200 }) } @@ -230,7 +231,7 @@ function getVideoChannelVideos ( sort }), token: accessToken, - statusCodeExpected: 200 + statusCodeExpected: HttpStatusCode.OK_200 }) } @@ -252,7 +253,7 @@ function getPlaylistVideos ( count }), token: accessToken, - statusCodeExpected: 200 + statusCodeExpected: HttpStatusCode.OK_200 }) } @@ -268,7 +269,7 @@ function getVideosListPagination (url: string, start: number, count: number, sor if (skipCount) req.query({ skipCount }) return req.set('Accept', 'application/json') - .expect(200) + .expect(HttpStatusCode.OK_200) .expect('Content-Type', /json/) } @@ -279,7 +280,7 @@ function getVideosListSort (url: string, sort: string) { .get(path) .query({ sort: sort }) .set('Accept', 'application/json') - .expect(200) + .expect(HttpStatusCode.OK_200) .expect('Content-Type', /json/) } @@ -290,11 +291,11 @@ function getVideosWithFilters (url: string, query: { tagsAllOf: string[], catego .get(path) .query(query) .set('Accept', 'application/json') - .expect(200) + .expect(HttpStatusCode.OK_200) .expect('Content-Type', /json/) } -function removeVideo (url: string, token: string, id: number | string, expectedStatus = 204) { +function removeVideo (url: string, token: string, id: number | string, expectedStatus = HttpStatusCode.NO_CONTENT_204) { const path = '/api/v1/videos' return request(url) @@ -339,7 +340,7 @@ async function checkVideoFilesWereRemoved ( } } -async function uploadVideo (url: string, accessToken: string, videoAttributesArg: VideoAttributes, specialStatus = 200) { +async function uploadVideo (url: string, accessToken: string, videoAttributesArg: VideoAttributes, specialStatus = HttpStatusCode.OK_200) { const path = '/api/v1/videos/upload' let defaultChannelId = '1' @@ -423,7 +424,13 @@ async function uploadVideo (url: string, accessToken: string, videoAttributesArg .expect(specialStatus) } -function updateVideo (url: string, accessToken: string, id: number | string, attributes: VideoAttributes, statusCodeExpected = 204) { +function updateVideo ( + url: string, + accessToken: string, + id: number | string, + attributes: VideoAttributes, + statusCodeExpected = HttpStatusCode.NO_CONTENT_204 +) { const path = '/api/v1/videos/' + id const body = {} @@ -467,7 +474,7 @@ function updateVideo (url: string, accessToken: string, id: number | string, att }) } -function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = 204) { +function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = HttpStatusCode.NO_CONTENT_204) { const path = '/api/v1/videos/' + id + '/rate' return request(url) -- cgit v1.2.3