1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Merge pull request #4298 from node-red/4295-urlencode-context-api

Handle non-url-safe chars in context api
This commit is contained in:
Nick O'Leary 2023-09-05 09:43:17 +01:00 committed by GitHub
commit 261998201b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 4 deletions

View File

@ -218,11 +218,11 @@ RED.sidebar.context = (function() {
var obj = $(propRow.children()[0]); var obj = $(propRow.children()[0]);
obj.text(k); obj.text(k);
var tools = $('<span class="button-group"></span>'); var tools = $('<span class="button-group"></span>');
const urlSafeK = encodeURIComponent(k)
var refreshItem = $('<button class="red-ui-button red-ui-button-small"><i class="fa fa-refresh"></i></button>').appendTo(tools).on("click", function(e) { var refreshItem = $('<button class="red-ui-button red-ui-button-small"><i class="fa fa-refresh"></i></button>').appendTo(tools).on("click", function(e) {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
$.getJSON(baseUrl+"/"+k+"?store="+v.store, function(data) { $.getJSON(baseUrl+"/"+urlSafeK+"?store="+v.store, function(data) {
if (data.msg !== payload || data.format !== format) { if (data.msg !== payload || data.format !== format) {
payload = data.msg; payload = data.msg;
format = data.format; format = data.format;
@ -258,11 +258,12 @@ RED.sidebar.context = (function() {
$('<button class="red-ui-button primary" data-i18n="common.label.delete"></button>').appendTo(bg).on("click", function(e) { $('<button class="red-ui-button primary" data-i18n="common.label.delete"></button>').appendTo(bg).on("click", function(e) {
e.preventDefault(); e.preventDefault();
popover.close(); popover.close();
const urlSafeK = encodeURIComponent(k)
$.ajax({ $.ajax({
url: baseUrl+"/"+k+"?store="+v.store, url: baseUrl+"/"+urlSafeK+"?store="+v.store,
type: "DELETE" type: "DELETE"
}).done(function(data,textStatus,xhr) { }).done(function(data,textStatus,xhr) {
$.getJSON(baseUrl+"/"+k+"?store="+v.store, function(data) { $.getJSON(baseUrl+"/"+urlSafeK+"?store="+v.store, function(data) {
if (data.format === 'undefined') { if (data.format === 'undefined') {
propRow.remove(); propRow.remove();
if (container.children().length === 0) { if (container.children().length === 0) {

View File

@ -126,6 +126,26 @@ describe("api/admin/context", function () {
}); });
}); });
it('should call context.getValue to get a node context value - url unsafe keyname', function (done) {
stub.returns(Promise.resolve(nContext));
request(app)
.get('/context/node/5678/foo%23123?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#123');
stub.args[0][0].should.have.property('store', 'file');
var body = res.body;
body.should.eql(nContext);
done();
});
});
it('should handle error which context.getValue causes', function (done) { it('should handle error which context.getValue causes', function (done) {
var stubbedResult = Promise.reject('error'); var stubbedResult = Promise.reject('error');
stubbedResult.catch(function() {}); stubbedResult.catch(function() {});
@ -214,6 +234,24 @@ describe("api/admin/context", function () {
}); });
}); });
it('should call context.delete to delete a node context - url unsafe keyname', function (done) {
stub.returns(Promise.resolve());
request(app)
.delete('/context/node/5678/foo%23123?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', 'node');
stub.args[0][0].should.have.property('id', '5678');
stub.args[0][0].should.have.property('key', 'foo#123');
stub.args[0][0].should.have.property('store', 'file');
done();
});
});
it('should handle error which context.delete causes', function (done) { it('should handle error which context.delete causes', function (done) {
var stubbedResult = Promise.reject('error'); var stubbedResult = Promise.reject('error');
stubbedResult.catch(function() {}); stubbedResult.catch(function() {});