Permalink
Browse files

Convert to specs.

  • Loading branch information...
1 parent bc9026a commit 6518f3d581c699b825b22b503e9a00d59c08b527 @dgraham dgraham committed Sep 13, 2012
Showing with 141 additions and 114 deletions.
  1. +141 −114 test/stanza/pubsub/delete_test.rb
View
255 test/stanza/pubsub/delete_test.rb
@@ -1,142 +1,169 @@
# encoding: UTF-8
-require 'tmpdir'
-require 'vines'
-require 'ext/nokogiri'
-require 'minitest/autorun'
-
-class DeletePubSubTest < MiniTest::Unit::TestCase
- def setup
- @user = Vines::User.new(jid: 'alice@wonderland.lit/tea')
- @config = Vines::Config.new do
+require 'test_helper'
+
+describe Vines::Stanza::PubSub::Delete do
+ subject { Vines::Stanza::PubSub::Delete.new(xml, stream) }
+ let(:alice) { Vines::User.new(jid: 'alice@wonderland.lit/tea') }
+ let(:stream) { MiniTest::Mock.new }
+ let(:config) do
+ Vines::Config.new do
host 'wonderland.lit' do
storage(:fs) { dir Dir.tmpdir }
pubsub 'games'
end
end
- @stream = MiniTest::Mock.new
- @stream.expect(:config, @config)
- @stream.expect(:user, @user)
end
- def test_missing_to_address_raises
- node = node(%q{
- <iq type='set' id='42'>
- <pubsub xmlns='http://jabber.org/protocol/pubsub'>
- <delete node='game_13'/>
- </pubsub>
- </iq>
- }.strip.gsub(/\n|\s{2,}/, ''))
+ before do
+ class << stream
+ attr_accessor :config, :domain, :nodes, :user
+ def write(node)
+ @nodes ||= []
+ @nodes << node
+ end
+ end
+ stream.config = config
+ stream.domain = 'wonderland.lit'
+ stream.user = alice
+ end
- @stream.expect(:domain, 'wonderland.lit')
+ describe 'when missing a to address' do
+ let(:xml) do
+ node(%q{
+ <iq type='set' id='42'>
+ <pubsub xmlns='http://jabber.org/protocol/pubsub'>
+ <delete node='game_13'/>
+ </pubsub>
+ </iq>
+ })
+ end
- stanza = Vines::Stanza::PubSub::Delete.new(node, @stream)
- assert_raises(Vines::StanzaErrors::FeatureNotImplemented) { stanza.process }
- assert @stream.verify
+ it 'raises a feature-not-implemented stanza error' do
+ -> { subject.process }.must_raise Vines::StanzaErrors::FeatureNotImplemented
+ stream.verify
+ end
end
- def test_server_domain_to_address_raises
- node = node(%q{
- <iq type='set' to='wonderland.lit' id='42'>
- <pubsub xmlns='http://jabber.org/protocol/pubsub'>
- <delete node='game_13'/>
- </pubsub>
- </iq>
- }.strip.gsub(/\n|\s{2,}/, ''))
-
- stanza = Vines::Stanza::PubSub::Delete.new(node, @stream)
- assert_raises(Vines::StanzaErrors::FeatureNotImplemented) { stanza.process }
- assert @stream.verify
+ describe 'when addressed to a bare server domain jid' do
+ let(:xml) do
+ node(%q{
+ <iq type='set' to='wonderland.lit' id='42'>
+ <pubsub xmlns='http://jabber.org/protocol/pubsub'>
+ <delete node='game_13'/>
+ </pubsub>
+ </iq>
+ })
+ end
+
+ it 'raises a feature-not-implemented stanza error' do
+ -> { subject.process }.must_raise Vines::StanzaErrors::FeatureNotImplemented
+ stream.verify
+ end
end
- def test_non_pubsub_to_address_routes
- node = node(%q{
- <iq type='set' to='bogus.wonderland.lit' id='42'>
- <pubsub xmlns='http://jabber.org/protocol/pubsub'>
- <delete node='game_13'/>
- </pubsub>
- </iq>
- }.strip.gsub(/\n|\s{2,}/, ''))
-
- router = MiniTest::Mock.new
- router.expect(:route, nil, [node])
- @stream.expect(:router, router)
-
- stanza = Vines::Stanza::PubSub::Delete.new(node, @stream)
- stanza.process
- assert @stream.verify
- assert router.verify
+ describe 'when addressed to a non-pubsub address' do
+ let(:router) { MiniTest::Mock.new }
+ let(:xml) do
+ node(%q{
+ <iq type='set' to='bogus.wonderland.lit' id='42'>
+ <pubsub xmlns='http://jabber.org/protocol/pubsub'>
+ <delete node='game_13'/>
+ </pubsub>
+ </iq>
+ })
+ end
+
+ before do
+ router.expect :route, nil, [xml]
+ stream.expect :router, router
+ end
+
+ it 'routes rather than handle locally' do
+ subject.process
+ stream.verify
+ router.verify
+ end
end
- def test_multiple_delete_elements_raises
- node = node(%q{
- <iq type='set' to='games.wonderland.lit' id='42'>
- <pubsub xmlns='http://jabber.org/protocol/pubsub'>
- <delete node='game_13'/>
- <delete node='game_14'/>
- </pubsub>
- </iq>
- }.strip.gsub(/\n|\s{2,}/, ''))
-
- stanza = Vines::Stanza::PubSub::Delete.new(node, @stream)
- assert_raises(Vines::StanzaErrors::BadRequest) { stanza.process }
- assert @stream.verify
+ describe 'when stanza contains multiple delete elements' do
+ let(:xml) do
+ node(%q{
+ <iq type='set' to='games.wonderland.lit' id='42'>
+ <pubsub xmlns='http://jabber.org/protocol/pubsub'>
+ <delete node='game_13'/>
+ <delete node='game_14'/>
+ </pubsub>
+ </iq>
+ })
+ end
+
+ it 'raises a bad-request stanza error' do
+ -> { subject.process }.must_raise Vines::StanzaErrors::BadRequest
+ stream.verify
+ end
end
- def test_delete_missing_node_raises
- node = node(%q{
- <iq type='set' to='games.wonderland.lit' id='42'>
- <pubsub xmlns='http://jabber.org/protocol/pubsub'>
- <delete node='game_13'/>
- </pubsub>
- </iq>
- }.strip.gsub(/\n|\s{2,}/, ''))
-
- stanza = Vines::Stanza::PubSub::Delete.new(node, @stream)
- assert_raises(Vines::StanzaErrors::ItemNotFound) { stanza.process }
- assert @stream.verify
+ describe 'when deleting a missing node' do
+ let(:xml) do
+ node(%q{
+ <iq type='set' to='games.wonderland.lit' id='42'>
+ <pubsub xmlns='http://jabber.org/protocol/pubsub'>
+ <delete node='game_13'/>
+ </pubsub>
+ </iq>
+ })
+ end
+
+ it 'raises an item-not-found stanza error' do
+ -> { subject.process }.must_raise Vines::StanzaErrors::ItemNotFound
+ stream.verify
+ end
end
- def test_good_stanza_processes
- node = node(%q{
- <iq type='set' to='games.wonderland.lit' id='42'>
- <pubsub xmlns='http://jabber.org/protocol/pubsub'>
- <delete node='game_13'/>
- </pubsub>
- </iq>
- }.strip.gsub(/\n|\s{2,}/, ''))
-
- def @stream.nodes; @nodes; end
- def @stream.write(node)
- @nodes ||= []
- @nodes << node
- end
-
- stanza = Vines::Stanza::PubSub::Delete.new(node, @stream)
- def stanza.mock_pubsub; @mock_pubsub; end
- def stanza.pubsub
- unless @mock_pubsub
- xml = %q{<message><event xmlns="http://jabber.org/protocol/pubsub#event"><delete node="game_13"/></event></message>}
- @mock_pubsub = MiniTest::Mock.new
- @mock_pubsub.expect(:node?, true, ['game_13'])
- @mock_pubsub.expect(:publish, nil, ['game_13', Nokogiri::XML(xml).root])
- @mock_pubsub.expect(:delete_node, nil, ['game_13'])
- end
- @mock_pubsub
+ describe 'when valid stanza is received' do
+ let(:pubsub) { MiniTest::Mock.new }
+ let(:xml) do
+ node(%q{
+ <iq type='set' to='games.wonderland.lit' id='42'>
+ <pubsub xmlns='http://jabber.org/protocol/pubsub'>
+ <delete node='game_13'/>
+ </pubsub>
+ </iq>
+ })
end
- stanza.process
- assert @stream.verify
- assert stanza.mock_pubsub.verify
- assert_equal 1, @stream.nodes.size
+ let(:result) { node(%Q{<iq from="games.wonderland.lit" id="42" to="#{alice.jid}" type="result"/>}) }
- expected = node(%q{<iq from="games.wonderland.lit" id="42" to="alice@wonderland.lit/tea" type="result"/>})
- assert_equal expected, @stream.nodes[0]
- end
+ let(:broadcast) do
+ node(%q{
+ <message>
+ <event xmlns="http://jabber.org/protocol/pubsub#event">
+ <delete node="game_13"/>
+ </event>
+ </message>})
+ end
- private
+ before do
+ pubsub.expect :node?, true, ['game_13']
+ pubsub.expect :publish, nil, ['game_13', broadcast]
+ pubsub.expect :delete_node, nil, ['game_13']
+ end
- def node(xml)
- Nokogiri::XML(xml).root
+ it 'broadcasts the delete to subscribers' do
+ subject.stub :pubsub, pubsub do
+ subject.process
+ end
+ stream.verify
+ pubsub.verify
+ end
+
+ it 'sends a result stanza to sender' do
+ subject.stub :pubsub, pubsub do
+ subject.process
+ end
+ stream.nodes.size.must_equal 1
+ stream.nodes.first.must_equal result
+ end
end
end

0 comments on commit 6518f3d

Please sign in to comment.