pyu-xmpp4r-simple 0.8.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,313 @@
1
+ # Jabber::Simple - An extremely easy-to-use Jabber client library.
2
+ # Copyright 2006 Blaine Cook <blaine@obvious.com>, Obvious Corp.
3
+ #
4
+ # Jabber::Simple is free software; you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation; either version 2 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # Jabber::Simple is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with Jabber::Simple; if not, write to the Free Software
16
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17
+ #
18
+ $:.unshift "#{File.dirname(__FILE__)}/../lib"
19
+
20
+ require 'test/unit'
21
+ require 'timeout'
22
+ require 'xmpp4r-simple'
23
+
24
+ class JabberSimpleTest < Test::Unit::TestCase
25
+
26
+ def setup
27
+ @@connections ||= {}
28
+
29
+ if @@connections.include?(:client1)
30
+ @client1 = @@connections[:client1]
31
+ @client2 = @@connections[:client2]
32
+ @client1.accept_subscriptions = true
33
+ @client2.accept_subscriptions = true
34
+ @jid1_raw = @@connections[:jid1_raw]
35
+ @jid2_raw = @@connections[:jid2_raw]
36
+ @jid1 = @jid1_raw.strip.to_s
37
+ @jid2 = @jid2_raw.strip.to_s
38
+ @domain1 = @jid1_raw.domain
39
+ @domain2 = @jid2_raw.domain
40
+ @pubsub1 = "pubsub." + @domain1
41
+ @pubsub2 = "pubsub." + @domain2
42
+ return true
43
+ end
44
+
45
+ logins = []
46
+ begin
47
+ logins = File.readlines(File.expand_path("~/.xmpp4r-simple-test-config")).map! { |login| login.split(" ") }
48
+ raise StandardError unless logins.size == 2
49
+ rescue => e
50
+ puts "\nConfiguration Error!\n\nYou must make available two unique Jabber accounts in order for the tests to pass."
51
+ puts "Place them in ~/.xmpp4r-simple-test-config, one per line like so:\n\n"
52
+ puts "user1@example.com/res password"
53
+ puts "user2@example.com/res password\n\n"
54
+ raise e
55
+ end
56
+
57
+ @@connections[:client1] = Jabber::Simple.new(*logins[0])
58
+ @@connections[:client2] = Jabber::Simple.new(*logins[1])
59
+
60
+ @@connections[:jid1_raw] = Jabber::JID.new(logins[0][0])
61
+ @@connections[:jid2_raw] = Jabber::JID.new(logins[1][0])
62
+
63
+ # Force load the client and roster, just to be safe.
64
+ @@connections[:client1].roster
65
+ @@connections[:client2].roster
66
+
67
+ # Re-run this method to setup the local instance variables the first time.
68
+ setup
69
+ end
70
+
71
+ def test_ensure_the_jabber_clients_are_connected_after_setup
72
+ assert @client1.client.is_connected?
73
+ assert @client2.client.is_connected?
74
+ end
75
+
76
+ def test_inspect_should_be_custom
77
+ assert_equal "Jabber::Simple #{@jid1_raw}", @client1.inspect
78
+ end
79
+
80
+ def test_inspect_contact_should_be_custom
81
+ assert_equal "Jabber::Contact #{@jid2}", @client1.contacts(@jid2).inspect
82
+ end
83
+
84
+ def test_remove_users_from_our_roster_should_succeed
85
+ @client2.remove(@jid1)
86
+ @client1.remove(@jid2)
87
+
88
+ sleep 3
89
+
90
+ assert_equal false, @client1.subscribed_to?(@jid2)
91
+ assert_equal false, @client2.subscribed_to?(@jid1)
92
+ end
93
+
94
+ def test_add_users_to_our_roster_should_succeed_with_automatic_approval
95
+ @client1.remove(@jid2)
96
+ @client2.remove(@jid1)
97
+
98
+ assert_before 10 do
99
+ assert_equal false, @client1.subscribed_to?(@jid2)
100
+ assert_equal false, @client2.subscribed_to?(@jid1)
101
+ end
102
+
103
+ @client1.new_subscriptions
104
+ @client1.add(@jid2)
105
+
106
+ assert_before 10 do
107
+ assert @client1.subscribed_to?(@jid2)
108
+ assert @client2.subscribed_to?(@jid1)
109
+ end
110
+
111
+ new_subscriptions = @client1.new_subscriptions
112
+ assert_equal 1, new_subscriptions.size
113
+ assert_equal @jid2, new_subscriptions[0][0].jid.strip.to_s
114
+ end
115
+
116
+ def test_sent_message_should_be_received
117
+ # First clear the client's message queue, just in case.
118
+ assert_kind_of Array, @client2.received_messages
119
+
120
+ # Next ensure that we're not subscribed, so that we can test the deferred message queue.
121
+ @client1.remove(@jid2)
122
+ @client2.remove(@jid1)
123
+ sleep 2
124
+
125
+ # Deliver the messages; this should be received by the other client.
126
+ @client1.deliver(@jid2, "test message")
127
+
128
+ sleep 2
129
+
130
+ # Fetch the message; allow up to ten seconds for the delivery to occur.
131
+ messages = []
132
+ begin
133
+ Timeout::timeout(20) {
134
+ loop do
135
+ messages = @client2.received_messages
136
+ break unless messages.empty?
137
+ sleep 1
138
+ end
139
+ }
140
+ rescue Timeout::Error
141
+ flunk "Timeout waiting for message"
142
+ end
143
+
144
+ # Ensure that the message was received intact.
145
+ assert_equal @jid1, messages.first.from.strip.to_s
146
+ assert_equal "test message", messages.first.body
147
+ end
148
+
149
+ def test_presence_updates_should_be_received
150
+
151
+ @client2.add(@client1)
152
+ @client1.add(@client2)
153
+
154
+ assert_before(60) { assert @client2.subscribed_to?(@jid1) }
155
+ assert_before(60) { assert_equal 0, @client2.presence_updates.size }
156
+
157
+ @client1.status(:away, "Doing something else.")
158
+
159
+ new_statuses = []
160
+ assert_before(60) do
161
+ new_statuses = @client2.presence_updates
162
+ assert_equal 1, new_statuses.size
163
+ end
164
+
165
+ new_status = new_statuses.first
166
+ assert_equal @jid1, new_status[0]
167
+ assert_equal "Doing something else.", new_status[2]
168
+ assert_equal :away, new_status[1]
169
+ end
170
+
171
+ def test_disable_auto_accept_subscription_requests
172
+ @client1.remove(@jid2)
173
+ @client2.remove(@jid1)
174
+
175
+ assert_before(60) do
176
+ assert_equal false, @client1.subscribed_to?(@jid2)
177
+ assert_equal false, @client2.subscribed_to?(@jid1)
178
+ end
179
+
180
+ @client1.accept_subscriptions = false
181
+ assert_equal false, @client1.accept_subscriptions?
182
+
183
+ assert_before(60) { assert_equal 0, @client1.subscription_requests.size }
184
+
185
+ @client2.add(@jid1)
186
+
187
+ new_subscription_requests = []
188
+ assert_before(60) do
189
+ new_subscription_requests = @client1.subscription_requests
190
+ assert_equal 1, new_subscription_requests.size
191
+ end
192
+
193
+ new_subscription = new_subscription_requests.first
194
+ assert_equal @jid2, new_subscription[0].jid.strip.to_s
195
+ assert_equal :subscribe, new_subscription[1].type
196
+ end
197
+
198
+ def test_automatically_reconnect
199
+ @client1.client.close
200
+
201
+ assert_before 60 do
202
+ assert_equal false, @client1.connected?
203
+ end
204
+
205
+ # empty client 2's received message queue.
206
+ @client2.received_messages
207
+
208
+ @client1.deliver(@jid2, "Testing")
209
+
210
+ assert_before(60) { assert @client1.connected? }
211
+ assert @client1.roster.instance_variable_get('@stream').is_connected?
212
+ assert_before(60) { assert_equal 1, @client2.received_messages.size }
213
+ end
214
+
215
+ def test_disconnect_doesnt_allow_auto_reconnects
216
+ @client1.disconnect
217
+
218
+ assert_equal false, @client1.connected?
219
+
220
+ assert_raises Jabber::ConnectionError do
221
+ @client1.deliver(@jid2, "testing")
222
+ end
223
+
224
+ @client1.reconnect
225
+ end
226
+
227
+ def test_set_pubsub_service
228
+ if ! @client1.has_pubsub?
229
+ @client1.set_pubsub_service(@pubsub1)
230
+ end
231
+ assert @client1.has_pubsub?
232
+ end
233
+
234
+ def test_create_delete_node
235
+ set_pubsub
236
+ delete_nodes
237
+
238
+ assert_nothing_raised @client1.create_node("/home/#{@domain1}/testnode1")
239
+ assert_nothing_raised @client2.create_node("/home/#{@domain2}/testnode2")
240
+ assert_nothing_raised @client1.create_node("/home/#{@domain1}/testnode1/level1")
241
+ assert_nothing_raised @client2.create_node("/home/#{@domain2}/testnode2/level2")
242
+
243
+ assert_nothing_raised @client1.delete_node("/home/#{@domain1}/testnode1")
244
+ assert_nothing_raised @client2.delete_node("/home/#{@domain2}/testnode2")
245
+ assert_nothing_raised @client1.delete_node("/home/#{@domain1}/testnode1/level1")
246
+ assert_nothing_raised @client2.delete_node("/home/#{@domain2}/testnode2/level2")
247
+ end
248
+
249
+ def test_publish_and_subscribe
250
+ set_pubsub
251
+ create_nodes
252
+
253
+ @client1.pubsubscribe_to("/home/#{@domain2}/testnode2/level2")
254
+ @client2.pubsubscribe_to("/home/#{@domain1}/testnode1/level1")
255
+
256
+ puts @client1.pubsubscriptions
257
+ puts @client2.pubsubscriptions
258
+
259
+ delete_nodes
260
+ end
261
+
262
+ private
263
+
264
+ def assert_before(seconds, &block)
265
+ error = nil
266
+
267
+ # This is for Ruby 1.9.1 compatibility
268
+ assertion_exception_class = begin
269
+ MiniTest::Assertion
270
+ rescue NameError
271
+ Test::Unit::AssertionFailedError
272
+ end
273
+
274
+ begin
275
+ Timeout::timeout(seconds) {
276
+ begin
277
+ yield
278
+ rescue assertion_exception_class => e
279
+ error = e
280
+ sleep 0.5
281
+ retry
282
+ end
283
+ }
284
+ rescue Timeout::Error
285
+ raise error
286
+ end
287
+ end
288
+
289
+ def create_nodes
290
+ @client1.create_node("/home/#{@domain1}/testnode1")
291
+ @client2.create_node("/home/#{@domain2}/testnode2")
292
+ @client1.create_node("/home/#{@domain1}/testnode1/level1")
293
+ @client2.create_node("/home/#{@domain2}/testnode2/level2")
294
+ end
295
+
296
+ def delete_nodes
297
+ begin
298
+ @client1.delete_node("/home/#{@domain1}/testnode1")
299
+ @client2.delete_node("/home/#{@domain2}/testnode2")
300
+ @client1.delete_node("/home/#{@domain1}/testnode1/level1")
301
+ @client2.delete_node("/home/#{@domain2}/testnode2/level2")
302
+ rescue
303
+ # => Do nothing
304
+ end
305
+ end
306
+
307
+ def set_pubsub
308
+ @client1.set_pubsub_service(@pubsub1) if ! @client1.has_pubsub?
309
+ @client2.set_pubsub_service(@pubsub2) if ! @client2.has_pubsub?
310
+ end
311
+
312
+
313
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pyu-xmpp4r-simple
3
+ version: !ruby/object:Gem::Version
4
+ hash: 47
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 8
9
+ - 8
10
+ version: 0.8.8
11
+ platform: ruby
12
+ authors:
13
+ - Blaine Cook
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-03 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: xmpp4r
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 23
30
+ segments:
31
+ - 0
32
+ - 3
33
+ - 2
34
+ version: 0.3.2
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: " Jabber::Simple takes the strong foundation laid by xmpp4r\n and hides the relatively high complexity of maintaining a simple instant\n messenger bot in Ruby.\n"
38
+ email: romeda@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - README
45
+ - COPYING
46
+ files:
47
+ - test/test_xmpp4r_simple.rb
48
+ - lib/xmpp4r-simple.rb
49
+ - README
50
+ - COPYING
51
+ - CHANGELOG
52
+ has_rdoc: true
53
+ homepage: http://xmpp4r-simple.rubyforge.org/
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options: []
58
+
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 3
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ requirements: []
80
+
81
+ rubyforge_project: xmpp4r-simple
82
+ rubygems_version: 1.3.7
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: A simplified Jabber client library.
86
+ test_files:
87
+ - test/test_xmpp4r_simple.rb