+
+
+def _muc_msg(nick="Alice", room="room@conf", type_="groupchat"):
+ from_ = Mock()
+ from_.jid = f"{room}/{nick}"
+ return {"type": type_, "mucroom": room, "mucnick": nick, "from": from_, "body": "urlbug: plugin deactivate dice"}
+
+
+def test_muc_rank_comes_from_xep_0045_roster():
+ fake = _fake_bot()
+ fake.plugin = {"xep_0045": Mock()}
+ fake.plugin["xep_0045"].get_jid_property.side_effect = \
+ lambda room, nick, prop: {"role": "moderator", "affiliation": "admin"}[prop]
+ assert Bot.get_muc_rank(fake, _muc_msg()) == {"role": "moderator", "affiliation": "admin"}
+ fake.plugin["xep_0045"].get_jid_property.assert_any_call("room@conf", "Alice", "role")
+
+
+def test_muc_rank_empty_for_private_chat():
+ fake = _fake_bot()
+ fake.plugin = {"xep_0045": Mock()}
+ assert Bot.get_muc_rank(fake, _muc_msg(type_="chat")) == {}
+ fake.plugin["xep_0045"].get_jid_property.assert_not_called()
+
+
+def test_muc_rank_lookup_failure_is_not_fatal():
+ fake = _fake_bot()
+ fake.plugin = {"xep_0045": Mock()}
+ fake.plugin["xep_0045"].get_jid_property.side_effect = KeyError("unknown occupant")
+ assert Bot.get_muc_rank(fake, _muc_msg()) == {}
+
+
+def test_message_body_carries_rank():
+ import json
+ body = json.loads(Bot.get_amqp_message_body(_muc_msg(), len("urlbug: "), "room@conf",
+ {"role": "moderator", "affiliation": "owner"}))
+ assert body == {"from": "room@conf/Alice", "to": "room@conf", "body": "plugin deactivate dice",
+ "role": "moderator", "affiliation": "owner"}
+
+
+def test_message_body_without_rank_is_unchanged():
+ import json
+ body = json.loads(Bot.get_amqp_message_body(_muc_msg(), len("urlbug: "), "room@conf"))
+ assert body == {"from": "room@conf/Alice", "to": "room@conf", "body": "plugin deactivate dice"}