From b65de1be4dcf626c552be613d531d3f6e23c6085 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 16 Dec 2021 09:38:27 +0100 Subject: Use different p2p policy for embeds and webapp --- server/controllers/api/users/index.ts | 4 +- server/initializers/config.ts | 7 ++- server/initializers/installer.ts | 2 +- server/lib/auth/oauth-model.ts | 2 +- server/lib/server-config-manager.ts | 7 ++- server/tests/api/server/config-defaults.ts | 97 +++++++++++++++++++++++------- 6 files changed, 90 insertions(+), 29 deletions(-) (limited to 'server') diff --git a/server/controllers/api/users/index.ts b/server/controllers/api/users/index.ts index f3b4508d9..fcee58f6b 100644 --- a/server/controllers/api/users/index.ts +++ b/server/controllers/api/users/index.ts @@ -183,7 +183,7 @@ async function createUser (req: express.Request, res: express.Response) { password: body.password, email: body.email, nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, - p2pEnabled: CONFIG.DEFAULTS.P2P.ENABLED, + p2pEnabled: CONFIG.DEFAULTS.P2P.WEBAPP.ENABLED, autoPlayVideo: true, role: body.role, videoQuota: body.videoQuota, @@ -233,7 +233,7 @@ async function registerUser (req: express.Request, res: express.Response) { password: body.password, email: body.email, nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, - p2pEnabled: CONFIG.DEFAULTS.P2P.ENABLED, + p2pEnabled: CONFIG.DEFAULTS.P2P.WEBAPP.ENABLED, autoPlayVideo: true, role: UserRole.USER, videoQuota: CONFIG.USER.VIDEO_QUOTA, diff --git a/server/initializers/config.ts b/server/initializers/config.ts index a6ea6d888..f2e9f7088 100644 --- a/server/initializers/config.ts +++ b/server/initializers/config.ts @@ -80,7 +80,12 @@ const CONFIG = { LICENCE: config.get('defaults.publish.licence') }, P2P: { - ENABLED: config.get('defaults.p2p.enabled') + WEBAPP: { + ENABLED: config.get('defaults.p2p.webapp.enabled') + }, + EMBED: { + ENABLED: config.get('defaults.p2p.embed.enabled') + } } }, diff --git a/server/initializers/installer.ts b/server/initializers/installer.ts index 19adaf177..7c29a42e1 100644 --- a/server/initializers/installer.ts +++ b/server/initializers/installer.ts @@ -144,7 +144,7 @@ async function createOAuthAdminIfNotExist () { role, verified: true, nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, - p2pEnabled: CONFIG.DEFAULTS.P2P.ENABLED, + p2pEnabled: CONFIG.DEFAULTS.P2P.WEBAPP.ENABLED, videoQuota: -1, videoQuotaDaily: -1 } diff --git a/server/lib/auth/oauth-model.ts b/server/lib/auth/oauth-model.ts index 754bee36d..b68cce6d2 100644 --- a/server/lib/auth/oauth-model.ts +++ b/server/lib/auth/oauth-model.ts @@ -226,7 +226,7 @@ async function createUserFromExternal (pluginAuth: string, options: { password: null, email: options.email, nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, - p2pEnabled: CONFIG.DEFAULTS.P2P.ENABLED, + p2pEnabled: CONFIG.DEFAULTS.P2P.WEBAPP.ENABLED, autoPlayVideo: true, role: options.role, videoQuota: CONFIG.USER.VIDEO_QUOTA, diff --git a/server/lib/server-config-manager.ts b/server/lib/server-config-manager.ts index d759f85e1..18032ef86 100644 --- a/server/lib/server-config-manager.ts +++ b/server/lib/server-config-manager.ts @@ -63,7 +63,12 @@ class ServerConfigManager { licence: CONFIG.DEFAULTS.PUBLISH.LICENCE }, p2p: { - enabled: CONFIG.DEFAULTS.P2P.ENABLED + webapp: { + enabled: CONFIG.DEFAULTS.P2P.WEBAPP.ENABLED + }, + embed: { + enabled: CONFIG.DEFAULTS.P2P.EMBED.ENABLED + } } }, diff --git a/server/tests/api/server/config-defaults.ts b/server/tests/api/server/config-defaults.ts index 340d4b44b..117fc20d7 100644 --- a/server/tests/api/server/config-defaults.ts +++ b/server/tests/api/server/config-defaults.ts @@ -125,40 +125,91 @@ describe('Test config defaults', function () { describe('Default P2P values', function () { - before(async function () { - const overrideConfig = { - defaults: { - p2p: { - enabled: false + describe('Webapp default value', function () { + + before(async function () { + const overrideConfig = { + defaults: { + p2p: { + webapp: { + enabled: false + } + } } } - } - await server.kill() - await server.run(overrideConfig) - }) + await server.kill() + await server.run(overrideConfig) + }) - it('Should not have P2P enabled', async function () { - const config = await server.config.getConfig() + it('Should have appropriate P2P config', async function () { + const config = await server.config.getConfig() - expect(config.defaults.p2p.enabled).to.be.false - }) + expect(config.defaults.p2p.webapp.enabled).to.be.false + expect(config.defaults.p2p.embed.enabled).to.be.true + }) + + it('Should create a user with this default setting', async function () { + await server.users.create({ username: 'user_p2p_1' }) + const userToken = await server.login.getAccessToken('user_p2p_1') + + const { p2pEnabled } = await server.users.getMyInfo({ token: userToken }) + expect(p2pEnabled).to.be.false + }) - it('Should create a user with this default setting', async function () { - await server.users.create({ username: 'user_p2p_1' }) - const userToken = await server.login.getAccessToken('user_p2p_1') + it('Should register a user with this default setting', async function () { + await server.users.register({ username: 'user_p2p_2' }) - const { p2pEnabled } = await server.users.getMyInfo({ token: userToken }) - expect(p2pEnabled).to.be.false + const userToken = await server.login.getAccessToken('user_p2p_2') + + const { p2pEnabled } = await server.users.getMyInfo({ token: userToken }) + expect(p2pEnabled).to.be.false + }) }) - it('Should register a user with this default setting', async function () { - await server.users.register({ username: 'user_p2p_2' }) + describe('Embed default value', function () { + + before(async function () { + const overrideConfig = { + defaults: { + p2p: { + embed: { + enabled: false + } + } + }, + signup: { + limit: 15 + } + } + + await server.kill() + await server.run(overrideConfig) + }) + + it('Should have appropriate P2P config', async function () { + const config = await server.config.getConfig() - const userToken = await server.login.getAccessToken('user_p2p_2') + expect(config.defaults.p2p.webapp.enabled).to.be.true + expect(config.defaults.p2p.embed.enabled).to.be.false + }) + + it('Should create a user with this default setting', async function () { + await server.users.create({ username: 'user_p2p_3' }) + const userToken = await server.login.getAccessToken('user_p2p_3') + + const { p2pEnabled } = await server.users.getMyInfo({ token: userToken }) + expect(p2pEnabled).to.be.true + }) - const { p2pEnabled } = await server.users.getMyInfo({ token: userToken }) - expect(p2pEnabled).to.be.false + it('Should register a user with this default setting', async function () { + await server.users.register({ username: 'user_p2p_4' }) + + const userToken = await server.login.getAccessToken('user_p2p_4') + + const { p2pEnabled } = await server.users.getMyInfo({ token: userToken }) + expect(p2pEnabled).to.be.true + }) }) }) -- cgit v1.2.3