Mercurial > prosody-hg
comparison spec/util_pubsub_spec.lua @ 11200:bf8f2da84007
Merge 0.11->trunk
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Thu, 05 Nov 2020 22:31:25 +0100 |
| parents | 3b8431eed785 |
| children | 7a77f0c05382 |
comparison
equal
deleted
inserted
replaced
| 11199:6c7c50a4de32 | 11200:bf8f2da84007 |
|---|---|
| 99 | 99 |
| 100 it("automatically creates node with requested config", function () | 100 it("automatically creates node with requested config", function () |
| 101 assert(service:publish("node", true, "1", "item 1", { myoption = true })); | 101 assert(service:publish("node", true, "1", "item 1", { myoption = true })); |
| 102 | 102 |
| 103 local ok, config = assert(service:get_node_config("node", true)); | 103 local ok, config = assert(service:get_node_config("node", true)); |
| 104 assert.truthy(ok); | |
| 104 assert.equals(true, config.myoption); | 105 assert.equals(true, config.myoption); |
| 105 end); | 106 end); |
| 106 | 107 |
| 107 it("fails to publish to a node with differing config", function () | 108 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 local ok, err = service:publish("node", true, "1", "item 2", { myoption = false }); |
| 109 assert.falsy(ok); | 110 assert.falsy(ok); |
| 110 assert.equals("precondition-not-met", err); | 111 assert.equals("precondition-not-met", err.pubsub_condition); |
| 111 end); | 112 end); |
| 112 | 113 |
| 113 it("allows to publish to a node with differing config when only defaults are suggested", function () | 114 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 assert(service:publish("node", true, "1", "item 2", { _defaults_only = true, myoption = false })); |
| 115 end); | 116 end); |
| 227 -- Do not supply any config, 'open' should be default | 228 -- Do not supply any config, 'open' should be default |
| 228 service:create("test", true); | 229 service:create("test", true); |
| 229 end); | 230 end); |
| 230 it("should be the default", function () | 231 it("should be the default", function () |
| 231 local ok, config = service:get_node_config("test", true); | 232 local ok, config = service:get_node_config("test", true); |
| 233 assert.truthy(ok); | |
| 232 assert.equal("open", config.access_model); | 234 assert.equal("open", config.access_model); |
| 233 end); | 235 end); |
| 234 it("should allow anyone to subscribe", function () | 236 it("should allow anyone to subscribe", function () |
| 235 local ok = service:add_subscription("test", "stranger", "stranger"); | 237 local ok = service:add_subscription("test", "stranger", "stranger"); |
| 236 assert.is_true(ok); | 238 assert.is_true(ok); |
| 248 service = assert(pubsub.new()); | 250 service = assert(pubsub.new()); |
| 249 assert.is_true(service:create("test", true, { access_model = "whitelist" })); | 251 assert.is_true(service:create("test", true, { access_model = "whitelist" })); |
| 250 end); | 252 end); |
| 251 it("should be present in the configuration", function () | 253 it("should be present in the configuration", function () |
| 252 local ok, config = service:get_node_config("test", true); | 254 local ok, config = service:get_node_config("test", true); |
| 255 assert.truthy(ok); | |
| 253 assert.equal("whitelist", config.access_model); | 256 assert.equal("whitelist", config.access_model); |
| 254 end); | 257 end); |
| 255 it("should not allow anyone to subscribe", function () | 258 it("should not allow anyone to subscribe", function () |
| 256 local ok, err = service:add_subscription("test", "stranger", "stranger"); | 259 local ok, err = service:add_subscription("test", "stranger", "stranger"); |
| 257 assert.is_false(ok); | 260 assert.is_false(ok); |
| 292 -- Do not supply any config, 'publishers' should be default | 295 -- Do not supply any config, 'publishers' should be default |
| 293 service:create("test", true); | 296 service:create("test", true); |
| 294 end); | 297 end); |
| 295 it("should be the default", function () | 298 it("should be the default", function () |
| 296 local ok, config = service:get_node_config("test", true); | 299 local ok, config = service:get_node_config("test", true); |
| 300 assert.truthy(ok); | |
| 297 assert.equal("publishers", config.publish_model); | 301 assert.equal("publishers", config.publish_model); |
| 298 end); | 302 end); |
| 299 it("should not allow anyone to publish", function () | 303 it("should not allow anyone to publish", function () |
| 300 assert.is_true(service:add_subscription("test", "stranger", "stranger")); | 304 assert.is_true(service:add_subscription("test", "stranger", "stranger")); |
| 301 local ok, err = service:publish("test", "stranger", "item1", "foo"); | 305 local ok, err = service:publish("test", "stranger", "item1", "foo"); |
| 302 assert.is_falsy(ok); | 306 assert.is_falsy(ok); |
| 303 assert.equals("forbidden", err); | 307 assert.equals("forbidden", err); |
| 304 end); | 308 end); |
| 305 it("should allow publishers to publish", function () | 309 it("should allow publishers to publish", function () |
| 306 assert(service:set_affiliation("test", true, "mypublisher", "publisher")); | 310 assert(service:set_affiliation("test", true, "mypublisher", "publisher")); |
| 311 -- luacheck: ignore 211/err | |
| 307 local ok, err = service:publish("test", "mypublisher", "item1", "foo"); | 312 local ok, err = service:publish("test", "mypublisher", "item1", "foo"); |
| 308 assert.is_true(ok); | 313 assert.is_true(ok); |
| 309 end); | 314 end); |
| 310 it("should allow owners to publish", function () | 315 it("should allow owners to publish", function () |
| 311 assert(service:set_affiliation("test", true, "myowner", "owner")); | 316 assert(service:set_affiliation("test", true, "myowner", "owner")); |
| 340 local ok = service:publish("test", "stranger", "item1", "foo"); | 345 local ok = service:publish("test", "stranger", "item1", "foo"); |
| 341 assert.is_true(ok); | 346 assert.is_true(ok); |
| 342 end); | 347 end); |
| 343 it("should allow publishers to publish without a subscription", function () | 348 it("should allow publishers to publish without a subscription", function () |
| 344 assert(service:set_affiliation("test", true, "mypublisher", "publisher")); | 349 assert(service:set_affiliation("test", true, "mypublisher", "publisher")); |
| 350 -- luacheck: ignore 211/err | |
| 345 local ok, err = service:publish("test", "mypublisher", "item1", "foo"); | 351 local ok, err = service:publish("test", "mypublisher", "item1", "foo"); |
| 346 assert.is_true(ok); | 352 assert.is_true(ok); |
| 347 end); | 353 end); |
| 348 it("should allow owners to publish without a subscription", function () | 354 it("should allow owners to publish without a subscription", function () |
| 349 assert(service:set_affiliation("test", true, "myowner", "owner")); | 355 assert(service:set_affiliation("test", true, "myowner", "owner")); |
| 475 end); | 481 end); |
| 476 | 482 |
| 477 | 483 |
| 478 end); | 484 end); |
| 479 | 485 |
| 486 describe("subscriber filter", function () | |
| 487 it("works", function () | |
| 488 local filter = spy.new(function (subs) -- luacheck: ignore 212/subs | |
| 489 return {["modified"] = true}; | |
| 490 end); | |
| 491 local broadcaster = spy.new(function (notif_type, node_name, subscribers, item) -- luacheck: ignore 212 | |
| 492 end); | |
| 493 local service = pubsub.new({ | |
| 494 subscriber_filter = filter; | |
| 495 broadcaster = broadcaster; | |
| 496 }); | |
| 497 | |
| 498 local ok = service:create("node", true); | |
| 499 assert.truthy(ok); | |
| 500 | |
| 501 local ok = service:add_subscription("node", true, "someone"); | |
| 502 assert.truthy(ok); | |
| 503 | |
| 504 local ok = service:publish("node", true, "1", "item"); | |
| 505 assert.truthy(ok); | |
| 506 -- TODO how to match table arguments? | |
| 507 assert.spy(filter).was_called(); | |
| 508 assert.spy(broadcaster).was_called(); | |
| 509 end); | |
| 510 end); | |
| 511 | |
| 480 end); | 512 end); |
