From fd206f0b2d7e5c8e00e2817266d90ec54f79e1da Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 17 Jan 2018 10:32:03 +0100 Subject: Add ability to update some configuration keys --- server/tests/api/check-params/config.ts | 152 +++++++++++++++++++++++++++++++ server/tests/api/server/config.ts | 112 ++++++++++++++++++++++- server/tests/api/videos/single-server.ts | 3 +- server/tests/utils/miscs/miscs.ts | 23 ----- server/tests/utils/requests/requests.ts | 2 +- server/tests/utils/server/config.ts | 41 ++++++++- server/tests/utils/videos/videos.ts | 5 +- 7 files changed, 309 insertions(+), 29 deletions(-) create mode 100644 server/tests/api/check-params/config.ts (limited to 'server/tests') diff --git a/server/tests/api/check-params/config.ts b/server/tests/api/check-params/config.ts new file mode 100644 index 000000000..59a0c3049 --- /dev/null +++ b/server/tests/api/check-params/config.ts @@ -0,0 +1,152 @@ +/* tslint:disable:no-unused-expression */ + +import { omit } from 'lodash' +import 'mocha' +import { CustomConfig } from '../../../../shared/models/config/custom-config.model' + +import { + createUser, flushTests, killallServers, makeDeleteRequest, makeGetRequest, makePutBodyRequest, runServer, ServerInfo, + setAccessTokensToServers, userLogin +} from '../../utils' + +describe('Test config API validators', function () { + const path = '/api/v1/config/custom' + let server: ServerInfo + let userAccessToken: string + const updateParams: CustomConfig = { + cache: { + previews: { + size: 2 + } + }, + signup: { + enabled: false, + limit: 5 + }, + admin: { + email: 'superadmin1@example.com' + }, + user: { + videoQuota: 5242881 + }, + transcoding: { + enabled: true, + threads: 1, + resolutions: { + '240p': false, + '360p': true, + '480p': true, + '720p': false, + '1080p': false + } + } + } + + // --------------------------------------------------------------- + + before(async function () { + this.timeout(20000) + + await flushTests() + server = await runServer(1) + + await setAccessTokensToServers([ server ]) + + const user = { + username: 'user1', + password: 'password' + } + await createUser(server.url, server.accessToken, user.username, user.password) + userAccessToken = await userLogin(server, user) + }) + + describe('When getting the configuration', function () { + it('Should fail without token', async function () { + await makeGetRequest({ + url: server.url, + path, + statusCodeExpected: 401 + }) + }) + + it('Should fail if the user is not an administrator', async function () { + await makeGetRequest({ + url: server.url, + path, + token: userAccessToken, + statusCodeExpected: 403 + }) + }) + }) + + describe('When updating the configuration', function () { + it('Should fail without token', async function () { + await makePutBodyRequest({ + url: server.url, + path, + fields: updateParams, + statusCodeExpected: 401 + }) + }) + + it('Should fail if the user is not an administrator', async function () { + await makePutBodyRequest({ + url: server.url, + path, + fields: updateParams, + token: userAccessToken, + statusCodeExpected: 403 + }) + }) + + it('Should fail if it misses a key', async function () { + const newUpdateParams = omit(updateParams, 'admin.email') + + await makePutBodyRequest({ + url: server.url, + path, + fields: newUpdateParams, + token: server.accessToken, + statusCodeExpected: 400 + }) + }) + + it('Should success with the correct parameters', async function () { + await makePutBodyRequest({ + url: server.url, + path, + fields: updateParams, + token: server.accessToken, + statusCodeExpected: 200 + }) + }) + }) + + describe('When deleting the configuration', function () { + it('Should fail without token', async function () { + await makeDeleteRequest({ + url: server.url, + path, + statusCodeExpected: 401 + }) + }) + + it('Should fail if the user is not an administrator', async function () { + await makeDeleteRequest({ + url: server.url, + path, + token: userAccessToken, + statusCodeExpected: 403 + }) + }) + }) + + after(async function () { + killallServers([ server ]) + + // Keep the logs if the test failed + if (this['ok']) { + await flushTests() + } + }) +}) diff --git a/server/tests/api/server/config.ts b/server/tests/api/server/config.ts index e8846c8db..8c1389e7f 100644 --- a/server/tests/api/server/config.ts +++ b/server/tests/api/server/config.ts @@ -2,13 +2,14 @@ import 'mocha' import * as chai from 'chai' +import { deleteCustomConfig, killallServers, reRunServer } from '../../utils' const expect = chai.expect import { getConfig, flushTests, runServer, - registerUser + registerUser, getCustomConfig, setAccessTokensToServers, updateCustomConfig } from '../../utils/index' describe('Test config', function () { @@ -19,6 +20,7 @@ describe('Test config', function () { await flushTests() server = await runServer(1) + await setAccessTokensToServers([ server ]) }) it('Should have a correct config on a server with registration enabled', async function () { @@ -43,6 +45,114 @@ describe('Test config', function () { expect(data.signup.allowed).to.be.false }) + it('Should get the customized configuration', async function () { + const res = await getCustomConfig(server.url, server.accessToken) + const data = res.body + + expect(data.cache.previews.size).to.equal(1) + expect(data.signup.enabled).to.be.true + expect(data.signup.limit).to.equal(4) + expect(data.admin.email).to.equal('admin1@example.com') + expect(data.user.videoQuota).to.equal(5242880) + expect(data.transcoding.enabled).to.be.false + expect(data.transcoding.threads).to.equal(2) + expect(data.transcoding.resolutions['240p']).to.be.true + expect(data.transcoding.resolutions['360p']).to.be.true + expect(data.transcoding.resolutions['480p']).to.be.true + expect(data.transcoding.resolutions['720p']).to.be.true + expect(data.transcoding.resolutions['1080p']).to.be.true + }) + + it('Should update the customized configuration', async function () { + const newCustomConfig = { + cache: { + previews: { + size: 2 + } + }, + signup: { + enabled: false, + limit: 5 + }, + admin: { + email: 'superadmin1@example.com' + }, + user: { + videoQuota: 5242881 + }, + transcoding: { + enabled: true, + threads: 1, + resolutions: { + '240p': false, + '360p': true, + '480p': true, + '720p': false, + '1080p': false + } + } + } + await updateCustomConfig(server.url, server.accessToken, newCustomConfig) + + const res = await getCustomConfig(server.url, server.accessToken) + const data = res.body + + expect(data.cache.previews.size).to.equal(2) + expect(data.signup.enabled).to.be.false + expect(data.signup.limit).to.equal(5) + expect(data.admin.email).to.equal('superadmin1@example.com') + expect(data.user.videoQuota).to.equal(5242881) + expect(data.transcoding.enabled).to.be.true + expect(data.transcoding.threads).to.equal(1) + expect(data.transcoding.resolutions['240p']).to.be.false + expect(data.transcoding.resolutions['360p']).to.be.true + expect(data.transcoding.resolutions['480p']).to.be.true + expect(data.transcoding.resolutions['720p']).to.be.false + expect(data.transcoding.resolutions['1080p']).to.be.false + }) + + it('Should have the configuration updated after a restart', async function () { + killallServers([ server ]) + + await reRunServer(server) + + const res = await getCustomConfig(server.url, server.accessToken) + const data = res.body + + expect(data.cache.previews.size).to.equal(2) + expect(data.signup.enabled).to.be.false + expect(data.signup.limit).to.equal(5) + expect(data.admin.email).to.equal('superadmin1@example.com') + expect(data.user.videoQuota).to.equal(5242881) + expect(data.transcoding.enabled).to.be.true + expect(data.transcoding.threads).to.equal(1) + expect(data.transcoding.resolutions['240p']).to.be.false + expect(data.transcoding.resolutions['360p']).to.be.true + expect(data.transcoding.resolutions['480p']).to.be.true + expect(data.transcoding.resolutions['720p']).to.be.false + expect(data.transcoding.resolutions['1080p']).to.be.false + }) + + it('Should remove the custom configuration', async function () { + await deleteCustomConfig(server.url, server.accessToken) + + const res = await getCustomConfig(server.url, server.accessToken) + const data = res.body + + expect(data.cache.previews.size).to.equal(1) + expect(data.signup.enabled).to.be.true + expect(data.signup.limit).to.equal(4) + expect(data.admin.email).to.equal('admin1@example.com') + expect(data.user.videoQuota).to.equal(5242880) + expect(data.transcoding.enabled).to.be.false + expect(data.transcoding.threads).to.equal(2) + expect(data.transcoding.resolutions['240p']).to.be.true + expect(data.transcoding.resolutions['360p']).to.be.true + expect(data.transcoding.resolutions['480p']).to.be.true + expect(data.transcoding.resolutions['720p']).to.be.true + expect(data.transcoding.resolutions['1080p']).to.be.true + }) + after(async function () { process.kill(-server.app.pid) diff --git a/server/tests/api/videos/single-server.ts b/server/tests/api/videos/single-server.ts index 0a0c95750..ca20f39a0 100644 --- a/server/tests/api/videos/single-server.ts +++ b/server/tests/api/videos/single-server.ts @@ -5,9 +5,10 @@ import { keyBy } from 'lodash' import 'mocha' import { join } from 'path' import { VideoPrivacy } from '../../../../shared/models/videos' +import { readdirPromise } from '../../../helpers/core-utils' import { completeVideoCheck, flushTests, getVideo, getVideoCategories, getVideoLanguages, getVideoLicences, getVideoPrivacies, - getVideosList, getVideosListPagination, getVideosListSort, killallServers, rateVideo, readdirPromise, removeVideo, runServer, searchVideo, + getVideosList, getVideosListPagination, getVideosListSort, killallServers, rateVideo, removeVideo, runServer, searchVideo, searchVideoWithPagination, searchVideoWithSort, ServerInfo, setAccessTokensToServers, testVideoImage, updateVideo, uploadVideo, viewVideo } from '../../utils' diff --git a/server/tests/utils/miscs/miscs.ts b/server/tests/utils/miscs/miscs.ts index 2c51d1f0a..2aac37791 100644 --- a/server/tests/utils/miscs/miscs.ts +++ b/server/tests/utils/miscs/miscs.ts @@ -1,5 +1,4 @@ import * as WebTorrent from 'webtorrent' -import { readFile, readdir } from 'fs' let webtorrent = new WebTorrent() @@ -7,26 +6,6 @@ function immutableAssign (target: T, source: U) { return Object.assign<{}, T, U>({}, target, source) } -function readFilePromise (path: string) { - return new Promise((res, rej) => { - readFile(path, (err, data) => { - if (err) return rej(err) - - return res(data) - }) - }) -} - -function readdirPromise (path: string) { - return new Promise((res, rej) => { - readdir(path, (err, files) => { - if (err) return rej(err) - - return res(files) - }) - }) -} - // Default interval -> 5 minutes function dateIsValid (dateString: string, interval = 300000) { const dateToCheck = new Date(dateString) @@ -48,8 +27,6 @@ function webtorrentAdd (torrent: string, refreshWebTorrent = false) { // --------------------------------------------------------------------------- export { - readFilePromise, - readdirPromise, dateIsValid, wait, webtorrentAdd, diff --git a/server/tests/utils/requests/requests.ts b/server/tests/utils/requests/requests.ts index eb02cf9e6..840072430 100644 --- a/server/tests/utils/requests/requests.ts +++ b/server/tests/utils/requests/requests.ts @@ -99,7 +99,7 @@ function makePostBodyRequest (options: { function makePutBodyRequest (options: { url: string, path: string, - token: string, + token?: string, fields: { [ fieldName: string ]: any }, statusCodeExpected?: number }) { diff --git a/server/tests/utils/server/config.ts b/server/tests/utils/server/config.ts index d09c19c60..b6905757a 100644 --- a/server/tests/utils/server/config.ts +++ b/server/tests/utils/server/config.ts @@ -1,4 +1,6 @@ import * as request from 'supertest' +import { makeDeleteRequest, makeGetRequest, makePutBodyRequest } from '../' +import { CustomConfig } from '../../../../shared/models/config/custom-config.model' function getConfig (url: string) { const path = '/api/v1/config' @@ -10,8 +12,45 @@ function getConfig (url: string) { .expect('Content-Type', /json/) } +function getCustomConfig (url: string, token: string, statusCodeExpected = 200) { + const path = '/api/v1/config/custom' + + return makeGetRequest({ + url, + token, + path, + statusCodeExpected + }) +} + +function updateCustomConfig (url: string, token: string, newCustomConfig: CustomConfig, statusCodeExpected = 200) { + const path = '/api/v1/config/custom' + + return makePutBodyRequest({ + url, + token, + path, + fields: newCustomConfig, + statusCodeExpected + }) +} + +function deleteCustomConfig (url: string, token: string, statusCodeExpected = 200) { + const path = '/api/v1/config/custom' + + return makeDeleteRequest({ + url, + token, + path, + statusCodeExpected + }) +} + // --------------------------------------------------------------------------- export { - getConfig + getConfig, + getCustomConfig, + updateCustomConfig, + deleteCustomConfig } diff --git a/server/tests/utils/videos/videos.ts b/server/tests/utils/videos/videos.ts index dc1327215..095d4e29d 100644 --- a/server/tests/utils/videos/videos.ts +++ b/server/tests/utils/videos/videos.ts @@ -5,8 +5,9 @@ import { readFile } from 'fs' import * as parseTorrent from 'parse-torrent' import { extname, isAbsolute, join } from 'path' import * as request from 'supertest' -import { getMyUserInformation, makeGetRequest, readFilePromise, ServerInfo } from '../' +import { getMyUserInformation, makeGetRequest, ServerInfo } from '../' import { VideoPrivacy } from '../../../../shared/models/videos' +import { readFileBufferPromise } from '../../../helpers/core-utils' import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../initializers' import { dateIsValid, webtorrentAdd } from '../index' @@ -210,7 +211,7 @@ async function testVideoImage (url: string, imageName: string, imagePath: string .get(imagePath) .expect(200) - const data = await readFilePromise(join(__dirname, '..', '..', 'api', 'fixtures', imageName + extension)) + const data = await readFileBufferPromise(join(__dirname, '..', '..', 'api', 'fixtures', imageName + extension)) return data.equals(res.body) } else { -- cgit v1.2.3