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/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 +- 8 files changed, 62 insertions(+), 45 deletions(-) (limited to 'shared/extra-utils/server') 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 }) } -- cgit v1.2.3