Changeset

9840:ec353524b739 0.11

util.pubsub: Validate node configuration on node creation (fixes #1328)
author Kim Alvefur <zash@zash.se>
date Sun, 03 Mar 2019 19:31:56 +0100
parents 9838:40ed04014b97
children 9841:a44f562e01a5
files spec/util_pubsub_spec.lua util/pubsub.lua
diffstat 2 files changed, 51 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/spec/util_pubsub_spec.lua	Sat Mar 02 22:26:01 2019 +0100
+++ b/spec/util_pubsub_spec.lua	Sun Mar 03 19:31:56 2019 +0100
@@ -436,4 +436,45 @@
 		end);
 	end);
 
+	describe("node config checking", function ()
+		local service;
+		before_each(function ()
+			service = pubsub.new({
+				check_node_config = function (node, actor, config) -- luacheck: ignore 212
+					return config["max_items"] <= 20;
+				end;
+			});
+		end);
+
+		it("defaults, then configure", function ()
+			local ok, err = service:create("node", true);
+			assert.is_true(ok, err);
+
+			local ok, err = service:set_node_config("node", true, { max_items = 10 });
+			assert.is_true(ok, err);
+
+			local ok, err = service:set_node_config("node", true, { max_items = 100 });
+			assert.falsy(ok, err);
+			assert.equals(err, "not-acceptable");
+		end);
+
+		it("create with ok config, then configure", function ()
+			local ok, err = service:create("node", true, { max_items = 10 });
+			assert.is_true(ok, err);
+
+			local ok, err = service:set_node_config("node", true, { max_items = 100 });
+			assert.falsy(ok, err);
+
+			local ok, err = service:set_node_config("node", true, { max_items = 10 });
+			assert.is_true(ok, err);
+		end);
+
+		it("create with unacceptable config", function ()
+			local ok, err = service:create("node", true, { max_items = 100 });
+			assert.falsy(ok, err);
+		end);
+
+
+	end);
+
 end);
--- a/util/pubsub.lua	Sat Mar 02 22:26:01 2019 +0100
+++ b/util/pubsub.lua	Sun Mar 03 19:31:56 2019 +0100
@@ -436,10 +436,19 @@
 		return false, "conflict";
 	end
 
+	local config = setmetatable(options or {}, {__index=self.node_defaults});
+
+	if self.config.check_node_config then
+		local ok = self.config.check_node_config(node, actor, config);
+		if not ok then
+			return false, "not-acceptable";
+		end
+	end
+
 	self.nodes[node] = {
 		name = node;
 		subscribers = {};
-		config = setmetatable(options or {}, {__index=self.node_defaults});
+		config = config;
 		affiliations = {};
 	};