# encoding: UTF-8
require 'vines'
require 'ext/nokogiri'
require 'minitest/mock'
require 'test/unit'
class MessageTest < Test::Unit::TestCase
def setup
@stream = MiniTest::Mock.new
end
def test_bad_type_returns_error
node = node('hello!')
stanza = Vines::Stanza::Message.new(node, @stream)
assert_raise(Vines::StanzaErrors::BadRequest) { stanza.process }
end
def test_missing_to_address_is_sent_to_sender
alice = Vines::User.new(:jid => 'alice@wonderland.lit/tea')
node = node('hello!')
recipient = MiniTest::Mock.new
router = MiniTest::Mock.new
router.expect(:local?, true, [node])
router.expect(:connected_resources, [recipient], [alice.jid.bare])
@stream.expect(:router, router)
@stream.expect(:user, alice)
@stream.expect(:broadcast, nil, [node, [recipient]])
stanza = Vines::Stanza::Message.new(node, @stream)
assert_nothing_raised { stanza.process }
assert @stream.verify
assert router.verify
assert recipient.verify
end
def test_message_to_non_user_is_ignored
bogus = Vines::JID.new('bogus@wonderland.lit/cake')
node = node(%Q{hello!})
router = MiniTest::Mock.new
router.expect(:local?, true, [node])
router.expect(:connected_resources, [], [bogus])
storage = MiniTest::Mock.new
storage.expect(:find_user, nil, [bogus])
@stream.expect(:router, router)
@stream.expect(:storage, storage, [bogus.domain])
stanza = Vines::Stanza::Message.new(node, @stream)
assert_nothing_raised { stanza.process }
assert @stream.verify
assert router.verify
assert storage.verify
end
def test_message_to_offline_user_returns_error
hatter = Vines::User.new(:jid => 'hatter@wonderland.lit/cake')
node = node(%Q{hello!})
router = MiniTest::Mock.new
router.expect(:local?, true, [node])
router.expect(:connected_resources, [], [hatter.jid])
storage = MiniTest::Mock.new
storage.expect(:find_user, hatter, [hatter.jid])
@stream.expect(:router, router)
@stream.expect(:storage, storage, [hatter.jid.domain])
stanza = Vines::Stanza::Message.new(node, @stream)
assert_raise(Vines::StanzaErrors::ServiceUnavailable) { stanza.process }
assert @stream.verify
assert router.verify
assert storage.verify
end
def test_message_to_local_user_in_different_domain_is_delivered
romeo = Vines::User.new(:jid => 'romeo@verona.lit/balcony')
node = node(%Q{hello!})
recipient = MiniTest::Mock.new
router = MiniTest::Mock.new
router.expect(:local?, true, [node])
router.expect(:connected_resources, [recipient], [romeo.jid])
@stream.expect(:router, router)
@stream.expect(:broadcast, nil, [node, [recipient]])
stanza = Vines::Stanza::Message.new(node, @stream)
assert_nothing_raised { stanza.process }
assert @stream.verify
assert router.verify
assert recipient.verify
end
def test_message_to_remote_user_is_routed
alice = Vines::User.new(:jid => 'alice@wonderland.lit/tea')
romeo = Vines::User.new(:jid => 'romeo@verona.lit/balcony')
node = node(%Q{hello!})
expected = node(%Q{hello!})
router = MiniTest::Mock.new
router.expect(:local?, false, [node])
router.expect(:route, nil, [expected])
@stream.expect(:router, router)
@stream.expect(:user, alice)
stanza = Vines::Stanza::Message.new(node, @stream)
assert_nothing_raised { stanza.process }
assert @stream.verify
assert router.verify
end
private
def node(xml)
Nokogiri::XML(xml).root
end
end