From e22528aca6eb58c8f8005790ac6e76ed4f8ad706 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 10 Mar 2017 11:32:39 +0100 Subject: [PATCH] Server: add config endpoint --- config/default.yaml | 3 ++ config/production.yaml.example | 3 ++ config/test.yaml | 3 ++ server/controllers/api/config.js | 22 +++++++++++++ server/controllers/api/index.js | 2 ++ server/initializers/checker.js | 2 +- server/initializers/constants.js | 3 ++ server/tests/api/config.js | 53 ++++++++++++++++++++++++++++++++ server/tests/api/index.js | 1 + server/tests/utils/config.js | 24 +++++++++++++++ 10 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 server/controllers/api/config.js create mode 100644 server/tests/api/config.js create mode 100644 server/tests/utils/config.js diff --git a/config/default.yaml b/config/default.yaml index 172bbef1e..a5ac15794 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -25,3 +25,6 @@ storage: admin: email: 'admin@example.com' + +signup: + enabled: false diff --git a/config/production.yaml.example b/config/production.yaml.example index cbfb0f46c..8e9d4b32d 100644 --- a/config/production.yaml.example +++ b/config/production.yaml.example @@ -26,3 +26,6 @@ storage: admin: email: 'admin@example.com' + +signup: + enabled: false diff --git a/config/test.yaml b/config/test.yaml index 493a23076..1a08d5ed1 100644 --- a/config/test.yaml +++ b/config/test.yaml @@ -7,3 +7,6 @@ webserver: database: hostname: 'localhost' port: 5432 + +signup: + enabled: true diff --git a/server/controllers/api/config.js b/server/controllers/api/config.js new file mode 100644 index 000000000..8154b6ad0 --- /dev/null +++ b/server/controllers/api/config.js @@ -0,0 +1,22 @@ +'use strict' + +const express = require('express') + +const constants = require('../../initializers/constants') + +const router = express.Router() + +router.get('/', getConfig) + +// Get the client credentials for the PeerTube front end +function getConfig (req, res, next) { + res.json({ + signup: { + enabled: constants.CONFIG.SIGNUP.ENABLED + } + }) +} + +// --------------------------------------------------------------------------- + +module.exports = router diff --git a/server/controllers/api/index.js b/server/controllers/api/index.js index f13ff922c..6edc089f4 100644 --- a/server/controllers/api/index.js +++ b/server/controllers/api/index.js @@ -7,6 +7,7 @@ const utils = require('../../helpers/utils') const router = express.Router() const clientsController = require('./clients') +const configController = require('./config') const podsController = require('./pods') const remoteController = require('./remote') const requestsController = require('./requests') @@ -14,6 +15,7 @@ const usersController = require('./users') const videosController = require('./videos') router.use('/clients', clientsController) +router.use('/config', configController) router.use('/pods', podsController) router.use('/remote', remoteController) router.use('/requests', requestsController) diff --git a/server/initializers/checker.js b/server/initializers/checker.js index 7adbbb37a..461a851fe 100644 --- a/server/initializers/checker.js +++ b/server/initializers/checker.js @@ -29,7 +29,7 @@ function checkMissedConfig () { 'webserver.https', 'webserver.hostname', 'webserver.port', 'database.hostname', 'database.port', 'database.suffix', 'database.username', 'database.password', 'storage.certs', 'storage.videos', 'storage.logs', 'storage.thumbnails', 'storage.previews', - 'admin.email' + 'admin.email', 'signup.enabled' ] const miss = [] diff --git a/server/initializers/constants.js b/server/initializers/constants.js index f9247e945..96321b211 100644 --- a/server/initializers/constants.js +++ b/server/initializers/constants.js @@ -61,6 +61,9 @@ const CONFIG = { }, ADMIN: { EMAIL: config.get('admin.email') + }, + SIGNUP: { + ENABLED: config.get('signup.enabled') } } CONFIG.WEBSERVER.URL = CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT diff --git a/server/tests/api/config.js b/server/tests/api/config.js new file mode 100644 index 000000000..08f955f2d --- /dev/null +++ b/server/tests/api/config.js @@ -0,0 +1,53 @@ +/* eslint-disable no-unused-expressions */ + +'use strict' + +const chai = require('chai') +const expect = chai.expect +const series = require('async/series') + +const serversUtils = require('../utils/servers') +const configUtils = require('../utils/config') + +describe('Test config', function () { + let server = null + + before(function (done) { + this.timeout(20000) + + series([ + function (next) { + serversUtils.flushTests(next) + }, + function (next) { + serversUtils.runServer(1, function (server1) { + server = server1 + next() + }) + } + ], done) + }) + + it('Should have a correct config', function (done) { + configUtils.getConfig(server.url, function (err, res) { + if (err) throw err + + const data = res.body + + expect(data.signup.enabled).to.be.truthy + + done() + }) + }) + + after(function (done) { + process.kill(-server.app.pid) + + // Keep the logs if the test failed + if (this.ok) { + serversUtils.flushTests(done) + } else { + done() + } + }) +}) diff --git a/server/tests/api/index.js b/server/tests/api/index.js index 11f49e1e2..dc6ef92ab 100644 --- a/server/tests/api/index.js +++ b/server/tests/api/index.js @@ -1,6 +1,7 @@ 'use strict' // Order of the tests we want to execute +require('./config') require('./check-params') require('./friends-basic') require('./users') diff --git a/server/tests/utils/config.js b/server/tests/utils/config.js new file mode 100644 index 000000000..0a507a60f --- /dev/null +++ b/server/tests/utils/config.js @@ -0,0 +1,24 @@ +'use strict' + +const request = require('supertest') + +const configsUtils = { + getConfig +} + +// ---------------------- Export functions -------------------- + +function getConfig (url, end) { + const path = '/api/v1/config' + + request(url) + .get(path) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) + .end(end) +} + +// --------------------------------------------------------------------------- + +module.exports = configsUtils -- 2.41.0