changeset 792:2136be5a44a8

test: define the constants JIDs and profiles as lists
author souliane <souliane@mailoo.org>
date Thu, 09 Jan 2014 11:18:33 +0100 (2014-01-09)
parents 23b0c949b86c
children cb2db0d85029
files src/test/constants.py src/test/helpers.py src/test/test_core_xmpp.py src/test/test_plugin_xep_0033.py src/test/test_plugin_xep_0085.py
diffstat 5 files changed, 53 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/src/test/constants.py	Fri Jan 10 18:07:36 2014 +0100
+++ b/src/test/constants.py	Thu Jan 09 11:18:33 2014 +0100
@@ -18,21 +18,17 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 from sat.core.i18n import _, D_
-from twisted.words.protocols.jabber.jid import JID
+from twisted.words.protocols.jabber import jid
 
 
 class Const(object):
 
-    TEST_JID_STR = u"test@example.org/SàT"
-    TEST_JID = JID(TEST_JID_STR)
+    PROFILE = ['test_profile', 'test_profile2', 'test_profile3', 'test_profile4']
+    JID_STR = [u"test@example.org/SàT", u"sender@example.net/house", u"sender@example.net/work", u"test@server.net/res"]
+    JID = [jid.JID(jid_s) for jid_s in JID_STR]
 
-    TEST_JID_2_STR = u"sender@example.net/house"
-    TEST_JID_2 = JID(TEST_JID_2_STR)
-
-    TEST_JID_3_STR = u"sender@example.net/work"
-    TEST_JID_3 = JID(TEST_JID_3_STR)
-
-    TEST_PROFILE = 'test_profile'
+    MUC_STR = [u"room@chat.server.domain", u"sat_game@chat.server.domain"]
+    MUC = [jid.JID(jid_s) for jid_s in MUC_STR]
 
     NO_SECURITY_LIMIT = -1
     SECURITY_LIMIT = 0
--- a/src/test/helpers.py	Fri Jan 10 18:07:36 2014 +0100
+++ b/src/test/helpers.py	Thu Jan 09 11:18:33 2014 +0100
@@ -104,7 +104,7 @@
         return defer.succeed(jid_ if self.memory.hasServerFeature(feature, jid_, profile_key) else None)
 
     def getClientHostJid(self, profile_key):
-        return JID(Const.TEST_JID.host)
+        return JID(Const.JID[0].host)
 
 
 class FakeBridge(object):
@@ -149,7 +149,7 @@
 
     def getProfileName(self, profile_key, return_profile_keys=False):
         if profile_key == '@DEFAULT@':
-            return Const.TEST_PROFILE
+            return Const.PROFILE[0]
         elif profile_key == '@NONE@':
             raise exceptions.ProfileNotSetError
         else:
@@ -222,7 +222,7 @@
     def __init__(self, host, parent):
         SatRosterProtocol.__init__(self, host)
         self.parent = parent
-        self.addItem(Const.TEST_JID)
+        self.addItem(Const.JID[0])
 
     def addItem(self, jid, *args, **kwargs):
         if not args and not kwargs:
@@ -241,7 +241,7 @@
     def __init__(self, host):
         self.host = host
         self.profile = 'test_profile'
-        self.jid = Const.TEST_JID
+        self.jid = Const.JID[0]
         self.roster = FakeRosterProtocol(host, self)
 
     def send(self, obj):
--- a/src/test/test_core_xmpp.py	Fri Jan 10 18:07:36 2014 +0100
+++ b/src/test/test_core_xmpp.py	Thu Jan 09 11:18:33 2014 +0100
@@ -32,11 +32,11 @@
 
     def setUp(self):
         self.host = helpers.FakeSAT()
-        self.client = xmpp.SatXMPPClient(self.host, "test_profile", JID("test@example.org"), "test")
+        self.client = xmpp.SatXMPPClient(self.host, Const.PROFILE[0], JID("test@example.org"), "test")
 
     def test_init(self):
         """Check that init values are correctly initialised"""
-        self.assertEqual(self.client.profile, "test_profile")
+        self.assertEqual(self.client.profile, Const.PROFILE[0])
         print self.client.host
         self.assertEqual(self.client.host_app, self.host)
         self.assertTrue(isinstance(self.client.client_initialized, defer.Deferred))
@@ -56,7 +56,7 @@
         </message>
         """
         stanza = parseXml(xml)
-        self.host.bridge.expectCall("newMessage", u"sender@example.net/house", u"test", u"chat", u"test@example.org/SàT", {}, profile="test_profile")
+        self.host.bridge.expectCall("newMessage", u"sender@example.net/house", u"test", u"chat", u"test@example.org/SàT", {}, profile=Const.PROFILE[0])
         self.message.onMessage(stanza)
 
 
@@ -68,13 +68,13 @@
         self.roster.parent = helpers.FakeClient(self.host)
 
     def test_onRosterSet(self):
-        roster_item = RosterItem(Const.TEST_JID)
+        roster_item = RosterItem(Const.JID[0])
         roster_item.name = u"Test Man"
         roster_item.subscriptionTo = True
         roster_item.subscriptionFrom = True
         roster_item.ask = False
         roster_item.groups = set([u"Test Group 1", u"Test Group 2", u"Test Group 3"])
-        self.host.bridge.expectCall("newContact", Const.TEST_JID_STR, {'to': 'True', 'from': 'True', 'ask': 'False', 'name': u'Test Man'}, set([u"Test Group 1", u"Test Group 2", u"Test Group 3"]), "test_profile")
+        self.host.bridge.expectCall("newContact", Const.JID_STR[0], {'to': 'True', 'from': 'True', 'ask': 'False', 'name': u'Test Man'}, set([u"Test Group 1", u"Test Group 2", u"Test Group 3"]), Const.PROFILE[0])
         self.roster.onRosterSet(roster_item)
 
 
@@ -86,29 +86,29 @@
         self.presence.parent = helpers.FakeClient(self.host)
 
     def test_availableReceived(self):
-        self.host.bridge.expectCall("presenceUpdate", Const.TEST_JID_STR, "xa", 15, {'default': "test status", 'fr': 'statut de test'}, Const.TEST_PROFILE)
-        self.presence.availableReceived(Const.TEST_JID, 'xa', {None: "test status", 'fr': 'statut de test'}, 15)
+        self.host.bridge.expectCall("presenceUpdate", Const.JID_STR[0], "xa", 15, {'default': "test status", 'fr': 'statut de test'}, Const.PROFILE[0])
+        self.presence.availableReceived(Const.JID[0], 'xa', {None: "test status", 'fr': 'statut de test'}, 15)
 
     def test_availableReceived_empty_statuses(self):
-        self.host.bridge.expectCall("presenceUpdate", Const.TEST_JID_STR, "xa", 15, {}, Const.TEST_PROFILE)
-        self.presence.availableReceived(Const.TEST_JID, 'xa', None, 15)
+        self.host.bridge.expectCall("presenceUpdate", Const.JID_STR[0], "xa", 15, {}, Const.PROFILE[0])
+        self.presence.availableReceived(Const.JID[0], 'xa', None, 15)
 
     def test_unavailableReceived(self):
-        self.host.bridge.expectCall("presenceUpdate", Const.TEST_JID_STR, "unavailable", 0, {}, Const.TEST_PROFILE)
-        self.presence.unavailableReceived(Const.TEST_JID, None)
+        self.host.bridge.expectCall("presenceUpdate", Const.JID_STR[0], "unavailable", 0, {}, Const.PROFILE[0])
+        self.presence.unavailableReceived(Const.JID[0], None)
 
     def test_subscribedReceived(self):
-        self.host.bridge.expectCall("subscribe", "subscribed", Const.TEST_JID.userhost(), Const.TEST_PROFILE)
-        self.presence.subscribedReceived(Const.TEST_JID)
+        self.host.bridge.expectCall("subscribe", "subscribed", Const.JID[0].userhost(), Const.PROFILE[0])
+        self.presence.subscribedReceived(Const.JID[0])
 
     def test_unsubscribedReceived(self):
-        self.host.bridge.expectCall("subscribe", "unsubscribed", Const.TEST_JID.userhost(), Const.TEST_PROFILE)
-        self.presence.unsubscribedReceived(Const.TEST_JID)
+        self.host.bridge.expectCall("subscribe", "unsubscribed", Const.JID[0].userhost(), Const.PROFILE[0])
+        self.presence.unsubscribedReceived(Const.JID[0])
 
     def test_subscribeReceived(self):
-        self.host.bridge.expectCall("subscribe", "subscribe", Const.TEST_JID.userhost(), Const.TEST_PROFILE)
-        self.presence.subscribeReceived(Const.TEST_JID)
+        self.host.bridge.expectCall("subscribe", "subscribe", Const.JID[0].userhost(), Const.PROFILE[0])
+        self.presence.subscribeReceived(Const.JID[0])
 
     def test_unsubscribeReceived(self):
-        self.host.bridge.expectCall("subscribe", "unsubscribe", Const.TEST_JID.userhost(), Const.TEST_PROFILE)
-        self.presence.unsubscribeReceived(Const.TEST_JID)
+        self.host.bridge.expectCall("subscribe", "unsubscribe", Const.JID[0].userhost(), Const.PROFILE[0])
+        self.presence.unsubscribeReceived(Const.JID[0])
--- a/src/test/test_plugin_xep_0033.py	Fri Jan 10 18:07:36 2014 +0100
+++ b/src/test/test_plugin_xep_0033.py	Thu Jan 09 11:18:33 2014 +0100
@@ -48,15 +48,15 @@
                 <address type='bcc' jid='%s'/>
             </addresses>
         </message>
-        """ % (Const.TEST_JID_2_STR, self.host.getClientHostJid(Const.TEST_PROFILE),
-               Const.TEST_JID_STR, Const.TEST_JID_2_STR, Const.TEST_JID_3_STR)
+        """ % (Const.JID_STR[1], self.host.getClientHostJid(Const.PROFILE[0]),
+               Const.JID_STR[0], Const.JID_STR[1], Const.JID_STR[2])
         stanza = parseXml(xml.encode("utf-8"))
         treatments = defer.Deferred()
-        self.plugin.messageReceivedTrigger(stanza, treatments, Const.TEST_PROFILE)
+        self.plugin.messageReceivedTrigger(stanza, treatments, Const.PROFILE[0])
         data = {'extra': {}}
 
         def cb(data):
-            expected = ('to', Const.TEST_JID_STR, 'cc', Const.TEST_JID_2_STR, 'bcc', Const.TEST_JID_3_STR)
+            expected = ('to', Const.JID_STR[0], 'cc', Const.JID_STR[1], 'bcc', Const.JID_STR[2])
             msg = 'Expected: %s\nGot:      %s' % (expected, data['extra']['addresses'])
             self.assertEqual(data['extra']['addresses'], '%s:%s\n%s:%s\n%s:%s\n' % expected, msg)
 
@@ -64,18 +64,18 @@
         treatments.callback(data)
 
     def test_sendMessageTrigger(self):
-        mess_data = {"to": self.host.getClientHostJid(Const.TEST_PROFILE),
+        mess_data = {"to": self.host.getClientHostJid(Const.PROFILE[0]),
                      "type": "chat",
                      "message": "content",
                      "extra": {}
                      }
-        addresses = ('to', Const.TEST_JID_STR, 'cc', Const.TEST_JID_2_STR, 'bcc', Const.TEST_JID_3_STR)
+        addresses = ('to', Const.JID_STR[0], 'cc', Const.JID_STR[1], 'bcc', Const.JID_STR[2])
         mess_data["extra"]["address"] = '%s:%s\n%s:%s\n%s:%s\n' % addresses
         original_stanza = u"""
         <message type="chat" from="%s" to="%s" id="test_1">
             <body>content</body>
         </message>
-        """ % (Const.TEST_JID_2_STR, self.host.getClientHostJid(Const.TEST_PROFILE))
+        """ % (Const.JID_STR[1], self.host.getClientHostJid(Const.PROFILE[0]))
         mess_data['xml'] = parseXml(original_stanza.encode("utf-8"))
         expected = deepcopy(mess_data['xml'])
         addresses_extra = """
@@ -108,7 +108,7 @@
             for to_s in [addresses[1], addresses[3], addresses[5]]:
                 to_jid = JID(to_s)
                 host = JID(to_jid.host)
-                if self.host.memory.hasServerFeature(plugin.NS_ADDRESS, host, Const.TEST_PROFILE):
+                if self.host.memory.hasServerFeature(plugin.NS_ADDRESS, host, Const.PROFILE[0]):
                     if host not in cache:
                         sent.append(host)
                         stored.append(host)
@@ -125,17 +125,17 @@
         self.host.memory.init()
         treatments = defer.Deferred()
         data = deepcopy(mess_data)
-        self.plugin.sendMessageTrigger(data, treatments, Const.TEST_PROFILE)
+        self.plugin.sendMessageTrigger(data, treatments, Const.PROFILE[0])
         treatments.addCallbacks(assertAddresses, lambda failure: sendMessageErrback(failure, AbortSendMessage))
         treatments.callback(data)
 
         # feature is supported
         self.host.init()
         self.host.memory.init()
-        self.host.memory.addServerFeature(plugin.NS_ADDRESS, self.host.getClientHostJid(Const.TEST_PROFILE), Const.TEST_PROFILE)
+        self.host.memory.addServerFeature(plugin.NS_ADDRESS, self.host.getClientHostJid(Const.PROFILE[0]), Const.PROFILE[0])
         treatments = defer.Deferred()
         data = deepcopy(mess_data)
-        self.plugin.sendMessageTrigger(data, treatments, Const.TEST_PROFILE)
+        self.plugin.sendMessageTrigger(data, treatments, Const.PROFILE[0])
         treatments.addCallbacks(assertAddresses, lambda failure: sendMessageErrback(failure, MessageSentAndStored))
         treatments.callback(data)
         checkSentAndStored()
@@ -143,11 +143,11 @@
         # check that a wrong recipient entity is fixed by the backend
         self.host.init()
         self.host.memory.init()
-        self.host.memory.addServerFeature(plugin.NS_ADDRESS, self.host.getClientHostJid(Const.TEST_PROFILE), Const.TEST_PROFILE)
+        self.host.memory.addServerFeature(plugin.NS_ADDRESS, self.host.getClientHostJid(Const.PROFILE[0]), Const.PROFILE[0])
         treatments = defer.Deferred()
         data = deepcopy(mess_data)
-        data["to"] = Const.TEST_JID
-        self.plugin.sendMessageTrigger(data, treatments, Const.TEST_PROFILE)
+        data["to"] = Const.JID[0]
+        self.plugin.sendMessageTrigger(data, treatments, Const.PROFILE[0])
         treatments.addCallbacks(assertAddresses, lambda failure: sendMessageErrback(failure, MessageSentAndStored))
         treatments.callback(mess_data)
         checkSentAndStored()
--- a/src/test/test_plugin_xep_0085.py	Fri Jan 10 18:07:36 2014 +0100
+++ b/src/test/test_plugin_xep_0085.py	Thu Jan 09 11:18:33 2014 +0100
@@ -38,26 +38,26 @@
 
     def test_messageReceived(self):
         self.host.memory.init()
-        self.host.memory.setParam(plugin.PARAM_NAME, True, plugin.PARAM_KEY, NO_SECURITY_LIMIT, Const.TEST_PROFILE)
+        self.host.memory.setParam(plugin.PARAM_NAME, True, plugin.PARAM_KEY, NO_SECURITY_LIMIT, Const.PROFILE[0])
         for state in plugin.CHAT_STATES:
             xml = u"""
             <message type="chat" from="%s" to="%s" id="test_1">
             %s
             <%s xmlns='%s'/>
             </message>
-            """ % (Const.TEST_JID_2_STR,
-                   Const.TEST_JID_STR,
+            """ % (Const.JID_STR[1],
+                   Const.JID_STR[0],
                    "<body>test</body>" if state == "active" else "",
                    state, plugin.NS_CHAT_STATES)
             stanza = parseXml(xml.encode("utf-8"))
-            self.host.bridge.expectCall("chatStateReceived", u"sender@example.net/house", state, Const.TEST_PROFILE)
-            self.plugin.messageReceivedTrigger(stanza, defer.Deferred(), Const.TEST_PROFILE)
+            self.host.bridge.expectCall("chatStateReceived", Const.JID_STR[1], state, Const.PROFILE[0])
+            self.plugin.messageReceivedTrigger(stanza, defer.Deferred(), Const.PROFILE[0])
 
     def test_sendMessageTrigger(self):
         self.host.memory.init()
-        self.host.memory.setParam(plugin.PARAM_NAME, True, plugin.PARAM_KEY, NO_SECURITY_LIMIT, Const.TEST_PROFILE)
+        self.host.memory.setParam(plugin.PARAM_NAME, True, plugin.PARAM_KEY, NO_SECURITY_LIMIT, Const.PROFILE[0])
         for state in plugin.CHAT_STATES:
-            mess_data = {"to": Const.TEST_JID,
+            mess_data = {"to": Const.JID[0],
                          "type": "chat",
                          "message": "content",
                          "extra": {} if state == "active" else {"chat_state": state}}
@@ -65,14 +65,14 @@
             <message type="chat" from="%s" to="%s" id="test_1">
             %s
             </message>
-            """ % (Const.TEST_JID_2_STR, Const.TEST_JID_STR,
+            """ % (Const.JID_STR[1], Const.JID_STR[0],
                    ("<body>%s</body>" % mess_data['message']) if state == "active" else "")
             mess_data['xml'] = parseXml(stanza.encode("utf-8"))
             expected = deepcopy(mess_data['xml'])
             expected.addElement(state, plugin.NS_CHAT_STATES)
             treatments = defer.Deferred()
-            self.plugin.sendMessageTrigger(mess_data, treatments, Const.TEST_PROFILE)
+            self.plugin.sendMessageTrigger(mess_data, treatments, Const.PROFILE[0])
             xml = treatments.callbacks[0][0][0](mess_data)
             # cancel the timer to not block the process
-            self.plugin.map[Const.TEST_PROFILE][Const.TEST_JID.userhostJID()].timer.cancel()
+            self.plugin.map[Const.PROFILE[0]][Const.JID[0].userhostJID()].timer.cancel()
             self.assertEqualXML(xml['xml'].toXml().encode("utf-8"), expected.toXml().encode("utf-8"))