diff --git a/server/config.js b/server/config.js index 9342466d..88a7407b 100644 --- a/server/config.js +++ b/server/config.js @@ -29,6 +29,8 @@ const readjsonSync = (name) => jju.parse(fs.readFileSync(name, 'utf8'), { mode: 'json' }); +const key = (a) => Object.keys(a).pop(); + let config; let error = tryCatch(() => { config = readjsonSync(ConfigHome); @@ -95,12 +97,13 @@ function connection(socket) { return socket.emit('err', 'Error: Wrong data type!'); cryptoPass(json); + traverse(json); save((error) => { if (error) return socket.emit('err', error.message); - const data = traverse(json); + const data = CloudFunc.formatMsg('config', key(json)); socket.broadcast.send(json); socket.send(json); @@ -158,14 +161,15 @@ function patch(req, res, callback) { if (error) return callback(error); - + cryptoPass(json); + traverse(json); save((error) => { if (error) return ponse.sendError(error, options); - const data = traverse(json); + const data = CloudFunc.formatMsg('config', key(json)); ponse.send(data, options); }); @@ -173,14 +177,9 @@ function patch(req, res, callback) { } function traverse(json) { - let data; - Object.keys(json).forEach((name) => { - data = CloudFunc.formatMsg('config', name); manage(name, json[name]); }); - - return data; } function cryptoPass(json) { diff --git a/test/rest/config.js b/test/rest/config.js index c2ab27f1..2e340fd5 100644 --- a/test/rest/config.js +++ b/test/rest/config.js @@ -1,17 +1,24 @@ 'use strict'; +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 manageConfig = require('../../server/config'); +const readjson = require('readjson'); +const writejson = require('writejson'); +const manageConfig = require('../../server/config'); const before = 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)); }); @@ -114,7 +121,7 @@ test('cloudcmd: rest: config: enabled by default', (t) => { patch(`http://localhost:${port}/api/v1/config`, json) .then(warp(_pullout, 'string')) .then((result) => { - t.equal(result, 'config: ok("auth")', 'should patch config'); + t.equal(result, 'config: ok("auth")', 'should send message'); t.end(); after(); }) @@ -124,3 +131,29 @@ test('cloudcmd: rest: config: enabled by default', (t) => { }); }); +test('cloudcmd: rest: config: patch: save config', (t) => { + before({}, (port, after) => { + const json = { + editor: 'dword', + }; + + let originalConfig = readjson.sync.try(pathConfig); + + patch(`http://localhost:${port}/api/v1/config`, json) + .then(warp(_pullout, 'string')) + .then(() => { + const config = readjson.sync(pathConfig); + t.equal(config.editor, 'dword', 'should change config file on patch'); + t.end(); + + if (originalConfig) + writejson.sync(pathConfig, originalConfig); + + after(); + }) + .catch((error) => { + console.log(error); + }); + }); +}); +