Refactoring: Migrate Cti::Log test cases in cti_caller_id_test.rb

This commit is contained in:
Ryan Lue 2019-02-04 23:42:18 +08:00 committed by Martin Edenhofer
parent 2dd4c74be4
commit c5afbaba00
2 changed files with 46 additions and 213 deletions

View file

@ -30,7 +30,7 @@ RSpec.describe Cti::Log do
describe '.process' do describe '.process' do
let(:attributes) do let(:attributes) do
{ {
'cause' => '', 'cause' => cause,
'event' => event, 'event' => event,
'user' => 'user 1', 'user' => 'user 1',
'from' => '49123456', 'from' => '49123456',
@ -40,6 +40,8 @@ RSpec.describe Cti::Log do
} }
end end
let(:cause) { '' }
context 'for event "newCall"' do context 'for event "newCall"' do
let(:event) { 'newCall' } let(:event) { 'newCall' }
@ -96,6 +98,16 @@ RSpec.describe Cti::Log do
end end
context 'with recognized "call_id"' do context 'with recognized "call_id"' do
context 'for Log with #state "newCall"' do
let(:log) { create(:'cti/log', call_id: 1, state: 'newCall', done: false) }
it 'returns early with no changes' do
expect { Cti::Log.process(attributes) }
.to change { log.reload.state }.to('answer')
.and change { log.reload.done }.to(true)
end
end
context 'for Log with #state "hangup"' do context 'for Log with #state "hangup"' do
let(:log) { create(:'cti/log', call_id: 1, state: 'hangup', done: false) } let(:log) { create(:'cti/log', call_id: 1, state: 'hangup', done: false) }
@ -118,11 +130,42 @@ RSpec.describe Cti::Log do
context 'with recognized "call_id"' do context 'with recognized "call_id"' do
context 'for Log with #state "newCall"' do context 'for Log with #state "newCall"' do
let(:log) { create(:'cti/log', call_id: 1, done: true) } let(:log) { create(:'cti/log', call_id: 1, state: 'newCall', done: false) }
it 'sets attributes #state: "hangup", #done: false' do it 'sets attributes #state: "hangup", #done: false' do
expect { Cti::Log.process(attributes) } expect { Cti::Log.process(attributes) }
.to change { log.reload.state }.to('hangup').and change { log.reload.done }.to(false) .to change { log.reload.state }.to('hangup')
.and not_change { log.reload.done }
end
context 'when call is forwarded' do
let(:cause) { 'forwarded' }
it 'sets attributes #state: "hangup", #done: true' do
expect { Cti::Log.process(attributes) }
.to change { log.reload.state }.to('hangup')
.and change { log.reload.done }.to(true)
end
end
end
context 'for Log with #state "answer"' do
let(:log) { create(:'cti/log', call_id: 1, state: 'answer', done: true) }
it 'sets attributes #state: "hangup"' do
expect { Cti::Log.process(attributes) }
.to change { log.reload.state }.to('hangup')
.and not_change { log.reload.done }
end
context 'when call is sent to voicemail' do
before { log.update(to_comment: 'voicemail') }
it 'sets attributes #state: "hangup", #done: false' do
expect { Cti::Log.process(attributes) }
.to change { log.reload.state }.to('hangup')
.and change { log.reload.done }.to(false)
end
end end
end end
end end

View file

@ -1,210 +0,0 @@
require 'test_helper'
class CtiCallerIdTest < ActiveSupport::TestCase
setup do
Ticket.destroy_all
Cti::CallerId.destroy_all
@agent1 = User.create_or_update(
login: 'ticket-caller_id-agent1@example.com',
firstname: 'CallerId',
lastname: 'Agent1',
email: 'ticket-caller_id-agent1@example.com',
active: true,
phone: '+49 1111 222222',
fax: '+49 1111 222223',
mobile: '+49 1111 222223',
note: 'Phone at home: +49 1111 222224',
updated_by_id: 1,
created_by_id: 1,
)
@agent2 = User.create_or_update(
login: 'ticket-caller_id-agent2@example.com',
firstname: 'CallerId',
lastname: 'Agent2',
email: 'ticket-caller_id-agent2@example.com',
phone: '+49 2222 222222',
note: 'Phone at home: <b>+49 2222 222224</b>',
active: true,
updated_by_id: 1,
created_by_id: 1,
)
@agent3 = User.create_or_update(
login: 'ticket-caller_id-agent3@example.com',
firstname: 'CallerId',
lastname: 'Agent3',
email: 'ticket-caller_id-agent3@example.com',
phone: '+49 2222 222222',
active: true,
updated_by_id: 1,
created_by_id: 1,
)
@customer1 = User.create_or_update(
login: 'ticket-caller_id-customer1@example.com',
firstname: 'CallerId',
lastname: 'Customer1',
email: 'ticket-caller_id-customer1@example.com',
phone: '+49 123 456',
active: true,
updated_by_id: 1,
created_by_id: 1,
)
Observer::Transaction.commit
Scheduler.worker(true)
end
test 'not answered should be not marked as done' do
Cti::Log.process(
'cause' => '',
'event' => 'newCall',
'user' => 'user 1',
'from' => '491111222222',
'to' => '4930600000000',
'callId' => 'touch-loop-1',
'direction' => 'in',
)
last = Cti::Log.last
assert_equal(last.state, 'newCall')
assert_equal(last.done, false)
travel 2.seconds
Cti::Log.process(
'cause' => '',
'event' => 'hangup',
'user' => 'user 1',
'from' => '491111222222',
'to' => '4930600000000',
'callId' => 'touch-loop-1',
'direction' => 'in',
)
last.reload
assert_equal(last.state, 'hangup')
assert_equal(last.done, false)
end
test 'answered should be marked as done' do
Cti::Log.process(
'cause' => '',
'event' => 'newCall',
'user' => 'user 1',
'from' => '491111222222',
'to' => '4930600000000',
'callId' => 'touch-loop-1',
'direction' => 'in',
)
last = Cti::Log.last
assert_equal(last.state, 'newCall')
assert_equal(last.done, false)
travel 2.seconds
Cti::Log.process(
'cause' => '',
'event' => 'answer',
'user' => 'user 1',
'from' => '491111222222',
'to' => '4930600000000',
'callId' => 'touch-loop-1',
'direction' => 'in',
)
last = Cti::Log.last
assert_equal(last.state, 'answer')
assert_equal(last.done, true)
travel 2.seconds
Cti::Log.process(
'cause' => '',
'event' => 'hangup',
'user' => 'user 1',
'from' => '491111222222',
'to' => '4930600000000',
'callId' => 'touch-loop-1',
'direction' => 'in',
)
last.reload
assert_equal(last.state, 'hangup')
assert_equal(last.done, true)
end
test 'voicemail should not be marked as done' do
Cti::Log.process(
'cause' => '',
'event' => 'newCall',
'user' => 'user 1',
'from' => '491111222222',
'to' => '4930600000000',
'callId' => 'touch-loop-1',
'direction' => 'in',
)
last = Cti::Log.last
assert_equal(last.state, 'newCall')
assert_equal(last.done, false)
Cti::Log.process(
'cause' => '',
'event' => 'answer',
'user' => 'voicemail',
'from' => '491111222222',
'to' => '4930600000000',
'callId' => 'touch-loop-1',
'direction' => 'in',
)
last = Cti::Log.last
assert_equal(last.state, 'answer')
assert_equal(last.done, true)
travel 2.seconds
Cti::Log.process(
'cause' => '',
'event' => 'hangup',
'user' => 'user 1',
'from' => '491111222222',
'to' => '4930600000000',
'callId' => 'touch-loop-1',
'direction' => 'in',
)
last.reload
assert_equal(last.state, 'hangup')
assert_equal(last.done, false)
end
test 'forwarded should be marked as done' do
Cti::Log.process(
'cause' => '',
'event' => 'newCall',
'user' => 'user 1',
'from' => '491111222222',
'to' => '4930600000000',
'callId' => 'touch-loop-1',
'direction' => 'in',
)
last = Cti::Log.last
assert_equal(last.state, 'newCall')
assert_equal(last.done, false)
travel 2.seconds
Cti::Log.process(
'cause' => 'forwarded',
'event' => 'hangup',
'user' => 'user 1',
'from' => '491111222222',
'to' => '4930600000000',
'callId' => 'touch-loop-1',
'direction' => 'in',
)
last.reload
assert_equal(last.state, 'hangup')
assert_equal(last.done, true)
end
end