diff src/test/helpers_plugins.py @ 829:187d2443c82d

test: improvements for the helpers classes: - simplification for retrieving the sent messages - an expected bridge call which is already set can no more be overwritten with new arguments for the same method name. The new arguments are stored in a FIFO list instead, and each call of the method also registers the arguments for the next call.
author souliane <souliane@mailoo.org>
date Wed, 15 Jan 2014 23:22:07 +0100
parents 1fe00f0c9a91
children 91e5becc6623
line wrap: on
line diff
--- a/src/test/helpers_plugins.py	Fri Jan 17 15:02:46 2014 +0100
+++ b/src/test/helpers_plugins.py	Wed Jan 15 23:22:07 2014 +0100
@@ -21,7 +21,7 @@
 """ Helpers class for plugin dependencies """
 
 from constants import Const
-from sat.plugins.plugin_xep_0045 import XEP_0045
+from sat.plugins import plugin_xep_0045
 from twisted.internet import defer
 from wokkel.muc import Room, User
 
@@ -33,6 +33,12 @@
         self.joined_rooms = {}
 
     def join(self, roomJID, nick, profile):
+        """
+        @param roomJID: the room JID
+        @param nick: nick to be used in the room
+        @param profile: the profile of the user joining the room
+        @return: the deferred joined wokkel.muc.Room instance
+        """
         roster = {}
 
         # ask the other profiles to fill our roster
@@ -69,10 +75,30 @@
             except (AttributeError, KeyError):
                 pass
 
-        return room
+        return defer.succeed(room)
+
+    def leave(self, roomJID, profile):
+        """
+        @param roomJID: the room JID
+        @param profile: the profile of the user joining the room
+        @return: a dummy deferred
+        """
+        room = self.joined_rooms[roomJID.userhost()]
+        # remove ourself from the other rosters
+        for i in xrange(0, len(Const.PROFILE)):
+            other_profile = Const.PROFILE[i]
+            if other_profile == profile:
+                continue
+            try:
+                other_room = self.plugin_parent.clients[other_profile].joined_rooms[roomJID.userhost()]
+                del other_room.roster[room.nick]
+            except (AttributeError, KeyError):
+                pass
+        del self.joined_rooms[roomJID.userhost()]
+        return defer.Deferred()
 
 
-class FakeXEP_0045(XEP_0045):
+class FakeXEP_0045(plugin_xep_0045.XEP_0045):
 
     def __init__(self, host):
         self.host = host
@@ -81,22 +107,50 @@
             self.clients[profile] = FakeMUCClient(self)
 
     def join(self, room_jid, nick, options={}, profile_key='@DEFAULT@'):
+        """
+        @param roomJID: the room JID
+        @param nick: nick to be used in the room
+        @param options: ignore
+        @param profile_key: the profile of the user joining the room
+        @return: the deferred joined wokkel.muc.Room instance or None
+        """
         profile = self.host.memory.getProfileName(profile_key)
         room_jid_s = room_jid.userhost()
         if room_jid_s in self.clients[profile].joined_rooms:
             return defer.succeed(None)
         room = self.clients[profile].join(room_jid, nick, profile)
-        return defer.succeed(room)
+        return room
 
     def joinRoom(self, muc_index, user_index):
         """Called by tests
-        @return: the nickname of the user in the joined room"""
-        muc = Const.MUC[muc_index]
+        @return: the nickname of the user who joined room"""
+        muc_jid = Const.MUC[muc_index]
         nick = Const.JID[user_index].user
         profile = Const.PROFILE[user_index]
-        self.join(muc, nick, profile_key=profile)
+        self.join(muc_jid, nick, profile_key=profile)
         return self.getNick(muc_index, user_index)
 
+    def leave(self, room_jid, profile_key='@DEFAULT@'):
+        """
+        @param roomJID: the room JID
+        @param profile_key: the profile of the user leaving the room
+        @return: a dummy deferred
+        """
+        profile = self.host.memory.getProfileName(profile_key)
+        room_jid_s = room_jid.userhost()
+        if room_jid_s not in self.clients[profile].joined_rooms:
+            raise plugin_xep_0045.UnknownRoom("This room has not been joined")
+        return self.clients[profile].leave(room_jid, profile)
+
+    def leaveRoom(self, muc_index, user_index):
+        """Called by tests
+        @return: the nickname of the user who left the room"""
+        muc_jid = Const.MUC[muc_index]
+        nick = self.getNick(muc_index, user_index)
+        profile = Const.PROFILE[user_index]
+        self.leave(muc_jid, profile_key=profile)
+        return nick
+
     def getRoom(self, muc_index, user_index):
         """Called by tests
         @return: a wokkel.muc.Room instance"""