ActivityPub-federated video streaming platform using P2P directly in your web browser
Introduce CustomPage command
Chocobozzze8bd7ce7ccaf
diff --git a/server/tests/api/server/homepage.ts b/server/tests/api/server/homepage.tsindex e8ba89ca67..4a3ec0479d 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: CustomPagesCommandbefore(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, '<picsou-magazine></picsou-magazine>')+ await command.updateInstanceHomepage({ content: '<picsou-magazine></picsou-magazine>' })- const res = await getInstanceHomepage(server.url)- const page: CustomPage = res.body+ const page = await command.getInstanceHomepage()expect(page.content).to.equal('<picsou-magazine></picsou-magazine>')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('<picsou-magazine></picsou-magazine>')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.emptyconst state = await getHomepageState(server)diff --git a/shared/extra-utils/bulk/bulk.ts b/shared/extra-utils/bulk/bulk.tsindex c102383e33..a58fb3fbba 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.tsindex 1bf1008690..bc1dddc686 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<string>((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.tsindex bf2d16c70b..56dabdc0f3 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<CustomPage>({+ ...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.tsnew file mode 100644index 0000000000..5e70778f89--- /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.tsindex 4bf057492e..10781d0493 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.tsindex 38e24d8971..eb59ca600f 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 <T> (test: request.Test): Promise<T> {+ 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.tsindex 1b07754214..e079260650 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?: BulkCommandcliCommand?: 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.tsindex bb06b6bdba..d8eba373d7 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?: stringexpectedStatus?: number}+interface CommonCommandOptions extends OverrideCommandOptions {+ path: string+ defaultExpectedStatus: number+}+abstract class AbstractCommand {- private expectedStatus = HttpStatusCode.OK_200+ private expectedStatus: HttpStatusCodeconstructor (protected server: ServerInfo@@ -25,20 +30,43 @@ abstract class AbstractCommand {this.expectedStatus = status}+ protected getRequestBody <T> (options: CommonCommandOptions) {+ return unwrap<T>(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: numberfields?: { [ fieldName: string ]: any }}) {- const { token, fields, expectedStatus, defaultExpectedStatus, path } = options+ const { fields } = optionsreturn 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- })+ }}}