spec/util_pubsub_spec.lua
author Matthew Wild <mwild1@gmail.com>
Sun, 12 Aug 2018 11:34:05 +0100
changeset 9176 c53663e13b51
parent 9174 5f03fe90704f
child 9177 160032d55ff1
permissions -rw-r--r--
util.pubsub tests: Extend publishing tests to check for correct notification behaviour on subscribe/unsubscribe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8567
fd41dc4a78e9 util_pubsub_spec: Move util.pubsub import into a setup block
Kim Alvefur <zash@zash.se>
parents: 8561
diff changeset
     1
local pubsub;
fd41dc4a78e9 util_pubsub_spec: Move util.pubsub import into a setup block
Kim Alvefur <zash@zash.se>
parents: 8561
diff changeset
     2
setup(function ()
fd41dc4a78e9 util_pubsub_spec: Move util.pubsub import into a setup block
Kim Alvefur <zash@zash.se>
parents: 8561
diff changeset
     3
	pubsub = require "util.pubsub";
fd41dc4a78e9 util_pubsub_spec: Move util.pubsub import into a setup block
Kim Alvefur <zash@zash.se>
parents: 8561
diff changeset
     4
end);
fd41dc4a78e9 util_pubsub_spec: Move util.pubsub import into a setup block
Kim Alvefur <zash@zash.se>
parents: 8561
diff changeset
     5
8561
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     6
describe("util.pubsub", function ()
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     7
	describe("simple node creation and deletion", function ()
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     8
		-- Roughly a port of scansion/scripts/pubsub_createdelete.scs
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     9
		local service = pubsub.new();
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    10
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    11
		describe("#create", function ()
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    12
			it("creates a new node", function ()
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    13
				assert.truthy(service:create("princely_musings", true));
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    14
			end);
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    15
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    16
			it("fails to create the same node again", function ()
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    17
				assert.falsy(service:create("princely_musings", true));
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    18
			end);
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    19
		end);
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    20
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    21
		describe("#delete", function ()
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    22
			it("deletes the node", function ()
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    23
				assert.truthy(service:delete("princely_musings", true));
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    24
			end);
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    25
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    26
			it("can't delete an already deleted node", function ()
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    27
				assert.falsy(service:delete("princely_musings", true));
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    28
			end);
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    29
		end);
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    30
	end);
8650
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8567
diff changeset
    31
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8567
diff changeset
    32
	describe("simple publishing", function ()
9176
c53663e13b51 util.pubsub tests: Extend publishing tests to check for correct notification behaviour on subscribe/unsubscribe
Matthew Wild <mwild1@gmail.com>
parents: 9174
diff changeset
    33
		local notified;
c53663e13b51 util.pubsub tests: Extend publishing tests to check for correct notification behaviour on subscribe/unsubscribe
Matthew Wild <mwild1@gmail.com>
parents: 9174
diff changeset
    34
		local broadcaster = spy.new(function (notif_type, node_name, subscribers, item)
c53663e13b51 util.pubsub tests: Extend publishing tests to check for correct notification behaviour on subscribe/unsubscribe
Matthew Wild <mwild1@gmail.com>
parents: 9174
diff changeset
    35
			notified = subscribers;
c53663e13b51 util.pubsub tests: Extend publishing tests to check for correct notification behaviour on subscribe/unsubscribe
Matthew Wild <mwild1@gmail.com>
parents: 9174
diff changeset
    36
		end);
8820
9a3066a580ad spec/util_pubsub: Test whether someone can subscribe to a node
Kim Alvefur <zash@zash.se>
parents: 8650
diff changeset
    37
		local service = pubsub.new({
9a3066a580ad spec/util_pubsub: Test whether someone can subscribe to a node
Kim Alvefur <zash@zash.se>
parents: 8650
diff changeset
    38
			broadcaster = broadcaster;
9a3066a580ad spec/util_pubsub: Test whether someone can subscribe to a node
Kim Alvefur <zash@zash.se>
parents: 8650
diff changeset
    39
		});
8650
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8567
diff changeset
    40
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8567
diff changeset
    41
		it("creates a node", function ()
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8567
diff changeset
    42
			assert.truthy(service:create("node", true));
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8567
diff changeset
    43
		end);
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8567
diff changeset
    44
8820
9a3066a580ad spec/util_pubsub: Test whether someone can subscribe to a node
Kim Alvefur <zash@zash.se>
parents: 8650
diff changeset
    45
		it("lets someone subscribe", function ()
9a3066a580ad spec/util_pubsub: Test whether someone can subscribe to a node
Kim Alvefur <zash@zash.se>
parents: 8650
diff changeset
    46
			assert.truthy(service:add_subscription("node", true, "someone"));
9a3066a580ad spec/util_pubsub: Test whether someone can subscribe to a node
Kim Alvefur <zash@zash.se>
parents: 8650
diff changeset
    47
		end);
9a3066a580ad spec/util_pubsub: Test whether someone can subscribe to a node
Kim Alvefur <zash@zash.se>
parents: 8650
diff changeset
    48
8650
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8567
diff changeset
    49
		it("publishes an item", function ()
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8567
diff changeset
    50
			assert.truthy(service:publish("node", true, "1", "item 1"));
9176
c53663e13b51 util.pubsub tests: Extend publishing tests to check for correct notification behaviour on subscribe/unsubscribe
Matthew Wild <mwild1@gmail.com>
parents: 9174
diff changeset
    51
			assert.truthy(notified["someone"]);
8650
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8567
diff changeset
    52
		end);
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8567
diff changeset
    53
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8567
diff changeset
    54
		it("called the broadcaster", function ()
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8567
diff changeset
    55
			assert.spy(broadcaster).was_called();
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8567
diff changeset
    56
		end);
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8567
diff changeset
    57
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8567
diff changeset
    58
		it("should return one item", function ()
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8567
diff changeset
    59
			local ok, ret = service:get_items("node", true);
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8567
diff changeset
    60
			assert.truthy(ok);
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8567
diff changeset
    61
			assert.same({ "1", ["1"] = "item 1" }, ret);
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8567
diff changeset
    62
		end);
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8567
diff changeset
    63
9176
c53663e13b51 util.pubsub tests: Extend publishing tests to check for correct notification behaviour on subscribe/unsubscribe
Matthew Wild <mwild1@gmail.com>
parents: 9174
diff changeset
    64
		it("lets someone unsubscribe", function ()
c53663e13b51 util.pubsub tests: Extend publishing tests to check for correct notification behaviour on subscribe/unsubscribe
Matthew Wild <mwild1@gmail.com>
parents: 9174
diff changeset
    65
			assert.truthy(service:remove_subscription("node", true, "someone"));
c53663e13b51 util.pubsub tests: Extend publishing tests to check for correct notification behaviour on subscribe/unsubscribe
Matthew Wild <mwild1@gmail.com>
parents: 9174
diff changeset
    66
		end);
c53663e13b51 util.pubsub tests: Extend publishing tests to check for correct notification behaviour on subscribe/unsubscribe
Matthew Wild <mwild1@gmail.com>
parents: 9174
diff changeset
    67
c53663e13b51 util.pubsub tests: Extend publishing tests to check for correct notification behaviour on subscribe/unsubscribe
Matthew Wild <mwild1@gmail.com>
parents: 9174
diff changeset
    68
		it("does not send notifications after subscription is removed", function ()
c53663e13b51 util.pubsub tests: Extend publishing tests to check for correct notification behaviour on subscribe/unsubscribe
Matthew Wild <mwild1@gmail.com>
parents: 9174
diff changeset
    69
			assert.truthy(service:publish("node", true, "1", "item 1"));
c53663e13b51 util.pubsub tests: Extend publishing tests to check for correct notification behaviour on subscribe/unsubscribe
Matthew Wild <mwild1@gmail.com>
parents: 9174
diff changeset
    70
			assert.is_nil(notified["someone"]);
c53663e13b51 util.pubsub tests: Extend publishing tests to check for correct notification behaviour on subscribe/unsubscribe
Matthew Wild <mwild1@gmail.com>
parents: 9174
diff changeset
    71
		end);
8650
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8567
diff changeset
    72
	end);
9007
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
    73
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
    74
	describe("#issue1082", function ()
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
    75
		local service = pubsub.new();
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
    76
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
    77
		it("creates a node with max_items = 1", function ()
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
    78
			assert.truthy(service:create("node", true, { max_items = 1 }));
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
    79
		end);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
    80
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
    81
		it("changes max_items to 2", function ()
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
    82
			assert.truthy(service:set_node_config("node", true, { max_items = 2 }));
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
    83
		end);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
    84
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
    85
		it("publishes one item", function ()
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
    86
			assert.truthy(service:publish("node", true, "1", "item 1"));
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
    87
		end);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
    88
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
    89
		it("should return one item", function ()
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
    90
			local ok, ret = service:get_items("node", true);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
    91
			assert.truthy(ok);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
    92
			assert.same({ "1", ["1"] = "item 1" }, ret);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
    93
		end);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
    94
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
    95
		it("publishes another item", function ()
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
    96
			assert.truthy(service:publish("node", true, "2", "item 2"));
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
    97
		end);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
    98
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
    99
		it("should return two items", function ()
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
   100
			local ok, ret = service:get_items("node", true);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
   101
			assert.truthy(ok);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
   102
			assert.same({
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
   103
				"2",
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
   104
				"1",
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
   105
				["1"] = "item 1",
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
   106
				["2"] = "item 2",
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
   107
			}, ret);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
   108
		end);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
   109
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
   110
		it("publishes yet another item", function ()
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
   111
			assert.truthy(service:publish("node", true, "3", "item 3"));
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
   112
		end);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
   113
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
   114
		it("should still return only two items", function ()
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
   115
			local ok, ret = service:get_items("node", true);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
   116
			assert.truthy(ok);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
   117
			assert.same({
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
   118
				"3",
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
   119
				"2",
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
   120
				["2"] = "item 2",
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
   121
				["3"] = "item 3",
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
   122
			}, ret);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
   123
		end);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
   124
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8820
diff changeset
   125
	end);
9172
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   126
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   127
	describe("access model", function ()
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   128
		describe("open", function ()
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   129
			local service;
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   130
			before_each(function ()
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   131
				service = pubsub.new();
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   132
				-- Do not supply any config, 'open' should be default
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   133
				service:create("test", true);
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   134
			end);
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   135
			it("should be the default", function ()
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   136
				local ok, config = service:get_node_config("test", true);
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   137
				assert.equal("open", config.access_model);
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   138
			end);
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   139
			it("should allow anyone to subscribe", function ()
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   140
				local ok = service:add_subscription("test", "stranger", "stranger");
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   141
				assert.is_true(ok);
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   142
			end);
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   143
			it("should still reject outcast-affiliated entities", function ()
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   144
				assert(service:set_affiliation("test", true, "enemy", "outcast"));
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   145
				local ok, err = service:add_subscription("test", "enemy", "enemy");
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   146
				assert.is_falsy(ok);
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   147
				assert.equal("forbidden", err);
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   148
			end);
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   149
		end);
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   150
		describe("whitelist", function ()
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   151
			local service;
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   152
			before_each(function ()
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   153
				service = assert(pubsub.new());
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   154
				assert.is_true(service:create("test", true, { access_model = "whitelist" }));
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   155
			end);
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   156
			it("should be present in the configuration", function ()
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   157
				local ok, config = service:get_node_config("test", true);
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   158
				assert.equal("whitelist", config.access_model);
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   159
			end);
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   160
			it("should not allow anyone to subscribe", function ()
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   161
				local ok, err = service:add_subscription("test", "stranger", "stranger");
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   162
				assert.is_false(ok);
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   163
				assert.equals("forbidden", err);
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   164
			end);
9174
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   165
		end);
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   166
	end);
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   167
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   168
	describe("publish model", function ()
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   169
		describe("publishers", function ()
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   170
			local service;
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   171
			before_each(function ()
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   172
				service = pubsub.new();
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   173
				-- Do not supply any config, 'publishers' should be default
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   174
				service:create("test", true);
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   175
			end);
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   176
			it("should be the default", function ()
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   177
				local ok, config = service:get_node_config("test", true);
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   178
				assert.equal("publishers", config.publish_model);
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   179
			end);
9172
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   180
			it("should not allow anyone to publish", function ()
9174
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   181
				assert.is_true(service:add_subscription("test", "stranger", "stranger"));
9172
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   182
				local ok, err = service:publish("test", "stranger", "item1", "foo");
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   183
				assert.is_falsy(ok);
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   184
				assert.equals("forbidden", err);
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   185
			end);
9174
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   186
			it("should allow publishers to publish", function ()
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   187
				assert(service:set_affiliation("test", true, "mypublisher", "publisher"));
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   188
				local ok, err = service:publish("test", "mypublisher", "item1", "foo");
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   189
				assert.is_true(ok);
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   190
			end);
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   191
			it("should allow owners to publish", function ()
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   192
				assert(service:set_affiliation("test", true, "myowner", "owner"));
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   193
				local ok = service:publish("test", "myowner", "item1", "foo");
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   194
				assert.is_true(ok);
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   195
			end);
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   196
		end);
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   197
		describe("open", function ()
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   198
			local service;
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   199
			before_each(function ()
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   200
				service = pubsub.new();
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   201
				service:create("test", true, { publish_model = "open" });
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   202
			end);
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   203
			it("should allow anyone to publish", function ()
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   204
				local ok = service:publish("test", "stranger", "item1", "foo");
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   205
				assert.is_true(ok);
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   206
			end);
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   207
		end);
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   208
		describe("subscribers", function ()
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   209
			local service;
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   210
			before_each(function ()
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   211
				service = pubsub.new();
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   212
				service:create("test", true, { publish_model = "subscribers" });
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   213
			end);
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   214
			it("should not allow non-subscribers to publish", function ()
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   215
				local ok, err = service:publish("test", "stranger", "item1", "foo");
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   216
				assert.is_falsy(ok);
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   217
				assert.equals("forbidden", err);
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   218
			end);
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   219
			it("should allow subscribers to publish without an affiliation", function ()
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   220
				assert.is_true(service:add_subscription("test", "stranger", "stranger"));
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   221
				local ok  = service:publish("test", "stranger", "item1", "foo");
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   222
				assert.is_true(ok);
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   223
			end);
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   224
			it("should allow publishers to publish without a subscription", function ()
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   225
				assert(service:set_affiliation("test", true, "mypublisher", "publisher"));
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   226
				local ok, err = service:publish("test", "mypublisher", "item1", "foo");
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   227
				assert.is_true(ok);
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   228
			end);
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   229
			it("should allow owners to publish without a subscription", function ()
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   230
				assert(service:set_affiliation("test", true, "myowner", "owner"));
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   231
				local ok = service:publish("test", "myowner", "item1", "foo");
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   232
				assert.is_true(ok);
5f03fe90704f util.pubsub tests: Add tests for publish_model (publishers, open, subscribers)
Matthew Wild <mwild1@gmail.com>
parents: 9172
diff changeset
   233
			end);
9172
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   234
		end);
3ec013185c15 util.pubsub tests: Add some initial access model tests (open and whitelist)
Matthew Wild <mwild1@gmail.com>
parents: 9162
diff changeset
   235
	end);
8561
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   236
end);