comparison src/test/helpers_plugins.py @ 1412:979210da778a

test: fix the tests
author souliane <souliane@mailoo.org>
date Fri, 17 Apr 2015 19:06:39 +0200
parents 069ad98b360d
children d17772b0fe22
comparison
equal deleted inserted replaced
1411:8767c0bb7d48 1412:979210da778a
38 def __init__(self, plugin_parent): 38 def __init__(self, plugin_parent):
39 self.plugin_parent = plugin_parent 39 self.plugin_parent = plugin_parent
40 self.host = plugin_parent.host 40 self.host = plugin_parent.host
41 self.joined_rooms = {} 41 self.joined_rooms = {}
42 42
43 def join(self, roomJID, nick, profile): 43 def join(self, room_jid, nick, options=None, profile_key=C.PROF_KEY_NONE):
44 """ 44 """
45 @param roomJID: the room JID 45 @param room_jid: the room JID
46 @param nick: nick to be used in the room 46 @param nick: nick to be used in the room
47 @param profile: the profile of the user joining the room 47 @param options: joining options
48 @param profile_key: the profile key of the user joining the room
48 @return: the deferred joined wokkel.muc.Room instance 49 @return: the deferred joined wokkel.muc.Room instance
49 """ 50 """
51 profile = self.host.memory.getProfileName(profile_key)
50 roster = {} 52 roster = {}
51 53
52 # ask the other profiles to fill our roster 54 # ask the other profiles to fill our roster
53 for i in xrange(0, len(C.PROFILE)): 55 for i in xrange(0, len(C.PROFILE)):
54 other_profile = C.PROFILE[i] 56 other_profile = C.PROFILE[i]
55 if other_profile == profile: 57 if other_profile == profile:
56 continue 58 continue
57 try: 59 try:
58 other_room = self.plugin_parent.clients[other_profile].joined_rooms[roomJID.userhost()] 60 other_room = self.plugin_parent.clients[other_profile].joined_rooms[room_jid]
59 roster.setdefault(other_room.nick, User(other_room.nick, C.PROFILE_DICT[other_profile])) 61 roster.setdefault(other_room.nick, User(other_room.nick, C.PROFILE_DICT[other_profile]))
60 for other_nick in other_room.roster: 62 for other_nick in other_room.roster:
61 roster.setdefault(other_nick, other_room.roster[other_nick]) 63 roster.setdefault(other_nick, other_room.roster[other_nick])
62 except (AttributeError, KeyError): 64 except (AttributeError, KeyError):
63 pass 65 pass
66 while nick in roster.keys(): 68 while nick in roster.keys():
67 if C.PROFILE_DICT[profile].userhost() == roster[nick].entity.userhost(): 69 if C.PROFILE_DICT[profile].userhost() == roster[nick].entity.userhost():
68 break # same user with different resource --> same nickname 70 break # same user with different resource --> same nickname
69 nick = nick + "_" 71 nick = nick + "_"
70 72
71 room = Room(roomJID, nick) 73 room = Room(room_jid, nick)
72 room.roster = roster 74 room.roster = roster
73 self.joined_rooms[roomJID.userhost()] = room 75 self.joined_rooms[room_jid] = room
74 76
75 # fill the other rosters with the new entry 77 # fill the other rosters with the new entry
76 for i in xrange(0, len(C.PROFILE)): 78 for i in xrange(0, len(C.PROFILE)):
77 other_profile = C.PROFILE[i] 79 other_profile = C.PROFILE[i]
78 if other_profile == profile: 80 if other_profile == profile:
79 continue 81 continue
80 try: 82 try:
81 other_room = self.plugin_parent.clients[other_profile].joined_rooms[roomJID.userhost()] 83 other_room = self.plugin_parent.clients[other_profile].joined_rooms[room_jid]
82 other_room.roster.setdefault(room.nick, User(room.nick, C.PROFILE_DICT[profile])) 84 other_room.roster.setdefault(room.nick, User(room.nick, C.PROFILE_DICT[profile]))
83 except (AttributeError, KeyError): 85 except (AttributeError, KeyError):
84 pass 86 pass
85 87
86 return defer.succeed(room) 88 return defer.succeed(room)
87 89
88 def leave(self, roomJID, profile): 90 def leave(self, roomJID, profile_key=C.PROF_KEY_NONE):
89 """ 91 """
90 @param roomJID: the room JID 92 @param roomJID: the room JID
91 @param profile: the profile of the user joining the room 93 @param profile_key: the profile key of the user joining the room
92 @return: a dummy deferred 94 @return: a dummy deferred
93 """ 95 """
94 room = self.joined_rooms[roomJID.userhost()] 96 profile = self.host.memory.getProfileName(profile_key)
97 room = self.joined_rooms[roomJID]
95 # remove ourself from the other rosters 98 # remove ourself from the other rosters
96 for i in xrange(0, len(C.PROFILE)): 99 for i in xrange(0, len(C.PROFILE)):
97 other_profile = C.PROFILE[i] 100 other_profile = C.PROFILE[i]
98 if other_profile == profile: 101 if other_profile == profile:
99 continue 102 continue
100 try: 103 try:
101 other_room = self.plugin_parent.clients[other_profile].joined_rooms[roomJID.userhost()] 104 other_room = self.plugin_parent.clients[other_profile].joined_rooms[roomJID]
102 del other_room.roster[room.nick] 105 del other_room.roster[room.nick]
103 except (AttributeError, KeyError): 106 except (AttributeError, KeyError):
104 pass 107 pass
105 del self.joined_rooms[roomJID.userhost()] 108 del self.joined_rooms[roomJID]
106 return defer.Deferred() 109 return defer.Deferred()
107 110
108 111
109 class FakeXEP_0045(plugin_xep_0045.XEP_0045): 112 class FakeXEP_0045(plugin_xep_0045.XEP_0045):
110 113
121 @param options: ignore 124 @param options: ignore
122 @param profile_key: the profile of the user joining the room 125 @param profile_key: the profile of the user joining the room
123 @return: the deferred joined wokkel.muc.Room instance or None 126 @return: the deferred joined wokkel.muc.Room instance or None
124 """ 127 """
125 profile = self.host.memory.getProfileName(profile_key) 128 profile = self.host.memory.getProfileName(profile_key)
126 room_jid_s = room_jid.userhost() 129 if room_jid in self.clients[profile].joined_rooms:
127 if room_jid_s in self.clients[profile].joined_rooms:
128 return defer.succeed(None) 130 return defer.succeed(None)
129 room = self.clients[profile].join(room_jid, nick, profile) 131 room = self.clients[profile].join(room_jid, nick, profile_key=profile)
130 return room 132 return room
131 133
132 def joinRoom(self, muc_index, user_index): 134 def joinRoom(self, muc_index, user_index):
133 """Called by tests 135 """Called by tests
134 @return: the nickname of the user who joined room""" 136 @return: the nickname of the user who joined room"""
143 @param roomJID: the room JID 145 @param roomJID: the room JID
144 @param profile_key: the profile of the user leaving the room 146 @param profile_key: the profile of the user leaving the room
145 @return: a dummy deferred 147 @return: a dummy deferred
146 """ 148 """
147 profile = self.host.memory.getProfileName(profile_key) 149 profile = self.host.memory.getProfileName(profile_key)
148 room_jid_s = room_jid.userhost() 150 if room_jid not in self.clients[profile].joined_rooms:
149 if room_jid_s not in self.clients[profile].joined_rooms:
150 raise plugin_xep_0045.UnknownRoom("This room has not been joined") 151 raise plugin_xep_0045.UnknownRoom("This room has not been joined")
151 return self.clients[profile].leave(room_jid, profile) 152 return self.clients[profile].leave(room_jid, profile)
152 153
153 def leaveRoom(self, muc_index, user_index): 154 def leaveRoom(self, muc_index, user_index):
154 """Called by tests 155 """Called by tests
161 162
162 def getRoom(self, muc_index, user_index): 163 def getRoom(self, muc_index, user_index):
163 """Called by tests 164 """Called by tests
164 @return: a wokkel.muc.Room instance""" 165 @return: a wokkel.muc.Room instance"""
165 profile = C.PROFILE[user_index] 166 profile = C.PROFILE[user_index]
166 muc_s = C.MUC_STR[muc_index] 167 muc_jid = C.MUC[muc_index]
167 try: 168 try:
168 return self.clients[profile].joined_rooms[muc_s] 169 return self.clients[profile].joined_rooms[muc_jid]
169 except (AttributeError, KeyError): 170 except (AttributeError, KeyError):
170 return None 171 return None
171 172
172 def getNick(self, muc_index, user_index): 173 def getNick(self, muc_index, user_index):
173 try: 174 try:
174 return self.getRoomNick(C.MUC_STR[muc_index], C.PROFILE[user_index]) 175 return self.getRoomNick(C.MUC[muc_index], C.PROFILE[user_index])
175 except (KeyError, AttributeError): 176 except (KeyError, AttributeError):
176 return '' 177 return ''
177 178
178 def getNickOfUser(self, muc_index, user_index, profile_index, secure=True): 179 def getNickOfUser(self, muc_index, user_index, profile_index, secure=True):
179 try: 180 try:
180 room = self.clients[C.PROFILE[profile_index]].joined_rooms[C.MUC_STR[muc_index]] 181 room = self.clients[C.PROFILE[profile_index]].joined_rooms[C.MUC[muc_index]]
181 return self.getRoomNickOfUser(room, C.JID_STR[user_index]) 182 return self.getRoomNickOfUser(room, C.JID[user_index])
182 except (KeyError, AttributeError): 183 except (KeyError, AttributeError):
183 return None 184 return None
184 185
185 186
186 class FakeXEP_0249(object): 187 class FakeXEP_0249(object):