Add test cases for context admin API

This commit is contained in:
Hiroki Uchikawa 2019-01-18 18:55:54 +09:00
parent 543519d055
commit f98f4085bf
1 changed files with 176 additions and 183 deletions

View File

@ -19,22 +19,15 @@ var request = require('supertest');
var express = require('express'); var express = require('express');
var bodyParser = require('body-parser'); var bodyParser = require('body-parser');
var sinon = require('sinon'); var sinon = require('sinon');
var when = require('when');
var NR_TEST_UTILS = require("nr-test-utils"); var NR_TEST_UTILS = require("nr-test-utils");
var context = NR_TEST_UTILS.require("@node-red/editor-api/lib/admin/context"); var context = NR_TEST_UTILS.require("@node-red/editor-api/lib/admin/context");
// var Context = require("../../../../red/runtime/nodes/context");
// var Util = require("../../../../red/runtime/util");
describe("api/admin/context", function () { describe("api/admin/context", function () {
it.skip("NEEDS TESTS WRITING",function() {});
});
/*
var app = undefined; var app = undefined;
before(function (done) { before(function () {
var node_context = undefined;
app = express(); app = express();
app.use(bodyParser.json()); app.use(bodyParser.json());
app.get("/context/:scope(global)", context.get); app.get("/context/:scope(global)", context.get);
@ -42,196 +35,196 @@ describe("api/admin/context", function() {
app.get("/context/:scope(node|flow)/:id", context.get); app.get("/context/:scope(node|flow)/:id", context.get);
app.get("/context/:scope(node|flow)/:id/*", context.get); app.get("/context/:scope(node|flow)/:id/*", context.get);
context.init({ app.delete("/context/:scope(global)/*", context.delete);
settings: { app.delete("/context/:scope(node|flow)/:id/*", context.delete);
}, });
log:{warn:function(){},_:function(){},audit:function(){}},
nodes: { describe("get", function () {
listContextStores: Context.listStores, var gContext = {
getContext: Context.get, default: { abc: { msg: '111', format: 'number' } },
getNode: function(id) { file: { abc: { msg: '222', format: 'number' } }
if (id === 'NID') {
return {
id: 'NID',
context: function () {
return node_context;
}
}; };
} var fContext = {
return null; default: { bool: { msg: 'true', format: 'boolean' } },
} file: { string: { msg: 'aaaa', format: 'string[7]' } }
}, };
util: Util var nContext = { msg: "1", format: "number" };
}); var stub = sinon.stub();
Context.init({ before(function () {
contextStorage: { context.init({
memory0: { context: {
module: "memory" getValue: stub
},
memory1: {
module: "memory"
}
} }
}); });
Context.load().then(function () {
var ctx = Context.get("NID", "FID");
node_context = ctx;
ctx.set("foo", "n_v00", "memory0");
ctx.set("bar", "n_v01", "memory0");
ctx.set("baz", "n_v10", "memory1");
ctx.set("bar", "n_v11", "memory1");
ctx.flow.set("foo", "f_v00", "memory0");
ctx.flow.set("bar", "f_v01", "memory0");
ctx.flow.set("baz", "f_v10", "memory1");
ctx.flow.set("bar", "f_v11", "memory1");
ctx.global.set("foo", "g_v00", "memory0");
ctx.global.set("bar", "g_v01", "memory0");
ctx.global.set("baz", "g_v10", "memory1");
ctx.global.set("bar", "g_v11", "memory1");
done();
}); });
afterEach(function () {
stub.reset();
}); });
after(function () { it('should call context.getValue to get global contexts', function (done) {
Context.clean({allNodes:{}}); stub.returns(Promise.resolve(gContext));
Context.close();
});
function check_mem(body, mem, name, val) {
var mem0 = body[mem];
mem0.should.have.property(name);
mem0[name].should.deepEqual(val);
}
function check_scope(scope, prefix, id) {
describe('# '+scope, function () {
var xid = id ? ("/"+id) : "";
it('should return '+scope+' contexts', function (done) {
request(app) request(app)
.get('/context/'+scope+xid) .get('/context/global')
.set('Accept', 'application/json') .set('Accept', 'application/json')
.expect(200) .expect(200)
.end(function (err, res) { .end(function (err, res) {
if (err) { if (err) {
return done(err); return done(err);
} }
stub.args[0][0].should.have.property('user', undefined);
stub.args[0][0].should.have.property('scope', 'global');
stub.args[0][0].should.have.property('id', undefined);
stub.args[0][0].should.have.property('key', undefined);
stub.args[0][0].should.have.property('store', undefined);
var body = res.body; var body = res.body;
body.should.have.key('memory0', 'memory1'); body.should.eql(gContext);
check_mem(body, 'memory0',
'foo', {msg:prefix+'_v00', format:'string[5]'});
check_mem(body, 'memory0',
'bar', {msg:prefix+'_v01', format:'string[5]'});
check_mem(body, 'memory1',
'baz', {msg:prefix+'_v10', format:'string[5]'});
check_mem(body, 'memory1',
'bar', {msg:prefix+'_v11', format:'string[5]'});
done(); done();
}); });
}); });
it('should return a value from default '+scope+' context', function (done) { it('should call context.getValue to get flow contexts', function (done) {
stub.returns(Promise.resolve(fContext));
request(app) request(app)
.get('/context/'+scope+xid+'/foo') .get('/context/flow/1234/')
.set('Accept', 'application/json') .set('Accept', 'application/json')
.expect(200) .expect(200)
.end(function (err, res) { .end(function (err, res) {
if (err) { if (err) {
return done(err); return done(err);
} }
stub.args[0][0].should.have.property('user', undefined);
stub.args[0][0].should.have.property('scope', 'flow');
stub.args[0][0].should.have.property('id', '1234');
stub.args[0][0].should.have.property('key', undefined);
stub.args[0][0].should.have.property('store', undefined);
var body = res.body; var body = res.body;
body.should.deepEqual({msg: prefix+'_v00', format: 'string[5]'}); body.should.eql(fContext);
done(); done();
}); });
}); });
it('should return a value from specified '+scope+' context', function (done) { it('should call context.getValue to get a node context', function (done) {
stub.returns(Promise.resolve(nContext));
request(app) request(app)
.get('/context/'+scope+xid+'/bar?store=memory1') .get('/context/node/5678/foo?store=file')
.set('Accept', 'application/json') .set('Accept', 'application/json')
.expect(200) .expect(200)
.end(function (err, res) { .end(function (err, res) {
if (err) { if (err) {
return done(err); return done(err);
} }
stub.args[0][0].should.have.property('user', undefined);
stub.args[0][0].should.have.property('scope', 'node');
stub.args[0][0].should.have.property('id', '5678');
stub.args[0][0].should.have.property('key', 'foo');
stub.args[0][0].should.have.property('store', 'file');
var body = res.body; var body = res.body;
body.should.deepEqual({msg: prefix+'_v11', format: 'string[5]', store: 'memory1'}); body.should.eql(nContext);
done(); done();
}); });
}); });
it('should return specified '+scope+' store', function (done) { it('should handle error which context.getValue causes', function (done) {
stub.returns(Promise.reject('error'));
request(app) request(app)
.get('/context/'+scope+xid+'?store=memory1') .get('/context/global')
.set('Accept', 'application/json') .set('Accept', 'application/json')
.expect(200) .expect(400)
.end(function (err, res) { .end(function (err, res) {
if (err) { if (err) {
return done(err); return done(err);
} }
var body = res.body; res.body.should.has.a.property('code', 'unexpected_error');
body.should.deepEqual({ res.body.should.has.a.property('message', 'error');
memory1: {
baz: { msg: prefix+'_v10', format: 'string[5]' },
bar: { msg: prefix+'_v11', format: 'string[5]' }
}
});
done(); done();
}); });
}); });
});
it('should return undefined for unknown key of default '+scope+' store', function (done) { describe("delete", function () {
var stub = sinon.stub();
before(function () {
context.init({
context: {
delete: stub
}
});
});
afterEach(function () {
stub.reset();
});
it('should call context.delete to delete a global context', function (done) {
stub.returns(Promise.resolve());
request(app) request(app)
.get('/context/'+scope+xid+'/unknown') .delete('/context/global/abc?store=default')
.set('Accept', 'application/json') .expect(204)
.expect(200)
.end(function (err, res) { .end(function (err, res) {
if (err) { if (err) {
return done(err); return done(err);
} }
var body = res.body; stub.args[0][0].should.have.property('user', undefined);
body.should.deepEqual({msg:'(undefined)', format:'undefined'}); stub.args[0][0].should.have.property('scope', 'global');
stub.args[0][0].should.have.property('id', undefined);
stub.args[0][0].should.have.property('key', 'abc');
stub.args[0][0].should.have.property('store', 'default');
done(); done();
}); });
}); });
it('should cause error for unknown '+scope+' store', function (done) { it('should call context.delete to delete a flow context', function (done) {
stub.returns(Promise.resolve());
request(app) request(app)
.get('/context/'+scope+xid+'?store=unknown') .delete('/context/flow/1234/abc?store=file')
.set('Accept', 'application/json') .expect(204)
.expect(200)
.end(function (err, res) { .end(function (err, res) {
if (err) { if (err) {
return done(); return done(err);
} }
done("unexpected"); stub.args[0][0].should.have.property('user', undefined);
stub.args[0][0].should.have.property('scope', 'flow');
stub.args[0][0].should.have.property('id', '1234');
stub.args[0][0].should.have.property('key', 'abc');
stub.args[0][0].should.have.property('store', 'file');
done();
}); });
}); });
});
}
check_scope("global", "g", undefined); it('should call context.delete to delete a node context', function (done) {
check_scope("node", "n", "NID"); stub.returns(Promise.resolve());
check_scope("flow", "f", "FID");
describe("# errors", function () {
it('should cause error for unknown scope', function (done) {
request(app) request(app)
.get('/context/scope') .delete('/context/node/5678/foo?store=file')
.set('Accept', 'application/json') .expect(204)
.expect(200)
.end(function (err, res) { .end(function (err, res) {
if (err) { if (err) {
return done(); return done(err);
} }
done("unexpected"); stub.args[0][0].should.have.property('user', undefined);
stub.args[0][0].should.have.property('scope', 'node');
stub.args[0][0].should.have.property('id', '5678');
stub.args[0][0].should.have.property('key', 'foo');
stub.args[0][0].should.have.property('store', 'file');
done();
}); });
}); });
it('should handle error which context.delete causes', function (done) {
stub.returns(Promise.reject('error'));
request(app)
.delete('/context/global/abc?store=default')
.expect(400)
.end(function (err, res) {
if (err) {
return done(err);
}
res.body.should.has.a.property('code', 'unexpected_error');
res.body.should.has.a.property('message', 'error');
done();
});
});
}); });
}); });
*/