util.pubsub: Allow publishing with a config that should be used as defaults only
authorMatthew Wild <mwild1@gmail.com>
Thu, 18 Oct 2018 18:00:54 +0100
changeset 9519 038446c50630
parent 9518 2571c65b972f
child 9520 b1c6ede17592
util.pubsub: Allow publishing with a config that should be used as defaults only
spec/util_pubsub_spec.lua
util/pubsub.lua
--- a/spec/util_pubsub_spec.lua	Thu Oct 18 13:33:19 2018 +0200
+++ b/spec/util_pubsub_spec.lua	Thu Oct 18 18:00:54 2018 +0100
@@ -87,6 +87,34 @@
 		end);
 	end);
 
+	describe("publish with config", function ()
+		randomize(false); -- These tests are ordered
+
+		local broadcaster = spy.new(function (notif_type, node_name, subscribers, item) -- luacheck: ignore 212
+		end);
+		local service = pubsub.new({
+			broadcaster = broadcaster;
+			autocreate_on_publish = true;
+		});
+
+		it("automatically creates node with requested config", function ()
+			assert(service:publish("node", true, "1", "item 1", { myoption = true }));
+
+			local ok, config = assert(service:get_node_config("node", true));
+			assert.equals(true, config.myoption);
+		end);
+
+		it("fails to publish to a node with differing config", function ()
+			local ok, err = service:publish("node", true, "1", "item 2", { myoption = false });
+			assert.falsy(ok);
+			assert.equals("precondition-not-met", err);
+		end);
+
+		it("allows to publish to a node with differing config when only defaults are suggested", function ()
+			assert(service:publish("node", true, "1", "item 2", { _defaults_only = true, myoption = false }));
+		end);
+	end);
+
 	describe("#issue1082", function ()
 		randomize(false); -- These tests are ordered
 
--- a/util/pubsub.lua	Thu Oct 18 13:33:19 2018 +0200
+++ b/util/pubsub.lua	Thu Oct 18 18:00:54 2018 +0100
@@ -493,7 +493,7 @@
 	return true;
 end
 
-function service:publish(node, actor, id, item, required_config) --> ok, err
+function service:publish(node, actor, id, item, requested_config) --> ok, err
 	-- Access checking
 	local may_publish = false;
 
@@ -516,13 +516,16 @@
 		if not self.config.autocreate_on_publish then
 			return false, "item-not-found";
 		end
-		local ok, err = self:create(node, true, required_config);
+		local ok, err = self:create(node, true, requested_config);
 		if not ok then
 			return ok, err;
 		end
 		node_obj = self.nodes[node];
-	elseif required_config and not check_preconditions(node_obj.config, required_config) then
-		return false, "precondition-not-met";
+	elseif requested_config and not requested_config._defaults_only then
+		-- Check that node has the requested config before we publish
+		if not check_preconditions(node_obj.config, requested_config) then
+			return false, "precondition-not-met";
+		end
 	end
 	if not self.config.itemcheck(item) then
 		return nil, "invalid-item";