From 3a4992633ee62d5edfbb484d9c6bcb3cf158489d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 31 Jul 2023 14:34:36 +0200 Subject: Migrate server to ESM Sorry for the very big commit that may lead to git log issues and merge conflicts, but it's a major step forward: * Server can be faster at startup because imports() are async and we can easily lazy import big modules * Angular doesn't seem to support ES import (with .js extension), so we had to correctly organize peertube into a monorepo: * Use yarn workspace feature * Use typescript reference projects for dependencies * Shared projects have been moved into "packages", each one is now a node module (with a dedicated package.json/tsconfig.json) * server/tools have been moved into apps/ and is now a dedicated app bundled and published on NPM so users don't have to build peertube cli tools manually * server/tests have been moved into packages/ so we don't compile them every time we want to run the server * Use isolatedModule option: * Had to move from const enum to const (https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums) * Had to explictely specify "type" imports when used in decorators * Prefer tsx (that uses esbuild under the hood) instead of ts-node to load typescript files (tests with mocha or scripts): * To reduce test complexity as esbuild doesn't support decorator metadata, we only test server files that do not import server models * We still build tests files into js files for a faster CI * Remove unmaintained peertube CLI import script * Removed some barrels to speed up execution (less imports) --- server/tests/api/check-params/plugins.ts | 490 ------------------------------- 1 file changed, 490 deletions(-) delete mode 100644 server/tests/api/check-params/plugins.ts (limited to 'server/tests/api/check-params/plugins.ts') diff --git a/server/tests/api/check-params/plugins.ts b/server/tests/api/check-params/plugins.ts deleted file mode 100644 index e08cd7ab8..000000000 --- a/server/tests/api/check-params/plugins.ts +++ /dev/null @@ -1,490 +0,0 @@ -/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ - -import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '@server/tests/shared' -import { HttpStatusCode, PeerTubePlugin, PluginType } from '@shared/models' -import { - cleanupTests, - createSingleServer, - makeGetRequest, - makePostBodyRequest, - makePutBodyRequest, - PeerTubeServer, - setAccessTokensToServers -} from '@shared/server-commands' - -describe('Test server plugins API validators', function () { - let server: PeerTubeServer - let userAccessToken = null - - const npmPlugin = 'peertube-plugin-hello-world' - const pluginName = 'hello-world' - let npmVersion: string - - const themePlugin = 'peertube-theme-background-red' - const themeName = 'background-red' - let themeVersion: string - - // --------------------------------------------------------------- - - before(async function () { - this.timeout(60000) - - server = await createSingleServer(1) - - await setAccessTokensToServers([ server ]) - - const user = { - username: 'user1', - password: 'password' - } - - await server.users.create({ username: user.username, password: user.password }) - userAccessToken = await server.login.getAccessToken(user) - - { - const res = await server.plugins.install({ npmName: npmPlugin }) - const plugin = res.body as PeerTubePlugin - npmVersion = plugin.version - } - - { - const res = await server.plugins.install({ npmName: themePlugin }) - const plugin = res.body as PeerTubePlugin - themeVersion = plugin.version - } - }) - - describe('With static plugin routes', function () { - it('Should fail with an unknown plugin name/plugin version', async function () { - const paths = [ - '/plugins/' + pluginName + '/0.0.1/auth/fake-auth', - '/plugins/' + pluginName + '/0.0.1/static/images/chocobo.png', - '/plugins/' + pluginName + '/0.0.1/client-scripts/client/common-client-plugin.js', - '/themes/' + themeName + '/0.0.1/static/images/chocobo.png', - '/themes/' + themeName + '/0.0.1/client-scripts/client/video-watch-client-plugin.js', - '/themes/' + themeName + '/0.0.1/css/assets/style1.css' - ] - - for (const p of paths) { - await makeGetRequest({ url: server.url, path: p, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) - } - }) - - it('Should fail when requesting a plugin in the theme path', async function () { - await makeGetRequest({ - url: server.url, - path: '/themes/' + pluginName + '/' + npmVersion + '/static/images/chocobo.png', - expectedStatus: HttpStatusCode.NOT_FOUND_404 - }) - }) - - it('Should fail with invalid versions', async function () { - const paths = [ - '/plugins/' + pluginName + '/0.0.1.1/auth/fake-auth', - '/plugins/' + pluginName + '/0.0.1.1/static/images/chocobo.png', - '/plugins/' + pluginName + '/0.1/client-scripts/client/common-client-plugin.js', - '/themes/' + themeName + '/1/static/images/chocobo.png', - '/themes/' + themeName + '/0.0.1000a/client-scripts/client/video-watch-client-plugin.js', - '/themes/' + themeName + '/0.a.1/css/assets/style1.css' - ] - - for (const p of paths) { - await makeGetRequest({ url: server.url, path: p, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) - } - }) - - it('Should fail with invalid paths', async function () { - const paths = [ - '/plugins/' + pluginName + '/' + npmVersion + '/static/images/../chocobo.png', - '/plugins/' + pluginName + '/' + npmVersion + '/client-scripts/../client/common-client-plugin.js', - '/themes/' + themeName + '/' + themeVersion + '/static/../images/chocobo.png', - '/themes/' + themeName + '/' + themeVersion + '/client-scripts/client/video-watch-client-plugin.js/..', - '/themes/' + themeName + '/' + themeVersion + '/css/../assets/style1.css' - ] - - for (const p of paths) { - await makeGetRequest({ url: server.url, path: p, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) - } - }) - - it('Should fail with an unknown auth name', async function () { - const path = '/plugins/' + pluginName + '/' + npmVersion + '/auth/bad-auth' - - await makeGetRequest({ url: server.url, path, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) - }) - - it('Should fail with an unknown static file', async function () { - const paths = [ - '/plugins/' + pluginName + '/' + npmVersion + '/static/fake/chocobo.png', - '/plugins/' + pluginName + '/' + npmVersion + '/client-scripts/client/fake.js', - '/themes/' + themeName + '/' + themeVersion + '/static/fake/chocobo.png', - '/themes/' + themeName + '/' + themeVersion + '/client-scripts/client/fake.js' - ] - - for (const p of paths) { - await makeGetRequest({ url: server.url, path: p, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) - } - }) - - it('Should fail with an unknown CSS file', async function () { - await makeGetRequest({ - url: server.url, - path: '/themes/' + themeName + '/' + themeVersion + '/css/assets/fake.css', - expectedStatus: HttpStatusCode.NOT_FOUND_404 - }) - }) - - it('Should succeed with the correct parameters', async function () { - const paths = [ - '/plugins/' + pluginName + '/' + npmVersion + '/static/images/chocobo.png', - '/plugins/' + pluginName + '/' + npmVersion + '/client-scripts/client/common-client-plugin.js', - '/themes/' + themeName + '/' + themeVersion + '/static/images/chocobo.png', - '/themes/' + themeName + '/' + themeVersion + '/client-scripts/client/video-watch-client-plugin.js', - '/themes/' + themeName + '/' + themeVersion + '/css/assets/style1.css' - ] - - for (const p of paths) { - await makeGetRequest({ url: server.url, path: p, expectedStatus: HttpStatusCode.OK_200 }) - } - - const authPath = '/plugins/' + pluginName + '/' + npmVersion + '/auth/fake-auth' - await makeGetRequest({ url: server.url, path: authPath, expectedStatus: HttpStatusCode.FOUND_302 }) - }) - }) - - describe('When listing available plugins/themes', function () { - const path = '/api/v1/plugins/available' - const baseQuery = { - search: 'super search', - pluginType: PluginType.PLUGIN, - currentPeerTubeEngine: '1.2.3' - } - - it('Should fail with an invalid token', async function () { - await makeGetRequest({ - url: server.url, - path, - token: 'fake_token', - query: baseQuery, - expectedStatus: HttpStatusCode.UNAUTHORIZED_401 - }) - }) - - it('Should fail if the user is not an administrator', async function () { - await makeGetRequest({ - url: server.url, - path, - token: userAccessToken, - query: baseQuery, - expectedStatus: HttpStatusCode.FORBIDDEN_403 - }) - }) - - it('Should fail with a bad start pagination', async function () { - await checkBadStartPagination(server.url, path, server.accessToken) - }) - - it('Should fail with a bad count pagination', async function () { - await checkBadCountPagination(server.url, path, server.accessToken) - }) - - it('Should fail with an incorrect sort', async function () { - await checkBadSortPagination(server.url, path, server.accessToken) - }) - - it('Should fail with an invalid plugin type', async function () { - const query = { ...baseQuery, pluginType: 5 } - - await makeGetRequest({ - url: server.url, - path, - token: server.accessToken, - query - }) - }) - - it('Should fail with an invalid current peertube engine', async function () { - const query = { ...baseQuery, currentPeerTubeEngine: '1.0' } - - await makeGetRequest({ - url: server.url, - path, - token: server.accessToken, - query - }) - }) - - it('Should success with the correct parameters', async function () { - await makeGetRequest({ - url: server.url, - path, - token: server.accessToken, - query: baseQuery, - expectedStatus: HttpStatusCode.OK_200 - }) - }) - }) - - describe('When listing local plugins/themes', function () { - const path = '/api/v1/plugins' - const baseQuery = { - pluginType: PluginType.THEME - } - - it('Should fail with an invalid token', async function () { - await makeGetRequest({ - url: server.url, - path, - token: 'fake_token', - query: baseQuery, - expectedStatus: HttpStatusCode.UNAUTHORIZED_401 - }) - }) - - it('Should fail if the user is not an administrator', async function () { - await makeGetRequest({ - url: server.url, - path, - token: userAccessToken, - query: baseQuery, - expectedStatus: HttpStatusCode.FORBIDDEN_403 - }) - }) - - it('Should fail with a bad start pagination', async function () { - await checkBadStartPagination(server.url, path, server.accessToken) - }) - - it('Should fail with a bad count pagination', async function () { - await checkBadCountPagination(server.url, path, server.accessToken) - }) - - it('Should fail with an incorrect sort', async function () { - await checkBadSortPagination(server.url, path, server.accessToken) - }) - - it('Should fail with an invalid plugin type', async function () { - const query = { ...baseQuery, pluginType: 5 } - - await makeGetRequest({ - url: server.url, - path, - token: server.accessToken, - query - }) - }) - - it('Should success with the correct parameters', async function () { - await makeGetRequest({ - url: server.url, - path, - token: server.accessToken, - query: baseQuery, - expectedStatus: HttpStatusCode.OK_200 - }) - }) - }) - - describe('When getting a plugin or the registered settings or public settings', function () { - const path = '/api/v1/plugins/' - - it('Should fail with an invalid token', async function () { - for (const suffix of [ npmPlugin, `${npmPlugin}/registered-settings` ]) { - await makeGetRequest({ - url: server.url, - path: path + suffix, - token: 'fake_token', - expectedStatus: HttpStatusCode.UNAUTHORIZED_401 - }) - } - }) - - it('Should fail if the user is not an administrator', async function () { - for (const suffix of [ npmPlugin, `${npmPlugin}/registered-settings` ]) { - await makeGetRequest({ - url: server.url, - path: path + suffix, - token: userAccessToken, - expectedStatus: HttpStatusCode.FORBIDDEN_403 - }) - } - }) - - it('Should fail with an invalid npm name', async function () { - for (const suffix of [ 'toto', 'toto/registered-settings', 'toto/public-settings' ]) { - await makeGetRequest({ - url: server.url, - path: path + suffix, - token: server.accessToken, - expectedStatus: HttpStatusCode.BAD_REQUEST_400 - }) - } - - for (const suffix of [ 'peertube-plugin-TOTO', 'peertube-plugin-TOTO/registered-settings', 'peertube-plugin-TOTO/public-settings' ]) { - await makeGetRequest({ - url: server.url, - path: path + suffix, - token: server.accessToken, - expectedStatus: HttpStatusCode.BAD_REQUEST_400 - }) - } - }) - - it('Should fail with an unknown plugin', async function () { - for (const suffix of [ 'peertube-plugin-toto', 'peertube-plugin-toto/registered-settings', 'peertube-plugin-toto/public-settings' ]) { - await makeGetRequest({ - url: server.url, - path: path + suffix, - token: server.accessToken, - expectedStatus: HttpStatusCode.NOT_FOUND_404 - }) - } - }) - - it('Should succeed with the correct parameters', async function () { - for (const suffix of [ npmPlugin, `${npmPlugin}/registered-settings`, `${npmPlugin}/public-settings` ]) { - await makeGetRequest({ - url: server.url, - path: path + suffix, - token: server.accessToken, - expectedStatus: HttpStatusCode.OK_200 - }) - } - }) - }) - - describe('When updating plugin settings', function () { - const path = '/api/v1/plugins/' - const settings = { setting1: 'value1' } - - it('Should fail with an invalid token', async function () { - await makePutBodyRequest({ - url: server.url, - path: path + npmPlugin + '/settings', - fields: { settings }, - token: 'fake_token', - expectedStatus: HttpStatusCode.UNAUTHORIZED_401 - }) - }) - - it('Should fail if the user is not an administrator', async function () { - await makePutBodyRequest({ - url: server.url, - path: path + npmPlugin + '/settings', - fields: { settings }, - token: userAccessToken, - expectedStatus: HttpStatusCode.FORBIDDEN_403 - }) - }) - - it('Should fail with an invalid npm name', async function () { - await makePutBodyRequest({ - url: server.url, - path: path + 'toto/settings', - fields: { settings }, - token: server.accessToken, - expectedStatus: HttpStatusCode.BAD_REQUEST_400 - }) - - await makePutBodyRequest({ - url: server.url, - path: path + 'peertube-plugin-TOTO/settings', - fields: { settings }, - token: server.accessToken, - expectedStatus: HttpStatusCode.BAD_REQUEST_400 - }) - }) - - it('Should fail with an unknown plugin', async function () { - await makePutBodyRequest({ - url: server.url, - path: path + 'peertube-plugin-toto/settings', - fields: { settings }, - token: server.accessToken, - expectedStatus: HttpStatusCode.NOT_FOUND_404 - }) - }) - - it('Should succeed with the correct parameters', async function () { - await makePutBodyRequest({ - url: server.url, - path: path + npmPlugin + '/settings', - fields: { settings }, - token: server.accessToken, - expectedStatus: HttpStatusCode.NO_CONTENT_204 - }) - }) - }) - - describe('When installing/updating/uninstalling a plugin', function () { - const path = '/api/v1/plugins/' - - it('Should fail with an invalid token', async function () { - for (const suffix of [ 'install', 'update', 'uninstall' ]) { - await makePostBodyRequest({ - url: server.url, - path: path + suffix, - fields: { npmName: npmPlugin }, - token: 'fake_token', - expectedStatus: HttpStatusCode.UNAUTHORIZED_401 - }) - } - }) - - it('Should fail if the user is not an administrator', async function () { - for (const suffix of [ 'install', 'update', 'uninstall' ]) { - await makePostBodyRequest({ - url: server.url, - path: path + suffix, - fields: { npmName: npmPlugin }, - token: userAccessToken, - expectedStatus: HttpStatusCode.FORBIDDEN_403 - }) - } - }) - - it('Should fail with an invalid npm name', async function () { - for (const suffix of [ 'install', 'update', 'uninstall' ]) { - await makePostBodyRequest({ - url: server.url, - path: path + suffix, - fields: { npmName: 'toto' }, - token: server.accessToken, - expectedStatus: HttpStatusCode.BAD_REQUEST_400 - }) - } - - for (const suffix of [ 'install', 'update', 'uninstall' ]) { - await makePostBodyRequest({ - url: server.url, - path: path + suffix, - fields: { npmName: 'peertube-plugin-TOTO' }, - token: server.accessToken, - expectedStatus: HttpStatusCode.BAD_REQUEST_400 - }) - } - }) - - it('Should succeed with the correct parameters', async function () { - const it = [ - { suffix: 'install', status: HttpStatusCode.OK_200 }, - { suffix: 'update', status: HttpStatusCode.OK_200 }, - { suffix: 'uninstall', status: HttpStatusCode.NO_CONTENT_204 } - ] - - for (const obj of it) { - await makePostBodyRequest({ - url: server.url, - path: path + obj.suffix, - fields: { npmName: npmPlugin }, - token: server.accessToken, - expectedStatus: obj.status - }) - } - }) - }) - - after(async function () { - await cleanupTests([ server ]) - }) -}) -- cgit v1.2.3