Mercurial > libervia-backend
comparison src/test/helpers_plugins.py @ 794:52c4b755aba6
test: make FakeClient profile dependent and add some tools to test MUC
author | souliane <souliane@mailoo.org> |
---|---|
date | Fri, 10 Jan 2014 18:15:02 +0100 |
parents | |
children | 1fe00f0c9a91 |
comparison
equal
deleted
inserted
replaced
793:cb2db0d85029 | 794:52c4b755aba6 |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SAT: a jabber client | |
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org) | |
6 # Copyright (C) 2013 Adrien Cossa (souliane@mailoo.org) | |
7 | |
8 # This program is free software: you can redistribute it and/or modify | |
9 # it under the terms of the GNU Affero General Public License as published by | |
10 # the Free Software Foundation, either version 3 of the License, or | |
11 # (at your option) any later version. | |
12 | |
13 # This program is distributed in the hope that it will be useful, | |
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 # GNU Affero General Public License for more details. | |
17 | |
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/>. | |
20 | |
21 """ Helpers class for plugin dependencies """ | |
22 | |
23 from constants import Const | |
24 from sat.plugins.plugin_xep_0045 import XEP_0045 | |
25 from twisted.internet import defer | |
26 from wokkel.muc import Room, User | |
27 | |
28 | |
29 class FakeMUCClient(object): | |
30 def __init__(self, plugin_parent): | |
31 self.plugin_parent = plugin_parent | |
32 self.host = plugin_parent.host | |
33 self.joined_rooms = {} | |
34 | |
35 def join(self, roomJID, nick, profile): | |
36 roster = {} | |
37 | |
38 # ask the other profiles to fill our roster | |
39 for i in xrange(0, len(Const.PROFILE)): | |
40 other_profile = Const.PROFILE[i] | |
41 if other_profile == profile: | |
42 continue | |
43 try: | |
44 other_room = self.plugin_parent.clients[other_profile].joined_rooms[roomJID.userhost()] | |
45 roster.setdefault(other_room.nick, User(other_room.nick, Const.PROFILE_DICT[other_profile])) | |
46 for other_nick in other_room.roster: | |
47 roster.setdefault(other_nick, other_room.roster[other_nick]) | |
48 except (AttributeError, KeyError): | |
49 pass | |
50 | |
51 # rename our nick if it already exists | |
52 while nick in roster.keys(): | |
53 if Const.PROFILE_DICT[profile].userhost() == roster[nick].entity.userhost(): | |
54 break # same user with different resource --> same nickname | |
55 nick = nick + "_" | |
56 | |
57 room = Room(roomJID, nick) | |
58 room.roster = roster | |
59 self.joined_rooms[roomJID.userhost()] = room | |
60 | |
61 # fill the other rosters with the new entry | |
62 for i in xrange(0, len(Const.PROFILE)): | |
63 other_profile = Const.PROFILE[i] | |
64 if other_profile == profile: | |
65 continue | |
66 try: | |
67 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])) | |
69 except (AttributeError, KeyError): | |
70 pass | |
71 | |
72 return room | |
73 | |
74 | |
75 class FakeXEP_0045(XEP_0045): | |
76 | |
77 def __init__(self, host): | |
78 self.host = host | |
79 self.clients = {} | |
80 for profile in Const.PROFILE: | |
81 self.clients[profile] = FakeMUCClient(self) | |
82 | |
83 def join(self, room_jid, nick, options={}, profile_key='@DEFAULT@'): | |
84 profile = self.host.memory.getProfileName(profile_key) | |
85 room_jid_s = room_jid.userhost() | |
86 if room_jid_s in self.clients[profile].joined_rooms: | |
87 return defer.succeed(None) | |
88 room = self.clients[profile].join(room_jid, nick, profile) | |
89 return defer.succeed(room) | |
90 | |
91 def joinRoom(self, muc_index, user_index): | |
92 """Called by tests | |
93 @return: the nickname of the user in the joined room""" | |
94 muc = Const.MUC[muc_index] | |
95 nick = Const.JID[user_index].user | |
96 profile = Const.PROFILE[user_index] | |
97 self.join(muc, nick, profile_key=profile) | |
98 return self.getNick(muc_index, user_index) | |
99 | |
100 def getRoom(self, muc_index, user_index): | |
101 """Called by tests | |
102 @return: a wokkel.muc.Room instance""" | |
103 profile = Const.PROFILE[user_index] | |
104 muc_s = Const.MUC_STR[muc_index] | |
105 try: | |
106 return self.clients[profile].joined_rooms[muc_s] | |
107 except (AttributeError, KeyError): | |
108 return None | |
109 | |
110 def getNick(self, muc_index, user_index): | |
111 try: | |
112 return self.getRoomNick(Const.MUC_STR[muc_index], Const.PROFILE[user_index]) | |
113 except (KeyError, AttributeError): | |
114 return '' | |
115 | |
116 def getNickOfUser(self, muc_index, user_index, profile_index, secure=True): | |
117 try: | |
118 room = self.clients[Const.PROFILE[profile_index]].joined_rooms[Const.MUC_STR[muc_index]] | |
119 return self.getRoomNickOfUser(room, Const.JID_STR[user_index]) | |
120 except (KeyError, AttributeError): | |
121 return None | |
122 | |
123 | |
124 class FakeXEP_0249(object): | |
125 | |
126 def __init__(self, host): | |
127 self.host = host | |
128 | |
129 def invite(self, target, room, options={}, profile_key='@DEFAULT@'): | |
130 """ | |
131 Invite a user to a room. To accept the invitation from a test, | |
132 just call FakeXEP_0045.joinRoom (no need to have a dedicated method). | |
133 @param target: jid of the user to invite | |
134 @param room: jid of the room where the user is invited | |
135 @options: attribute with extra info (reason, password) as in #XEP-0249 | |
136 @profile_key: %(doc_profile_key)s | |
137 """ | |
138 pass |