From e8bd7ce7ccafe3e064b03978e9b512c1a4cc99e6 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 6 Jul 2021 09:55:05 +0200 Subject: [PATCH] Introduce CustomPage command --- server/tests/api/server/homepage.ts | 25 +++++----- shared/extra-utils/bulk/bulk.ts | 10 ++-- shared/extra-utils/cli/cli.ts | 6 +-- .../extra-utils/custom-pages/custom-pages.ts | 47 +++++++++---------- shared/extra-utils/custom-pages/index.ts | 1 + shared/extra-utils/index.ts | 2 +- shared/extra-utils/requests/requests.ts | 5 ++ shared/extra-utils/server/servers.ts | 3 ++ shared/extra-utils/shared/abstract-command.ts | 44 +++++++++++++---- 9 files changed, 85 insertions(+), 58 deletions(-) create mode 100644 shared/extra-utils/custom-pages/index.ts diff --git a/server/tests/api/server/homepage.ts b/server/tests/api/server/homepage.ts index e8ba89ca6..4a3ec0479 100644 --- a/server/tests/api/server/homepage.ts +++ b/server/tests/api/server/homepage.ts @@ -3,17 +3,16 @@ import 'mocha' import * as chai from 'chai' import { HttpStatusCode } from '@shared/core-utils' -import { CustomPage, ServerConfig } from '@shared/models' +import { ServerConfig } from '@shared/models' import { cleanupTests, + CustomPagesCommand, flushAndRunServer, getConfig, - getInstanceHomepage, killallServers, reRunServer, ServerInfo, - setAccessTokensToServers, - updateInstanceHomepage + setAccessTokensToServers } from '../../../../shared/extra-utils/index' const expect = chai.expect @@ -27,26 +26,28 @@ async function getHomepageState (server: ServerInfo) { describe('Test instance homepage actions', function () { let server: ServerInfo + let command: CustomPagesCommand before(async function () { this.timeout(30000) server = await flushAndRunServer(1) await setAccessTokensToServers([ server ]) + + command = server.customPageCommand }) it('Should not have a homepage', async function () { const state = await getHomepageState(server) expect(state).to.be.false - await getInstanceHomepage(server.url, HttpStatusCode.NOT_FOUND_404) + await command.getInstanceHomepage({ expectedStatus: HttpStatusCode.NOT_FOUND_404 }) }) it('Should set a homepage', async function () { - await updateInstanceHomepage(server.url, server.accessToken, '') + await command.updateInstanceHomepage({ content: '' }) - const res = await getInstanceHomepage(server.url) - const page: CustomPage = res.body + const page = await command.getInstanceHomepage() expect(page.content).to.equal('') const state = await getHomepageState(server) @@ -60,8 +61,7 @@ describe('Test instance homepage actions', function () { await reRunServer(server) - const res = await getInstanceHomepage(server.url) - const page: CustomPage = res.body + const page = await command.getInstanceHomepage() expect(page.content).to.equal('') const state = await getHomepageState(server) @@ -69,10 +69,9 @@ describe('Test instance homepage actions', function () { }) it('Should empty the homepage', async function () { - await updateInstanceHomepage(server.url, server.accessToken, '') + await command.updateInstanceHomepage({ content: '' }) - const res = await getInstanceHomepage(server.url) - const page: CustomPage = res.body + const page = await command.getInstanceHomepage() expect(page.content).to.be.empty const state = await getHomepageState(server) diff --git a/shared/extra-utils/bulk/bulk.ts b/shared/extra-utils/bulk/bulk.ts index c102383e3..a58fb3fbb 100644 --- a/shared/extra-utils/bulk/bulk.ts +++ b/shared/extra-utils/bulk/bulk.ts @@ -1,11 +1,11 @@ import { BulkRemoveCommentsOfBody } from '@shared/models/bulk/bulk-remove-comments-of-body.model' import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' -import { AbstractCommand, CommonCommandOptions } from '../shared' +import { AbstractCommand, OverrideCommandOptions } from '../shared' -class BulkCommand extends AbstractCommand { +export class BulkCommand extends AbstractCommand { - removeCommentsOf (options: CommonCommandOptions & { + removeCommentsOf (options: OverrideCommandOptions & { attributes: BulkRemoveCommentsOfBody }) { const { attributes } = options @@ -18,7 +18,3 @@ class BulkCommand extends AbstractCommand { }) } } - -export { - BulkCommand -} diff --git a/shared/extra-utils/cli/cli.ts b/shared/extra-utils/cli/cli.ts index 1bf100869..bc1dddc68 100644 --- a/shared/extra-utils/cli/cli.ts +++ b/shared/extra-utils/cli/cli.ts @@ -1,7 +1,7 @@ import { exec } from 'child_process' import { AbstractCommand } from '../shared' -class CLICommand extends AbstractCommand { +export class CLICommand extends AbstractCommand { static exec (command: string) { return new Promise((res, rej) => { @@ -21,7 +21,3 @@ class CLICommand extends AbstractCommand { return CLICommand.exec(`${this.getEnv()} ${command}`) } } - -export { - CLICommand -} diff --git a/shared/extra-utils/custom-pages/custom-pages.ts b/shared/extra-utils/custom-pages/custom-pages.ts index bf2d16c70..56dabdc0f 100644 --- a/shared/extra-utils/custom-pages/custom-pages.ts +++ b/shared/extra-utils/custom-pages/custom-pages.ts @@ -1,31 +1,30 @@ +import { CustomPage } from '@shared/models' import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' -import { makeGetRequest, makePutBodyRequest } from '../requests/requests' +import { AbstractCommand, OverrideCommandOptions } from '../shared' -function getInstanceHomepage (url: string, statusCodeExpected = HttpStatusCode.OK_200) { - const path = '/api/v1/custom-pages/homepage/instance' +export class CustomPagesCommand extends AbstractCommand { - return makeGetRequest({ - url, - path, - statusCodeExpected - }) -} - -function updateInstanceHomepage (url: string, token: string, content: string) { - const path = '/api/v1/custom-pages/homepage/instance' + getInstanceHomepage (options: OverrideCommandOptions = {}) { + const path = '/api/v1/custom-pages/homepage/instance' - return makePutBodyRequest({ - url, - path, - token, - fields: { content }, - statusCodeExpected: HttpStatusCode.NO_CONTENT_204 - }) -} + return this.getRequestBody({ + ...options, + path, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } -// --------------------------------------------------------------------------- + updateInstanceHomepage (options: OverrideCommandOptions & { + content: string + }) { + const { content } = options + const path = '/api/v1/custom-pages/homepage/instance' -export { - getInstanceHomepage, - updateInstanceHomepage + return this.putBodyRequest({ + ...options, + path, + fields: { content }, + defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 + }) + } } diff --git a/shared/extra-utils/custom-pages/index.ts b/shared/extra-utils/custom-pages/index.ts new file mode 100644 index 000000000..5e70778f8 --- /dev/null +++ b/shared/extra-utils/custom-pages/index.ts @@ -0,0 +1 @@ +export * from './custom-pages' diff --git a/shared/extra-utils/index.ts b/shared/extra-utils/index.ts index 4bf057492..10781d049 100644 --- a/shared/extra-utils/index.ts +++ b/shared/extra-utils/index.ts @@ -2,7 +2,7 @@ export * from './bulk' export * from './cli' -export * from './custom-pages/custom-pages' +export * from './custom-pages' export * from './feeds/feeds' diff --git a/shared/extra-utils/requests/requests.ts b/shared/extra-utils/requests/requests.ts index 38e24d897..eb59ca600 100644 --- a/shared/extra-utils/requests/requests.ts +++ b/shared/extra-utils/requests/requests.ts @@ -182,6 +182,10 @@ function decodeQueryString (path: string) { return decode(path.split('?')[1]) } +function unwrap (test: request.Test): Promise { + return test.then(res => res.body) +} + // --------------------------------------------------------------------------- export { @@ -194,5 +198,6 @@ export { makePutBodyRequest, makeDeleteRequest, makeRawRequest, + unwrap, updateImageRequest } diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts index 1b0775421..e07926065 100644 --- a/shared/extra-utils/server/servers.ts +++ b/shared/extra-utils/server/servers.ts @@ -8,6 +8,7 @@ import { randomInt } from '../../core-utils/miscs/miscs' import { VideoChannel } from '../../models/videos' import { BulkCommand } from '../bulk' import { CLICommand } from '../cli' +import { CustomPagesCommand } from '../custom-pages' import { buildServerDirectory, getFileSize, isGithubCI, root, wait } from '../miscs/miscs' import { makeGetRequest } from '../requests/requests' @@ -65,6 +66,7 @@ interface ServerInfo { bulkCommand?: BulkCommand cliCommand?: CLICommand + customPageCommand?: CustomPagesCommand } function parallelTests () { @@ -272,6 +274,7 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = [] server.bulkCommand = new BulkCommand(server) server.cliCommand = new CLICommand(server) + server.customPageCommand = new CustomPagesCommand(server) res(server) }) diff --git a/shared/extra-utils/shared/abstract-command.ts b/shared/extra-utils/shared/abstract-command.ts index bb06b6bdb..d8eba373d 100644 --- a/shared/extra-utils/shared/abstract-command.ts +++ b/shared/extra-utils/shared/abstract-command.ts @@ -1,15 +1,20 @@ import { HttpStatusCode } from '@shared/core-utils' -import { makePostBodyRequest } from '../requests/requests' +import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, unwrap } from '../requests/requests' import { ServerInfo } from '../server/servers' -export interface CommonCommandOptions { +export interface OverrideCommandOptions { token?: string expectedStatus?: number } +interface CommonCommandOptions extends OverrideCommandOptions { + path: string + defaultExpectedStatus: number +} + abstract class AbstractCommand { - private expectedStatus = HttpStatusCode.OK_200 + private expectedStatus: HttpStatusCode constructor ( protected server: ServerInfo @@ -25,20 +30,43 @@ abstract class AbstractCommand { this.expectedStatus = status } + protected getRequestBody (options: CommonCommandOptions) { + return unwrap(makeGetRequest(this.buildCommonRequestOptions(options))) + } + + protected putBodyRequest (options: CommonCommandOptions & { + fields?: { [ fieldName: string ]: any } + }) { + const { fields } = options + + return makePutBodyRequest({ + ...this.buildCommonRequestOptions(options), + + fields + }) + } + protected postBodyRequest (options: CommonCommandOptions & { - path: string - defaultExpectedStatus: number fields?: { [ fieldName: string ]: any } }) { - const { token, fields, expectedStatus, defaultExpectedStatus, path } = options + const { fields } = options return makePostBodyRequest({ + ...this.buildCommonRequestOptions(options), + + fields + }) + } + + private buildCommonRequestOptions (options: CommonCommandOptions) { + const { token, expectedStatus, defaultExpectedStatus, path } = options + + return { url: this.server.url, path, token: token ?? this.server.accessToken, - fields, statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus - }) + } } } -- 2.41.0