]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Introduce CustomPage command
authorChocobozzz <me@florianbigard.com>
Tue, 6 Jul 2021 07:55:05 +0000 (09:55 +0200)
committerChocobozzz <me@florianbigard.com>
Tue, 20 Jul 2021 13:27:16 +0000 (15:27 +0200)
server/tests/api/server/homepage.ts
shared/extra-utils/bulk/bulk.ts
shared/extra-utils/cli/cli.ts
shared/extra-utils/custom-pages/custom-pages.ts
shared/extra-utils/custom-pages/index.ts [new file with mode: 0644]
shared/extra-utils/index.ts
shared/extra-utils/requests/requests.ts
shared/extra-utils/server/servers.ts
shared/extra-utils/shared/abstract-command.ts

index e8ba89ca67612eafd69580cbf1fff4060b33f055..4a3ec0479d386f3d054c72cf898297a962ebd1f8 100644 (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)
index c102383e33605caf59b1b0fa8b8948b8cd678983..a58fb3fbba7781d9b88cebbc12006b209e6e4606 100644 (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
-}
index 1bf100869017c19b9bf8cc398f622d8f425603ed..bc1dddc686120e051fdf86c01589655f29323386 100644 (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
-}
index bf2d16c70b0a19f8a3bbaed35f96ff99c0d4fe2d..56dabdc0f30cf08f1c7b114d38fb8d93553f9325 100644 (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'
+  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.ts
new file mode 100644 (file)
index 0000000..5e70778
--- /dev/null
@@ -0,0 +1 @@
+export * from './custom-pages'
index 4bf057492ec4b53b2492fc995497794636b7366d..10781d04930667bea3e7ef071a9c139b9b8be941 100644 (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'
 
index 38e24d89716967b8c9fc04da1177a11d603885ad..eb59ca600f8f7e1f8f282fccd17d4da0ec410d46 100644 (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
 }
index 1b07754214c068a3d47a0288c33e5810b3cb5a94..e079260650fc85f5fadc9e61c8dcae1be30ccc0a 100644 (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)
     })
index bb06b6bdba43f2fec26900d0eabc7d6653037f29..d8eba373d7afb2f5ba1a2e2fdcc4935261452b74 100644 (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 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: 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
-    })
+    }
   }
 }