Introduce CustomPage command

This commit is contained in:
Chocobozzz 2021-07-06 09:55:05 +02:00
parent 329619b345
commit e8bd7ce7cc
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
9 changed files with 89 additions and 62 deletions

View File

@ -3,17 +3,16 @@
import 'mocha' import 'mocha'
import * as chai from 'chai' import * as chai from 'chai'
import { HttpStatusCode } from '@shared/core-utils' import { HttpStatusCode } from '@shared/core-utils'
import { CustomPage, ServerConfig } from '@shared/models' import { ServerConfig } from '@shared/models'
import { import {
cleanupTests, cleanupTests,
CustomPagesCommand,
flushAndRunServer, flushAndRunServer,
getConfig, getConfig,
getInstanceHomepage,
killallServers, killallServers,
reRunServer, reRunServer,
ServerInfo, ServerInfo,
setAccessTokensToServers, setAccessTokensToServers
updateInstanceHomepage
} from '../../../../shared/extra-utils/index' } from '../../../../shared/extra-utils/index'
const expect = chai.expect const expect = chai.expect
@ -27,26 +26,28 @@ async function getHomepageState (server: ServerInfo) {
describe('Test instance homepage actions', function () { describe('Test instance homepage actions', function () {
let server: ServerInfo let server: ServerInfo
let command: CustomPagesCommand
before(async function () { before(async function () {
this.timeout(30000) this.timeout(30000)
server = await flushAndRunServer(1) server = await flushAndRunServer(1)
await setAccessTokensToServers([ server ]) await setAccessTokensToServers([ server ])
command = server.customPageCommand
}) })
it('Should not have a homepage', async function () { it('Should not have a homepage', async function () {
const state = await getHomepageState(server) const state = await getHomepageState(server)
expect(state).to.be.false 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 () { 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 = await command.getInstanceHomepage()
const page: CustomPage = res.body
expect(page.content).to.equal('<picsou-magazine></picsou-magazine>') expect(page.content).to.equal('<picsou-magazine></picsou-magazine>')
const state = await getHomepageState(server) const state = await getHomepageState(server)
@ -60,8 +61,7 @@ describe('Test instance homepage actions', function () {
await reRunServer(server) await reRunServer(server)
const res = await getInstanceHomepage(server.url) const page = await command.getInstanceHomepage()
const page: CustomPage = res.body
expect(page.content).to.equal('<picsou-magazine></picsou-magazine>') expect(page.content).to.equal('<picsou-magazine></picsou-magazine>')
const state = await getHomepageState(server) const state = await getHomepageState(server)
@ -69,10 +69,9 @@ describe('Test instance homepage actions', function () {
}) })
it('Should empty the homepage', async 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 = await command.getInstanceHomepage()
const page: CustomPage = res.body
expect(page.content).to.be.empty expect(page.content).to.be.empty
const state = await getHomepageState(server) const state = await getHomepageState(server)

View File

@ -1,11 +1,11 @@
import { BulkRemoveCommentsOfBody } from '@shared/models/bulk/bulk-remove-comments-of-body.model' import { BulkRemoveCommentsOfBody } from '@shared/models/bulk/bulk-remove-comments-of-body.model'
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' 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 attributes: BulkRemoveCommentsOfBody
}) { }) {
const { attributes } = options const { attributes } = options
@ -18,7 +18,3 @@ class BulkCommand extends AbstractCommand {
}) })
} }
} }
export {
BulkCommand
}

View File

@ -1,7 +1,7 @@
import { exec } from 'child_process' import { exec } from 'child_process'
import { AbstractCommand } from '../shared' import { AbstractCommand } from '../shared'
class CLICommand extends AbstractCommand { export class CLICommand extends AbstractCommand {
static exec (command: string) { static exec (command: string) {
return new Promise<string>((res, rej) => { return new Promise<string>((res, rej) => {
@ -21,7 +21,3 @@ class CLICommand extends AbstractCommand {
return CLICommand.exec(`${this.getEnv()} ${command}`) return CLICommand.exec(`${this.getEnv()} ${command}`)
} }
} }
export {
CLICommand
}

View File

@ -1,31 +1,30 @@
import { CustomPage } from '@shared/models'
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' 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) { export class CustomPagesCommand extends AbstractCommand {
const path = '/api/v1/custom-pages/homepage/instance'
return makeGetRequest({ getInstanceHomepage (options: OverrideCommandOptions = {}) {
url, const path = '/api/v1/custom-pages/homepage/instance'
path,
statusCodeExpected return this.getRequestBody<CustomPage>({
}) ...options,
} path,
defaultExpectedStatus: HttpStatusCode.OK_200
function updateInstanceHomepage (url: string, token: string, content: string) { })
const path = '/api/v1/custom-pages/homepage/instance' }
return makePutBodyRequest({ updateInstanceHomepage (options: OverrideCommandOptions & {
url, content: string
path, }) {
token, const { content } = options
fields: { content }, const path = '/api/v1/custom-pages/homepage/instance'
statusCodeExpected: HttpStatusCode.NO_CONTENT_204
}) return this.putBodyRequest({
} ...options,
path,
// --------------------------------------------------------------------------- fields: { content },
defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
export { })
getInstanceHomepage, }
updateInstanceHomepage
} }

View File

@ -0,0 +1 @@
export * from './custom-pages'

View File

@ -2,7 +2,7 @@ export * from './bulk'
export * from './cli' export * from './cli'
export * from './custom-pages/custom-pages' export * from './custom-pages'
export * from './feeds/feeds' export * from './feeds/feeds'

View File

@ -182,6 +182,10 @@ function decodeQueryString (path: string) {
return decode(path.split('?')[1]) return decode(path.split('?')[1])
} }
function unwrap <T> (test: request.Test): Promise<T> {
return test.then(res => res.body)
}
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
export { export {
@ -194,5 +198,6 @@ export {
makePutBodyRequest, makePutBodyRequest,
makeDeleteRequest, makeDeleteRequest,
makeRawRequest, makeRawRequest,
unwrap,
updateImageRequest updateImageRequest
} }

View File

@ -8,6 +8,7 @@ import { randomInt } from '../../core-utils/miscs/miscs'
import { VideoChannel } from '../../models/videos' import { VideoChannel } from '../../models/videos'
import { BulkCommand } from '../bulk' import { BulkCommand } from '../bulk'
import { CLICommand } from '../cli' import { CLICommand } from '../cli'
import { CustomPagesCommand } from '../custom-pages'
import { buildServerDirectory, getFileSize, isGithubCI, root, wait } from '../miscs/miscs' import { buildServerDirectory, getFileSize, isGithubCI, root, wait } from '../miscs/miscs'
import { makeGetRequest } from '../requests/requests' import { makeGetRequest } from '../requests/requests'
@ -65,6 +66,7 @@ interface ServerInfo {
bulkCommand?: BulkCommand bulkCommand?: BulkCommand
cliCommand?: CLICommand cliCommand?: CLICommand
customPageCommand?: CustomPagesCommand
} }
function parallelTests () { function parallelTests () {
@ -272,6 +274,7 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = []
server.bulkCommand = new BulkCommand(server) server.bulkCommand = new BulkCommand(server)
server.cliCommand = new CLICommand(server) server.cliCommand = new CLICommand(server)
server.customPageCommand = new CustomPagesCommand(server)
res(server) res(server)
}) })

View File

@ -1,15 +1,20 @@
import { HttpStatusCode } from '@shared/core-utils' import { HttpStatusCode } from '@shared/core-utils'
import { makePostBodyRequest } from '../requests/requests' import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, unwrap } from '../requests/requests'
import { ServerInfo } from '../server/servers' import { ServerInfo } from '../server/servers'
export interface CommonCommandOptions { export interface OverrideCommandOptions {
token?: string token?: string
expectedStatus?: number expectedStatus?: number
} }
interface CommonCommandOptions extends OverrideCommandOptions {
path: string
defaultExpectedStatus: number
}
abstract class AbstractCommand { abstract class AbstractCommand {
private expectedStatus = HttpStatusCode.OK_200 private expectedStatus: HttpStatusCode
constructor ( constructor (
protected server: ServerInfo protected server: ServerInfo
@ -25,20 +30,43 @@ abstract class AbstractCommand {
this.expectedStatus = status this.expectedStatus = status
} }
protected postBodyRequest (options: CommonCommandOptions & { protected getRequestBody <T> (options: CommonCommandOptions) {
path: string return unwrap<T>(makeGetRequest(this.buildCommonRequestOptions(options)))
defaultExpectedStatus: number }
protected putBodyRequest (options: CommonCommandOptions & {
fields?: { [ fieldName: string ]: any } fields?: { [ fieldName: string ]: any }
}) { }) {
const { token, fields, expectedStatus, defaultExpectedStatus, path } = options const { fields } = options
return makePutBodyRequest({
...this.buildCommonRequestOptions(options),
fields
})
}
protected postBodyRequest (options: CommonCommandOptions & {
fields?: { [ fieldName: string ]: any }
}) {
const { fields } = options
return makePostBodyRequest({ return makePostBodyRequest({
...this.buildCommonRequestOptions(options),
fields
})
}
private buildCommonRequestOptions (options: CommonCommandOptions) {
const { token, expectedStatus, defaultExpectedStatus, path } = options
return {
url: this.server.url, url: this.server.url,
path, path,
token: token ?? this.server.accessToken, token: token ?? this.server.accessToken,
fields,
statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus
}) }
} }
} }