/
outbound_call_spec.rb
392 lines (317 loc) · 12.5 KB
/
outbound_call_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# encoding: utf-8
require 'spec_helper'
module Adhearsion
describe OutboundCall do
it { is_expected.to be_a Call }
describe '#id' do
subject { super().id }
it { is_expected.to be_nil }
end
describe '#variables' do
subject { super().variables }
it { is_expected.to eq({}) }
end
let(:mock_client) { double 'Rayo Client', execute_command: true, new_call_uri: call_uri }
before do
Adhearsion::Rayo::Initializer.client = mock_client
Adhearsion.active_calls.clear
end
describe '#client' do
subject { super().client }
it { is_expected.to be mock_client }
end
describe '#start_time' do
subject { super().start_time }
it { is_expected.to be nil }
end
let(:transport) { 'xmpp' }
let(:call_id) { SecureRandom.uuid }
let(:domain) { 'rayo.net' }
let(:call_uri) { "xmpp:#{call_id}@rayo.net" }
let(:to) { '+1800 555-0199' }
let(:from) { '+1800 555-0122' }
it "should allow timers to be registered from outside" do
foo = :bar
subject.after(1) { foo = :baz }
sleep 1.1
expect(foo).to eq(:baz)
end
describe ".originate" do
let(:to) { 'sip:foo@bar.com' }
let(:mock_call) { OutboundCall.new }
before do
mock_call
expect(OutboundCall).to receive(:new).and_return mock_call
end
it "should dial the call to the correct endpoint and return it" do
expect(mock_call.wrapped_object).to receive(:dial).with(to, :from => 'foo').once
expect(OutboundCall.originate(to, :from => 'foo')).to be mock_call
end
it "should run through the router when the call is answered" do
expect(mock_call.wrapped_object).to receive(:dial).once
expect(Adhearsion.router).to receive(:handle).once.with(mock_call)
OutboundCall.originate(to) << Adhearsion::Event::Answered.new
end
context "when a controller class is specified for the call" do
let(:controller) { CallController }
it "should execute the controller on the call when it is answered" do
expect(mock_call).to receive(:dial).once.with(to, {})
expect(mock_call).to receive(:execute_controller).once.with kind_of(controller), kind_of(Proc)
call = OutboundCall.originate to, :controller => controller
call << Adhearsion::Event::Answered.new
end
it "should hangup the call after all controllers have executed" do
expect(mock_call).to receive(:dial).once
expect(mock_call).to receive(:hangup).once
call = OutboundCall.originate to, :controller => controller
call << Adhearsion::Event::Answered.new
sleep 0.5
end
context "with controller metadata specified" do
it "should set the metadata on the controller" do
expect(mock_call).to receive(:dial).once.with(to, {})
expected_controller = controller.new mock_call, foo: 'bar'
expect(mock_call).to receive(:execute_controller).with(expected_controller, kind_of(Proc)).once
call = OutboundCall.originate to, :controller => controller, :controller_metadata => {:foo => 'bar'}
call << Adhearsion::Event::Answered.new
end
end
end
context "when given a block" do
it "should execute the block as a controller on the call when it is answered" do
expect(mock_call).to receive(:dial).once.with(to, {})
expect(mock_call).to receive(:execute_controller).once.with(kind_of(CallController), kind_of(Proc)) do |controller|
expect(controller.block.call).to eq(:foobar)
end
call = OutboundCall.originate to do
:foobar
end
call << Adhearsion::Event::Answered.new
end
end
context "when the dial fails" do
before do
expect(subject.wrapped_object).to receive(:write_command)
expect_any_instance_of(Adhearsion::Rayo::Command::Dial).to receive(:response).and_return StandardError.new("User not registered")
end
after do
Adhearsion.active_calls.restart_supervisor
end
it "should raise the exception in the caller" do
expect { subject.dial to }.to raise_error("User not registered")
end
it "should kill the actor" do
expect { subject.dial to }.to raise_error("User not registered")
sleep 0.1
expect(subject.alive?).to be false
end
end
end
describe "event handlers" do
let(:response) { double 'Response' }
describe "for answered events" do
let(:event) { Adhearsion::Event::Answered.new }
it "should trigger any on_answer callbacks set" do
expect(response).to receive(:call).once.with(event)
subject.on_answer { |event| response.call event }
subject << event
end
it "should record the call start time" do
originate_time = Time.local(2008, 9, 1, 12, 0, 0)
Timecop.freeze originate_time
expect(subject.duration).to eq(0.0)
mid_point_time = Time.local(2008, 9, 1, 12, 0, 20)
Timecop.freeze mid_point_time
expect(subject.duration).to eq(0.0)
answer_time = Time.local(2008, 9, 1, 12, 0, 40)
Timecop.freeze answer_time
subject << event
expect(subject.start_time).to eq(answer_time)
later_time = Time.local(2008, 9, 1, 12, 0, 50)
Timecop.freeze later_time
expect(subject.duration).to eq(10.0)
end
end
end
describe "#dial" do
def expect_message_waiting_for_response(message, uri = call_uri)
expect(subject.wrapped_object).to receive(:write_and_await_response).once.with(message, 60, true) do |real_message|
real_message.request!
real_message.response = Adhearsion::Rayo::Ref.new(uri: uri)
real_message
end
end
let(:expected_dial_command) { Adhearsion::Rayo::Command::Dial.new(:to => to, :from => from, :uri => call_uri) }
context "while waiting for a response" do
before do
expect(mock_client).to receive(:execute_command).once.with(expected_dial_command).and_return true
subject.async.dial to, from: from
sleep 1
end
it "should set the dial command" do
expect(subject.dial_command).to eq(expected_dial_command)
end
it "should know its requested URI" do
expect(subject.uri).to eq(call_uri)
end
it "should know its requested ID" do
expect(subject.id).to eq(call_id)
end
it "should know its domain" do
expect(subject.domain).to eq(domain)
end
it "should be entered in the active calls registry" do
expect(Adhearsion.active_calls[call_id]).to be subject
end
end
context "with a successful response" do
let(:returned_uri) { call_uri }
before do
expect_message_waiting_for_response expected_dial_command, returned_uri
end
it "should send a dial stanza, wait for the response" do
subject.dial to, :from => from
end
it "should set the dial command" do
subject.dial to, :from => from
expect(subject.dial_command).to eq(Adhearsion::Rayo::Command::Dial.new(:to => to, :from => from, :uri => call_uri, target_call_id: call_id, domain: domain, transport: transport))
end
it "should set the URI from the reference" do
subject.dial to, :from => from
expect(subject.uri).to eq(call_uri)
end
it "should set the call ID from the reference" do
subject.dial to, :from => from
expect(subject.id).to eq(call_id)
end
it "should set the call domain from the reference" do
subject.dial to, :from => from
expect(subject.domain).to eq(domain)
end
it "should set the to from the dial command" do
subject.dial to, :from => from
expect(subject.to).to eq(to)
end
it "should set the 'from' from the dial command" do
subject.dial to, :from => from
expect(subject.from).to eq(from)
end
it "should add the call to the active calls registry" do
subject.dial to, :from => from
expect(Adhearsion.active_calls[call_id]).to be subject
end
context "when a different ref is returned than the one expected" do
let(:returned_uri) { 'xmpp:otherid@wonderland.lit' }
before do
subject.dial to, :from => from
end
it "should set the URI from the reference" do
expect(subject.uri).to eq(returned_uri)
end
it "should set the call ID from the reference" do
expect(subject.id).to eq('otherid')
end
it "should set the call domain from the reference" do
expect(subject.domain).to eq('wonderland.lit')
end
it "should make the call addressible in the active calls registry by the new ID" do
expect(Adhearsion.active_calls[call_id]).to be_nil
expect(Adhearsion.active_calls['otherid']).to be subject
end
end
it "should immediately fire the :call_dialed event giving the call" do
expect(Adhearsion::Events).to receive(:trigger).once.with(:call_dialed, subject)
subject.dial to, :from => from
end
it "should not modify the provided options" do
options = {:from => from}
original_options = Marshal.load(Marshal.dump(options))
expect(options).to eq(original_options)
subject.dial to, options
expect(options).to eq(original_options)
end
end
context "when the dial fails" do
before do
expect(subject.wrapped_object).to receive(:write_command)
expect_any_instance_of(Adhearsion::Rayo::Command::Dial).to receive(:response).and_return StandardError.new("User not registered")
end
after do
Adhearsion.active_calls.restart_supervisor
end
it "should raise the exception in the caller" do
expect { subject.dial to }.to raise_error("User not registered")
end
it "should kill the actor" do
expect { subject.dial to }.to raise_error("User not registered")
sleep 0.1
expect(subject.alive?).to be false
end
it "should remove the call from the active calls hash" do
expect { subject.dial to }.to raise_error("User not registered")
sleep 0.1
expect(Adhearsion.active_calls[call_id]).to be_nil
end
end
end
describe "basic control commands" do
def expect_no_message_waiting_for_response
expect(subject.wrapped_object).to receive(:write_and_await_response).never
end
describe '#accept' do
describe "with no headers" do
it 'should not send any message' do
expect_no_message_waiting_for_response
subject.accept
end
end
describe "with headers set" do
it 'should not send any message' do
expect_no_message_waiting_for_response
subject.accept :foo => 'bar'
end
end
end
describe '#answer' do
describe "with no headers" do
it 'should not send any message' do
expect_no_message_waiting_for_response
subject.answer
end
end
describe "with headers set" do
it 'should not send any message' do
expect_no_message_waiting_for_response
subject.answer :foo => 'bar'
end
end
end
describe '#reject' do
describe "with a reason given" do
it 'should not send any message' do
expect_no_message_waiting_for_response
subject.reject :decline
end
end
describe "with no reason given" do
it 'should not send any message' do
expect_no_message_waiting_for_response
subject.reject
end
end
describe "with no headers" do
it 'should not send any message' do
expect_no_message_waiting_for_response
subject.reject
end
end
describe "with headers set" do
it 'should not send any message' do
expect_no_message_waiting_for_response
subject.reject nil, :foo => 'bar'
end
end
end
end
end
end