# encoding: UTF-8
require 'vines'
require 'minitest/mock'
require 'test/unit'
class ServerReadyTest < Test::Unit::TestCase
STANZAS = []
def setup
@stream = MiniTest::Mock.new
@state = Vines::Stream::Server::Ready.new(@stream, nil)
def @state.to_stanza(node)
if node.name == 'bogus'
nil
else
MiniTest::Mock.new.tap do |stanza|
stanza.expect(:process, nil)
stanza.class.send(:define_method, :[]) do |a|
node[a]
end
ServerReadyTest::STANZAS << stanza
end
end
end
end
def teardown
STANZAS.clear
end
def test_good_node_processes
@stream.expect(:remote_domain, 'wonderland.lit')
@stream.expect(:domain, 'verona.lit')
@stream.expect(:user=, nil, [Vines::User.new(:jid => 'alice@wonderland.lit')])
node = node(%Q{})
assert_nothing_raised { @state.node(node) }
assert_equal 1, STANZAS.size
assert STANZAS.map {|s| s.verify }.all?
assert @stream.verify
end
def test_unsupported_stanza_type
node = node('')
assert_raise(Vines::StreamErrors::UnsupportedStanzaType) { @state.node(node) }
assert STANZAS.empty?
assert @stream.verify
end
def test_improper_addressing_missing_to
node = node(%Q{})
assert_raise(Vines::StreamErrors::ImproperAddressing) { @state.node(node) }
assert_equal 1, STANZAS.size
assert @stream.verify
end
def test_improper_addressing_empty_to
node = node(%Q{})
assert_raise(Vines::StreamErrors::ImproperAddressing) { @state.node(node) }
assert_equal 1, STANZAS.size
assert @stream.verify
end
def test_improper_addressing_missing_from
node = node(%Q{})
assert_raise(Vines::StreamErrors::ImproperAddressing) { @state.node(node) }
assert_equal 1, STANZAS.size
assert @stream.verify
end
def test_improper_addressing_empty_from
node = node(%Q{})
assert_raise(Vines::StreamErrors::ImproperAddressing) { @state.node(node) }
assert_equal 1, STANZAS.size
assert @stream.verify
end
def test_invalid_from
@stream.expect(:remote_domain, 'wonderland.lit')
node = node(%Q{})
assert_raise(Vines::StreamErrors::InvalidFrom) { @state.node(node) }
assert_equal 1, STANZAS.size
assert @stream.verify
end
def test_host_unknown
@stream.expect(:remote_domain, 'wonderland.lit')
@stream.expect(:domain, 'verona.lit')
node = node(%Q{})
assert_raise(Vines::StreamErrors::HostUnknown) { @state.node(node) }
assert_equal 1, STANZAS.size
assert @stream.verify
end
private
def node(xml)
Nokogiri::XML(xml).root
end
end