Diff

spec/util_pubsub_spec.lua @ 9169:3ec013185c15

util.pubsub tests: Add some initial access model tests (open and whitelist)
author Matthew Wild <mwild1@gmail.com>
date Sat, 11 Aug 2018 19:18:13 +0100
parent 9159:a0fd7064f4ac
child 9171:5f03fe90704f
line wrap: on
line diff
--- a/spec/util_pubsub_spec.lua	Sat Aug 11 19:59:19 2018 +0200
+++ b/spec/util_pubsub_spec.lua	Sat Aug 11 19:18:13 2018 +0100
@@ -111,4 +111,56 @@
 		end);
 
 	end);
+
+	describe("access model", function ()
+		describe("open", function ()
+			local service;
+			before_each(function ()
+				service = pubsub.new();
+				-- Do not supply any config, 'open' should be default
+				service:create("test", true);
+			end);
+			it("should be the default", function ()
+				local ok, config = service:get_node_config("test", true);
+				assert.equal("open", config.access_model);
+			end);
+			it("should allow anyone to subscribe", function ()
+				local ok = service:add_subscription("test", "stranger", "stranger");
+				assert.is_true(ok);
+			end);
+			it("should not allow anyone to publish", function ()
+				assert.is_true(service:add_subscription("test", "stranger", "stranger"));
+				local ok, err = service:publish("test", "stranger", "item1", "foo");
+				assert.is_falsy(ok);
+				assert.equals("forbidden", err);
+			end);
+			it("should still reject outcast-affiliated entities", function ()
+				assert(service:set_affiliation("test", true, "enemy", "outcast"));
+				local ok, err = service:add_subscription("test", "enemy", "enemy");
+				assert.is_falsy(ok);
+				assert.equal("forbidden", err);
+			end);
+		end);
+		describe("whitelist", function ()
+			local service;
+			before_each(function ()
+				service = assert(pubsub.new());
+				assert.is_true(service:create("test", true, { access_model = "whitelist" }));
+			end);
+			it("should be present in the configuration", function ()
+				local ok, config = service:get_node_config("test", true);
+				assert.equal("whitelist", config.access_model);
+			end);
+			it("should not allow anyone to subscribe", function ()
+				local ok, err = service:add_subscription("test", "stranger", "stranger");
+				assert.is_false(ok);
+				assert.equals("forbidden", err);
+			end);
+			it("should not allow anyone to publish", function ()
+				local ok, err = service:publish("test", "stranger", "item1", "foo");
+				assert.is_falsy(ok);
+				assert.equals("forbidden", err);
+			end);
+		end);
+	end);
 end);