diff src/test/helpers_plugins.py @ 1269:91e5becc6623

test: add tests for plugin_misc_groupblog
author souliane <souliane@mailoo.org>
date Mon, 15 Dec 2014 14:05:28 +0100
parents 187d2443c82d
children 037ec0795a85
line wrap: on
line diff
--- a/src/test/helpers_plugins.py	Mon Dec 15 14:04:19 2014 +0100
+++ b/src/test/helpers_plugins.py	Mon Dec 15 14:05:28 2014 +0100
@@ -20,10 +20,16 @@
 
 """ Helpers class for plugin dependencies """
 
-from constants import Const
+from twisted.internet import defer
+
+from wokkel.muc import Room, User
+from wokkel.rsm import RSMResponse
+from wokkel.generic import parseXml
+from wokkel.disco import DiscoItem, DiscoItems
+
+from constants import Const as C
 from sat.plugins import plugin_xep_0045
-from twisted.internet import defer
-from wokkel.muc import Room, User
+from collections import OrderedDict
 
 
 class FakeMUCClient(object):
@@ -42,13 +48,13 @@
         roster = {}
 
         # ask the other profiles to fill our roster
-        for i in xrange(0, len(Const.PROFILE)):
-            other_profile = Const.PROFILE[i]
+        for i in xrange(0, len(C.PROFILE)):
+            other_profile = C.PROFILE[i]
             if other_profile == profile:
                 continue
             try:
                 other_room = self.plugin_parent.clients[other_profile].joined_rooms[roomJID.userhost()]
-                roster.setdefault(other_room.nick, User(other_room.nick, Const.PROFILE_DICT[other_profile]))
+                roster.setdefault(other_room.nick, User(other_room.nick, C.PROFILE_DICT[other_profile]))
                 for other_nick in other_room.roster:
                     roster.setdefault(other_nick, other_room.roster[other_nick])
             except (AttributeError, KeyError):
@@ -56,7 +62,7 @@
 
         # rename our nick if it already exists
         while nick in roster.keys():
-            if Const.PROFILE_DICT[profile].userhost() == roster[nick].entity.userhost():
+            if C.PROFILE_DICT[profile].userhost() == roster[nick].entity.userhost():
                 break  # same user with different resource --> same nickname
             nick = nick + "_"
 
@@ -65,13 +71,13 @@
         self.joined_rooms[roomJID.userhost()] = room
 
         # fill the other rosters with the new entry
-        for i in xrange(0, len(Const.PROFILE)):
-            other_profile = Const.PROFILE[i]
+        for i in xrange(0, len(C.PROFILE)):
+            other_profile = C.PROFILE[i]
             if other_profile == profile:
                 continue
             try:
                 other_room = self.plugin_parent.clients[other_profile].joined_rooms[roomJID.userhost()]
-                other_room.roster.setdefault(room.nick, User(room.nick, Const.PROFILE_DICT[profile]))
+                other_room.roster.setdefault(room.nick, User(room.nick, C.PROFILE_DICT[profile]))
             except (AttributeError, KeyError):
                 pass
 
@@ -85,8 +91,8 @@
         """
         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]
+        for i in xrange(0, len(C.PROFILE)):
+            other_profile = C.PROFILE[i]
             if other_profile == profile:
                 continue
             try:
@@ -103,7 +109,7 @@
     def __init__(self, host):
         self.host = host
         self.clients = {}
-        for profile in Const.PROFILE:
+        for profile in C.PROFILE:
             self.clients[profile] = FakeMUCClient(self)
 
     def join(self, room_jid, nick, options={}, profile_key='@DEFAULT@'):
@@ -124,9 +130,9 @@
     def joinRoom(self, muc_index, user_index):
         """Called by tests
         @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]
+        muc_jid = C.MUC[muc_index]
+        nick = C.JID[user_index].user
+        profile = C.PROFILE[user_index]
         self.join(muc_jid, nick, profile_key=profile)
         return self.getNick(muc_index, user_index)
 
@@ -145,17 +151,17 @@
     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]
+        muc_jid = C.MUC[muc_index]
         nick = self.getNick(muc_index, user_index)
-        profile = Const.PROFILE[user_index]
+        profile = C.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"""
-        profile = Const.PROFILE[user_index]
-        muc_s = Const.MUC_STR[muc_index]
+        profile = C.PROFILE[user_index]
+        muc_s = C.MUC_STR[muc_index]
         try:
             return self.clients[profile].joined_rooms[muc_s]
         except (AttributeError, KeyError):
@@ -163,14 +169,14 @@
 
     def getNick(self, muc_index, user_index):
         try:
-            return self.getRoomNick(Const.MUC_STR[muc_index], Const.PROFILE[user_index])
+            return self.getRoomNick(C.MUC_STR[muc_index], C.PROFILE[user_index])
         except (KeyError, AttributeError):
             return ''
 
     def getNickOfUser(self, muc_index, user_index, profile_index, secure=True):
         try:
-            room = self.clients[Const.PROFILE[profile_index]].joined_rooms[Const.MUC_STR[muc_index]]
-            return self.getRoomNickOfUser(room, Const.JID_STR[user_index])
+            room = self.clients[C.PROFILE[profile_index]].joined_rooms[C.MUC_STR[muc_index]]
+            return self.getRoomNickOfUser(room, C.JID_STR[user_index])
         except (KeyError, AttributeError):
             return None
 
@@ -190,3 +196,84 @@
         @profile_key: %(doc_profile_key)s
         """
         pass
+
+
+class FakeSatPubSubClient(object):
+
+    def __init__(self, host, parent_plugin):
+        self.host = host
+        self.parent_plugin = parent_plugin
+        self.__items = OrderedDict()
+        self.__rsm_responses = {}
+
+    def createNode(self, service, nodeIdentifier=None, options=None,
+                   sender=None):
+        return defer.succeed(None)
+
+    def deleteNode(self, service, nodeIdentifier, sender=None):
+        try:
+            del self.__items[nodeIdentifier]
+        except KeyError:
+            pass
+        return defer.succeed(None)
+
+    def subscribe(self, service, nodeIdentifier, subscriber,
+                  options=None, sender=None):
+        return defer.succeed(None)
+
+    def unsubscribe(self, service, nodeIdentifier, subscriber,
+                    subscriptionIdentifier=None, sender=None):
+        return defer.succeed(None)
+
+    def publish(self, service, nodeIdentifier, items=None, sender=None):
+        node = self.__items.setdefault(nodeIdentifier, [])
+
+        def replace(item_obj):
+            index = 0
+            for current in node:
+                if current['id'] == item_obj['id']:
+                    node[index] = item_obj
+                    return True
+                index += 1
+            return False
+
+        for item in items:
+            item_obj = parseXml(item) if isinstance(item, unicode) else item
+            if not replace(item_obj):
+                node.append(item_obj)
+        return defer.succeed(None)
+
+    def items(self, service, nodeIdentifier, maxItems=None, itemIdentifiers=None,
+              subscriptionIdentifier=None, sender=None, ext_data=None):
+        try:
+            items = self.__items[nodeIdentifier]
+        except KeyError:
+            items = []
+        if ext_data:
+            assert('id' in ext_data)
+            if 'rsm' in ext_data:
+                args = (0, items[0]['id'], items[-1]['id']) if items else ()
+                self.__rsm_responses[ext_data['id']] = RSMResponse(len(items), *args)
+        return defer.succeed(items)
+
+    def retractItems(self, service, nodeIdentifier, itemIdentifiers, sender=None):
+        node = self.__items[nodeIdentifier]
+        for item in [item for item in node if item['id'] in itemIdentifiers]:
+            node.remove(item)
+        return defer.succeed(None)
+
+    def getRSMResponse(self, id):
+        if id not in self.__rsm_responses:
+            return {}
+        result = self.__rsm_responses[id].toDict()
+        del self.__rsm_responses[id]
+        return result
+
+    def subscriptions(self, service, nodeIdentifier, sender=None):
+        return defer.succeed([])
+
+    def service_getDiscoItems(self, service, nodeIdentifier, profile_key=C.PROF_KEY_NONE):
+        items = DiscoItems()
+        for item in self.__items.keys():
+            items.append(DiscoItem(service, item))
+        return defer.succeed(items)