annotate src/test/helpers.py @ 1317:bd69d341d969 frontends_multi_profiles

plugin xep-0054: various improvments on avatars management: - avatars_cache is now managing cache load per profile - use of new profileConnecting method to load persistent data, this prevent to have presence_available trigger called before cache data is loaded - better management of avatars already in cache (prevent decoding avatars when it is already in cache)
author Goffi <goffi@goffi.org>
date Mon, 09 Feb 2015 21:39:51 +0100
parents be3a301540c0
children f71a0fc26886
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
335
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
Goffi <goffi@goffi.org>
parents:
diff changeset
3
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 591
diff changeset
4 # SAT: a jabber client
811
1fe00f0c9a91 dates update
Goffi <goffi@goffi.org>
parents: 794
diff changeset
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org)
335
Goffi <goffi@goffi.org>
parents:
diff changeset
6
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 591
diff changeset
7 # This program is free software: you can redistribute it and/or modify
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 591
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 591
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 591
diff changeset
10 # (at your option) any later version.
335
Goffi <goffi@goffi.org>
parents:
diff changeset
11
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 591
diff changeset
12 # This program is distributed in the hope that it will be useful,
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 591
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 591
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 591
diff changeset
15 # GNU Affero General Public License for more details.
335
Goffi <goffi@goffi.org>
parents:
diff changeset
16
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 591
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 591
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
335
Goffi <goffi@goffi.org>
parents:
diff changeset
19
998
f5761534e0f3 tests: fixed log init in helpers (log init must be done before any call to a log method)
Goffi <goffi@goffi.org>
parents: 997
diff changeset
20
f5761534e0f3 tests: fixed log init in helpers (log init must be done before any call to a log method)
Goffi <goffi@goffi.org>
parents: 997
diff changeset
21 ## logging configuration for tests ##
1022
002ee8397208 test: fix the tests after the changes of logging system
souliane <souliane@mailoo.org>
parents: 999
diff changeset
22 from sat.core import log_config
002ee8397208 test: fix the tests after the changes of logging system
souliane <souliane@mailoo.org>
parents: 999
diff changeset
23 log_config.satConfigure()
998
f5761534e0f3 tests: fixed log init in helpers (log init must be done before any call to a log method)
Goffi <goffi@goffi.org>
parents: 997
diff changeset
24
786
c3acc1298a2f test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents: 782
diff changeset
25 from sat.core import exceptions
780
9810f22ba733 test: store the constants in constants.py + better PEP8 compliance
souliane <souliane@mailoo.org>
parents: 771
diff changeset
26 from constants import Const
649
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
27 from wokkel.xmppim import RosterItem
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
28 from sat.core.xmpp import SatRosterProtocol
786
c3acc1298a2f test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents: 782
diff changeset
29 from sat.memory.memory import Params, Memory
647
63b6e684bbfa tests: Use of FailTest for DifferentArgsException instead of Exception, so the test fail instead of raising en error.
Goffi <goffi@goffi.org>
parents: 609
diff changeset
30 from twisted.trial.unittest import FailTest
693
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
31 from twisted.trial import unittest
782
0e5807193721 test: added some tests for Memory
souliane <souliane@mailoo.org>
parents: 781
diff changeset
32 from twisted.internet import defer
786
c3acc1298a2f test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents: 782
diff changeset
33 from twisted.words.protocols.jabber.jid import JID
693
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
34 from xml.etree import cElementTree as etree
789
0cb423500fbb test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
souliane <souliane@mailoo.org>
parents: 786
diff changeset
35 from collections import Counter
693
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
36 import re
335
Goffi <goffi@goffi.org>
parents:
diff changeset
37
Goffi <goffi@goffi.org>
parents:
diff changeset
38
649
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
39 def b2s(value):
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
40 """Convert a bool to a unicode string used in bridge
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
41 @param value: boolean value
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
42 @return: unicode conversion, according to bridge convention
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
43
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
44 """
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
45 return u"True" if value else u"False"
591
65821b3fa7ab Fix pep8 support in src/test.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
46
780
9810f22ba733 test: store the constants in constants.py + better PEP8 compliance
souliane <souliane@mailoo.org>
parents: 771
diff changeset
47
647
63b6e684bbfa tests: Use of FailTest for DifferentArgsException instead of Exception, so the test fail instead of raising en error.
Goffi <goffi@goffi.org>
parents: 609
diff changeset
48 class DifferentArgsException(FailTest):
335
Goffi <goffi@goffi.org>
parents:
diff changeset
49 pass
Goffi <goffi@goffi.org>
parents:
diff changeset
50
780
9810f22ba733 test: store the constants in constants.py + better PEP8 compliance
souliane <souliane@mailoo.org>
parents: 771
diff changeset
51
693
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
52 class DifferentXMLException(FailTest):
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
53 pass
591
65821b3fa7ab Fix pep8 support in src/test.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
54
780
9810f22ba733 test: store the constants in constants.py + better PEP8 compliance
souliane <souliane@mailoo.org>
parents: 771
diff changeset
55
789
0cb423500fbb test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
souliane <souliane@mailoo.org>
parents: 786
diff changeset
56 class DifferentListException(FailTest):
0cb423500fbb test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
souliane <souliane@mailoo.org>
parents: 786
diff changeset
57 pass
0cb423500fbb test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
souliane <souliane@mailoo.org>
parents: 786
diff changeset
58
0cb423500fbb test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
souliane <souliane@mailoo.org>
parents: 786
diff changeset
59
335
Goffi <goffi@goffi.org>
parents:
diff changeset
60 class FakeSAT(object):
Goffi <goffi@goffi.org>
parents:
diff changeset
61 """Class to simulate a SAT instance"""
Goffi <goffi@goffi.org>
parents:
diff changeset
62
Goffi <goffi@goffi.org>
parents:
diff changeset
63 def __init__(self):
Goffi <goffi@goffi.org>
parents:
diff changeset
64 self.bridge = FakeBridge()
781
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
65 self.memory = FakeMemory(self)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
66 self.trigger = FakeTriggerManager()
786
c3acc1298a2f test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents: 782
diff changeset
67 self.init()
c3acc1298a2f test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents: 782
diff changeset
68
c3acc1298a2f test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents: 782
diff changeset
69 def init(self):
794
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
70 """This can be called by tests that check for sent and stored messages,
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
71 uses FakeClient or get/set some other data that need to be cleaned"""
786
c3acc1298a2f test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents: 782
diff changeset
72 self.sent_messages = []
c3acc1298a2f test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents: 782
diff changeset
73 self.stored_messages = []
794
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
74 self.plugins = {}
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
75 self.profiles = {}
335
Goffi <goffi@goffi.org>
parents:
diff changeset
76
649
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
77 def delContact(self, to, profile_key):
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
78 #TODO
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
79 pass
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
80
781
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
81 def registerCallback(self, callback, *args, **kwargs):
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
82 pass
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
83
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
84 def sendMessage(self, to_s, msg, subject=None, mess_type='auto', extra={}, profile_key='@NONE@'):
786
c3acc1298a2f test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents: 782
diff changeset
85 self.sendAndStoreMessage({"to": JID(to_s)})
c3acc1298a2f test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents: 782
diff changeset
86
1206
020e663bc286 test: fixes FakeSAT for plugin XEP-0033
souliane <souliane@mailoo.org>
parents: 1037
diff changeset
87 def _sendMessageToStream(self, mess_data, client):
020e663bc286 test: fixes FakeSAT for plugin XEP-0033
souliane <souliane@mailoo.org>
parents: 1037
diff changeset
88 """Save the information to check later to whom messages have been sent.
020e663bc286 test: fixes FakeSAT for plugin XEP-0033
souliane <souliane@mailoo.org>
parents: 1037
diff changeset
89
020e663bc286 test: fixes FakeSAT for plugin XEP-0033
souliane <souliane@mailoo.org>
parents: 1037
diff changeset
90 @param mess_data: message data dictionnary
020e663bc286 test: fixes FakeSAT for plugin XEP-0033
souliane <souliane@mailoo.org>
parents: 1037
diff changeset
91 @param client: profile's client
020e663bc286 test: fixes FakeSAT for plugin XEP-0033
souliane <souliane@mailoo.org>
parents: 1037
diff changeset
92 """
020e663bc286 test: fixes FakeSAT for plugin XEP-0033
souliane <souliane@mailoo.org>
parents: 1037
diff changeset
93 self.sent_messages.append(mess_data["to"])
020e663bc286 test: fixes FakeSAT for plugin XEP-0033
souliane <souliane@mailoo.org>
parents: 1037
diff changeset
94 return mess_data
020e663bc286 test: fixes FakeSAT for plugin XEP-0033
souliane <souliane@mailoo.org>
parents: 1037
diff changeset
95
020e663bc286 test: fixes FakeSAT for plugin XEP-0033
souliane <souliane@mailoo.org>
parents: 1037
diff changeset
96 def _storeMessage(self, mess_data, client):
020e663bc286 test: fixes FakeSAT for plugin XEP-0033
souliane <souliane@mailoo.org>
parents: 1037
diff changeset
97 """Save the information to check later if entries have been added to the history.
020e663bc286 test: fixes FakeSAT for plugin XEP-0033
souliane <souliane@mailoo.org>
parents: 1037
diff changeset
98
020e663bc286 test: fixes FakeSAT for plugin XEP-0033
souliane <souliane@mailoo.org>
parents: 1037
diff changeset
99 @param mess_data: message data dictionnary
020e663bc286 test: fixes FakeSAT for plugin XEP-0033
souliane <souliane@mailoo.org>
parents: 1037
diff changeset
100 @param client: profile's client
020e663bc286 test: fixes FakeSAT for plugin XEP-0033
souliane <souliane@mailoo.org>
parents: 1037
diff changeset
101 """
786
c3acc1298a2f test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents: 782
diff changeset
102 self.stored_messages.append(mess_data["to"])
1206
020e663bc286 test: fixes FakeSAT for plugin XEP-0033
souliane <souliane@mailoo.org>
parents: 1037
diff changeset
103 return mess_data
020e663bc286 test: fixes FakeSAT for plugin XEP-0033
souliane <souliane@mailoo.org>
parents: 1037
diff changeset
104
020e663bc286 test: fixes FakeSAT for plugin XEP-0033
souliane <souliane@mailoo.org>
parents: 1037
diff changeset
105 def sendMessageToBridge(self, mess_data, client):
020e663bc286 test: fixes FakeSAT for plugin XEP-0033
souliane <souliane@mailoo.org>
parents: 1037
diff changeset
106 """Simulate the message being sent to the frontends.
020e663bc286 test: fixes FakeSAT for plugin XEP-0033
souliane <souliane@mailoo.org>
parents: 1037
diff changeset
107
020e663bc286 test: fixes FakeSAT for plugin XEP-0033
souliane <souliane@mailoo.org>
parents: 1037
diff changeset
108 @param mess_data: message data dictionnary
020e663bc286 test: fixes FakeSAT for plugin XEP-0033
souliane <souliane@mailoo.org>
parents: 1037
diff changeset
109 @param client: profile's client
020e663bc286 test: fixes FakeSAT for plugin XEP-0033
souliane <souliane@mailoo.org>
parents: 1037
diff changeset
110 """
020e663bc286 test: fixes FakeSAT for plugin XEP-0033
souliane <souliane@mailoo.org>
parents: 1037
diff changeset
111 return mess_data # TODO
781
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
112
794
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
113 def getClient(self, profile_key):
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
114 """Convenient method to get client from profile key
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
115 @return: client or None if it doesn't exist"""
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
116 profile = self.memory.getProfileName(profile_key)
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
117 if not profile:
941
c6d8fc63b1db core, plugins: host.getClient now raise an exception instead of returning None when no profile is found, plugins have been adapted consequently and a bit cleaned
Goffi <goffi@goffi.org>
parents: 859
diff changeset
118 raise exceptions.ProfileKeyUnknownError
794
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
119 if profile not in self.profiles:
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
120 self.profiles[profile] = FakeClient(self, profile)
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
121 return self.profiles[profile]
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
122
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
123 def getJidNStream(self, profile_key):
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
124 """Convenient method to get jid and stream from profile key
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
125 @return: tuple (jid, xmlstream) from profile, can be None"""
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
126 return (Const.PROFILE_DICT[profile_key], None)
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
127
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
128 def isConnected(self, profile):
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
129 return True
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
130
829
187d2443c82d test: improvements for the helpers classes:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
131 def getSentMessageRaw(self, profile_index):
187d2443c82d test: improvements for the helpers classes:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
132 """Pop and return the sent message in first position (works like a FIFO).
187d2443c82d test: improvements for the helpers classes:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
133 Called by tests. FakeClient instances associated to each profile must have
794
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
134 been previously initialized with the method FakeSAT.getClient.
829
187d2443c82d test: improvements for the helpers classes:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
135 @return: the sent message for given profile, or None"""
187d2443c82d test: improvements for the helpers classes:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
136 try:
187d2443c82d test: improvements for the helpers classes:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
137 return self.profiles[Const.PROFILE[profile_index]].xmlstream.sent.pop(0)
187d2443c82d test: improvements for the helpers classes:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
138 except IndexError:
187d2443c82d test: improvements for the helpers classes:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
139 return None
794
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
140
829
187d2443c82d test: improvements for the helpers classes:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
141 def getSentMessage(self, profile_index):
187d2443c82d test: improvements for the helpers classes:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
142 """Pop and return the sent message in first position (works like a FIFO).
187d2443c82d test: improvements for the helpers classes:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
143 Called by tests. FakeClient instances associated to each profile must have
794
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
144 been previously initialized with the method FakeSAT.getClient.
829
187d2443c82d test: improvements for the helpers classes:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
145 @return: XML representation of the sent message for given profile, or None"""
187d2443c82d test: improvements for the helpers classes:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
146 entry = self.getSentMessageRaw(profile_index)
187d2443c82d test: improvements for the helpers classes:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
147 return entry.toXml() if entry else None
786
c3acc1298a2f test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents: 782
diff changeset
148
999
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 998
diff changeset
149 def findFeaturesSet(self, features, category=None, type_=None, jid_=None, profile_key=None):
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 998
diff changeset
150 """Call self.addFeature from your tests to change the return value.
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 998
diff changeset
151
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 998
diff changeset
152 @return: a set of entities
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 998
diff changeset
153 """
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 998
diff changeset
154 client = self.getClient(profile_key)
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 998
diff changeset
155 if jid_ is None:
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 998
diff changeset
156 jid_ = JID(client.jid.host)
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 998
diff changeset
157 try:
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 998
diff changeset
158 if set(features).issubset(client.features[jid_]):
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 998
diff changeset
159 return defer.succeed(set([jid_]))
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 998
diff changeset
160 except (TypeError, AttributeError, KeyError):
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 998
diff changeset
161 pass
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 998
diff changeset
162 return defer.succeed(set())
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 998
diff changeset
163
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 998
diff changeset
164 def addFeature(self, jid_, feature, profile_key):
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 998
diff changeset
165 """Add a feature to an entity.
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 998
diff changeset
166
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 998
diff changeset
167 To be called from your tests when needed.
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 998
diff changeset
168 """
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 998
diff changeset
169 client = self.getClient(profile_key)
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 998
diff changeset
170 if not hasattr(client, 'features'):
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 998
diff changeset
171 client.features = {}
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 998
diff changeset
172 if jid_ not in client.features:
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 998
diff changeset
173 client.features[jid_] = set()
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 998
diff changeset
174 client.features[jid_].add(feature)
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 998
diff changeset
175
335
Goffi <goffi@goffi.org>
parents:
diff changeset
176
Goffi <goffi@goffi.org>
parents:
diff changeset
177 class FakeBridge(object):
Goffi <goffi@goffi.org>
parents:
diff changeset
178 """Class to simulate and test bridge calls"""
Goffi <goffi@goffi.org>
parents:
diff changeset
179
794
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
180 def __init__(self):
829
187d2443c82d test: improvements for the helpers classes:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
181 self.expected_calls = {}
794
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
182
335
Goffi <goffi@goffi.org>
parents:
diff changeset
183 def expectCall(self, name, *check_args, **check_kwargs):
829
187d2443c82d test: improvements for the helpers classes:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
184 if hasattr(self, name): # queue this new call as one already exists
187d2443c82d test: improvements for the helpers classes:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
185 self.expected_calls.setdefault(name, [])
187d2443c82d test: improvements for the helpers classes:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
186 self.expected_calls[name].append((check_args, check_kwargs))
187d2443c82d test: improvements for the helpers classes:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
187 return
187d2443c82d test: improvements for the helpers classes:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
188
335
Goffi <goffi@goffi.org>
parents:
diff changeset
189 def checkCall(*args, **kwargs):
Goffi <goffi@goffi.org>
parents:
diff changeset
190 if args != check_args or kwargs != check_kwargs:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 print "\n\n--------------------"
Goffi <goffi@goffi.org>
parents:
diff changeset
192 print "Args are not equals:"
Goffi <goffi@goffi.org>
parents:
diff changeset
193 print "args\n----\n%s (sent)\n%s (wanted)" % (args, check_args)
Goffi <goffi@goffi.org>
parents:
diff changeset
194 print "kwargs\n------\n%s (sent)\n%s (wanted)" % (kwargs, check_kwargs)
Goffi <goffi@goffi.org>
parents:
diff changeset
195 print "--------------------\n\n"
Goffi <goffi@goffi.org>
parents:
diff changeset
196 raise DifferentArgsException
829
187d2443c82d test: improvements for the helpers classes:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
197 delattr(self, name)
187d2443c82d test: improvements for the helpers classes:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
198
187d2443c82d test: improvements for the helpers classes:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
199 if name in self.expected_calls: # register the next call
187d2443c82d test: improvements for the helpers classes:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
200 args, kwargs = self.expected_calls[name].pop(0)
187d2443c82d test: improvements for the helpers classes:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
201 if len(self.expected_calls[name]) == 0:
187d2443c82d test: improvements for the helpers classes:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
202 del self.expected_calls[name]
187d2443c82d test: improvements for the helpers classes:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
203 self.expectCall(name, *args, **kwargs)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
204
335
Goffi <goffi@goffi.org>
parents:
diff changeset
205 setattr(self, name, checkCall)
Goffi <goffi@goffi.org>
parents:
diff changeset
206
859
64ec04991d9d plugin XEP-0277: fix pubsub entry parsing using lxml instead of feed.atom
souliane <souliane@mailoo.org>
parents: 849
diff changeset
207 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False, doc=None):
694
4284b6ad8aa3 tests: plugin text syntaxes sanitisation tests
Goffi <goffi@goffi.org>
parents: 693
diff changeset
208 pass
335
Goffi <goffi@goffi.org>
parents:
diff changeset
209
781
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
210 def addSignal(self, name, int_suffix, signature):
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
211 pass
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
212
794
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
213 def addTestCallback(self, name, method):
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
214 """This can be used to register callbacks for bridge methods AND signals.
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
215 Contrary to expectCall, this will not check if the method or signal is
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
216 called/sent with the correct arguments, it will instead run the callback
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
217 of your choice."""
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
218 setattr(self, name, method)
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
219
781
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
220
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
221 class FakeParams(Params):
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
222 """Class to simulate and test params object. The methods of Params that could
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
223 not be run (for example those using the storage attribute must be overwritten
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
224 by a naive simulation of what they should do."""
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
225
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
226 def __init__(self, host, storage):
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
227 Params.__init__(self, host, storage)
782
0e5807193721 test: added some tests for Memory
souliane <souliane@mailoo.org>
parents: 781
diff changeset
228 self.params = {} # naive simulation of values storage
781
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
229
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
230 def setParam(self, name, value, category, security_limit=-1, profile_key='@NONE@'):
782
0e5807193721 test: added some tests for Memory
souliane <souliane@mailoo.org>
parents: 781
diff changeset
231 profile = self.getProfileName(profile_key)
0e5807193721 test: added some tests for Memory
souliane <souliane@mailoo.org>
parents: 781
diff changeset
232 self.params.setdefault(profile, {})
0e5807193721 test: added some tests for Memory
souliane <souliane@mailoo.org>
parents: 781
diff changeset
233 self.params[profile_key][(category, name)] = value
781
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
234
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
235 def getParamA(self, name, category, attr="value", profile_key='@NONE@'):
782
0e5807193721 test: added some tests for Memory
souliane <souliane@mailoo.org>
parents: 781
diff changeset
236 profile = self.getProfileName(profile_key)
0e5807193721 test: added some tests for Memory
souliane <souliane@mailoo.org>
parents: 781
diff changeset
237 return self.params[profile][(category, name)]
0e5807193721 test: added some tests for Memory
souliane <souliane@mailoo.org>
parents: 781
diff changeset
238
0e5807193721 test: added some tests for Memory
souliane <souliane@mailoo.org>
parents: 781
diff changeset
239 def getProfileName(self, profile_key, return_profile_keys=False):
0e5807193721 test: added some tests for Memory
souliane <souliane@mailoo.org>
parents: 781
diff changeset
240 if profile_key == '@DEFAULT@':
792
2136be5a44a8 test: define the constants JIDs and profiles as lists
souliane <souliane@mailoo.org>
parents: 789
diff changeset
241 return Const.PROFILE[0]
782
0e5807193721 test: added some tests for Memory
souliane <souliane@mailoo.org>
parents: 781
diff changeset
242 elif profile_key == '@NONE@':
0e5807193721 test: added some tests for Memory
souliane <souliane@mailoo.org>
parents: 781
diff changeset
243 raise exceptions.ProfileNotSetError
0e5807193721 test: added some tests for Memory
souliane <souliane@mailoo.org>
parents: 781
diff changeset
244 else:
0e5807193721 test: added some tests for Memory
souliane <souliane@mailoo.org>
parents: 781
diff changeset
245 return profile_key
0e5807193721 test: added some tests for Memory
souliane <souliane@mailoo.org>
parents: 781
diff changeset
246
0e5807193721 test: added some tests for Memory
souliane <souliane@mailoo.org>
parents: 781
diff changeset
247 def loadIndParams(self, profile, cache=None):
0e5807193721 test: added some tests for Memory
souliane <souliane@mailoo.org>
parents: 781
diff changeset
248 self.params[profile] = {}
0e5807193721 test: added some tests for Memory
souliane <souliane@mailoo.org>
parents: 781
diff changeset
249 return defer.succeed(None)
781
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
250
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
251
786
c3acc1298a2f test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents: 782
diff changeset
252 class FakeMemory(Memory):
335
Goffi <goffi@goffi.org>
parents:
diff changeset
253 """Class to simulate and test memory object"""
Goffi <goffi@goffi.org>
parents:
diff changeset
254
781
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
255 def __init__(self, host):
786
c3acc1298a2f test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents: 782
diff changeset
256 # do not call Memory.__init__, we just want to call the methods that are
c3acc1298a2f test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents: 782
diff changeset
257 # manipulating basic stuff, the others should be overwritten when needed
781
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
258 self.host = host
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
259 self.params = FakeParams(host, None)
794
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
260 self.config = self.parseMainConf()
781
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
261 self.init()
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
262
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
263 def init(self):
786
c3acc1298a2f test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents: 782
diff changeset
264 """Tests that manipulate params, entities, features should
781
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
265 re-initialise the memory first to not fake the result."""
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
266 self.params.load_default_params()
782
0e5807193721 test: added some tests for Memory
souliane <souliane@mailoo.org>
parents: 781
diff changeset
267 self.params.params.clear()
833
9bac2fc74968 memory: bug fix to not register twice frontends parameters + added some tests for param update
souliane <souliane@mailoo.org>
parents: 829
diff changeset
268 self.params.frontends_cache = []
786
c3acc1298a2f test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents: 782
diff changeset
269 self.entities_data = {}
781
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
270
782
0e5807193721 test: added some tests for Memory
souliane <souliane@mailoo.org>
parents: 781
diff changeset
271 def getProfileName(self, profile_key, return_profile_keys=False):
0e5807193721 test: added some tests for Memory
souliane <souliane@mailoo.org>
parents: 781
diff changeset
272 return self.params.getProfileName(profile_key, return_profile_keys)
335
Goffi <goffi@goffi.org>
parents:
diff changeset
273
669
ffb716804580 core, bridge: extra parameter is saved in history:
Goffi <goffi@goffi.org>
parents: 649
diff changeset
274 def addToHistory(self, from_jid, to_jid, message, _type='chat', extra=None, timestamp=None, profile="@NONE@"):
335
Goffi <goffi@goffi.org>
parents:
diff changeset
275 pass
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
276
335
Goffi <goffi@goffi.org>
parents:
diff changeset
277 def addContact(self, contact_jid, attributes, groups, profile_key='@DEFAULT@'):
Goffi <goffi@goffi.org>
parents:
diff changeset
278 pass
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
279
484
23cbdf0a0777 core: presence status + last resource refactored and kept in entitiesCache in memory.py, profile cache is purged on disconnection
Goffi <goffi@goffi.org>
parents: 480
diff changeset
280 def setPresenceStatus(self, contact_jid, show, priority, statuses, profile_key='@DEFAULT@'):
335
Goffi <goffi@goffi.org>
parents:
diff changeset
281 pass
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
282
780
9810f22ba733 test: store the constants in constants.py + better PEP8 compliance
souliane <souliane@mailoo.org>
parents: 771
diff changeset
283 def addWaitingSub(self, type_, contact_jid, profile_key):
335
Goffi <goffi@goffi.org>
parents:
diff changeset
284 pass
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
285
335
Goffi <goffi@goffi.org>
parents:
diff changeset
286 def delWaitingSub(self, contact_jid, profile_key):
Goffi <goffi@goffi.org>
parents:
diff changeset
287 pass
Goffi <goffi@goffi.org>
parents:
diff changeset
288
1315
be3a301540c0 core (memory): updateEntityData now accept a "silent" argument to avoid sending signal to frontends when updating an attribute with "signalOnUpdate" flag.
Goffi <goffi@goffi.org>
parents: 1206
diff changeset
289 def updateEntityData(self, entity_jid, key, value, silent=False, profile_key="@NONE@"):
781
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
290 self.entities_data.setdefault(entity_jid, {})
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
291 self.entities_data[entity_jid][key] = value
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
292
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
293 def getEntityData(self, entity_jid, keys, profile_key):
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
294 result = {}
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
295 for key in keys:
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
296 result[key] = self.entities_data[entity_jid][key]
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
297 return result
80ab2b58e205 test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents: 780
diff changeset
298
591
65821b3fa7ab Fix pep8 support in src/test.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
299
335
Goffi <goffi@goffi.org>
parents:
diff changeset
300 class FakeTriggerManager(object):
Goffi <goffi@goffi.org>
parents:
diff changeset
301
786
c3acc1298a2f test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents: 782
diff changeset
302 def add(self, point_name, callback, priority=0):
335
Goffi <goffi@goffi.org>
parents:
diff changeset
303 pass
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
304
335
Goffi <goffi@goffi.org>
parents:
diff changeset
305 def point(self, point_name, *args, **kwargs):
Goffi <goffi@goffi.org>
parents:
diff changeset
306 """We always return true to continue the action"""
Goffi <goffi@goffi.org>
parents:
diff changeset
307 return True
Goffi <goffi@goffi.org>
parents:
diff changeset
308
780
9810f22ba733 test: store the constants in constants.py + better PEP8 compliance
souliane <souliane@mailoo.org>
parents: 771
diff changeset
309
649
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
310 class FakeRosterProtocol(SatRosterProtocol):
794
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
311 """This class is used by FakeClient (one instance per profile)"""
591
65821b3fa7ab Fix pep8 support in src/test.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
312
649
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
313 def __init__(self, host, parent):
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
314 SatRosterProtocol.__init__(self, host)
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
315 self.parent = parent
794
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
316 self.addItem(parent.jid)
649
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
317
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
318 def addItem(self, jid, *args, **kwargs):
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
319 if not args and not kwargs:
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
320 # defaults values setted for the tests only
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
321 kwargs["subscriptionTo"] = True
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
322 kwargs["subscriptionFrom"] = True
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
323 roster_item = RosterItem(jid, *args, **kwargs)
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
324 attrs = {'to': b2s(roster_item.subscriptionTo), 'from': b2s(roster_item.subscriptionFrom), 'ask': b2s(roster_item.pendingOut)}
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
325 if roster_item.name:
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
326 attrs['name'] = roster_item.name
693
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
327 self.host.bridge.expectCall("newContact", jid.full(), attrs, roster_item.groups, self.parent.profile)
649
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
328 self.onRosterSet(roster_item)
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
329
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
330
794
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
331 class FakeXmlStream(object):
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
332 """This class is used by FakeClient (one instance per profile)"""
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
333
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
334 def __init__(self):
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
335 self.sent = []
649
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
336
e20c823f23e2 tests: helpers improvments:
Goffi <goffi@goffi.org>
parents: 647
diff changeset
337 def send(self, obj):
794
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
338 """Save the sent messages to compare them later"""
829
187d2443c82d test: improvements for the helpers classes:
souliane <souliane@mailoo.org>
parents: 811
diff changeset
339 self.sent.append(obj)
849
c5a8f602662b plugin room_game, radiocol: RoomGame.send returns a Deferred.
souliane <souliane@mailoo.org>
parents: 833
diff changeset
340 return defer.succeed(None)
794
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
341
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
342
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
343 class FakeClient(object):
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
344 """Tests involving more than one profile need one instance of this class per profile"""
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
345
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
346 def __init__(self, host, profile=None):
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
347 self.host = host
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
348 self.profile = profile if profile else Const.PROFILE[0]
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
349 self.jid = Const.PROFILE_DICT[self.profile]
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
350 self.roster = FakeRosterProtocol(host, self)
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
351 self.xmlstream = FakeXmlStream()
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
352
52c4b755aba6 test: make FakeClient profile dependent and add some tools to test MUC
souliane <souliane@mailoo.org>
parents: 792
diff changeset
353 def send(self, obj):
849
c5a8f602662b plugin room_game, radiocol: RoomGame.send returns a Deferred.
souliane <souliane@mailoo.org>
parents: 833
diff changeset
354 return self.xmlstream.send(obj)
335
Goffi <goffi@goffi.org>
parents:
diff changeset
355
591
65821b3fa7ab Fix pep8 support in src/test.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
356
693
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
357 class SatTestCase(unittest.TestCase):
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
358
780
9810f22ba733 test: store the constants in constants.py + better PEP8 compliance
souliane <souliane@mailoo.org>
parents: 771
diff changeset
359 def assertEqualXML(self, xml, expected, ignore_blank=False):
693
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
360 def equalElt(got_elt, exp_elt):
696
f1a2831d549d test: better ignore_blank in helpers's assertEqualXML
Goffi <goffi@goffi.org>
parents: 694
diff changeset
361 if ignore_blank:
f1a2831d549d test: better ignore_blank in helpers's assertEqualXML
Goffi <goffi@goffi.org>
parents: 694
diff changeset
362 for elt in got_elt, exp_elt:
780
9810f22ba733 test: store the constants in constants.py + better PEP8 compliance
souliane <souliane@mailoo.org>
parents: 771
diff changeset
363 for attr in ('text', 'tail'):
696
f1a2831d549d test: better ignore_blank in helpers's assertEqualXML
Goffi <goffi@goffi.org>
parents: 694
diff changeset
364 value = getattr(elt, attr)
f1a2831d549d test: better ignore_blank in helpers's assertEqualXML
Goffi <goffi@goffi.org>
parents: 694
diff changeset
365 try:
f1a2831d549d test: better ignore_blank in helpers's assertEqualXML
Goffi <goffi@goffi.org>
parents: 694
diff changeset
366 value = value.strip() or None
f1a2831d549d test: better ignore_blank in helpers's assertEqualXML
Goffi <goffi@goffi.org>
parents: 694
diff changeset
367 except AttributeError:
f1a2831d549d test: better ignore_blank in helpers's assertEqualXML
Goffi <goffi@goffi.org>
parents: 694
diff changeset
368 value = None
f1a2831d549d test: better ignore_blank in helpers's assertEqualXML
Goffi <goffi@goffi.org>
parents: 694
diff changeset
369 setattr(elt, attr, value)
693
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
370 if (got_elt.tag != exp_elt.tag):
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
371 print "XML are not equals (elt %s/%s):" % (got_elt, exp_elt)
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
372 print "tag: got [%s] expected: [%s]" % (got_elt.tag, exp_elt.tag)
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
373 return False
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
374 if (got_elt.attrib != exp_elt.attrib):
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
375 print "XML are not equals (elt %s/%s):" % (got_elt, exp_elt)
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
376 print "attribs: got %s expected %s" % (got_elt.attrib, exp_elt.attrib)
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
377 return False
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
378 if (got_elt.tail != exp_elt.tail or got_elt.text != exp_elt.text):
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
379 print "XML are not equals (elt %s/%s):" % (got_elt, exp_elt)
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
380 print "text: got [%s] expected: [%s]" % (got_elt.text, exp_elt.text)
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
381 print "tail: got [%s] expected: [%s]" % (got_elt.tail, exp_elt.tail)
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
382 return False
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
383 if (len(got_elt) != len(exp_elt)):
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
384 print "XML are not equals (elt %s/%s):" % (got_elt, exp_elt)
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
385 print "children len: got %d expected: %d" % (len(got_elt), len(exp_elt))
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
386 return False
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
387 for idx, child in enumerate(got_elt):
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
388 if not equalElt(child, exp_elt[idx]):
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
389 return False
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
390 return True
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
391
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
392 def remove_blank(xml):
780
9810f22ba733 test: store the constants in constants.py + better PEP8 compliance
souliane <souliane@mailoo.org>
parents: 771
diff changeset
393 lines = [line.strip() for line in re.sub(r'[ \t\r\f\v]+', ' ', xml).split('\n')]
693
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
394 return '\n'.join([line for line in lines if line])
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
395
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
396 xml_elt = etree.fromstring(remove_blank(xml) if ignore_blank else xml)
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
397 expected_elt = etree.fromstring(remove_blank(expected) if ignore_blank else expected)
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
398
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
399 if not equalElt(xml_elt, expected_elt):
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
400 print "---"
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
401 print "XML are not equals:"
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
402 print "got:\n-\n%s\n-\n\n" % etree.tostring(xml_elt, encoding='utf-8')
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
403 print "was expecting:\n-\n%s\n-\n\n" % etree.tostring(expected_elt, encoding='utf-8')
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
404 print "---"
65b30bc7f1b3 tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents: 669
diff changeset
405 raise DifferentXMLException
789
0cb423500fbb test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
souliane <souliane@mailoo.org>
parents: 786
diff changeset
406
0cb423500fbb test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
souliane <souliane@mailoo.org>
parents: 786
diff changeset
407 def assertEqualUnsortedList(self, a, b, msg):
0cb423500fbb test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
souliane <souliane@mailoo.org>
parents: 786
diff changeset
408 counter_a = Counter(a)
0cb423500fbb test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
souliane <souliane@mailoo.org>
parents: 786
diff changeset
409 counter_b = Counter(b)
0cb423500fbb test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
souliane <souliane@mailoo.org>
parents: 786
diff changeset
410 if counter_a != counter_b:
0cb423500fbb test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
souliane <souliane@mailoo.org>
parents: 786
diff changeset
411 print "---"
0cb423500fbb test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
souliane <souliane@mailoo.org>
parents: 786
diff changeset
412 print "Unsorted lists are not equals:"
0cb423500fbb test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
souliane <souliane@mailoo.org>
parents: 786
diff changeset
413 print "got : %s" % counter_a
0cb423500fbb test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
souliane <souliane@mailoo.org>
parents: 786
diff changeset
414 print "was expecting: %s" % counter_b
0cb423500fbb test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
souliane <souliane@mailoo.org>
parents: 786
diff changeset
415 if msg:
0cb423500fbb test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
souliane <souliane@mailoo.org>
parents: 786
diff changeset
416 print msg
0cb423500fbb test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
souliane <souliane@mailoo.org>
parents: 786
diff changeset
417 print "---"
0cb423500fbb test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
souliane <souliane@mailoo.org>
parents: 786
diff changeset
418 raise DifferentListException