Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index: spec/voip/asterisk/mock_ami_server.rb
- ===================================================================
- --- spec/voip/asterisk/mock_ami_server.rb (revision 589)
- +++ spec/voip/asterisk/mock_ami_server.rb (working copy)
- @@ -1,4 +1,124 @@
- -class AmiServer
- +class MockAmiServer
- +
- + class << self
- +
- + def unresponsive_server
- + new {}
- + end
- +
- + def simple_server_with_successful_login
- + new do |dialog|
- + dialog.login_successfully
- + end
- + end
- +
- +
- + end
- +
- + module CapturedResponses
- + class << self
- +
- + def login_successful
- + { "Response" => "Error", "Message" => "Authentication failed" }
- + end
- +
- + def good_login_response
- + { "Response" => "Success", "Message" => "Authentication accepted" }
- + end
- +
- + def follows_response
- + { "Response" => "Follows", "Privilege" => "Command" }
- + end
- +
- + def bad_dbget_response
- + { "Response" => "Error", "Message" => "Database entry not found" }
- + end
- +
- + def good_dbget_response
- + { "Response" => "Success", "Message" => "Result will follow" }
- + end
- +
- + def dbput_response
- + { "Response" => "Success", "Message" => "Updated database successfully" }
- + end
- +
- + def dbget_event_response
- + { "Event" => "DBGetResponse" }
- + end
- +
- + def queue_status_response
- + { "Response" => "Success", "Message" => "Queue status will follow" }
- + end
- +
- + def queue_status_event_complete_response
- + { "Event" => "QueueStatusComplete" }
- + end
- +
- + def queue_status_event_params_response
- + { "Event" => "QueueParams", "Queue" => "default", "Max" => 0, "Min" => 0, "Holdtime" => 0,
- + "Completed" => 0, "Abandoned" => 0, "ServiceLevel" => 0, "ServiceLevelPerf" => 0 }
- + end
- +
- + def events_on_response
- + { "Response" => "Events On" }
- + end
- +
- + def events_off_response
- + { "Response" => "Events Off" }
- + end
- + end
- + end
- +
- + class DialogCatcher
- + attr_reader :instructions
- + def initialize
- + @instructions = []
- + end
- + def method_missing(*args)
- + instructions << args
- + end
- + end
- +
- + attr_reader :io, :dialog
- + def initialize
- + yield caught_dialog = DialogCatcher.new
- + @dialog = caught_dialog.instructions
- + canned_response_buffer = ""
- + @dialog.each do |instruction|
- + canned_response_buffer << CapturedResponses.send(*instruction)
- + end
- + @io = StringIO.new canned_response_buffer
- + end
- +
- + def extend_dialog
- + yield caught_dialog = DialogCatcher
- + dialog.concat caught_dialog.instructions
- + end
- +
- + def closed?
- +
- + end
- +
- + def close
- +
- + end
- +
- + def read_nonblock(number_of_bytes_to_read)
- + raise Errno::EAGAIN if io.string.empty?
- + @io.read number_of_bytes_to_read
- + end
- +
- + def gets
- +
- + end
- +
- + def write(data)
- +
- + end
- +
- +end
- +
- +class OldMockAmiServer
- class AMIResponseHelper
- class << self
- def bad_login_response
- @@ -52,10 +172,15 @@
- end
- end
- + attr_reader :prompt, :buffer, :db, :username, :password
- +
- def initialize(*args)
- - @closed = false
- - @buffer = ""
- - @db = {}
- + @prompt = "Asterisk Call Manager/1.0"
- + @username = "admin"
- + @password = "password"
- + @closed = false
- + @buffer = ""
- + @db = {}
- extend MonitorMixin
- fill(prompt, 1)
- end
- @@ -70,7 +195,7 @@
- def read_nonblock(count)
- str = @buffer.slice!(0..count-1)
- - raise Errno::EAGAIN if str == ""
- + raise Errno::EAGAIN if str.empty?
- str
- end
- @@ -150,8 +275,4 @@
- @buffer += line
- @buffer += "\r\n" * blanks
- end
- -
- - def prompt; "Asterisk Call Manager/1.0" end
- - def username; "admin" end
- - def password; "password" end
- end
- Index: spec/voip/asterisk/test_ami.rb
- ===================================================================
- --- spec/voip/asterisk/test_ami.rb (revision 589)
- +++ spec/voip/asterisk/test_ami.rb (working copy)
- @@ -5,23 +5,23 @@
- require 'adhearsion/voip/asterisk/ami'
- context "Connecting via AMI" do
- +
- + include AmiTestHelper
- +
- test "should raise an exception if the password was invalid" do
- - host, port = "localhost", 5038
- - ami = Adhearsion::VoIP::Asterisk::AMI.new "admin", "bad_password", "localhost", :port => port
- + ami = Adhearsion::VoIP::Asterisk::AMI.new "admin", "bad_password", "localhost"
- - flexmock(TCPSocket).should_receive(:new).once.with(host, port).and_return AmiServer.new
- + stub_tcp_socket_creation_with_mock_agi_server
- the_following_code do
- ami.connect!
- end.should.raise Adhearsion::VoIP::Asterisk::AMI::AuthenticationFailedException
- ami.disconnect!
- end
- - test "should discover its own permissions and make them available as connection attributes"
- test "should start a new thread if events are enabled" do
- - host, port = "localhost", 5038
- - ami = Adhearsion::VoIP::Asterisk::AMI.new "admin", "password", "localhost", :port => port, :events => true
- + ami = Adhearsion::VoIP::Asterisk::AMI.new "admin", "password", "localhost", :events => true
- - flexmock(TCPSocket).should_receive(:new).once.with(host, port).and_return(AmiServer.new)
- + stub_tcp_socket_creation_with_mock_agi_server
- ami.connect!
- ami.instance_eval { meta_eval { attr_accessor :event_thread } }
- ami.event_thread.should.be.a.kind_of Thread
- @@ -29,26 +29,38 @@
- end
- test "should find the Asterisk version when connecting" do
- - host, port = "localhost", 5038
- - ami = Adhearsion::VoIP::Asterisk::AMI.new "admin", "password", "localhost", :port => port
- + ami = new_default_ami_instance
- - flexmock(TCPSocket).should_receive(:new).once.with(host, port).and_return AmiServer.new
- + stub_tcp_socket_creation_with_mock_agi_server
- ami.connect!
- - ami.version.should == "1.0"
- + ami.version.should == 1.0
- ami.disconnect!
- end
- end
- context "The AMI command interface" do
- - before do
- - host, port = "localhost", 5038
- - @ami = Adhearsion::VoIP::Asterisk::AMI.new "admin", "password", "localhost", :port => port, :events => false
- - flexmock(TCPSocket).should_receive(:new).once.with(host, port).and_return(AmiServer.new)
- +
- + include AmiTestHelper
- +
- + attr_reader :ami, :host, :port, :username, :password, :events
- +
- + before :all do
- + @host, @port, @username, @password, @events = "localhost", 5038, "admin", "password", false
- + end
- +
- + before :each do
- + @ami = Adhearsion::VoIP::Asterisk::AMI.new :username => @username,
- + :port => @port,
- + :username => @username,
- + :password => @password,
- + :events => @events
- +
- + stub_tcp_socket_creation_with_mock_agi_server
- @ami.connect!
- end
- after do
- - @ami.disconnect!
- + ami.disconnect!
- end
- test "should respond to an immediate command" do
- @@ -114,3 +126,29 @@
- test "should allow a Hash to specify multiple matches"
- end
- +
- +BEGIN {
- +module AmiTestHelper
- +
- + def new_default_ami_instance
- + Adhearsion::VoIP::Asterisk::AMI.new "admin", "password", "localhost"
- + end
- +
- + def stub_tcp_socket_creation_with_mock_agi_server
- + flexmock(TCPSocket).should_receive(:new).once.with.any_args.and_return new_mock_ami_server
- + end
- +
- + def new_mock_ami_server
- + MockAmiServer.new
- + end
- +
- + def default_ami_connection_options
- + { :username => "admin",
- + :password => "password",
- + :host => "localhost",
- + :port => 5038,
- + :events => false }
- + end
- +
- +end
- +}
- \ No newline at end of file
- Index: lib/adhearsion/initializer/asterisk.rb
- ===================================================================
- --- lib/adhearsion/initializer/asterisk.rb (revision 589)
- +++ lib/adhearsion/initializer/asterisk.rb (working copy)
- @@ -8,8 +8,8 @@
- def start
- @@config = Adhearsion::AHN_CONFIG.asterisk
- - @@agi_server = Adhearsion::VoIP::Asterisk::AGI::Server.new :host => @@config.listening_host,
- - :port => @@config.listening_port
- + @@agi_server = initialize_agi
- + @@ami_server = initialize_ami if @@config.ami_enabled?
- if @@config.ami_enabled?
- require 'adhearsion/voip/asterisk/ami'
- @@ami = Adhearsion::VoIP::Asterisk::AMI.new @@config.ami.username,
- @@ -31,6 +31,26 @@
- private
- + def initialize_agi
- + Adhearsion::VoIP::Asterisk::AGI::Server.new :host => @@config.listening_host,
- + :port => @@config.listening_port
- + end
- +
- + def initialize(user, pass, host='127.0.0.1', hash={})
- +
- + def initialize_ami
- + options = ami_options
- + Adhearsion::VoIP::Asterisk::AMI.new options[:username], options[:password],
- + options[:host], :port => options[:port]
- + end
- +
- + def ami_options
- + %w(host port username password events).inject({}) do |options, property|
- + options[property.to_sym] = @@config.ami.send property
- + options
- + end
- + end
- +
- def join_server_thread_after_initialized
- Adhearsion::Hooks::AfterInitialized.create_hook do
- @@agi_server.start
- Index: lib/adhearsion/initializer/configuration.rb
- ===================================================================
- --- lib/adhearsion/initializer/configuration.rb (revision 589)
- +++ lib/adhearsion/initializer/configuration.rb (working copy)
- @@ -90,6 +90,7 @@
- class AMIConfiguration < AbstractConfiguration
- + attr_accessor :host
- attr_accessor :port
- attr_accessor :username
- attr_accessor :password
- Index: app_generators/ahn/templates/config/startup.rb
- ===================================================================
- --- app_generators/ahn/templates/config/startup.rb (revision 589)
- +++ app_generators/ahn/templates/config/startup.rb (working copy)
- @@ -10,8 +10,10 @@
- # By default Asterisk is enabled with the default settings
- config.enable_asterisk
- + config.asterisk.enable_ami :host => "172.16.83.129",
- + :username => "admin",
- + :password => "password"
- - #
- # config.asterisk.speech_engine = :cepstral
- # Configure FreeSwitch
Add Comment
Please, Sign In to add comment