Introduce CustomPage command

pull/4271/head
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 * 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, '<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.empty
const state = await getHomepageState(server)

View File

@ -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
}

View File

@ -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
}

View File

@ -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'
return makePutBodyRequest({
url,
path,
token,
fields: { content },
statusCodeExpected: HttpStatusCode.NO_CONTENT_204
})
}
// ---------------------------------------------------------------------------
export {
getInstanceHomepage,
updateInstanceHomepage
getInstanceHomepage (options: OverrideCommandOptions = {}) {
const path = '/api/v1/custom-pages/homepage/instance'
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'
return this.putBodyRequest({
...options,
path,
fields: { content },
defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
})
}
}

View File

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

View File

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

View File

@ -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
}

View File

@ -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)
})

View File

@ -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 postBodyRequest (options: CommonCommandOptions & {
path: string
defaultExpectedStatus: number
protected getRequestBody <T> (options: CommonCommandOptions) {
return unwrap<T>(makeGetRequest(this.buildCommonRequestOptions(options)))
}
protected putBodyRequest (options: CommonCommandOptions & {
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({
...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
})
}
}
}