File

tests/test_util_stanza.lua @ 8791:8da11142fabf

muc: Allow clients to change multiple affiliations or roles at once (#345) According to XEP-0045 sections 9.2, 9.5 and 9.8 affiliation lists and role lists should allow mass-modification. Prosody however would just use the first entry of the list and ignore the rest. This is fixed by introducing a `for` loop to `set` stanzas of the respective `muc#admin` namespace. In order for this loop to work, the error handling was changed a little. Prosody no longer returns after the first error. Instead, an error reply is sent for each malformed or otherwise wrong entry, but the loop keeps going over the other entries. This may lead to multiple error messages being sent for one client request. A notable exception from this is when the XML Schema for `muc#admin` requests is violated. In that case the loop is aborted with an error message to the client. The change is a bit bigger than that in order to have the loop only for `set` stanzas without changing the behaviour of the `get` stanzas. This is now more in line with trunk, where there are separate methods for each stanza type. References: #345
author Lennart Sauerbeck <devel@lennart.sauerbeck.org>
date Sat, 18 Mar 2017 18:47:28 +0100
parent 7961:ff556d010225
line wrap: on
line source

-- Prosody IM
-- Copyright (C) 2008-2010 Matthew Wild
-- Copyright (C) 2008-2010 Waqas Hussain
--
-- This project is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
--


function preserialize(preserialize, st)
	local stanza = st.stanza("message", { a = "a" });
	local stanza2 = preserialize(stanza);
	assert_is(stanza2 and stanza.name, "preserialize returns a stanza");
	assert_is_not(stanza2.tags, "Preserialized stanza has no tag list");
	assert_is_not(stanza2.last_add, "Preserialized stanza has no last_add marker");
	assert_is_not(getmetatable(stanza2), "Preserialized stanza has no metatable");
end

function deserialize(deserialize, st)
	local stanza = st.stanza("message", { a = "a" });

	local stanza2 = deserialize(st.preserialize(stanza));
	assert_is(stanza2 and stanza.name, "deserialize returns a stanza");
	assert_table(stanza2.attr, "Deserialized stanza has attributes");
	assert_equal(stanza2.attr.a, "a", "Deserialized stanza retains attributes");
	assert_table(getmetatable(stanza2), "Deserialized stanza has metatable");
end

function stanza(stanza)
	local s = stanza("foo", { xmlns = "myxmlns", a = "attr-a" });
	assert_equal(s.name, "foo");
	assert_equal(s.attr.xmlns, "myxmlns");
	assert_equal(s.attr.a, "attr-a");

	local s1 = stanza("s1");
	assert_equal(s1.name, "s1");
	assert_equal(s1.attr.xmlns, nil);
	assert_equal(#s1, 0);
	assert_equal(#s1.tags, 0);

	s1:tag("child1");
	assert_equal(#s1.tags, 1);
	assert_equal(s1.tags[1].name, "child1");

	s1:tag("grandchild1"):up();
	assert_equal(#s1.tags, 1);
	assert_equal(s1.tags[1].name, "child1");
	assert_equal(#s1.tags[1], 1);
	assert_equal(s1.tags[1][1].name, "grandchild1");

	s1:up():tag("child2");
	assert_equal(#s1.tags, 2, tostring(s1));
	assert_equal(s1.tags[1].name, "child1");
	assert_equal(s1.tags[2].name, "child2");
	assert_equal(#s1.tags[1], 1);
	assert_equal(s1.tags[1][1].name, "grandchild1");

	s1:up():text("Hello world");
	assert_equal(#s1.tags, 2);
	assert_equal(#s1, 3);
	assert_equal(s1.tags[1].name, "child1");
	assert_equal(s1.tags[2].name, "child2");
	assert_equal(#s1.tags[1], 1);
	assert_equal(s1.tags[1][1].name, "grandchild1");
end

function message(message)
	local m = message();
	assert_equal(m.name, "message");
end

function iq(iq)
	local i = iq();
	assert_equal(i.name, "iq");
end

function presence(presence)
	local p = presence();
	assert_equal(p.name, "presence");
end

function reply(reply, _M)
	do
		-- Test stanza
		local s = _M.stanza("s", { to = "touser", from = "fromuser", id = "123" })
			:tag("child1");
		-- Make reply stanza
		local r = reply(s);
		assert_equal(r.name, s.name);
		assert_equal(r.id, s.id);
		assert_equal(r.attr.to, s.attr.from);
		assert_equal(r.attr.from, s.attr.to);
		assert_equal(#r.tags, 0, "A reply should not include children of the original stanza");
	end

	do
		-- Test stanza
		local s = _M.stanza("iq", { to = "touser", from = "fromuser", id = "123", type = "get" })
			:tag("child1");
		-- Make reply stanza
		local r = reply(s);
		assert_equal(r.name, s.name);
		assert_equal(r.id, s.id);
		assert_equal(r.attr.to, s.attr.from);
		assert_equal(r.attr.from, s.attr.to);
		assert_equal(r.attr.type, "result");
		assert_equal(#r.tags, 0, "A reply should not include children of the original stanza");
	end

	do
		-- Test stanza
		local s = _M.stanza("iq", { to = "touser", from = "fromuser", id = "123", type = "set" })
			:tag("child1");
		-- Make reply stanza
		local r = reply(s);
		assert_equal(r.name, s.name);
		assert_equal(r.id, s.id);
		assert_equal(r.attr.to, s.attr.from);
		assert_equal(r.attr.from, s.attr.to);
		assert_equal(r.attr.type, "result");
		assert_equal(#r.tags, 0, "A reply should not include children of the original stanza");
	end
end

function error_reply(error_reply, _M)
	do
		-- Test stanza
		local s = _M.stanza("s", { to = "touser", from = "fromuser", id = "123" })
			:tag("child1");
		-- Make reply stanza
		local r = error_reply(s);
		assert_equal(r.name, s.name);
		assert_equal(r.id, s.id);
		assert_equal(r.attr.to, s.attr.from);
		assert_equal(r.attr.from, s.attr.to);
		assert_equal(#r.tags, 1);
	end

	do
		-- Test stanza
		local s = _M.stanza("iq", { to = "touser", from = "fromuser", id = "123", type = "get" })
			:tag("child1");
		-- Make reply stanza
		local r = error_reply(s);
		assert_equal(r.name, s.name);
		assert_equal(r.id, s.id);
		assert_equal(r.attr.to, s.attr.from);
		assert_equal(r.attr.from, s.attr.to);
		assert_equal(r.attr.type, "error");
		assert_equal(#r.tags, 1);
	end
end