mod_admin_telnet: Identify native bidi sessions
[prosody.git] / spec / util_pubsub_spec.lua
blobc982fb36a163ead701764553e2ed95c318c8b145
1 local pubsub;
2 setup(function ()
3 pubsub = require "util.pubsub";
4 end);
6 --[[TODO:
7 Retract
8 Purge
9 auto-create/auto-subscribe
10 Item store/node store
11 resize on max_items change
12 service creation config provides alternative node_defaults
13 get subscriptions
16 describe("util.pubsub", function ()
17 describe("simple node creation and deletion", function ()
18 randomize(false); -- These tests are ordered
20 -- Roughly a port of scansion/scripts/pubsub_createdelete.scs
21 local service = pubsub.new();
23 describe("#create", function ()
24 randomize(false); -- These tests are ordered
25 it("creates a new node", function ()
26 assert.truthy(service:create("princely_musings", true));
27 end);
29 it("fails to create the same node again", function ()
30 assert.falsy(service:create("princely_musings", true));
31 end);
32 end);
34 describe("#delete", function ()
35 randomize(false); -- These tests are ordered
36 it("deletes the node", function ()
37 assert.truthy(service:delete("princely_musings", true));
38 end);
40 it("can't delete an already deleted node", function ()
41 assert.falsy(service:delete("princely_musings", true));
42 end);
43 end);
44 end);
46 describe("simple publishing", function ()
47 randomize(false); -- These tests are ordered
49 local notified;
50 local broadcaster = spy.new(function (notif_type, node_name, subscribers, item) -- luacheck: ignore 212
51 notified = subscribers;
52 end);
53 local service = pubsub.new({
54 broadcaster = broadcaster;
55 });
57 it("creates a node", function ()
58 assert.truthy(service:create("node", true));
59 end);
61 it("lets someone subscribe", function ()
62 assert.truthy(service:add_subscription("node", true, "someone"));
63 end);
65 it("publishes an item", function ()
66 assert.truthy(service:publish("node", true, "1", "item 1"));
67 assert.truthy(notified["someone"]);
68 end);
70 it("called the broadcaster", function ()
71 assert.spy(broadcaster).was_called();
72 end);
74 it("should return one item", function ()
75 local ok, ret = service:get_items("node", true);
76 assert.truthy(ok);
77 assert.same({ "1", ["1"] = "item 1" }, ret);
78 end);
80 it("lets someone unsubscribe", function ()
81 assert.truthy(service:remove_subscription("node", true, "someone"));
82 end);
84 it("does not send notifications after subscription is removed", function ()
85 assert.truthy(service:publish("node", true, "1", "item 1"));
86 assert.is_nil(notified["someone"]);
87 end);
88 end);
90 describe("publish with config", function ()
91 randomize(false); -- These tests are ordered
93 local broadcaster = spy.new(function (notif_type, node_name, subscribers, item) -- luacheck: ignore 212
94 end);
95 local service = pubsub.new({
96 broadcaster = broadcaster;
97 autocreate_on_publish = true;
98 });
100 it("automatically creates node with requested config", function ()
101 assert(service:publish("node", true, "1", "item 1", { myoption = true }));
103 local ok, config = assert(service:get_node_config("node", true));
104 assert.equals(true, config.myoption);
105 end);
107 it("fails to publish to a node with differing config", function ()
108 local ok, err = service:publish("node", true, "1", "item 2", { myoption = false });
109 assert.falsy(ok);
110 assert.equals("precondition-not-met", err);
111 end);
113 it("allows to publish to a node with differing config when only defaults are suggested", function ()
114 assert(service:publish("node", true, "1", "item 2", { _defaults_only = true, myoption = false }));
115 end);
116 end);
118 describe("#issue1082", function ()
119 randomize(false); -- These tests are ordered
121 local service = pubsub.new();
123 it("creates a node with max_items = 1", function ()
124 assert.truthy(service:create("node", true, { max_items = 1 }));
125 end);
127 it("changes max_items to 2", function ()
128 assert.truthy(service:set_node_config("node", true, { max_items = 2 }));
129 end);
131 it("publishes one item", function ()
132 assert.truthy(service:publish("node", true, "1", "item 1"));
133 end);
135 it("should return one item", function ()
136 local ok, ret = service:get_items("node", true);
137 assert.truthy(ok);
138 assert.same({ "1", ["1"] = "item 1" }, ret);
139 end);
141 it("publishes another item", function ()
142 assert.truthy(service:publish("node", true, "2", "item 2"));
143 end);
145 it("should return two items", function ()
146 local ok, ret = service:get_items("node", true);
147 assert.truthy(ok);
148 assert.same({
149 "2",
150 "1",
151 ["1"] = "item 1",
152 ["2"] = "item 2",
153 }, ret);
154 end);
156 it("publishes yet another item", function ()
157 assert.truthy(service:publish("node", true, "3", "item 3"));
158 end);
160 it("should still return only two items", function ()
161 local ok, ret = service:get_items("node", true);
162 assert.truthy(ok);
163 assert.same({
164 "3",
165 "2",
166 ["2"] = "item 2",
167 ["3"] = "item 3",
168 }, ret);
169 end);
171 end);
173 describe("the thing", function ()
174 randomize(false); -- These tests are ordered
176 local service = pubsub.new();
178 it("creates a node with some items", function ()
179 assert.truthy(service:create("node", true, { max_items = 3 }));
180 assert.truthy(service:publish("node", true, "1", "item 1"));
181 assert.truthy(service:publish("node", true, "2", "item 2"));
182 assert.truthy(service:publish("node", true, "3", "item 3"));
183 end);
185 it("should return the requested item", function ()
186 local ok, ret = service:get_items("node", true, "1");
187 assert.truthy(ok);
188 assert.same({ "1", ["1"] = "item 1" }, ret);
189 end);
191 it("should return multiple requested items", function ()
192 local ok, ret = service:get_items("node", true, { "1", "2" });
193 assert.truthy(ok);
194 assert.same({
195 "1",
196 "2",
197 ["1"] = "item 1",
198 ["2"] = "item 2",
199 }, ret);
200 end);
201 end);
204 describe("node config", function ()
205 local service;
206 before_each(function ()
207 service = pubsub.new();
208 service:create("test", true);
209 end);
210 it("access is forbidden for unaffiliated entities", function ()
211 local ok, err = service:get_node_config("test", "stranger");
212 assert.is_falsy(ok);
213 assert.equals("forbidden", err);
214 end);
215 it("returns an error for nodes that do not exist", function ()
216 local ok, err = service:get_node_config("nonexistent", true);
217 assert.is_falsy(ok);
218 assert.equals("item-not-found", err);
219 end);
220 end);
222 describe("access model", function ()
223 describe("open", function ()
224 local service;
225 before_each(function ()
226 service = pubsub.new();
227 -- Do not supply any config, 'open' should be default
228 service:create("test", true);
229 end);
230 it("should be the default", function ()
231 local ok, config = service:get_node_config("test", true);
232 assert.equal("open", config.access_model);
233 end);
234 it("should allow anyone to subscribe", function ()
235 local ok = service:add_subscription("test", "stranger", "stranger");
236 assert.is_true(ok);
237 end);
238 it("should still reject outcast-affiliated entities", function ()
239 assert(service:set_affiliation("test", true, "enemy", "outcast"));
240 local ok, err = service:add_subscription("test", "enemy", "enemy");
241 assert.is_falsy(ok);
242 assert.equal("forbidden", err);
243 end);
244 end);
245 describe("whitelist", function ()
246 local service;
247 before_each(function ()
248 service = assert(pubsub.new());
249 assert.is_true(service:create("test", true, { access_model = "whitelist" }));
250 end);
251 it("should be present in the configuration", function ()
252 local ok, config = service:get_node_config("test", true);
253 assert.equal("whitelist", config.access_model);
254 end);
255 it("should not allow anyone to subscribe", function ()
256 local ok, err = service:add_subscription("test", "stranger", "stranger");
257 assert.is_false(ok);
258 assert.equals("forbidden", err);
259 end);
260 end);
261 describe("change", function ()
262 local service;
263 before_each(function ()
264 service = pubsub.new();
265 service:create("test", true, { access_model = "open" });
266 end);
267 it("affects existing subscriptions", function ()
269 local ok = service:add_subscription("test", "stranger", "stranger");
270 assert.is_true(ok);
273 local ok, sub = service:get_subscription("test", "stranger", "stranger");
274 assert.is_true(ok);
275 assert.is_true(sub);
277 assert(service:set_node_config("test", true, { access_model = "whitelist" }));
279 local ok, sub = service:get_subscription("test", "stranger", "stranger");
280 assert.is_true(ok);
281 assert.is_nil(sub);
283 end);
284 end);
285 end);
287 describe("publish model", function ()
288 describe("publishers", function ()
289 local service;
290 before_each(function ()
291 service = pubsub.new();
292 -- Do not supply any config, 'publishers' should be default
293 service:create("test", true);
294 end);
295 it("should be the default", function ()
296 local ok, config = service:get_node_config("test", true);
297 assert.equal("publishers", config.publish_model);
298 end);
299 it("should not allow anyone to publish", function ()
300 assert.is_true(service:add_subscription("test", "stranger", "stranger"));
301 local ok, err = service:publish("test", "stranger", "item1", "foo");
302 assert.is_falsy(ok);
303 assert.equals("forbidden", err);
304 end);
305 it("should allow publishers to publish", function ()
306 assert(service:set_affiliation("test", true, "mypublisher", "publisher"));
307 local ok, err = service:publish("test", "mypublisher", "item1", "foo");
308 assert.is_true(ok);
309 end);
310 it("should allow owners to publish", function ()
311 assert(service:set_affiliation("test", true, "myowner", "owner"));
312 local ok = service:publish("test", "myowner", "item1", "foo");
313 assert.is_true(ok);
314 end);
315 end);
316 describe("open", function ()
317 local service;
318 before_each(function ()
319 service = pubsub.new();
320 service:create("test", true, { publish_model = "open" });
321 end);
322 it("should allow anyone to publish", function ()
323 local ok = service:publish("test", "stranger", "item1", "foo");
324 assert.is_true(ok);
325 end);
326 end);
327 describe("subscribers", function ()
328 local service;
329 before_each(function ()
330 service = pubsub.new();
331 service:create("test", true, { publish_model = "subscribers" });
332 end);
333 it("should not allow non-subscribers to publish", function ()
334 local ok, err = service:publish("test", "stranger", "item1", "foo");
335 assert.is_falsy(ok);
336 assert.equals("forbidden", err);
337 end);
338 it("should allow subscribers to publish without an affiliation", function ()
339 assert.is_true(service:add_subscription("test", "stranger", "stranger"));
340 local ok = service:publish("test", "stranger", "item1", "foo");
341 assert.is_true(ok);
342 end);
343 it("should allow publishers to publish without a subscription", function ()
344 assert(service:set_affiliation("test", true, "mypublisher", "publisher"));
345 local ok, err = service:publish("test", "mypublisher", "item1", "foo");
346 assert.is_true(ok);
347 end);
348 it("should allow owners to publish without a subscription", function ()
349 assert(service:set_affiliation("test", true, "myowner", "owner"));
350 local ok = service:publish("test", "myowner", "item1", "foo");
351 assert.is_true(ok);
352 end);
353 end);
354 end);
356 describe("item API", function ()
357 local service;
358 before_each(function ()
359 service = pubsub.new();
360 service:create("test", true, { publish_model = "subscribers" });
361 end);
362 describe("get_last_item()", function ()
363 it("succeeds with nil on empty nodes", function ()
364 local ok, id, item = service:get_last_item("test", true);
365 assert.is_true(ok);
366 assert.is_nil(id);
367 assert.is_nil(item);
368 end);
369 it("succeeds and returns the last item", function ()
370 service:publish("test", true, "one", "hello world");
371 service:publish("test", true, "two", "hello again");
372 service:publish("test", true, "three", "hey");
373 service:publish("test", true, "one", "bye");
374 local ok, id, item = service:get_last_item("test", true);
375 assert.is_true(ok);
376 assert.equal("one", id);
377 assert.equal("bye", item);
378 end);
379 end);
380 describe("get_items()", function ()
381 it("fails on non-existent nodes", function ()
382 local ok, err = service:get_items("no-node", true);
383 assert.is_falsy(ok);
384 assert.equal("item-not-found", err);
385 end);
386 it("returns no items on an empty node", function ()
387 local ok, items = service:get_items("test", true);
388 assert.is_true(ok);
389 assert.equal(0, #items);
390 assert.is_nil(next(items));
391 end);
392 it("returns no items on an empty node", function ()
393 local ok, items = service:get_items("test", true);
394 assert.is_true(ok);
395 assert.equal(0, #items);
396 assert.is_nil((next(items)));
397 end);
398 it("returns all published items", function ()
399 service:publish("test", true, "one", "hello world");
400 service:publish("test", true, "two", "hello again");
401 service:publish("test", true, "three", "hey");
402 service:publish("test", true, "one", "bye");
403 local ok, items = service:get_items("test", true);
404 assert.is_true(ok);
405 assert.same({ "one", "three", "two", two = "hello again", three = "hey", one = "bye" }, items);
406 end);
407 end);
408 end);
410 describe("restoring data from nodestore", function ()
411 local nodestore = {
412 data = {
413 test = {
414 name = "test";
415 config = {};
416 affiliations = {};
417 subscribers = {
418 ["someone"] = true;
423 function nodestore:users()
424 return pairs(self.data)
426 function nodestore:get(key)
427 return self.data[key];
429 local service = pubsub.new({
430 nodestore = nodestore;
432 it("subscriptions", function ()
433 local ok, ret = service:get_subscriptions(nil, true, nil)
434 assert.is_true(ok);
435 assert.same({ { node = "test", jid = "someone", subscription = true, } }, ret);
436 end);
437 end);
439 describe("node config checking", function ()
440 local service;
441 before_each(function ()
442 service = pubsub.new({
443 check_node_config = function (node, actor, config) -- luacheck: ignore 212
444 return config["max_items"] <= 20;
445 end;
447 end);
449 it("defaults, then configure", function ()
450 local ok, err = service:create("node", true);
451 assert.is_true(ok, err);
453 local ok, err = service:set_node_config("node", true, { max_items = 10 });
454 assert.is_true(ok, err);
456 local ok, err = service:set_node_config("node", true, { max_items = 100 });
457 assert.falsy(ok, err);
458 assert.equals(err, "not-acceptable");
459 end);
461 it("create with ok config, then configure", function ()
462 local ok, err = service:create("node", true, { max_items = 10 });
463 assert.is_true(ok, err);
465 local ok, err = service:set_node_config("node", true, { max_items = 100 });
466 assert.falsy(ok, err);
468 local ok, err = service:set_node_config("node", true, { max_items = 10 });
469 assert.is_true(ok, err);
470 end);
472 it("create with unacceptable config", function ()
473 local ok, err = service:create("node", true, { max_items = 100 });
474 assert.falsy(ok, err);
475 end);
478 end);
480 end);