1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00
node-red/test/unit/@node-red/editor-api/lib/admin/context_spec.js

231 lines
8.8 KiB
JavaScript
Raw Permalink Normal View History

2018-06-28 18:00:17 +02:00
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var should = require("should");
var request = require('supertest');
var express = require('express');
var bodyParser = require('body-parser');
var sinon = require('sinon');
2018-08-20 17:17:24 +02:00
var NR_TEST_UTILS = require("nr-test-utils");
var context = NR_TEST_UTILS.require("@node-red/editor-api/lib/admin/context");
2018-06-28 18:00:17 +02:00
2019-01-18 10:55:54 +01:00
describe("api/admin/context", function () {
2018-07-21 05:00:54 +02:00
var app = undefined;
2019-01-18 10:55:54 +01:00
before(function () {
2018-07-21 05:00:54 +02:00
app = express();
app.use(bodyParser.json());
app.get("/context/:scope(global)", context.get);
app.get("/context/:scope(global)/*", context.get);
app.get("/context/:scope(node|flow)/:id", context.get);
app.get("/context/:scope(node|flow)/:id/*", context.get);
2019-01-18 10:55:54 +01:00
app.delete("/context/:scope(global)/*", context.delete);
app.delete("/context/:scope(node|flow)/:id/*", context.delete);
});
2019-01-18 10:55:54 +01:00
describe("get", function () {
var gContext = {
default: { abc: { msg: '111', format: 'number' } },
file: { abc: { msg: '222', format: 'number' } }
};
var fContext = {
default: { bool: { msg: 'true', format: 'boolean' } },
file: { string: { msg: 'aaaa', format: 'string[7]' } }
};
var nContext = { msg: "1", format: "number" };
var stub = sinon.stub();
before(function () {
context.init({
context: {
getValue: stub
2018-07-21 05:00:54 +02:00
}
2019-01-18 10:55:54 +01:00
});
2018-07-21 05:00:54 +02:00
});
2019-01-18 10:55:54 +01:00
afterEach(function () {
stub.reset();
2018-07-21 05:00:54 +02:00
});
2019-01-18 10:55:54 +01:00
it('should call context.getValue to get global contexts', function (done) {
stub.returns(Promise.resolve(gContext));
request(app)
.get('/context/global')
.set('Accept', 'application/json')
.expect(200)
.end(function (err, res) {
if (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;
body.should.eql(gContext);
done();
});
});
2018-07-21 05:00:54 +02:00
2019-01-18 10:55:54 +01:00
it('should call context.getValue to get flow contexts', function (done) {
stub.returns(Promise.resolve(fContext));
request(app)
.get('/context/flow/1234/')
.set('Accept', 'application/json')
.expect(200)
.end(function (err, res) {
if (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;
body.should.eql(fContext);
done();
});
});
2018-07-21 05:00:54 +02:00
2019-01-18 10:55:54 +01:00
it('should call context.getValue to get a node context', function (done) {
stub.returns(Promise.resolve(nContext));
request(app)
.get('/context/node/5678/foo?store=file')
.set('Accept', 'application/json')
.expect(200)
.end(function (err, res) {
if (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;
body.should.eql(nContext);
done();
});
});
2018-07-21 05:00:54 +02:00
2019-01-18 10:55:54 +01:00
it('should handle error which context.getValue causes', function (done) {
stub.returns(Promise.reject('error'));
request(app)
.get('/context/global')
.set('Accept', 'application/json')
.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();
});
});
});
2018-07-21 05:00:54 +02:00
2019-01-18 10:55:54 +01:00
describe("delete", function () {
var stub = sinon.stub();
2018-07-21 05:00:54 +02:00
2019-01-18 10:55:54 +01:00
before(function () {
context.init({
context: {
delete: stub
}
2018-07-21 05:00:54 +02:00
});
2019-01-18 10:55:54 +01:00
});
2018-07-21 05:00:54 +02:00
2019-01-18 10:55:54 +01:00
afterEach(function () {
stub.reset();
});
2018-07-21 05:00:54 +02:00
2019-01-18 10:55:54 +01:00
it('should call context.delete to delete a global context', function (done) {
stub.returns(Promise.resolve());
request(app)
.delete('/context/global/abc?store=default')
.expect(204)
.end(function (err, res) {
if (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', 'abc');
stub.args[0][0].should.have.property('store', 'default');
done();
});
2018-07-21 05:00:54 +02:00
});
2019-01-18 10:55:54 +01:00
it('should call context.delete to delete a flow context', function (done) {
stub.returns(Promise.resolve());
request(app)
.delete('/context/flow/1234/abc?store=file')
.expect(204)
.end(function (err, res) {
if (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', 'abc');
stub.args[0][0].should.have.property('store', 'file');
done();
});
});
2018-07-21 05:00:54 +02:00
2019-01-18 10:55:54 +01:00
it('should call context.delete to delete a node context', function (done) {
stub.returns(Promise.resolve());
2018-07-21 05:00:54 +02:00
request(app)
2019-01-18 10:55:54 +01:00
.delete('/context/node/5678/foo?store=file')
.expect(204)
2018-07-21 05:00:54 +02:00
.end(function (err, res) {
if (err) {
2019-01-18 10:55:54 +01:00
return done(err);
2018-07-21 05:00:54 +02:00
}
2019-01-18 10:55:54 +01:00
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();
2018-07-21 05:00:54 +02:00
});
});
2019-01-18 10:55:54 +01:00
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();
});
});
2018-07-21 05:00:54 +02:00
});
2018-06-28 18:00:17 +02:00
});