test: make FakeClient profile dependent and add some tools to test MUC

This commit is contained in:
souliane 2014-01-10 18:15:02 +01:00
commit ddcfdd99ac

View file

@ -63,9 +63,12 @@ class FakeSAT(object):
self.init()
def init(self):
"""This can be called by tests that check for sent and stored messages"""
"""This can be called by tests that check for sent and stored messages,
uses FakeClient or get/set some other data that need to be cleaned"""
self.sent_messages = []
self.stored_messages = []
self.plugins = {}
self.profiles = {}
def delContact(self, to, profile_key):
#TODO
@ -81,6 +84,8 @@ class FakeSAT(object):
self.sendAndStoreMessage({"to": JID(to_s)})
def sendAndStoreMessage(self, mess_data, skip_send=False, profile=None):
"""Save the information to check later to whom messages have been sent and
if entries have been added to the history"""
if not skip_send:
self.sent_messages.append(mess_data["to"])
self.stored_messages.append(mess_data["to"])
@ -104,12 +109,50 @@ class FakeSAT(object):
return defer.succeed(jid_ if self.memory.hasServerFeature(feature, jid_, profile_key) else None)
def getClientHostJid(self, profile_key):
return JID(Const.JID[0].host)
return Const.PROFILE_DICT[profile_key].host
def getClient(self, profile_key):
"""Convenient method to get client from profile key
@return: client or None if it doesn't exist"""
profile = self.memory.getProfileName(profile_key)
if not profile:
return None
if profile not in self.profiles:
self.profiles[profile] = FakeClient(self, profile)
self.profiles[profile].client_initialized.callback(None)
return self.profiles[profile]
def getJidNStream(self, profile_key):
"""Convenient method to get jid and stream from profile key
@return: tuple (jid, xmlstream) from profile, can be None"""
return (Const.PROFILE_DICT[profile_key], None)
def isConnected(self, profile):
return True
def getSentMessage(self, message_index, profile_index):
"""Called by tests. FakeClient instances associated to each profile must have
been previously initialized with the method FakeSAT.getClient.
@return: XML representation of the <message_index>th sent message for given profile"""
return self.profiles[Const.PROFILE[profile_index]].xmlstream.sent[message_index]
def countSentMessages(self, profiles=Const.PROFILE):
"""Called by tests. FakeClient instances associated to each profile must have
been previously initialized with the method FakeSAT.getClient.
@param profiles: list of profiles
@return: a list containing the number of sent messages for each of the specified profiles"""
result = []
for profile in profiles:
result.append(len(self.profiles[profile].xmlstream.sent))
return result
class FakeBridge(object):
"""Class to simulate and test bridge calls"""
def __init__(self):
self.methods = {}
def expectCall(self, name, *check_args, **check_kwargs):
def checkCall(*args, **kwargs):
if args != check_args or kwargs != check_kwargs:
@ -128,6 +171,13 @@ class FakeBridge(object):
def addSignal(self, name, int_suffix, signature):
pass
def addTestCallback(self, name, method):
"""This can be used to register callbacks for bridge methods AND signals.
Contrary to expectCall, this will not check if the method or signal is
called/sent with the correct arguments, it will instead run the callback
of your choice."""
setattr(self, name, method)
class FakeParams(Params):
"""Class to simulate and test params object. The methods of Params that could
@ -168,6 +218,7 @@ class FakeMemory(Memory):
# manipulating basic stuff, the others should be overwritten when needed
self.host = host
self.params = FakeParams(host, None)
self.config = self.parseMainConf()
self.init()
def init(self):
@ -218,11 +269,12 @@ class FakeTriggerManager(object):
class FakeRosterProtocol(SatRosterProtocol):
"""This class is used by FakeClient (one instance per profile)"""
def __init__(self, host, parent):
SatRosterProtocol.__init__(self, host)
self.parent = parent
self.addItem(Const.JID[0])
self.addItem(parent.jid)
def addItem(self, jid, *args, **kwargs):
if not args and not kwargs:
@ -237,15 +289,30 @@ class FakeRosterProtocol(SatRosterProtocol):
self.onRosterSet(roster_item)
class FakeClient(object):
def __init__(self, host):
self.host = host
self.profile = 'test_profile'
self.jid = Const.JID[0]
self.roster = FakeRosterProtocol(host, self)
class FakeXmlStream(object):
"""This class is used by FakeClient (one instance per profile)"""
def __init__(self):
self.sent = []
def send(self, obj):
pass
"""Save the sent messages to compare them later"""
self.sent.append(obj.toXml())
class FakeClient(object):
"""Tests involving more than one profile need one instance of this class per profile"""
def __init__(self, host, profile=None):
self.host = host
self.profile = profile if profile else Const.PROFILE[0]
self.jid = Const.PROFILE_DICT[self.profile]
self.roster = FakeRosterProtocol(host, self)
self.client_initialized = defer.Deferred()
self.xmlstream = FakeXmlStream()
def send(self, obj):
self.xmlstream.send(obj)
class SatTestCase(unittest.TestCase):