comparison 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
comparison
equal deleted inserted replaced
1268:bb30bf3ae932 1269:91e5becc6623
18 # You should have received a copy of the GNU Affero General Public License 18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>. 19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 20
21 """ Helpers class for plugin dependencies """ 21 """ Helpers class for plugin dependencies """
22 22
23 from constants import Const 23 from twisted.internet import defer
24
25 from wokkel.muc import Room, User
26 from wokkel.rsm import RSMResponse
27 from wokkel.generic import parseXml
28 from wokkel.disco import DiscoItem, DiscoItems
29
30 from constants import Const as C
24 from sat.plugins import plugin_xep_0045 31 from sat.plugins import plugin_xep_0045
25 from twisted.internet import defer 32 from collections import OrderedDict
26 from wokkel.muc import Room, User
27 33
28 34
29 class FakeMUCClient(object): 35 class FakeMUCClient(object):
30 def __init__(self, plugin_parent): 36 def __init__(self, plugin_parent):
31 self.plugin_parent = plugin_parent 37 self.plugin_parent = plugin_parent
40 @return: the deferred joined wokkel.muc.Room instance 46 @return: the deferred joined wokkel.muc.Room instance
41 """ 47 """
42 roster = {} 48 roster = {}
43 49
44 # ask the other profiles to fill our roster 50 # ask the other profiles to fill our roster
45 for i in xrange(0, len(Const.PROFILE)): 51 for i in xrange(0, len(C.PROFILE)):
46 other_profile = Const.PROFILE[i] 52 other_profile = C.PROFILE[i]
47 if other_profile == profile: 53 if other_profile == profile:
48 continue 54 continue
49 try: 55 try:
50 other_room = self.plugin_parent.clients[other_profile].joined_rooms[roomJID.userhost()] 56 other_room = self.plugin_parent.clients[other_profile].joined_rooms[roomJID.userhost()]
51 roster.setdefault(other_room.nick, User(other_room.nick, Const.PROFILE_DICT[other_profile])) 57 roster.setdefault(other_room.nick, User(other_room.nick, C.PROFILE_DICT[other_profile]))
52 for other_nick in other_room.roster: 58 for other_nick in other_room.roster:
53 roster.setdefault(other_nick, other_room.roster[other_nick]) 59 roster.setdefault(other_nick, other_room.roster[other_nick])
54 except (AttributeError, KeyError): 60 except (AttributeError, KeyError):
55 pass 61 pass
56 62
57 # rename our nick if it already exists 63 # rename our nick if it already exists
58 while nick in roster.keys(): 64 while nick in roster.keys():
59 if Const.PROFILE_DICT[profile].userhost() == roster[nick].entity.userhost(): 65 if C.PROFILE_DICT[profile].userhost() == roster[nick].entity.userhost():
60 break # same user with different resource --> same nickname 66 break # same user with different resource --> same nickname
61 nick = nick + "_" 67 nick = nick + "_"
62 68
63 room = Room(roomJID, nick) 69 room = Room(roomJID, nick)
64 room.roster = roster 70 room.roster = roster
65 self.joined_rooms[roomJID.userhost()] = room 71 self.joined_rooms[roomJID.userhost()] = room
66 72
67 # fill the other rosters with the new entry 73 # fill the other rosters with the new entry
68 for i in xrange(0, len(Const.PROFILE)): 74 for i in xrange(0, len(C.PROFILE)):
69 other_profile = Const.PROFILE[i] 75 other_profile = C.PROFILE[i]
70 if other_profile == profile: 76 if other_profile == profile:
71 continue 77 continue
72 try: 78 try:
73 other_room = self.plugin_parent.clients[other_profile].joined_rooms[roomJID.userhost()] 79 other_room = self.plugin_parent.clients[other_profile].joined_rooms[roomJID.userhost()]
74 other_room.roster.setdefault(room.nick, User(room.nick, Const.PROFILE_DICT[profile])) 80 other_room.roster.setdefault(room.nick, User(room.nick, C.PROFILE_DICT[profile]))
75 except (AttributeError, KeyError): 81 except (AttributeError, KeyError):
76 pass 82 pass
77 83
78 return defer.succeed(room) 84 return defer.succeed(room)
79 85
83 @param profile: the profile of the user joining the room 89 @param profile: the profile of the user joining the room
84 @return: a dummy deferred 90 @return: a dummy deferred
85 """ 91 """
86 room = self.joined_rooms[roomJID.userhost()] 92 room = self.joined_rooms[roomJID.userhost()]
87 # remove ourself from the other rosters 93 # remove ourself from the other rosters
88 for i in xrange(0, len(Const.PROFILE)): 94 for i in xrange(0, len(C.PROFILE)):
89 other_profile = Const.PROFILE[i] 95 other_profile = C.PROFILE[i]
90 if other_profile == profile: 96 if other_profile == profile:
91 continue 97 continue
92 try: 98 try:
93 other_room = self.plugin_parent.clients[other_profile].joined_rooms[roomJID.userhost()] 99 other_room = self.plugin_parent.clients[other_profile].joined_rooms[roomJID.userhost()]
94 del other_room.roster[room.nick] 100 del other_room.roster[room.nick]
101 class FakeXEP_0045(plugin_xep_0045.XEP_0045): 107 class FakeXEP_0045(plugin_xep_0045.XEP_0045):
102 108
103 def __init__(self, host): 109 def __init__(self, host):
104 self.host = host 110 self.host = host
105 self.clients = {} 111 self.clients = {}
106 for profile in Const.PROFILE: 112 for profile in C.PROFILE:
107 self.clients[profile] = FakeMUCClient(self) 113 self.clients[profile] = FakeMUCClient(self)
108 114
109 def join(self, room_jid, nick, options={}, profile_key='@DEFAULT@'): 115 def join(self, room_jid, nick, options={}, profile_key='@DEFAULT@'):
110 """ 116 """
111 @param roomJID: the room JID 117 @param roomJID: the room JID
122 return room 128 return room
123 129
124 def joinRoom(self, muc_index, user_index): 130 def joinRoom(self, muc_index, user_index):
125 """Called by tests 131 """Called by tests
126 @return: the nickname of the user who joined room""" 132 @return: the nickname of the user who joined room"""
127 muc_jid = Const.MUC[muc_index] 133 muc_jid = C.MUC[muc_index]
128 nick = Const.JID[user_index].user 134 nick = C.JID[user_index].user
129 profile = Const.PROFILE[user_index] 135 profile = C.PROFILE[user_index]
130 self.join(muc_jid, nick, profile_key=profile) 136 self.join(muc_jid, nick, profile_key=profile)
131 return self.getNick(muc_index, user_index) 137 return self.getNick(muc_index, user_index)
132 138
133 def leave(self, room_jid, profile_key='@DEFAULT@'): 139 def leave(self, room_jid, profile_key='@DEFAULT@'):
134 """ 140 """
143 return self.clients[profile].leave(room_jid, profile) 149 return self.clients[profile].leave(room_jid, profile)
144 150
145 def leaveRoom(self, muc_index, user_index): 151 def leaveRoom(self, muc_index, user_index):
146 """Called by tests 152 """Called by tests
147 @return: the nickname of the user who left the room""" 153 @return: the nickname of the user who left the room"""
148 muc_jid = Const.MUC[muc_index] 154 muc_jid = C.MUC[muc_index]
149 nick = self.getNick(muc_index, user_index) 155 nick = self.getNick(muc_index, user_index)
150 profile = Const.PROFILE[user_index] 156 profile = C.PROFILE[user_index]
151 self.leave(muc_jid, profile_key=profile) 157 self.leave(muc_jid, profile_key=profile)
152 return nick 158 return nick
153 159
154 def getRoom(self, muc_index, user_index): 160 def getRoom(self, muc_index, user_index):
155 """Called by tests 161 """Called by tests
156 @return: a wokkel.muc.Room instance""" 162 @return: a wokkel.muc.Room instance"""
157 profile = Const.PROFILE[user_index] 163 profile = C.PROFILE[user_index]
158 muc_s = Const.MUC_STR[muc_index] 164 muc_s = C.MUC_STR[muc_index]
159 try: 165 try:
160 return self.clients[profile].joined_rooms[muc_s] 166 return self.clients[profile].joined_rooms[muc_s]
161 except (AttributeError, KeyError): 167 except (AttributeError, KeyError):
162 return None 168 return None
163 169
164 def getNick(self, muc_index, user_index): 170 def getNick(self, muc_index, user_index):
165 try: 171 try:
166 return self.getRoomNick(Const.MUC_STR[muc_index], Const.PROFILE[user_index]) 172 return self.getRoomNick(C.MUC_STR[muc_index], C.PROFILE[user_index])
167 except (KeyError, AttributeError): 173 except (KeyError, AttributeError):
168 return '' 174 return ''
169 175
170 def getNickOfUser(self, muc_index, user_index, profile_index, secure=True): 176 def getNickOfUser(self, muc_index, user_index, profile_index, secure=True):
171 try: 177 try:
172 room = self.clients[Const.PROFILE[profile_index]].joined_rooms[Const.MUC_STR[muc_index]] 178 room = self.clients[C.PROFILE[profile_index]].joined_rooms[C.MUC_STR[muc_index]]
173 return self.getRoomNickOfUser(room, Const.JID_STR[user_index]) 179 return self.getRoomNickOfUser(room, C.JID_STR[user_index])
174 except (KeyError, AttributeError): 180 except (KeyError, AttributeError):
175 return None 181 return None
176 182
177 183
178 class FakeXEP_0249(object): 184 class FakeXEP_0249(object):
188 @param room: jid of the room where the user is invited 194 @param room: jid of the room where the user is invited
189 @options: attribute with extra info (reason, password) as in #XEP-0249 195 @options: attribute with extra info (reason, password) as in #XEP-0249
190 @profile_key: %(doc_profile_key)s 196 @profile_key: %(doc_profile_key)s
191 """ 197 """
192 pass 198 pass
199
200
201 class FakeSatPubSubClient(object):
202
203 def __init__(self, host, parent_plugin):
204 self.host = host
205 self.parent_plugin = parent_plugin
206 self.__items = OrderedDict()
207 self.__rsm_responses = {}
208
209 def createNode(self, service, nodeIdentifier=None, options=None,
210 sender=None):
211 return defer.succeed(None)
212
213 def deleteNode(self, service, nodeIdentifier, sender=None):
214 try:
215 del self.__items[nodeIdentifier]
216 except KeyError:
217 pass
218 return defer.succeed(None)
219
220 def subscribe(self, service, nodeIdentifier, subscriber,
221 options=None, sender=None):
222 return defer.succeed(None)
223
224 def unsubscribe(self, service, nodeIdentifier, subscriber,
225 subscriptionIdentifier=None, sender=None):
226 return defer.succeed(None)
227
228 def publish(self, service, nodeIdentifier, items=None, sender=None):
229 node = self.__items.setdefault(nodeIdentifier, [])
230
231 def replace(item_obj):
232 index = 0
233 for current in node:
234 if current['id'] == item_obj['id']:
235 node[index] = item_obj
236 return True
237 index += 1
238 return False
239
240 for item in items:
241 item_obj = parseXml(item) if isinstance(item, unicode) else item
242 if not replace(item_obj):
243 node.append(item_obj)
244 return defer.succeed(None)
245
246 def items(self, service, nodeIdentifier, maxItems=None, itemIdentifiers=None,
247 subscriptionIdentifier=None, sender=None, ext_data=None):
248 try:
249 items = self.__items[nodeIdentifier]
250 except KeyError:
251 items = []
252 if ext_data:
253 assert('id' in ext_data)
254 if 'rsm' in ext_data:
255 args = (0, items[0]['id'], items[-1]['id']) if items else ()
256 self.__rsm_responses[ext_data['id']] = RSMResponse(len(items), *args)
257 return defer.succeed(items)
258
259 def retractItems(self, service, nodeIdentifier, itemIdentifiers, sender=None):
260 node = self.__items[nodeIdentifier]
261 for item in [item for item in node if item['id'] in itemIdentifiers]:
262 node.remove(item)
263 return defer.succeed(None)
264
265 def getRSMResponse(self, id):
266 if id not in self.__rsm_responses:
267 return {}
268 result = self.__rsm_responses[id].toDict()
269 del self.__rsm_responses[id]
270 return result
271
272 def subscriptions(self, service, nodeIdentifier, sender=None):
273 return defer.succeed([])
274
275 def service_getDiscoItems(self, service, nodeIdentifier, profile_key=C.PROF_KEY_NONE):
276 items = DiscoItems()
277 for item in self.__items.keys():
278 items.append(DiscoItem(service, item))
279 return defer.succeed(items)