test(rest) config: add serve-once

This commit is contained in:
coderaiser 2018-11-06 20:34:02 +02:00
parent d4d9a5d30f
commit a9635b49ef
2 changed files with 47 additions and 58 deletions

View file

@ -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",

View file

@ -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();