From a9635b49ef2d3471679c7a4bdccc05daab2ee559 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Tue, 6 Nov 2018 20:34:02 +0200 Subject: [PATCH] test(rest) config: add serve-once --- package.json | 1 + test/rest/config.js | 104 ++++++++++++++++++++------------------------ 2 files changed, 47 insertions(+), 58 deletions(-) diff --git a/package.json b/package.json index 40bc6de0..441f0103 100644 --- a/package.json +++ b/package.json @@ -206,6 +206,7 @@ "request": "^2.76.0", "rimraf": "^2.5.4", "scroll-into-view-if-needed": "^2.2.5", + "serve-once": "^1.2.1", "serviceworker-webpack-plugin": "^1.0.1", "sinon": "^7.0.0", "sinon-called-with-diff": "^3.0.0", diff --git a/test/rest/config.js b/test/rest/config.js index 57a993b6..e33a23a5 100644 --- a/test/rest/config.js +++ b/test/rest/config.js @@ -4,38 +4,23 @@ const path = require('path'); const os = require('os'); const test = require('tape'); -const {promisify} = require('es6-promisify'); -const pullout = require('pullout'); -const request = require('request'); const readjson = require('readjson'); const writejson = require('writejson'); +const cloudcmd = require('../..'); +const {request} = require('serve-once')(cloudcmd, { + config: { + auth: false, + }, +}); + const manageConfig = require('../../server/config'); -const {connect} = require('../before'); - -const warp = (fn, ...a) => (...b) => fn(...b, ...a); - -const _pullout = promisify(pullout); const pathConfig = path.join(os.homedir(), '.cloudcmd.json'); -const get = promisify((url, fn) => { - fn(null, request(url)); -}); - -const patch = promisify((url, json, fn) => { - fn(null, request.patch({url, json})); -}); - test('cloudcmd: rest: config: get', async (t) => { - const {port, done} = await connect(); - - const config = await get(`http://localhost:${port}/api/v1/config`) - .then(warp(_pullout, 'string')) - .then(JSON.parse) - .catch(console.error); - - await done(); + const {body} = await request.get('/api/v1/config'); + const config = JSON.parse(body); t.notOk(config.auth, 'should config.auth to be false'); t.end(); @@ -47,16 +32,20 @@ test('cloudcmd: rest: config: patch', async (t) => { configDialog, }; - const {port, done} = await connect({config}); + const options = { + config + }; + const json = { auth: false, }; - const result = await patch(`http://localhost:${port}/api/v1/config`, json) - .then(warp(_pullout, 'string')) - .catch(console.error); + const res = await request.patch('/api/v1/config', { + options, + body: json + }); - await done(); + const result = res.body; t.equal(result, 'config: ok("auth")', 'should patch config'); t.end(); @@ -67,18 +56,20 @@ test('cloudcmd: rest: config: patch: no configDialog', async (t) => { configDialog: false }; - const {port, done} = await connect({config}); - const json = { + const options = { + config, + }; + + const body = { ip: null }; - const result = await patch(`http://localhost:${port}/api/v1/config`, json) - .then(warp(_pullout, 'string')) - .catch(console.error); + const result = await request.patch(`/api/v1/config`, { + body, + options, + }); - await done(); - - t.equal(result, 'Config is disabled', 'should return error'); + t.equal(result.body, 'Config is disabled', 'should return error'); t.end(); }); @@ -87,41 +78,38 @@ test('cloudcmd: rest: config: patch: no configDialog: statusCode', async (t) => configDialog: false }; - const {port, done} = await connect({config}); - const json = { + const options = { + config, + }; + + const body = { ip: null, }; - const result = await patch(`http://localhost:${port}/api/v1/config`, json) - .catch((error) => { - console.log(error); - }); - - result.on('response', async (response) => { - manageConfig('configDialog', true); - - await done(); - t.equal(response.statusCode, 404); - t.end(); + const response = await request.patch(`/api/v1/config`, { + body, + options, }); + + manageConfig('configDialog', true); + + t.equal(response.status, 404); + t.end(); }); test('cloudcmd: rest: config: patch: save config', async (t) => { - const {port, done} = await connect(); - const json = { + const body = { editor: 'dword', }; const originalConfig = readjson.sync.try(pathConfig); - await patch(`http://localhost:${port}/api/v1/config`, json) - .then(warp(_pullout, 'string')) - .catch(console.error); - + await request.patch(`/api/v1/config`, { + body, + }); + const config = readjson.sync(pathConfig); - await done(); - t.equal(config.editor, 'dword', 'should change config file on patch'); t.end();