tests/integration/roster.lua

1
local harness = require "harness";
2
local contact = "carol@"..harness.domain;
3
 
4
return {
5
	{ "fetches an empty roster", function (t)
6
		t.client("bob", function (c)
7
			c.roster:fetch(t.cb(function (roster, err)
8
				assert(roster, "fetch failed: "..tostring(err));
9
				assert(next(roster.items) == nil, "expected empty roster");
10
				t.done();
11
			end));
12
		end, { plugins = { "roster" } });
13
	end };
14
 
15
	{ "adds, pushes, fetches and removes a contact", function (t)
16
		t.client("alice", function (c)
17
			-- The server only pushes roster changes to resources that have requested
18
			-- the roster, and the push may arrive after the result of the change.
19
			c.roster:fetch(t.cb(function (roster, err)
20
				assert(roster, "fetch failed: "..tostring(err));
21
				c:hook("roster/item-added", t.cb(function (item)
22
					assert(item.jid == contact, "unexpected push: "..tostring(item.jid));
23
					assert(item.name == "Carol", "name not in push");
24
					assert(item.groups[1] == "Friends", "group not in push");
25
					c.roster:fetch(t.cb(function (roster, err)
26
						assert(roster, "fetch failed: "..tostring(err));
27
						assert(roster.items[contact], "contact missing from fetched roster");
28
						assert(roster.items[contact].name == "Carol");
29
						c:hook("roster/item-removed", t.cb(function (item)
30
							assert(item.jid == contact, "unexpected push: "..tostring(item.jid));
31
							assert(not c.roster.items[contact], "contact still in roster");
32
							t.done();
33
						end));
34
						c.roster:delete_contact(contact, t.cb(function (ok, err)
35
							assert(ok, "delete_contact failed: "..tostring(err));
36
						end));
37
					end));
38
				end));
39
				c.roster:add_contact(contact, "Carol", { "Friends" }, t.cb(function (ok, err)
40
					assert(ok, "add_contact failed: "..tostring(err));
41
				end));
42
			end));
43
		end, { plugins = { "roster" } });
44
	end };
45
};