Skip to content

Commit

Permalink
Convert to specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
dgraham committed Sep 17, 2012
1 parent bbfe797 commit f78d0d5
Showing 1 changed file with 92 additions and 89 deletions.
181 changes: 92 additions & 89 deletions test/stanza/message_test.rb
Original file line number Diff line number Diff line change
@@ -1,123 +1,126 @@
# encoding: UTF-8

require 'tmpdir'
require 'vines'
require 'ext/nokogiri'
require 'minitest/autorun'

class MessageTest < MiniTest::Unit::TestCase
def setup
@stream = MiniTest::Mock.new
@config = Vines::Config.new do
require 'test_helper'

describe Vines::Stanza::Message do
subject { Vines::Stanza::Message.new(xml, stream) }
let(:stream) { MiniTest::Mock.new }
let(:alice) { Vines::User.new(jid: 'alice@wonderland.lit/tea') }
let(:romeo) { Vines::User.new(jid: 'romeo@verona.lit/balcony') }
let(:config) do
Vines::Config.new do
host 'wonderland.lit' do
storage(:fs) { dir Dir.tmpdir }
end
end
end

def test_bad_type_returns_error
node = node('<message type="bogus">hello!</message>')
stanza = Vines::Stanza::Message.new(node, @stream)
assert_raises(Vines::StanzaErrors::BadRequest) { stanza.process }
before do
class << stream
attr_accessor :config, :user
end
stream.user = alice
stream.config = config
end

def test_missing_to_address_is_sent_to_sender
alice = Vines::User.new(:jid => 'alice@wonderland.lit/tea')
node = node('<message>hello!</message>')

recipient = MiniTest::Mock.new
recipient.expect(:user, alice)
recipient.expect(:write, nil, [node])

@stream.expect(:user, alice)
@stream.expect(:connected_resources, [recipient], [alice.jid.bare])
describe 'when message type attribute is invalid' do
let(:xml) { node('<message type="bogus">hello!</message>') }

stanza = Vines::Stanza::Message.new(node, @stream)
stanza.process
assert @stream.verify
assert recipient.verify
it 'raises a bad-request stanza error' do
-> { subject.process }.must_raise Vines::StanzaErrors::BadRequest
end
end

def test_message_to_non_user_is_ignored
bogus = Vines::JID.new('bogus@wonderland.lit/cake')
node = node(%Q{<message to="#{bogus}">hello!</message>})

storage = MiniTest::Mock.new
storage.expect(:find_user, nil, [bogus])
describe 'when the to address is missing' do
let(:xml) { node('<message>hello!</message>') }
let(:recipient) { MiniTest::Mock.new }

@stream.expect(:config, @config)
@stream.expect(:storage, storage, [bogus.domain])
@stream.expect(:connected_resources, [], [bogus])
before do
recipient.expect :user, alice
recipient.expect :write, nil, [xml]
stream.expect :connected_resources, [recipient], [alice.jid.bare]
end

stanza = Vines::Stanza::Message.new(node, @stream)
stanza.process
assert @stream.verify
assert storage.verify
it 'sends the message to the senders connected streams' do
subject.process
stream.verify
recipient.verify
end
end

def test_message_to_offline_user_returns_error
hatter = Vines::User.new(:jid => 'hatter@wonderland.lit/cake')
node = node(%Q{<message to="#{hatter.jid}">hello!</message>})

storage = MiniTest::Mock.new
storage.expect(:find_user, hatter, [hatter.jid])
describe 'when addressed to a non-user' do
let(:bogus) { Vines::JID.new('bogus@wonderland.lit/cake') }
let(:xml) { node(%Q{<message to="#{bogus}">hello!</message>}) }
let(:storage) { MiniTest::Mock.new }

@stream.expect(:config, @config)
@stream.expect(:storage, storage, [hatter.jid.domain])
@stream.expect(:connected_resources, [], [hatter.jid])
before do
storage.expect :find_user, nil, [bogus]
stream.expect :storage, storage, [bogus.domain]
stream.expect :connected_resources, [], [bogus]
end

stanza = Vines::Stanza::Message.new(node, @stream)
assert_raises(Vines::StanzaErrors::ServiceUnavailable) { stanza.process }
assert @stream.verify
assert storage.verify
it 'ignores the stanza' do
subject.process
stream.verify
storage.verify
end
end

def test_message_to_local_user_in_different_domain_is_delivered
alice = Vines::User.new(:jid => 'alice@wonderland.lit/tea')
romeo = Vines::User.new(:jid => 'romeo@verona.lit/balcony')
node = node(%Q{<message to="#{romeo.jid}">hello!</message>})
expected = node(%Q{<message to="#{romeo.jid}" from="#{alice.jid}">hello!</message>})

recipient = MiniTest::Mock.new
recipient.expect(:user, romeo)
recipient.expect(:write, nil, [expected])
describe 'when addressed to an offline user' do
let(:hatter) { Vines::User.new(jid: 'hatter@wonderland.lit/cake') }
let(:xml) { node(%Q{<message to="#{hatter.jid}">hello!</message>}) }
let(:storage) { MiniTest::Mock.new }

@config.host 'verona.lit' do
storage(:fs) { dir Dir.tmpdir }
before do
storage.expect :find_user, hatter, [hatter.jid]
stream.expect :storage, storage, [hatter.jid.domain]
stream.expect :connected_resources, [], [hatter.jid]
end

@stream.expect(:config, @config)
@stream.expect(:user, alice)
@stream.expect(:connected_resources, [recipient], [romeo.jid])

stanza = Vines::Stanza::Message.new(node, @stream)
stanza.process
assert @stream.verify
assert recipient.verify
it 'raises a service-unavailable stanza error' do
-> { subject.process }.must_raise Vines::StanzaErrors::ServiceUnavailable
stream.verify
storage.verify
end
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{<message to="#{romeo.jid}">hello!</message>})
expected = node(%Q{<message to="#{romeo.jid}" from="#{alice.jid}">hello!</message>})
describe 'when address to a local user in a different domain' do
let(:xml) { node(%Q{<message to="#{romeo.jid}">hello!</message>}) }
let(:expected) { node(%Q{<message to="#{romeo.jid}" from="#{alice.jid}">hello!</message>}) }
let(:recipient) { MiniTest::Mock.new }

before do
recipient.expect :user, romeo
recipient.expect :write, nil, [expected]

router = MiniTest::Mock.new
@stream.expect(:config, @config)
router.expect(:route, nil, [expected])
config.host 'verona.lit' do
storage(:fs) { dir Dir.tmpdir }
end

@stream.expect(:router, router)
@stream.expect(:user, alice)
stream.expect :connected_resources, [recipient], [romeo.jid]
end

stanza = Vines::Stanza::Message.new(node, @stream)
stanza.process
assert @stream.verify
assert router.verify
it 'delivers the stanza to the user' do
subject.process
stream.verify
recipient.verify
end
end

private
describe 'when addressed to a remote user' do
let(:xml) { node(%Q{<message to="#{romeo.jid}">hello!</message>}) }
let(:expected) { node(%Q{<message to="#{romeo.jid}" from="#{alice.jid}">hello!</message>}) }
let(:router) { MiniTest::Mock.new }

def node(xml)
Nokogiri::XML(xml).root
before do
router.expect :route, nil, [expected]
stream.expect :router, router
end

it 'routes rather than handle locally' do
subject.process
stream.verify
router.verify
end
end
end

0 comments on commit f78d0d5

Please sign in to comment.