comparison src/test/helpers.py @ 794:52c4b755aba6

test: make FakeClient profile dependent and add some tools to test MUC
author souliane <souliane@mailoo.org>
date Fri, 10 Jan 2014 18:15:02 +0100
parents 2136be5a44a8
children 1fe00f0c9a91
comparison
equal deleted inserted replaced
793:cb2db0d85029 794:52c4b755aba6
61 self.memory = FakeMemory(self) 61 self.memory = FakeMemory(self)
62 self.trigger = FakeTriggerManager() 62 self.trigger = FakeTriggerManager()
63 self.init() 63 self.init()
64 64
65 def init(self): 65 def init(self):
66 """This can be called by tests that check for sent and stored messages""" 66 """This can be called by tests that check for sent and stored messages,
67 uses FakeClient or get/set some other data that need to be cleaned"""
67 self.sent_messages = [] 68 self.sent_messages = []
68 self.stored_messages = [] 69 self.stored_messages = []
70 self.plugins = {}
71 self.profiles = {}
69 72
70 def delContact(self, to, profile_key): 73 def delContact(self, to, profile_key):
71 #TODO 74 #TODO
72 pass 75 pass
73 76
79 82
80 def sendMessage(self, to_s, msg, subject=None, mess_type='auto', extra={}, profile_key='@NONE@'): 83 def sendMessage(self, to_s, msg, subject=None, mess_type='auto', extra={}, profile_key='@NONE@'):
81 self.sendAndStoreMessage({"to": JID(to_s)}) 84 self.sendAndStoreMessage({"to": JID(to_s)})
82 85
83 def sendAndStoreMessage(self, mess_data, skip_send=False, profile=None): 86 def sendAndStoreMessage(self, mess_data, skip_send=False, profile=None):
87 """Save the information to check later to whom messages have been sent and
88 if entries have been added to the history"""
84 if not skip_send: 89 if not skip_send:
85 self.sent_messages.append(mess_data["to"]) 90 self.sent_messages.append(mess_data["to"])
86 self.stored_messages.append(mess_data["to"]) 91 self.stored_messages.append(mess_data["to"])
87 pass 92 pass
88 93
102 # call FakeMemory.init and FakeMemory.addServerFeature 107 # call FakeMemory.init and FakeMemory.addServerFeature
103 # in your tests to change the return value of this method 108 # in your tests to change the return value of this method
104 return defer.succeed(jid_ if self.memory.hasServerFeature(feature, jid_, profile_key) else None) 109 return defer.succeed(jid_ if self.memory.hasServerFeature(feature, jid_, profile_key) else None)
105 110
106 def getClientHostJid(self, profile_key): 111 def getClientHostJid(self, profile_key):
107 return JID(Const.JID[0].host) 112 return Const.PROFILE_DICT[profile_key].host
113
114 def getClient(self, profile_key):
115 """Convenient method to get client from profile key
116 @return: client or None if it doesn't exist"""
117 profile = self.memory.getProfileName(profile_key)
118 if not profile:
119 return None
120 if profile not in self.profiles:
121 self.profiles[profile] = FakeClient(self, profile)
122 self.profiles[profile].client_initialized.callback(None)
123 return self.profiles[profile]
124
125 def getJidNStream(self, profile_key):
126 """Convenient method to get jid and stream from profile key
127 @return: tuple (jid, xmlstream) from profile, can be None"""
128 return (Const.PROFILE_DICT[profile_key], None)
129
130 def isConnected(self, profile):
131 return True
132
133 def getSentMessage(self, message_index, profile_index):
134 """Called by tests. FakeClient instances associated to each profile must have
135 been previously initialized with the method FakeSAT.getClient.
136 @return: XML representation of the <message_index>th sent message for given profile"""
137 return self.profiles[Const.PROFILE[profile_index]].xmlstream.sent[message_index]
138
139 def countSentMessages(self, profiles=Const.PROFILE):
140 """Called by tests. FakeClient instances associated to each profile must have
141 been previously initialized with the method FakeSAT.getClient.
142 @param profiles: list of profiles
143 @return: a list containing the number of sent messages for each of the specified profiles"""
144 result = []
145 for profile in profiles:
146 result.append(len(self.profiles[profile].xmlstream.sent))
147 return result
108 148
109 149
110 class FakeBridge(object): 150 class FakeBridge(object):
111 """Class to simulate and test bridge calls""" 151 """Class to simulate and test bridge calls"""
152
153 def __init__(self):
154 self.methods = {}
112 155
113 def expectCall(self, name, *check_args, **check_kwargs): 156 def expectCall(self, name, *check_args, **check_kwargs):
114 def checkCall(*args, **kwargs): 157 def checkCall(*args, **kwargs):
115 if args != check_args or kwargs != check_kwargs: 158 if args != check_args or kwargs != check_kwargs:
116 print "\n\n--------------------" 159 print "\n\n--------------------"
126 pass 169 pass
127 170
128 def addSignal(self, name, int_suffix, signature): 171 def addSignal(self, name, int_suffix, signature):
129 pass 172 pass
130 173
174 def addTestCallback(self, name, method):
175 """This can be used to register callbacks for bridge methods AND signals.
176 Contrary to expectCall, this will not check if the method or signal is
177 called/sent with the correct arguments, it will instead run the callback
178 of your choice."""
179 setattr(self, name, method)
180
131 181
132 class FakeParams(Params): 182 class FakeParams(Params):
133 """Class to simulate and test params object. The methods of Params that could 183 """Class to simulate and test params object. The methods of Params that could
134 not be run (for example those using the storage attribute must be overwritten 184 not be run (for example those using the storage attribute must be overwritten
135 by a naive simulation of what they should do.""" 185 by a naive simulation of what they should do."""
166 def __init__(self, host): 216 def __init__(self, host):
167 # do not call Memory.__init__, we just want to call the methods that are 217 # do not call Memory.__init__, we just want to call the methods that are
168 # manipulating basic stuff, the others should be overwritten when needed 218 # manipulating basic stuff, the others should be overwritten when needed
169 self.host = host 219 self.host = host
170 self.params = FakeParams(host, None) 220 self.params = FakeParams(host, None)
221 self.config = self.parseMainConf()
171 self.init() 222 self.init()
172 223
173 def init(self): 224 def init(self):
174 """Tests that manipulate params, entities, features should 225 """Tests that manipulate params, entities, features should
175 re-initialise the memory first to not fake the result.""" 226 re-initialise the memory first to not fake the result."""
216 """We always return true to continue the action""" 267 """We always return true to continue the action"""
217 return True 268 return True
218 269
219 270
220 class FakeRosterProtocol(SatRosterProtocol): 271 class FakeRosterProtocol(SatRosterProtocol):
272 """This class is used by FakeClient (one instance per profile)"""
221 273
222 def __init__(self, host, parent): 274 def __init__(self, host, parent):
223 SatRosterProtocol.__init__(self, host) 275 SatRosterProtocol.__init__(self, host)
224 self.parent = parent 276 self.parent = parent
225 self.addItem(Const.JID[0]) 277 self.addItem(parent.jid)
226 278
227 def addItem(self, jid, *args, **kwargs): 279 def addItem(self, jid, *args, **kwargs):
228 if not args and not kwargs: 280 if not args and not kwargs:
229 # defaults values setted for the tests only 281 # defaults values setted for the tests only
230 kwargs["subscriptionTo"] = True 282 kwargs["subscriptionTo"] = True
235 attrs['name'] = roster_item.name 287 attrs['name'] = roster_item.name
236 self.host.bridge.expectCall("newContact", jid.full(), attrs, roster_item.groups, self.parent.profile) 288 self.host.bridge.expectCall("newContact", jid.full(), attrs, roster_item.groups, self.parent.profile)
237 self.onRosterSet(roster_item) 289 self.onRosterSet(roster_item)
238 290
239 291
292 class FakeXmlStream(object):
293 """This class is used by FakeClient (one instance per profile)"""
294
295 def __init__(self):
296 self.sent = []
297
298 def send(self, obj):
299 """Save the sent messages to compare them later"""
300 self.sent.append(obj.toXml())
301
302
240 class FakeClient(object): 303 class FakeClient(object):
241 def __init__(self, host): 304 """Tests involving more than one profile need one instance of this class per profile"""
305
306 def __init__(self, host, profile=None):
242 self.host = host 307 self.host = host
243 self.profile = 'test_profile' 308 self.profile = profile if profile else Const.PROFILE[0]
244 self.jid = Const.JID[0] 309 self.jid = Const.PROFILE_DICT[self.profile]
245 self.roster = FakeRosterProtocol(host, self) 310 self.roster = FakeRosterProtocol(host, self)
311 self.client_initialized = defer.Deferred()
312 self.xmlstream = FakeXmlStream()
246 313
247 def send(self, obj): 314 def send(self, obj):
248 pass 315 self.xmlstream.send(obj)
249 316
250 317
251 class SatTestCase(unittest.TestCase): 318 class SatTestCase(unittest.TestCase):
252 319
253 def assertEqualXML(self, xml, expected, ignore_blank=False): 320 def assertEqualXML(self, xml, expected, ignore_blank=False):