spec/muc_util_spec.lua
author Matthew Wild <mwild1@gmail.com>
Mon, 20 Feb 2023 18:10:15 +0000
branch0.12
changeset 12898 0598d822614f
parent 10719 ad86b93093a3
permissions -rw-r--r--
mod_websocket: Fire pre-session-close event (fixes #1800) This event was added in a7c183bb4e64 and is required to make mod_smacks know that a session was intentionally closed and shouldn't be hibernated (see fcea4d9e7502). Because this was missing from mod_websocket's session.close(), mod_smacks would always attempt to hibernate websocket sessions even if they closed cleanly. That mod_websocket has its own copy of session.close() is something to fix another day (probably not in the stable branch). So for now this commit makes the minimal change to get things working again. Thanks to Damian and the Jitsi team for reporting.

local muc_util;

local st = require "util.stanza";

do
	-- XXX Hack for lack of a mock moduleapi
	local env = setmetatable({
		module = {
			_shared = {};
			-- Close enough to the real module:shared() for our purposes here
			shared = function (self, name)
				local t = self._shared[name];
				if t == nil then
					t = {};
					self._shared[name] = t;
				end
				return t;
			end;
		}
	}, { __index = _ENV or _G });
	muc_util = require "util.envload".envloadfile("plugins/muc/util.lib.lua", env)();
	end

describe("muc/util", function ()
	describe("filter_muc_x()", function ()
		it("correctly filters muc#user", function ()
			local stanza = st.message({ to = "to", from = "from", id = "foo" })
				:tag("x", { xmlns = "http://jabber.org/protocol/muc#user" })
					:tag("invite", { to = "user@example.com" });

			assert.equal(1, #stanza.tags);
			assert.equal(stanza, muc_util.filter_muc_x(stanza));
			assert.equal(0, #stanza.tags);
		end);

		it("correctly filters muc#user on a cloned stanza", function ()
			local stanza = st.message({ to = "to", from = "from", id = "foo" })
				:tag("x", { xmlns = "http://jabber.org/protocol/muc#user" })
					:tag("invite", { to = "user@example.com" });

			assert.equal(1, #stanza.tags);
			local filtered = muc_util.filter_muc_x(st.clone(stanza));
			assert.equal(1, #stanza.tags);
			assert.equal(0, #filtered.tags);
		end);
	end);
end);