From 302eba0d898e38dca14739486441c27c0be6c62f Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 22 Apr 2021 10:55:28 +0200 Subject: Add data directory for plugins and some helpers --- .../fixtures/peertube-plugin-test-four/main.js | 23 ++++++- .../fixtures/peertube-plugin-test-six/main.js | 18 ++++- server/tests/plugins/plugin-helpers.ts | 40 +++++++++++ server/tests/plugins/plugin-storage.ts | 80 +++++++++++++++++++++- 4 files changed, 156 insertions(+), 5 deletions(-) (limited to 'server/tests') diff --git a/server/tests/fixtures/peertube-plugin-test-four/main.js b/server/tests/fixtures/peertube-plugin-test-four/main.js index ea0599997..6930ac511 100644 --- a/server/tests/fixtures/peertube-plugin-test-four/main.js +++ b/server/tests/fixtures/peertube-plugin-test-four/main.js @@ -77,10 +77,31 @@ async function register ({ }) router.get('/static-route', async (req, res) => { - const staticRoute = await peertubeHelpers.plugin.getBaseStaticRoute() + const staticRoute = peertubeHelpers.plugin.getBaseStaticRoute() return res.json({ staticRoute }) }) + + router.get('/router-route', async (req, res) => { + const routerRoute = peertubeHelpers.plugin.getBaseRouterRoute() + + return res.json({ routerRoute }) + }) + + router.get('/user', async (req, res) => { + const user = peertubeHelpers.user.getAuthUser(res) + + const isAdmin = user.role === 0 + const isModerator = user.role === 1 + const isUser = user.role === 2 + + return res.json({ + username: user.username, + isAdmin, + isModerator, + isUser + }) + }) } } diff --git a/server/tests/fixtures/peertube-plugin-test-six/main.js b/server/tests/fixtures/peertube-plugin-test-six/main.js index bb9aaffa7..858bdb2df 100644 --- a/server/tests/fixtures/peertube-plugin-test-six/main.js +++ b/server/tests/fixtures/peertube-plugin-test-six/main.js @@ -1,6 +1,10 @@ +const fs = require('fs') +const path = require('path') + async function register ({ storageManager, - peertubeHelpers + peertubeHelpers, + getRouter }) { const { logger } = peertubeHelpers @@ -11,6 +15,18 @@ async function register ({ const result = await storageManager.getData('superkey') logger.info('superkey stored value is %s', result.value) } + + { + getRouter().get('/create-file', async (req, res) => { + const basePath = peertubeHelpers.plugin.getDataDirectoryPath() + + fs.writeFile(path.join(basePath, 'Aladdin.txt'), 'Prince Ali', function (err) { + if (err) return res.sendStatus(500) + + res.sendStatus(200) + }) + }) + } } async function unregister () { diff --git a/server/tests/plugins/plugin-helpers.ts b/server/tests/plugins/plugin-helpers.ts index 325d20e84..2ac070b41 100644 --- a/server/tests/plugins/plugin-helpers.ts +++ b/server/tests/plugins/plugin-helpers.ts @@ -100,6 +100,46 @@ describe('Test plugin helpers', function () { expect(res.body.staticRoute).to.equal('/plugins/test-four/0.0.1/static/') }) + + it('Should get the base static route', async function () { + const baseRouter = '/plugins/test-four/0.0.1/router/' + + const res = await makeGetRequest({ + url: servers[0].url, + path: baseRouter + 'router-route', + statusCodeExpected: HttpStatusCode.OK_200 + }) + + expect(res.body.routerRoute).to.equal(baseRouter) + }) + }) + + describe('User', function () { + + it('Should not get a user if not authenticated', async function () { + const res = await makeGetRequest({ + url: servers[0].url, + path: '/plugins/test-four/router/user', + statusCodeExpected: HttpStatusCode.OK_200 + }) + + expect(res.body.user).to.be.undefined + }) + + it('Should get a user if authenticated', async function () { + const res = await makeGetRequest({ + url: servers[0].url, + token: servers[0].accessToken, + path: '/plugins/test-four/router/user', + statusCodeExpected: HttpStatusCode.OK_200 + }) + + expect(res.body.user).to.exist + expect(res.body.username).to.equal('root') + expect(res.body.isAdmin).to.be.true + expect(res.body.isModerator).to.be.false + expect(res.body.isUser).to.be.false + }) }) describe('Moderation', function () { diff --git a/server/tests/plugins/plugin-storage.ts b/server/tests/plugins/plugin-storage.ts index 356692eb9..3c46b2585 100644 --- a/server/tests/plugins/plugin-storage.ts +++ b/server/tests/plugins/plugin-storage.ts @@ -1,7 +1,18 @@ /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ import 'mocha' -import { getPluginTestPath, installPlugin, setAccessTokensToServers } from '../../../shared/extra-utils' +import { expect } from 'chai' +import { pathExists, readdir, readFile } from 'fs-extra' +import { join } from 'path' +import { HttpStatusCode } from '@shared/core-utils' +import { + buildServerDirectory, + getPluginTestPath, + installPlugin, + makeGetRequest, + setAccessTokensToServers, + uninstallPlugin +} from '../../../shared/extra-utils' import { cleanupTests, flushAndRunServer, ServerInfo, waitUntilLog } from '../../../shared/extra-utils/server/servers' describe('Test plugin storage', function () { @@ -20,8 +31,71 @@ describe('Test plugin storage', function () { }) }) - it('Should correctly store a subkey', async function () { - await waitUntilLog(server, 'superkey stored value is toto') + describe('DB storage', function () { + + it('Should correctly store a subkey', async function () { + await waitUntilLog(server, 'superkey stored value is toto') + }) + }) + + describe('Disk storage', function () { + let dataPath: string + let pluginDataPath: string + + async function getFileContent () { + const files = await readdir(pluginDataPath) + expect(files).to.have.lengthOf(1) + + return readFile(join(pluginDataPath, files[0]), 'utf8') + } + + before(function () { + dataPath = buildServerDirectory(server, 'plugins/data') + pluginDataPath = join(dataPath, 'peertube-plugin-test-six') + }) + + it('Should have created the directory on install', async function () { + const dataPath = buildServerDirectory(server, 'plugins/data') + const pluginDataPath = join(dataPath, 'peertube-plugin-test-six') + + expect(await pathExists(dataPath)).to.be.true + expect(await pathExists(pluginDataPath)).to.be.true + expect(await readdir(pluginDataPath)).to.have.lengthOf(0) + }) + + it('Should have created a file', async function () { + await makeGetRequest({ + url: server.url, + token: server.accessToken, + path: '/plugins/test-six/router/create-file', + statusCodeExpected: HttpStatusCode.OK_200 + }) + + const content = await getFileContent() + expect(content).to.equal('Prince Ali') + }) + + it('Should still have the file after an uninstallation', async function () { + await uninstallPlugin({ + url: server.url, + accessToken: server.accessToken, + npmName: 'peertube-plugin-test-six' + }) + + const content = await getFileContent() + expect(content).to.equal('Prince Ali') + }) + + it('Should still have the file after the reinstallation', async function () { + await installPlugin({ + url: server.url, + accessToken: server.accessToken, + path: getPluginTestPath('-six') + }) + + const content = await getFileContent() + expect(content).to.equal('Prince Ali') + }) }) after(async function () { -- cgit v1.2.3