diff options
author | Chocobozzz <me@florianbigard.com> | 2021-07-06 09:55:05 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-07-20 15:27:16 +0200 |
commit | e8bd7ce7ccafe3e064b03978e9b512c1a4cc99e6 (patch) | |
tree | c888e13aca3fc239f46d44045df6bf8e2a2ef0d3 | |
parent | 329619b3453479f76c049816b7403b86e9d45cb5 (diff) | |
download | PeerTube-e8bd7ce7ccafe3e064b03978e9b512c1a4cc99e6.tar.gz PeerTube-e8bd7ce7ccafe3e064b03978e9b512c1a4cc99e6.tar.zst PeerTube-e8bd7ce7ccafe3e064b03978e9b512c1a4cc99e6.zip |
Introduce CustomPage command
-rw-r--r-- | server/tests/api/server/homepage.ts | 25 | ||||
-rw-r--r-- | shared/extra-utils/bulk/bulk.ts | 10 | ||||
-rw-r--r-- | shared/extra-utils/cli/cli.ts | 6 | ||||
-rw-r--r-- | shared/extra-utils/custom-pages/custom-pages.ts | 47 | ||||
-rw-r--r-- | shared/extra-utils/custom-pages/index.ts | 1 | ||||
-rw-r--r-- | shared/extra-utils/index.ts | 2 | ||||
-rw-r--r-- | shared/extra-utils/requests/requests.ts | 5 | ||||
-rw-r--r-- | shared/extra-utils/server/servers.ts | 3 | ||||
-rw-r--r-- | shared/extra-utils/shared/abstract-command.ts | 44 |
9 files changed, 85 insertions, 58 deletions
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 @@ | |||
3 | import 'mocha' | 3 | import 'mocha' |
4 | import * as chai from 'chai' | 4 | import * as chai from 'chai' |
5 | import { HttpStatusCode } from '@shared/core-utils' | 5 | import { HttpStatusCode } from '@shared/core-utils' |
6 | import { CustomPage, ServerConfig } from '@shared/models' | 6 | import { ServerConfig } from '@shared/models' |
7 | import { | 7 | import { |
8 | cleanupTests, | 8 | cleanupTests, |
9 | CustomPagesCommand, | ||
9 | flushAndRunServer, | 10 | flushAndRunServer, |
10 | getConfig, | 11 | getConfig, |
11 | getInstanceHomepage, | ||
12 | killallServers, | 12 | killallServers, |
13 | reRunServer, | 13 | reRunServer, |
14 | ServerInfo, | 14 | ServerInfo, |
15 | setAccessTokensToServers, | 15 | setAccessTokensToServers |
16 | updateInstanceHomepage | ||
17 | } from '../../../../shared/extra-utils/index' | 16 | } from '../../../../shared/extra-utils/index' |
18 | 17 | ||
19 | const expect = chai.expect | 18 | const expect = chai.expect |
@@ -27,26 +26,28 @@ async function getHomepageState (server: ServerInfo) { | |||
27 | 26 | ||
28 | describe('Test instance homepage actions', function () { | 27 | describe('Test instance homepage actions', function () { |
29 | let server: ServerInfo | 28 | let server: ServerInfo |
29 | let command: CustomPagesCommand | ||
30 | 30 | ||
31 | before(async function () { | 31 | before(async function () { |
32 | this.timeout(30000) | 32 | this.timeout(30000) |
33 | 33 | ||
34 | server = await flushAndRunServer(1) | 34 | server = await flushAndRunServer(1) |
35 | await setAccessTokensToServers([ server ]) | 35 | await setAccessTokensToServers([ server ]) |
36 | |||
37 | command = server.customPageCommand | ||
36 | }) | 38 | }) |
37 | 39 | ||
38 | it('Should not have a homepage', async function () { | 40 | it('Should not have a homepage', async function () { |
39 | const state = await getHomepageState(server) | 41 | const state = await getHomepageState(server) |
40 | expect(state).to.be.false | 42 | expect(state).to.be.false |
41 | 43 | ||
42 | await getInstanceHomepage(server.url, HttpStatusCode.NOT_FOUND_404) | 44 | await command.getInstanceHomepage({ expectedStatus: HttpStatusCode.NOT_FOUND_404 }) |
43 | }) | 45 | }) |
44 | 46 | ||
45 | it('Should set a homepage', async function () { | 47 | it('Should set a homepage', async function () { |
46 | await updateInstanceHomepage(server.url, server.accessToken, '<picsou-magazine></picsou-magazine>') | 48 | await command.updateInstanceHomepage({ content: '<picsou-magazine></picsou-magazine>' }) |
47 | 49 | ||
48 | const res = await getInstanceHomepage(server.url) | 50 | const page = await command.getInstanceHomepage() |
49 | const page: CustomPage = res.body | ||
50 | expect(page.content).to.equal('<picsou-magazine></picsou-magazine>') | 51 | expect(page.content).to.equal('<picsou-magazine></picsou-magazine>') |
51 | 52 | ||
52 | const state = await getHomepageState(server) | 53 | const state = await getHomepageState(server) |
@@ -60,8 +61,7 @@ describe('Test instance homepage actions', function () { | |||
60 | 61 | ||
61 | await reRunServer(server) | 62 | await reRunServer(server) |
62 | 63 | ||
63 | const res = await getInstanceHomepage(server.url) | 64 | const page = await command.getInstanceHomepage() |
64 | const page: CustomPage = res.body | ||
65 | expect(page.content).to.equal('<picsou-magazine></picsou-magazine>') | 65 | expect(page.content).to.equal('<picsou-magazine></picsou-magazine>') |
66 | 66 | ||
67 | const state = await getHomepageState(server) | 67 | const state = await getHomepageState(server) |
@@ -69,10 +69,9 @@ describe('Test instance homepage actions', function () { | |||
69 | }) | 69 | }) |
70 | 70 | ||
71 | it('Should empty the homepage', async function () { | 71 | it('Should empty the homepage', async function () { |
72 | await updateInstanceHomepage(server.url, server.accessToken, '') | 72 | await command.updateInstanceHomepage({ content: '' }) |
73 | 73 | ||
74 | const res = await getInstanceHomepage(server.url) | 74 | const page = await command.getInstanceHomepage() |
75 | const page: CustomPage = res.body | ||
76 | expect(page.content).to.be.empty | 75 | expect(page.content).to.be.empty |
77 | 76 | ||
78 | const state = await getHomepageState(server) | 77 | 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 @@ | |||
1 | 1 | ||
2 | import { BulkRemoveCommentsOfBody } from '@shared/models/bulk/bulk-remove-comments-of-body.model' | 2 | import { BulkRemoveCommentsOfBody } from '@shared/models/bulk/bulk-remove-comments-of-body.model' |
3 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | 3 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' |
4 | import { AbstractCommand, CommonCommandOptions } from '../shared' | 4 | import { AbstractCommand, OverrideCommandOptions } from '../shared' |
5 | 5 | ||
6 | class BulkCommand extends AbstractCommand { | 6 | export class BulkCommand extends AbstractCommand { |
7 | 7 | ||
8 | removeCommentsOf (options: CommonCommandOptions & { | 8 | removeCommentsOf (options: OverrideCommandOptions & { |
9 | attributes: BulkRemoveCommentsOfBody | 9 | attributes: BulkRemoveCommentsOfBody |
10 | }) { | 10 | }) { |
11 | const { attributes } = options | 11 | const { attributes } = options |
@@ -18,7 +18,3 @@ class BulkCommand extends AbstractCommand { | |||
18 | }) | 18 | }) |
19 | } | 19 | } |
20 | } | 20 | } |
21 | |||
22 | export { | ||
23 | BulkCommand | ||
24 | } | ||
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 @@ | |||
1 | import { exec } from 'child_process' | 1 | import { exec } from 'child_process' |
2 | import { AbstractCommand } from '../shared' | 2 | import { AbstractCommand } from '../shared' |
3 | 3 | ||
4 | class CLICommand extends AbstractCommand { | 4 | export class CLICommand extends AbstractCommand { |
5 | 5 | ||
6 | static exec (command: string) { | 6 | static exec (command: string) { |
7 | return new Promise<string>((res, rej) => { | 7 | return new Promise<string>((res, rej) => { |
@@ -21,7 +21,3 @@ class CLICommand extends AbstractCommand { | |||
21 | return CLICommand.exec(`${this.getEnv()} ${command}`) | 21 | return CLICommand.exec(`${this.getEnv()} ${command}`) |
22 | } | 22 | } |
23 | } | 23 | } |
24 | |||
25 | export { | ||
26 | CLICommand | ||
27 | } | ||
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 @@ | |||
1 | import { CustomPage } from '@shared/models' | ||
1 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | 2 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' |
2 | import { makeGetRequest, makePutBodyRequest } from '../requests/requests' | 3 | import { AbstractCommand, OverrideCommandOptions } from '../shared' |
3 | 4 | ||
4 | function getInstanceHomepage (url: string, statusCodeExpected = HttpStatusCode.OK_200) { | 5 | export class CustomPagesCommand extends AbstractCommand { |
5 | const path = '/api/v1/custom-pages/homepage/instance' | ||
6 | 6 | ||
7 | return makeGetRequest({ | 7 | getInstanceHomepage (options: OverrideCommandOptions = {}) { |
8 | url, | 8 | const path = '/api/v1/custom-pages/homepage/instance' |
9 | path, | ||
10 | statusCodeExpected | ||
11 | }) | ||
12 | } | ||
13 | |||
14 | function updateInstanceHomepage (url: string, token: string, content: string) { | ||
15 | const path = '/api/v1/custom-pages/homepage/instance' | ||
16 | 9 | ||
17 | return makePutBodyRequest({ | 10 | return this.getRequestBody<CustomPage>({ |
18 | url, | 11 | ...options, |
19 | path, | 12 | path, |
20 | token, | 13 | defaultExpectedStatus: HttpStatusCode.OK_200 |
21 | fields: { content }, | 14 | }) |
22 | statusCodeExpected: HttpStatusCode.NO_CONTENT_204 | 15 | } |
23 | }) | ||
24 | } | ||
25 | 16 | ||
26 | // --------------------------------------------------------------------------- | 17 | updateInstanceHomepage (options: OverrideCommandOptions & { |
18 | content: string | ||
19 | }) { | ||
20 | const { content } = options | ||
21 | const path = '/api/v1/custom-pages/homepage/instance' | ||
27 | 22 | ||
28 | export { | 23 | return this.putBodyRequest({ |
29 | getInstanceHomepage, | 24 | ...options, |
30 | updateInstanceHomepage | 25 | path, |
26 | fields: { content }, | ||
27 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
28 | }) | ||
29 | } | ||
31 | } | 30 | } |
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' | |||
2 | 2 | ||
3 | export * from './cli' | 3 | export * from './cli' |
4 | 4 | ||
5 | export * from './custom-pages/custom-pages' | 5 | export * from './custom-pages' |
6 | 6 | ||
7 | export * from './feeds/feeds' | 7 | export * from './feeds/feeds' |
8 | 8 | ||
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) { | |||
182 | return decode(path.split('?')[1]) | 182 | return decode(path.split('?')[1]) |
183 | } | 183 | } |
184 | 184 | ||
185 | function unwrap <T> (test: request.Test): Promise<T> { | ||
186 | return test.then(res => res.body) | ||
187 | } | ||
188 | |||
185 | // --------------------------------------------------------------------------- | 189 | // --------------------------------------------------------------------------- |
186 | 190 | ||
187 | export { | 191 | export { |
@@ -194,5 +198,6 @@ export { | |||
194 | makePutBodyRequest, | 198 | makePutBodyRequest, |
195 | makeDeleteRequest, | 199 | makeDeleteRequest, |
196 | makeRawRequest, | 200 | makeRawRequest, |
201 | unwrap, | ||
197 | updateImageRequest | 202 | updateImageRequest |
198 | } | 203 | } |
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' | |||
8 | import { VideoChannel } from '../../models/videos' | 8 | import { VideoChannel } from '../../models/videos' |
9 | import { BulkCommand } from '../bulk' | 9 | import { BulkCommand } from '../bulk' |
10 | import { CLICommand } from '../cli' | 10 | import { CLICommand } from '../cli' |
11 | import { CustomPagesCommand } from '../custom-pages' | ||
11 | import { buildServerDirectory, getFileSize, isGithubCI, root, wait } from '../miscs/miscs' | 12 | import { buildServerDirectory, getFileSize, isGithubCI, root, wait } from '../miscs/miscs' |
12 | import { makeGetRequest } from '../requests/requests' | 13 | import { makeGetRequest } from '../requests/requests' |
13 | 14 | ||
@@ -65,6 +66,7 @@ interface ServerInfo { | |||
65 | 66 | ||
66 | bulkCommand?: BulkCommand | 67 | bulkCommand?: BulkCommand |
67 | cliCommand?: CLICommand | 68 | cliCommand?: CLICommand |
69 | customPageCommand?: CustomPagesCommand | ||
68 | } | 70 | } |
69 | 71 | ||
70 | function parallelTests () { | 72 | function parallelTests () { |
@@ -272,6 +274,7 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = [] | |||
272 | 274 | ||
273 | server.bulkCommand = new BulkCommand(server) | 275 | server.bulkCommand = new BulkCommand(server) |
274 | server.cliCommand = new CLICommand(server) | 276 | server.cliCommand = new CLICommand(server) |
277 | server.customPageCommand = new CustomPagesCommand(server) | ||
275 | 278 | ||
276 | res(server) | 279 | res(server) |
277 | }) | 280 | }) |
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 @@ | |||
1 | import { HttpStatusCode } from '@shared/core-utils' | 1 | import { HttpStatusCode } from '@shared/core-utils' |
2 | import { makePostBodyRequest } from '../requests/requests' | 2 | import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, unwrap } from '../requests/requests' |
3 | import { ServerInfo } from '../server/servers' | 3 | import { ServerInfo } from '../server/servers' |
4 | 4 | ||
5 | export interface CommonCommandOptions { | 5 | export interface OverrideCommandOptions { |
6 | token?: string | 6 | token?: string |
7 | expectedStatus?: number | 7 | expectedStatus?: number |
8 | } | 8 | } |
9 | 9 | ||
10 | interface CommonCommandOptions extends OverrideCommandOptions { | ||
11 | path: string | ||
12 | defaultExpectedStatus: number | ||
13 | } | ||
14 | |||
10 | abstract class AbstractCommand { | 15 | abstract class AbstractCommand { |
11 | 16 | ||
12 | private expectedStatus = HttpStatusCode.OK_200 | 17 | private expectedStatus: HttpStatusCode |
13 | 18 | ||
14 | constructor ( | 19 | constructor ( |
15 | protected server: ServerInfo | 20 | protected server: ServerInfo |
@@ -25,20 +30,43 @@ abstract class AbstractCommand { | |||
25 | this.expectedStatus = status | 30 | this.expectedStatus = status |
26 | } | 31 | } |
27 | 32 | ||
33 | protected getRequestBody <T> (options: CommonCommandOptions) { | ||
34 | return unwrap<T>(makeGetRequest(this.buildCommonRequestOptions(options))) | ||
35 | } | ||
36 | |||
37 | protected putBodyRequest (options: CommonCommandOptions & { | ||
38 | fields?: { [ fieldName: string ]: any } | ||
39 | }) { | ||
40 | const { fields } = options | ||
41 | |||
42 | return makePutBodyRequest({ | ||
43 | ...this.buildCommonRequestOptions(options), | ||
44 | |||
45 | fields | ||
46 | }) | ||
47 | } | ||
48 | |||
28 | protected postBodyRequest (options: CommonCommandOptions & { | 49 | protected postBodyRequest (options: CommonCommandOptions & { |
29 | path: string | ||
30 | defaultExpectedStatus: number | ||
31 | fields?: { [ fieldName: string ]: any } | 50 | fields?: { [ fieldName: string ]: any } |
32 | }) { | 51 | }) { |
33 | const { token, fields, expectedStatus, defaultExpectedStatus, path } = options | 52 | const { fields } = options |
34 | 53 | ||
35 | return makePostBodyRequest({ | 54 | return makePostBodyRequest({ |
55 | ...this.buildCommonRequestOptions(options), | ||
56 | |||
57 | fields | ||
58 | }) | ||
59 | } | ||
60 | |||
61 | private buildCommonRequestOptions (options: CommonCommandOptions) { | ||
62 | const { token, expectedStatus, defaultExpectedStatus, path } = options | ||
63 | |||
64 | return { | ||
36 | url: this.server.url, | 65 | url: this.server.url, |
37 | path, | 66 | path, |
38 | token: token ?? this.server.accessToken, | 67 | token: token ?? this.server.accessToken, |
39 | fields, | ||
40 | statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus | 68 | statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus |
41 | }) | 69 | } |
42 | } | 70 | } |
43 | } | 71 | } |
44 | 72 | ||