aboutsummaryrefslogtreecommitdiffstats
path: root/spec/util_pubsub_spec.lua
diff options
context:
space:
mode:
Diffstat (limited to 'spec/util_pubsub_spec.lua')
-rw-r--r--spec/util_pubsub_spec.lua110
1 files changed, 109 insertions, 1 deletions
diff --git a/spec/util_pubsub_spec.lua b/spec/util_pubsub_spec.lua
index c982fb36..2dc5ca60 100644
--- a/spec/util_pubsub_spec.lua
+++ b/spec/util_pubsub_spec.lua
@@ -101,13 +101,14 @@ describe("util.pubsub", function ()
assert(service:publish("node", true, "1", "item 1", { myoption = true }));
local ok, config = assert(service:get_node_config("node", true));
+ assert.truthy(ok);
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);
+ assert.equals("precondition-not-met", err.pubsub_condition);
end);
it("allows to publish to a node with differing config when only defaults are suggested", function ()
@@ -229,6 +230,7 @@ describe("util.pubsub", function ()
end);
it("should be the default", function ()
local ok, config = service:get_node_config("test", true);
+ assert.truthy(ok);
assert.equal("open", config.access_model);
end);
it("should allow anyone to subscribe", function ()
@@ -250,6 +252,7 @@ describe("util.pubsub", function ()
end);
it("should be present in the configuration", function ()
local ok, config = service:get_node_config("test", true);
+ assert.truthy(ok);
assert.equal("whitelist", config.access_model);
end);
it("should not allow anyone to subscribe", function ()
@@ -294,6 +297,7 @@ describe("util.pubsub", function ()
end);
it("should be the default", function ()
local ok, config = service:get_node_config("test", true);
+ assert.truthy(ok);
assert.equal("publishers", config.publish_model);
end);
it("should not allow anyone to publish", function ()
@@ -304,6 +308,7 @@ describe("util.pubsub", function ()
end);
it("should allow publishers to publish", function ()
assert(service:set_affiliation("test", true, "mypublisher", "publisher"));
+ -- luacheck: ignore 211/err
local ok, err = service:publish("test", "mypublisher", "item1", "foo");
assert.is_true(ok);
end);
@@ -342,6 +347,7 @@ describe("util.pubsub", function ()
end);
it("should allow publishers to publish without a subscription", function ()
assert(service:set_affiliation("test", true, "mypublisher", "publisher"));
+ -- luacheck: ignore 211/err
local ok, err = service:publish("test", "mypublisher", "item1", "foo");
assert.is_true(ok);
end);
@@ -477,4 +483,106 @@ describe("util.pubsub", function ()
end);
+ describe("subscriber filter", function ()
+ it("works", function ()
+ local filter = spy.new(function (subs) -- luacheck: ignore 212/subs
+ return {["modified"] = true};
+ end);
+ local broadcaster = spy.new(function (notif_type, node_name, subscribers, item) -- luacheck: ignore 212
+ end);
+ local service = pubsub.new({
+ subscriber_filter = filter;
+ broadcaster = broadcaster;
+ });
+
+ local ok = service:create("node", true);
+ assert.truthy(ok);
+
+ local ok = service:add_subscription("node", true, "someone");
+ assert.truthy(ok);
+
+ local ok = service:publish("node", true, "1", "item");
+ assert.truthy(ok);
+ -- TODO how to match table arguments?
+ assert.spy(filter).was_called();
+ assert.spy(broadcaster).was_called();
+ end);
+ end);
+
+ describe("persist_items", function()
+ it("can be disabled", function()
+ local broadcaster = spy.new(function(notif_type, node_name, subscribers, item) -- luacheck: ignore 212
+ end);
+ local service = pubsub.new { node_defaults = { persist_items = false }, broadcaster = broadcaster }
+
+ local ok = service:create("node", true)
+ assert.truthy(ok);
+
+ local ok = service:publish("node", true, "1", "item");
+ assert.truthy(ok);
+ assert.spy(broadcaster).was_called();
+
+ local ok, items = service:get_items("node", true);
+ assert.not_truthy(ok);
+ assert.equal(items, "persistent-items-unsupported");
+ end);
+
+ end)
+
+ describe("max_items", function ()
+ it("works", function ()
+ local service = pubsub.new { };
+
+ local ok = service:create("node", true)
+ assert.truthy(ok);
+
+ for i = 1, 20 do
+ assert.truthy(service:publish("node", true, "item"..tostring(i), "data"..tostring(i)));
+ end
+
+ do
+ local ok, items = service:get_items("node", true, nil, { max = 3 });
+ assert.truthy(ok, items);
+ assert.equal(3, #items);
+ assert.same({
+ "item20",
+ "item19",
+ "item18",
+ item20 = "data20",
+ item19 = "data19",
+ item18 = "data18",
+ }, items, "items should be ordered by oldest first");
+ end
+
+ do
+ local ok, items = service:get_items("node", true, nil, { max = 10 });
+ assert.truthy(ok, items);
+ assert.equal(10, #items);
+ assert.same({
+ "item20",
+ "item19",
+ "item18",
+ "item17",
+ "item16",
+ "item15",
+ "item14",
+ "item13",
+ "item12",
+ "item11",
+ item20 = "data20",
+ item19 = "data19",
+ item18 = "data18",
+ item17 = "data17",
+ item16 = "data16",
+ item15 = "data15",
+ item14 = "data14",
+ item13 = "data13",
+ item12 = "data12",
+ item11 = "data11",
+ }, items, "items should be ordered by oldest first");
+ end
+
+ end);
+
+ end)
end);