File

tests/test_util_xmppstream.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 7241:5e7797822f19
line wrap: on
line source

function new(new_stream, _M)
	local function test(xml, expect_success, ex)
		local stanzas = {};
		local session = { notopen = true };
		local callbacks = {
			stream_ns = "streamns";
			stream_tag = "stream";
			default_ns = "stanzans";
			streamopened = function (_session)
				assert_equal(session, _session);
				assert_equal(session.notopen, true);
				_session.notopen = nil;
				return true;
			end;
			handlestanza = function (_session, stanza)
				assert_equal(session, _session);
				assert_equal(_session.notopen, nil);
				table.insert(stanzas, stanza);
			end;
			streamclosed = function (_session)
				assert_equal(session, _session);
				assert_equal(_session.notopen, nil);
				_session.notopen = nil;
			end;
		}
		if type(ex) == "table" then
			for k, v in pairs(ex) do
				if k ~= "_size_limit" then
					callbacks[k] = v;
				end
			end
		end
		local stream = new_stream(session, callbacks, size_limit);
		local ok, err = pcall(function ()
			assert(stream:feed(xml));
		end);

		if ok and type(expect_success) == "function" then
			expect_success(stanzas);
		end
		assert_equal(not not ok, not not expect_success, "Expected "..(expect_success and ("success ("..tostring(err)..")") or "failure"));
	end

	local function test_stanza(stanza, expect_success, ex)
		return test([[<stream:stream xmlns:stream="streamns" xmlns="stanzans">]]..stanza, expect_success, ex);
	end

	test([[<stream:stream xmlns:stream="streamns"/>]], true);
	test([[<stream xmlns="streamns"/>]], true);

	test([[<stream1 xmlns="streamns"/>]], false);
	test([[<stream xmlns="streamns1"/>]], false);
	test("<>", false);

	test_stanza("<message/>", function (stanzas)
		assert_equal(#stanzas, 1);
		assert_equal(stanzas[1].name, "message");
	end);
	test_stanza("< message>>>>/>\n", false);

	test_stanza([[<x xmlns:a="b">
		<y xmlns:a="c">
			<a:z/>
		</y>
		<a:z/>
	</x>]], function (stanzas)
		assert_equal(#stanzas, 1);
		local s = stanzas[1];
		assert_equal(s.name, "x");
		assert_equal(#s.tags, 2);

		assert_equal(s.tags[1].name, "y");
		assert_equal(s.tags[1].attr.xmlns, nil);

		assert_equal(s.tags[1].tags[1].name, "z");
		assert_equal(s.tags[1].tags[1].attr.xmlns, "c");

		assert_equal(s.tags[2].name, "z");
		assert_equal(s.tags[2].attr.xmlns, "b");

		assert_equal(s.namespaces, nil);
	end);
end