Mercurial > libervia-backend
comparison src/test/helpers_plugins.py @ 829:187d2443c82d
test: improvements for the helpers classes:
- simplification for retrieving the sent messages
- an expected bridge call which is already set can no more be
overwritten with new arguments for the same method name. The
new arguments are stored in a FIFO list instead, and each call
of the method also registers the arguments for the next call.
author | souliane <souliane@mailoo.org> |
---|---|
date | Wed, 15 Jan 2014 23:22:07 +0100 |
parents | 1fe00f0c9a91 |
children | 91e5becc6623 |
comparison
equal
deleted
inserted
replaced
828:8f335c03eebb | 829:187d2443c82d |
---|---|
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 constants import Const |
24 from sat.plugins.plugin_xep_0045 import XEP_0045 | 24 from sat.plugins import plugin_xep_0045 |
25 from twisted.internet import defer | 25 from twisted.internet import defer |
26 from wokkel.muc import Room, User | 26 from wokkel.muc import Room, User |
27 | 27 |
28 | 28 |
29 class FakeMUCClient(object): | 29 class FakeMUCClient(object): |
31 self.plugin_parent = plugin_parent | 31 self.plugin_parent = plugin_parent |
32 self.host = plugin_parent.host | 32 self.host = plugin_parent.host |
33 self.joined_rooms = {} | 33 self.joined_rooms = {} |
34 | 34 |
35 def join(self, roomJID, nick, profile): | 35 def join(self, roomJID, nick, profile): |
36 """ | |
37 @param roomJID: the room JID | |
38 @param nick: nick to be used in the room | |
39 @param profile: the profile of the user joining the room | |
40 @return: the deferred joined wokkel.muc.Room instance | |
41 """ | |
36 roster = {} | 42 roster = {} |
37 | 43 |
38 # ask the other profiles to fill our roster | 44 # ask the other profiles to fill our roster |
39 for i in xrange(0, len(Const.PROFILE)): | 45 for i in xrange(0, len(Const.PROFILE)): |
40 other_profile = Const.PROFILE[i] | 46 other_profile = Const.PROFILE[i] |
67 other_room = self.plugin_parent.clients[other_profile].joined_rooms[roomJID.userhost()] | 73 other_room = self.plugin_parent.clients[other_profile].joined_rooms[roomJID.userhost()] |
68 other_room.roster.setdefault(room.nick, User(room.nick, Const.PROFILE_DICT[profile])) | 74 other_room.roster.setdefault(room.nick, User(room.nick, Const.PROFILE_DICT[profile])) |
69 except (AttributeError, KeyError): | 75 except (AttributeError, KeyError): |
70 pass | 76 pass |
71 | 77 |
72 return room | 78 return defer.succeed(room) |
79 | |
80 def leave(self, roomJID, profile): | |
81 """ | |
82 @param roomJID: the room JID | |
83 @param profile: the profile of the user joining the room | |
84 @return: a dummy deferred | |
85 """ | |
86 room = self.joined_rooms[roomJID.userhost()] | |
87 # remove ourself from the other rosters | |
88 for i in xrange(0, len(Const.PROFILE)): | |
89 other_profile = Const.PROFILE[i] | |
90 if other_profile == profile: | |
91 continue | |
92 try: | |
93 other_room = self.plugin_parent.clients[other_profile].joined_rooms[roomJID.userhost()] | |
94 del other_room.roster[room.nick] | |
95 except (AttributeError, KeyError): | |
96 pass | |
97 del self.joined_rooms[roomJID.userhost()] | |
98 return defer.Deferred() | |
73 | 99 |
74 | 100 |
75 class FakeXEP_0045(XEP_0045): | 101 class FakeXEP_0045(plugin_xep_0045.XEP_0045): |
76 | 102 |
77 def __init__(self, host): | 103 def __init__(self, host): |
78 self.host = host | 104 self.host = host |
79 self.clients = {} | 105 self.clients = {} |
80 for profile in Const.PROFILE: | 106 for profile in Const.PROFILE: |
81 self.clients[profile] = FakeMUCClient(self) | 107 self.clients[profile] = FakeMUCClient(self) |
82 | 108 |
83 def join(self, room_jid, nick, options={}, profile_key='@DEFAULT@'): | 109 def join(self, room_jid, nick, options={}, profile_key='@DEFAULT@'): |
110 """ | |
111 @param roomJID: the room JID | |
112 @param nick: nick to be used in the room | |
113 @param options: ignore | |
114 @param profile_key: the profile of the user joining the room | |
115 @return: the deferred joined wokkel.muc.Room instance or None | |
116 """ | |
84 profile = self.host.memory.getProfileName(profile_key) | 117 profile = self.host.memory.getProfileName(profile_key) |
85 room_jid_s = room_jid.userhost() | 118 room_jid_s = room_jid.userhost() |
86 if room_jid_s in self.clients[profile].joined_rooms: | 119 if room_jid_s in self.clients[profile].joined_rooms: |
87 return defer.succeed(None) | 120 return defer.succeed(None) |
88 room = self.clients[profile].join(room_jid, nick, profile) | 121 room = self.clients[profile].join(room_jid, nick, profile) |
89 return defer.succeed(room) | 122 return room |
90 | 123 |
91 def joinRoom(self, muc_index, user_index): | 124 def joinRoom(self, muc_index, user_index): |
92 """Called by tests | 125 """Called by tests |
93 @return: the nickname of the user in the joined room""" | 126 @return: the nickname of the user who joined room""" |
94 muc = Const.MUC[muc_index] | 127 muc_jid = Const.MUC[muc_index] |
95 nick = Const.JID[user_index].user | 128 nick = Const.JID[user_index].user |
96 profile = Const.PROFILE[user_index] | 129 profile = Const.PROFILE[user_index] |
97 self.join(muc, nick, profile_key=profile) | 130 self.join(muc_jid, nick, profile_key=profile) |
98 return self.getNick(muc_index, user_index) | 131 return self.getNick(muc_index, user_index) |
132 | |
133 def leave(self, room_jid, profile_key='@DEFAULT@'): | |
134 """ | |
135 @param roomJID: the room JID | |
136 @param profile_key: the profile of the user leaving the room | |
137 @return: a dummy deferred | |
138 """ | |
139 profile = self.host.memory.getProfileName(profile_key) | |
140 room_jid_s = room_jid.userhost() | |
141 if room_jid_s not in self.clients[profile].joined_rooms: | |
142 raise plugin_xep_0045.UnknownRoom("This room has not been joined") | |
143 return self.clients[profile].leave(room_jid, profile) | |
144 | |
145 def leaveRoom(self, muc_index, user_index): | |
146 """Called by tests | |
147 @return: the nickname of the user who left the room""" | |
148 muc_jid = Const.MUC[muc_index] | |
149 nick = self.getNick(muc_index, user_index) | |
150 profile = Const.PROFILE[user_index] | |
151 self.leave(muc_jid, profile_key=profile) | |
152 return nick | |
99 | 153 |
100 def getRoom(self, muc_index, user_index): | 154 def getRoom(self, muc_index, user_index): |
101 """Called by tests | 155 """Called by tests |
102 @return: a wokkel.muc.Room instance""" | 156 @return: a wokkel.muc.Room instance""" |
103 profile = Const.PROFILE[user_index] | 157 profile = Const.PROFILE[user_index] |