Guest User

Untitled

a guest
Mar 9th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.85 KB | None | 0 0
  1. Index: spec/voip/asterisk/mock_ami_server.rb
  2. ===================================================================
  3. --- spec/voip/asterisk/mock_ami_server.rb (revision 589)
  4. +++ spec/voip/asterisk/mock_ami_server.rb (working copy)
  5. @@ -1,4 +1,124 @@
  6. -class AmiServer
  7. +class MockAmiServer
  8. +
  9. + class << self
  10. +
  11. + def unresponsive_server
  12. + new {}
  13. + end
  14. +
  15. + def simple_server_with_successful_login
  16. + new do |dialog|
  17. + dialog.login_successfully
  18. + end
  19. + end
  20. +
  21. +
  22. + end
  23. +
  24. + module CapturedResponses
  25. + class << self
  26. +
  27. + def login_successful
  28. + { "Response" => "Error", "Message" => "Authentication failed" }
  29. + end
  30. +
  31. + def good_login_response
  32. + { "Response" => "Success", "Message" => "Authentication accepted" }
  33. + end
  34. +
  35. + def follows_response
  36. + { "Response" => "Follows", "Privilege" => "Command" }
  37. + end
  38. +
  39. + def bad_dbget_response
  40. + { "Response" => "Error", "Message" => "Database entry not found" }
  41. + end
  42. +
  43. + def good_dbget_response
  44. + { "Response" => "Success", "Message" => "Result will follow" }
  45. + end
  46. +
  47. + def dbput_response
  48. + { "Response" => "Success", "Message" => "Updated database successfully" }
  49. + end
  50. +
  51. + def dbget_event_response
  52. + { "Event" => "DBGetResponse" }
  53. + end
  54. +
  55. + def queue_status_response
  56. + { "Response" => "Success", "Message" => "Queue status will follow" }
  57. + end
  58. +
  59. + def queue_status_event_complete_response
  60. + { "Event" => "QueueStatusComplete" }
  61. + end
  62. +
  63. + def queue_status_event_params_response
  64. + { "Event" => "QueueParams", "Queue" => "default", "Max" => 0, "Min" => 0, "Holdtime" => 0,
  65. + "Completed" => 0, "Abandoned" => 0, "ServiceLevel" => 0, "ServiceLevelPerf" => 0 }
  66. + end
  67. +
  68. + def events_on_response
  69. + { "Response" => "Events On" }
  70. + end
  71. +
  72. + def events_off_response
  73. + { "Response" => "Events Off" }
  74. + end
  75. + end
  76. + end
  77. +
  78. + class DialogCatcher
  79. + attr_reader :instructions
  80. + def initialize
  81. + @instructions = []
  82. + end
  83. + def method_missing(*args)
  84. + instructions << args
  85. + end
  86. + end
  87. +
  88. + attr_reader :io, :dialog
  89. + def initialize
  90. + yield caught_dialog = DialogCatcher.new
  91. + @dialog = caught_dialog.instructions
  92. + canned_response_buffer = ""
  93. + @dialog.each do |instruction|
  94. + canned_response_buffer << CapturedResponses.send(*instruction)
  95. + end
  96. + @io = StringIO.new canned_response_buffer
  97. + end
  98. +
  99. + def extend_dialog
  100. + yield caught_dialog = DialogCatcher
  101. + dialog.concat caught_dialog.instructions
  102. + end
  103. +
  104. + def closed?
  105. +
  106. + end
  107. +
  108. + def close
  109. +
  110. + end
  111. +
  112. + def read_nonblock(number_of_bytes_to_read)
  113. + raise Errno::EAGAIN if io.string.empty?
  114. + @io.read number_of_bytes_to_read
  115. + end
  116. +
  117. + def gets
  118. +
  119. + end
  120. +
  121. + def write(data)
  122. +
  123. + end
  124. +
  125. +end
  126. +
  127. +class OldMockAmiServer
  128. class AMIResponseHelper
  129. class << self
  130. def bad_login_response
  131. @@ -52,10 +172,15 @@
  132. end
  133. end
  134.  
  135. + attr_reader :prompt, :buffer, :db, :username, :password
  136. +
  137. def initialize(*args)
  138. - @closed = false
  139. - @buffer = ""
  140. - @db = {}
  141. + @prompt = "Asterisk Call Manager/1.0"
  142. + @username = "admin"
  143. + @password = "password"
  144. + @closed = false
  145. + @buffer = ""
  146. + @db = {}
  147. extend MonitorMixin
  148. fill(prompt, 1)
  149. end
  150. @@ -70,7 +195,7 @@
  151.  
  152. def read_nonblock(count)
  153. str = @buffer.slice!(0..count-1)
  154. - raise Errno::EAGAIN if str == ""
  155. + raise Errno::EAGAIN if str.empty?
  156. str
  157. end
  158.  
  159. @@ -150,8 +275,4 @@
  160. @buffer += line
  161. @buffer += "\r\n" * blanks
  162. end
  163. -
  164. - def prompt; "Asterisk Call Manager/1.0" end
  165. - def username; "admin" end
  166. - def password; "password" end
  167. end
  168. Index: spec/voip/asterisk/test_ami.rb
  169. ===================================================================
  170. --- spec/voip/asterisk/test_ami.rb (revision 589)
  171. +++ spec/voip/asterisk/test_ami.rb (working copy)
  172. @@ -5,23 +5,23 @@
  173. require 'adhearsion/voip/asterisk/ami'
  174.  
  175. context "Connecting via AMI" do
  176. +
  177. + include AmiTestHelper
  178. +
  179. test "should raise an exception if the password was invalid" do
  180. - host, port = "localhost", 5038
  181. - ami = Adhearsion::VoIP::Asterisk::AMI.new "admin", "bad_password", "localhost", :port => port
  182. + ami = Adhearsion::VoIP::Asterisk::AMI.new "admin", "bad_password", "localhost"
  183.  
  184. - flexmock(TCPSocket).should_receive(:new).once.with(host, port).and_return AmiServer.new
  185. + stub_tcp_socket_creation_with_mock_agi_server
  186. the_following_code do
  187. ami.connect!
  188. end.should.raise Adhearsion::VoIP::Asterisk::AMI::AuthenticationFailedException
  189. ami.disconnect!
  190. end
  191.  
  192. - test "should discover its own permissions and make them available as connection attributes"
  193. test "should start a new thread if events are enabled" do
  194. - host, port = "localhost", 5038
  195. - ami = Adhearsion::VoIP::Asterisk::AMI.new "admin", "password", "localhost", :port => port, :events => true
  196. + ami = Adhearsion::VoIP::Asterisk::AMI.new "admin", "password", "localhost", :events => true
  197.  
  198. - flexmock(TCPSocket).should_receive(:new).once.with(host, port).and_return(AmiServer.new)
  199. + stub_tcp_socket_creation_with_mock_agi_server
  200. ami.connect!
  201. ami.instance_eval { meta_eval { attr_accessor :event_thread } }
  202. ami.event_thread.should.be.a.kind_of Thread
  203. @@ -29,26 +29,38 @@
  204. end
  205.  
  206. test "should find the Asterisk version when connecting" do
  207. - host, port = "localhost", 5038
  208. - ami = Adhearsion::VoIP::Asterisk::AMI.new "admin", "password", "localhost", :port => port
  209. + ami = new_default_ami_instance
  210.  
  211. - flexmock(TCPSocket).should_receive(:new).once.with(host, port).and_return AmiServer.new
  212. + stub_tcp_socket_creation_with_mock_agi_server
  213. ami.connect!
  214. - ami.version.should == "1.0"
  215. + ami.version.should == 1.0
  216. ami.disconnect!
  217. end
  218. end
  219.  
  220. context "The AMI command interface" do
  221. - before do
  222. - host, port = "localhost", 5038
  223. - @ami = Adhearsion::VoIP::Asterisk::AMI.new "admin", "password", "localhost", :port => port, :events => false
  224. - flexmock(TCPSocket).should_receive(:new).once.with(host, port).and_return(AmiServer.new)
  225. +
  226. + include AmiTestHelper
  227. +
  228. + attr_reader :ami, :host, :port, :username, :password, :events
  229. +
  230. + before :all do
  231. + @host, @port, @username, @password, @events = "localhost", 5038, "admin", "password", false
  232. + end
  233. +
  234. + before :each do
  235. + @ami = Adhearsion::VoIP::Asterisk::AMI.new :username => @username,
  236. + :port => @port,
  237. + :username => @username,
  238. + :password => @password,
  239. + :events => @events
  240. +
  241. + stub_tcp_socket_creation_with_mock_agi_server
  242. @ami.connect!
  243. end
  244.  
  245. after do
  246. - @ami.disconnect!
  247. + ami.disconnect!
  248. end
  249.  
  250. test "should respond to an immediate command" do
  251. @@ -114,3 +126,29 @@
  252. test "should allow a Hash to specify multiple matches"
  253.  
  254. end
  255. +
  256. +BEGIN {
  257. +module AmiTestHelper
  258. +
  259. + def new_default_ami_instance
  260. + Adhearsion::VoIP::Asterisk::AMI.new "admin", "password", "localhost"
  261. + end
  262. +
  263. + def stub_tcp_socket_creation_with_mock_agi_server
  264. + flexmock(TCPSocket).should_receive(:new).once.with.any_args.and_return new_mock_ami_server
  265. + end
  266. +
  267. + def new_mock_ami_server
  268. + MockAmiServer.new
  269. + end
  270. +
  271. + def default_ami_connection_options
  272. + { :username => "admin",
  273. + :password => "password",
  274. + :host => "localhost",
  275. + :port => 5038,
  276. + :events => false }
  277. + end
  278. +
  279. +end
  280. +}
  281. \ No newline at end of file
  282. Index: lib/adhearsion/initializer/asterisk.rb
  283. ===================================================================
  284. --- lib/adhearsion/initializer/asterisk.rb (revision 589)
  285. +++ lib/adhearsion/initializer/asterisk.rb (working copy)
  286. @@ -8,8 +8,8 @@
  287.  
  288. def start
  289. @@config = Adhearsion::AHN_CONFIG.asterisk
  290. - @@agi_server = Adhearsion::VoIP::Asterisk::AGI::Server.new :host => @@config.listening_host,
  291. - :port => @@config.listening_port
  292. + @@agi_server = initialize_agi
  293. + @@ami_server = initialize_ami if @@config.ami_enabled?
  294. if @@config.ami_enabled?
  295. require 'adhearsion/voip/asterisk/ami'
  296. @@ami = Adhearsion::VoIP::Asterisk::AMI.new @@config.ami.username,
  297. @@ -31,6 +31,26 @@
  298.  
  299. private
  300.  
  301. + def initialize_agi
  302. + Adhearsion::VoIP::Asterisk::AGI::Server.new :host => @@config.listening_host,
  303. + :port => @@config.listening_port
  304. + end
  305. +
  306. + def initialize(user, pass, host='127.0.0.1', hash={})
  307. +
  308. + def initialize_ami
  309. + options = ami_options
  310. + Adhearsion::VoIP::Asterisk::AMI.new options[:username], options[:password],
  311. + options[:host], :port => options[:port]
  312. + end
  313. +
  314. + def ami_options
  315. + %w(host port username password events).inject({}) do |options, property|
  316. + options[property.to_sym] = @@config.ami.send property
  317. + options
  318. + end
  319. + end
  320. +
  321. def join_server_thread_after_initialized
  322. Adhearsion::Hooks::AfterInitialized.create_hook do
  323. @@agi_server.start
  324. Index: lib/adhearsion/initializer/configuration.rb
  325. ===================================================================
  326. --- lib/adhearsion/initializer/configuration.rb (revision 589)
  327. +++ lib/adhearsion/initializer/configuration.rb (working copy)
  328. @@ -90,6 +90,7 @@
  329.  
  330.  
  331. class AMIConfiguration < AbstractConfiguration
  332. + attr_accessor :host
  333. attr_accessor :port
  334. attr_accessor :username
  335. attr_accessor :password
  336. Index: app_generators/ahn/templates/config/startup.rb
  337. ===================================================================
  338. --- app_generators/ahn/templates/config/startup.rb (revision 589)
  339. +++ app_generators/ahn/templates/config/startup.rb (working copy)
  340. @@ -10,8 +10,10 @@
  341.  
  342. # By default Asterisk is enabled with the default settings
  343. config.enable_asterisk
  344. + config.asterisk.enable_ami :host => "172.16.83.129",
  345. + :username => "admin",
  346. + :password => "password"
  347.  
  348. - #
  349. # config.asterisk.speech_engine = :cepstral
  350.  
  351. # Configure FreeSwitch
Add Comment
Please, Sign In to add comment