Class: JabberSimpleTest

Inherits:
Test::Unit::TestCase show all
Defined in:
lib/gems/xmpp4r-simple-0.8.8/test/test_xmpp4r_simple.rb

Instance Method Summary collapse

Methods inherited from Test::Unit::TestCase

#assert_array_equal, expect, #run

Instance Method Details

#setupObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/gems/xmpp4r-simple-0.8.8/test/test_xmpp4r_simple.rb', line 26

def setup
  @@connections ||= {}

  if @@connections.include?(:client1)
    @client1 = @@connections[:client1]
    @client2 = @@connections[:client2]
    @client1.accept_subscriptions = true
    @client2.accept_subscriptions = true
    @jid1_raw = @@connections[:jid1_raw]
    @jid2_raw = @@connections[:jid2_raw]
    @jid1 = @jid1_raw.strip.to_s
    @jid2 = @jid2_raw.strip.to_s
    return true
  end

  logins = []
  begin
    logins = File.readlines(File.expand_path("~/.xmpp4r-simple-test-config")).map! { || .split(" ") }
    raise StandardError unless logins.size == 2
  rescue => e
    puts "\nConfiguration Error!\n\nYou must make available two unique Jabber accounts in order for the tests to pass."
    puts "Place them in ~/.xmpp4r-simple-test-config, one per line like so:\n\n"
    puts "[email protected]/res password"
    puts "[email protected]/res password\n\n"
    raise e
  end

  @@connections[:client1] = Jabber::Simple.new(*logins[0])
  @@connections[:client2] = Jabber::Simple.new(*logins[1])

  @@connections[:jid1_raw] = Jabber::JID.new(logins[0][0])
  @@connections[:jid2_raw] = Jabber::JID.new(logins[1][0])

  # Force load the client and roster, just to be safe.
  @@connections[:client1].roster
  @@connections[:client2].roster

  # Re-run this method to setup the local instance variables the first time.
  setup
end

#test_add_users_to_our_roster_should_succeed_with_automatic_approvalObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/gems/xmpp4r-simple-0.8.8/test/test_xmpp4r_simple.rb', line 90

def test_add_users_to_our_roster_should_succeed_with_automatic_approval
  @client1.remove(@jid2)
  @client2.remove(@jid1)

  assert_before 60 do
    assert_equal false, @client1.subscribed_to?(@jid2)
    assert_equal false, @client2.subscribed_to?(@jid1)
  end

  @client1.new_subscriptions
  @client1.add(@jid2)

  assert_before 60 do
    assert @client1.subscribed_to?(@jid2)
    assert @client2.subscribed_to?(@jid1)
  end

  new_subscriptions = @client1.new_subscriptions
  assert_equal 1, new_subscriptions.size
  assert_equal @jid2, new_subscriptions[0][0].jid.strip.to_s
end

#test_automatically_reconnectObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/gems/xmpp4r-simple-0.8.8/test/test_xmpp4r_simple.rb', line 194

def test_automatically_reconnect
  @client1.client.close

  assert_before 60 do
    assert_equal false, @client1.connected?
  end

  # empty client 2's received message queue.
  @client2.received_messages

  @client1.deliver(@jid2, "Testing")

  assert_before(60) { assert @client1.connected? }
  assert @client1.roster.instance_variable_get('@stream').is_connected?
  assert_before(60) { assert_equal 1, @client2.received_messages.size }
end

#test_disable_auto_accept_subscription_requestsObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/gems/xmpp4r-simple-0.8.8/test/test_xmpp4r_simple.rb', line 167

def test_disable_auto_accept_subscription_requests
  @client1.remove(@jid2)
  @client2.remove(@jid1)

  assert_before(60) do
    assert_equal false, @client1.subscribed_to?(@jid2)
    assert_equal false, @client2.subscribed_to?(@jid1)
  end

  @client1.accept_subscriptions = false
  assert_equal false, @client1.accept_subscriptions?
  
  assert_before(60) { assert_equal 0, @client1.subscription_requests.size }

  @client2.add(@jid1)

  new_subscription_requests = []
  assert_before(60) do
    new_subscription_requests = @client1.subscription_requests
    assert_equal 1, new_subscription_requests.size
  end

  new_subscription = new_subscription_requests.first
  assert_equal @jid2, new_subscription[0].jid.strip.to_s
  assert_equal :subscribe, new_subscription[1].type
end

#test_disconnect_doesnt_allow_auto_reconnectsObject



211
212
213
214
215
216
217
218
219
220
221
# File 'lib/gems/xmpp4r-simple-0.8.8/test/test_xmpp4r_simple.rb', line 211

def test_disconnect_doesnt_allow_auto_reconnects
  @client1.disconnect

  assert_equal false, @client1.connected?
  
  assert_raises Jabber::ConnectionError do
    @client1.deliver(@jid2, "testing")
  end

  @client1.reconnect
end

#test_ensure_the_jabber_clients_are_connected_after_setupObject



67
68
69
70
# File 'lib/gems/xmpp4r-simple-0.8.8/test/test_xmpp4r_simple.rb', line 67

def test_ensure_the_jabber_clients_are_connected_after_setup
  assert @client1.client.is_connected?
  assert @client2.client.is_connected?
end

#test_inspect_contact_should_be_customObject



76
77
78
# File 'lib/gems/xmpp4r-simple-0.8.8/test/test_xmpp4r_simple.rb', line 76

def test_inspect_contact_should_be_custom
  assert_equal "Jabber::Contact #{@jid2}", @client1.contacts(@jid2).inspect
end

#test_inspect_should_be_customObject



72
73
74
# File 'lib/gems/xmpp4r-simple-0.8.8/test/test_xmpp4r_simple.rb', line 72

def test_inspect_should_be_custom
  assert_equal "Jabber::Simple #{@jid1_raw}", @client1.inspect
end

#test_presence_updates_should_be_receivedObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/gems/xmpp4r-simple-0.8.8/test/test_xmpp4r_simple.rb', line 145

def test_presence_updates_should_be_received

  @client2.add(@client1)
  @client1.add(@client2)

  assert_before(60) { assert @client2.subscribed_to?(@jid1) }
  assert_before(60) { assert_equal 0, @client2.presence_updates.size }

  @client1.status(:away, "Doing something else.")
  
  new_statuses = []
  assert_before(60) do
    new_statuses = @client2.presence_updates
    assert_equal 1, new_statuses.size
  end

  new_status = new_statuses.first
  assert_equal @jid1, new_status[0]
  assert_equal "Doing something else.", new_status[2]
  assert_equal :away, new_status[1]
end

#test_remove_users_from_our_roster_should_succeedObject



80
81
82
83
84
85
86
87
88
# File 'lib/gems/xmpp4r-simple-0.8.8/test/test_xmpp4r_simple.rb', line 80

def test_remove_users_from_our_roster_should_succeed
  @client2.remove(@jid1)
  @client1.remove(@jid2)

  sleep 3

  assert_equal false, @client1.subscribed_to?(@jid2)
  assert_equal false, @client2.subscribed_to?(@jid1)
end

#test_sent_message_should_be_receivedObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/gems/xmpp4r-simple-0.8.8/test/test_xmpp4r_simple.rb', line 112

def test_sent_message_should_be_received
  # First clear the client's message queue, just in case.
  assert_kind_of Array, @client2.received_messages

  # Next ensure that we're not subscribed, so that we can test the deferred message queue.
  @client1.remove(@jid2)
  @client2.remove(@jid1)
  sleep 2

  # Deliver the messages; this should be received by the other client.
  @client1.deliver(@jid2, "test message")

  sleep 2

  # Fetch the message; allow up to ten seconds for the delivery to occur.
  messages = []
  begin
    Timeout::timeout(20) {
      loop do
        messages = @client2.received_messages
        break unless messages.empty?
        sleep 1
      end
    }
  rescue Timeout::Error
    flunk "Timeout waiting for message"
  end

  # Ensure that the message was received intact.
  assert_equal @jid1, messages.first.from.strip.to_s
  assert_equal "test message", messages.first.body
end