test/unit/connection_test.rb in robut-0.3.0 vs test/unit/connection_test.rb in robut-0.4.0

- old
+ new

@@ -1,6 +1,7 @@ require 'test_helper' +require 'robut/plugin/echo' class SimplePlugin include Robut::Plugin attr_accessor :run @@ -50,79 +51,111 @@ end def room "my_room@test.com" end + + def add_message_callback(code, reply_to) + true + end + + def join(path) + true + end end +class RoomMock < Robut::Room + + def initialize(connection) + @muc = ReplyMock.new + @connection = connection + end + +end + +class PMMock < Robut::PM + + def initialize(connection) + @connection = connection + end +end + + class ConnectionTest < Test::Unit::TestCase def setup Robut::Plugin.plugins = [SimplePlugin] @connection = Robut::Connection.new({ :jid => 'abc@def.com', - :nick => "Test Robut" + :nick => "Test Robut", + :mention_name => 'test' }) end def test_end_to_end_message Robut::Plugin.plugins = [Robut::Plugin::Echo] - @connection.muc = ReplyMock.new - @connection.handle_message(plugins(@connection), Time.now, 'Justin', '@test echo Test Message') - message = @connection.muc.messages.first - assert_equal(@connection.muc.room, message.to.to_s) + justin = Jabber::JID.new('justin@example.com') + @connection.roster = mock_roster(justin) + room = RoomMock.new(@connection) + room.handle_message(plugins(room), Time.now, 'Justin Weiss', '@test echo Test Message') + message = room.muc.messages.first + assert_equal(room.muc.room, message.to.to_s) assert_equal("Test Message", message.body) end def test_handle_message_delegates_to_plugin - plugins = plugins(@connection) + presence = Robut::Presence.new(@connection) + plugins = plugins(presence) assert !plugins.first.run, "The plugin was not set up correctly." - @connection.handle_message(plugins, Time.now, 'Justin', 'Test Message') + + presence.handle_message(plugins, Time.now, 'Justin', 'Test Message') assert plugins.first.run, "The plugin's handle_message method should have been run" end def test_handle_message_from_person Robut::Plugin.plugins = [Robut::Plugin::Echo] sender = Jabber::JID.new('justin@example.com') @connection.client = ReplyMock.new - @connection.handle_message(plugins(@connection, sender), Time.now, 'Justin', '@test echo Test Message') - message = @connection.client.messages.first + pm = PMMock.new(@connection) + + pm.handle_message(plugins(pm, sender), Time.now, 'Justin', '@test echo Test Message') + message = pm.connection.client.messages.first assert_equal(sender, message.to) assert_equal("Test Message", message.body) end def test_reply_directly_to_user Robut::Plugin.plugins = [ReplyToUserPlugin] @connection.client = ReplyMock.new justin = Jabber::JID.new('justin@example.com') - @connection.roster = OpenStruct.new({ - :items => { - justin => OpenStruct.new({ - :iname => "Justin Weiss" - }) - } - }) + @connection.roster = mock_roster(justin) + pm = Robut::PM.new(@connection, justin) - @connection.handle_message(plugins(@connection), Time.now, 'justin WEISS', 'Test Message') - message = @connection.client.messages.first + pm.handle_message(plugins(pm), Time.now, 'justin WEISS', 'Test Message') + message = pm.connection.client.messages.first assert_equal(justin, message.to.to_s) assert_equal(:chat, message.type) assert_equal("Reply", message.body) end def test_reply_directly_to_room Robut::Plugin.plugins = [ReplyToRoomPlugin] sender = Jabber::JID.new('justin@example.com') - @connection.muc = ReplyMock.new - @connection.handle_message(plugins(@connection, sender), Time.now, 'Justin', '@test echo Test Message') - message = @connection.muc.messages.first - assert_equal(@connection.muc.room, message.to.to_s) + room = RoomMock.new(@connection) + + room.handle_message(plugins(room, sender), Time.now, 'Justin', '@test echo Test Message') + message = room.muc.messages.first + assert_equal(room.muc.room, message.to.to_s) assert_equal("Reply", message.body) end private - def plugins(connection, sender = nil) - Robut::Plugin.plugins.map { |p| p.new(connection, sender) } + def plugins(presence, sender = nil) + Robut::Plugin.plugins.map { |p| p.new(presence, sender) } + end + + def mock_roster(jid) + OpenStruct.new( :items => { jid => OpenStruct.new(:iname => "Justin Weiss") } ) end end