diff --git a/spec/factories/cti/log.rb b/spec/factories/cti/log.rb index c2f3b0cdd..8118a6c62 100644 --- a/spec/factories/cti/log.rb +++ b/spec/factories/cti/log.rb @@ -1,9 +1,9 @@ FactoryBot.define do - factory :cti_log, class: 'cti/log' do + factory :'cti/log', aliases: %i[cti_log] do direction { %w[in out].sample } state { %w[newCall answer hangup].sample } - from '4930609854180' - to '4930609811111' - call_id { (Cti::Log.pluck(:call_id).max || '0').next } # has SQL UNIQUE constraint + from { '4930609854180' } + to { '4930609811111' } + call_id { (Cti::Log.pluck(:call_id).map(&:to_i).max || 0).next } # has SQL UNIQUE constraint end end diff --git a/spec/models/cti/caller_id_spec.rb b/spec/models/cti/caller_id_spec.rb index f30d8dd15..5c40de9f2 100644 --- a/spec/models/cti/caller_id_spec.rb +++ b/spec/models/cti/caller_id_spec.rb @@ -219,7 +219,7 @@ RSpec.describe Cti::CallerId do let(:attributes) { attributes_for(:caller_id) } it 'wraps .find_or_initialize_by (passing only five defining attributes)' do - expect(described_class) + expect(Cti::CallerId) .to receive(:find_or_initialize_by) .with(attributes.slice(:caller_id, :level, :object, :o_id, :user_id)) .and_call_original diff --git a/spec/models/cti/log.rb b/spec/models/cti/log.rb deleted file mode 100644 index 519b1b978..000000000 --- a/spec/models/cti/log.rb +++ /dev/null @@ -1,46 +0,0 @@ -require 'rails_helper' - -RSpec.describe Cti::Log do - subject { create(:cti_log, **factory_attributes) } - let(:factory_attributes) { {} } - - context 'with complete, E164 international numbers' do - let(:factory_attributes) { { from: '4930609854180', to: '4930609811111' } } - - describe '#from_pretty' do - it 'gives the number in prettified format' do - expect(subject.from_pretty).to eq('+49 30 609854180') - end - end - - describe '#to_pretty' do - it 'gives the number in prettified format' do - expect(subject.to_pretty).to eq('+49 30 609811111') - end - end - end - - context 'with private network numbers' do - let(:factory_attributes) { { from: '007', to: '008' } } - - describe '#from_pretty' do - it 'gives the number unaltered' do - expect(subject.from_pretty).to eq('007') - end - end - - describe '#to_pretty' do - it 'gives the number unaltered' do - expect(subject.to_pretty).to eq('008') - end - end - end - - describe '#to_json' do - let(:virtual_attributes) { %w[from_pretty to_pretty] } - - it 'includes virtual attributes' do - expect(subject.as_json).to include(*virtual_attributes) - end - end -end diff --git a/spec/models/cti/log_spec.rb b/spec/models/cti/log_spec.rb new file mode 100644 index 000000000..ba8b299f7 --- /dev/null +++ b/spec/models/cti/log_spec.rb @@ -0,0 +1,116 @@ +require 'rails_helper' + +RSpec.describe Cti::Log do + subject(:log) { create(:'cti/log') } + + describe '.log' do + it 'returns a hash with :list and :assets keys' do + expect(Cti::Log.log).to be_a(Hash).and include(:list, :assets) + end + + context 'when over 60 Log records exist' do + subject!(:cti_logs) { create_list(:'cti/log', 61) } + + it 'returns the 60 latest ones in the :list key' do + expect(Cti::Log.log[:list]).to match_array(cti_logs.last(60)) + end + end + + context 'when Log records have arrays of CallerId attributes in #preferences[:to] / #preferences[:from]' do + subject!(:cti_log) { create(:'cti/log', preferences: { from: [caller_id] }) } + let(:caller_id) { create(:caller_id) } + let(:user) { User.find_by(id: caller_id.user_id) } + + it 'returns a hash of the CallerId Users and their assets in the :assets key' do + expect(Cti::Log.log[:assets]).to eq(user.assets({})) + end + end + end + + describe '.process' do + context 'for event "newCall"' do + let(:attributes) do + { + 'cause' => '', + 'event' => 'newCall', + 'user' => 'user 1', + 'from' => '49123456', + 'to' => '49123457', + 'callId' => '1', + 'direction' => 'in', + } + end + + it 'creates a new Log record' do + expect { Cti::Log.process(attributes) } + .to change { Cti::Log.count }.by(1) + end + + context 'for direction "in", with a CallerId record matching the "from" number' do + let!(:caller_id) { create(:caller_id, caller_id: '49123456') } + before { attributes.merge!('direction' => 'in') } + + it 'saves that CallerId’s attributes in the new Log’s #preferences[:from] attribute' do + Cti::Log.process(attributes) + + expect(Cti::Log.last.preferences[:from].first) + .to include(caller_id.attributes.except('created_at')) # Checking equality of Time objects is error-prone + end + end + + context 'for direction "out", with a CallerId record matching the "to" number' do + let!(:caller_id) { create(:caller_id, caller_id: '49123457') } + before { attributes.merge!('direction' => 'out') } + + it 'saves that CallerId’s attributes in the new Log’s #preferences[:to] attribute' do + Cti::Log.process(attributes) + + expect(Cti::Log.last.preferences[:to].first) + .to include(caller_id.attributes.except('created_at')) # Checking equality of Time objects is error-prone + end + end + end + end + + describe '#from_pretty' do + context 'with complete, E164 international numbers' do + subject(:log) { create(:cti_log, from: '4930609854180') } + + it 'gives the number in prettified format' do + expect(log.from_pretty).to eq('+49 30 609854180') + end + end + + context 'with private network numbers' do + subject(:log) { create(:cti_log, from: '007') } + + it 'gives the number unaltered' do + expect(log.from_pretty).to eq('007') + end + end + end + + describe '#to_pretty' do + context 'with complete, E164 international numbers' do + subject(:log) { create(:cti_log, to: '4930609811111') } + + it 'gives the number in prettified format' do + expect(log.to_pretty).to eq('+49 30 609811111') + end + end + + context 'with private network numbers' do + subject(:log) { create(:cti_log, to: '008') } + + it 'gives the number unaltered' do + expect(log.to_pretty).to eq('008') + end + end + end + + describe '#to_json' do + it 'includes virtual attributes' do + expect(log.as_json).to include('from_pretty', 'to_pretty') + end + end +end diff --git a/test/unit/cti_caller_id_test.rb b/test/unit/cti_caller_id_test.rb index 290d4bcb7..a5c6b5832 100644 --- a/test/unit/cti_caller_id_test.rb +++ b/test/unit/cti_caller_id_test.rb @@ -46,6 +46,7 @@ class CtiCallerIdTest < ActiveSupport::TestCase 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, @@ -55,84 +56,6 @@ class CtiCallerIdTest < ActiveSupport::TestCase Scheduler.worker(true) end - test '3 process - log' do - - ticket1 = Ticket.create!( - title: 'some caller id test 1', - group: Group.lookup(name: 'Users'), - customer: @customer1, - state: Ticket::State.lookup(name: 'new'), - priority: Ticket::Priority.lookup(name: '2 normal'), - updated_by_id: @agent1.id, - created_by_id: @agent1.id, - ) - article1 = Ticket::Article.create!( - ticket_id: ticket1.id, - from: 'some_sender@example.com', - to: 'some_recipient@example.com', - subject: 'some subject', - message_id: 'some@id', - body: "some message\nFon (GEL): +49 111 366-1111 Mi-Fr -Fon (LIN): +49 222 6112222 Mo-Di -Mob: +49 333 8362222", - internal: false, - sender: Ticket::Article::Sender.where(name: 'Customer').first, - type: Ticket::Article::Type.where(name: 'email').first, - updated_by_id: @customer1.id, - created_by_id: @customer1.id, - ) - assert(ticket1) - ticket2 = Ticket.create!( - title: 'some caller id test 2', - group: Group.lookup(name: 'Users'), - customer: @customer1, - state: Ticket::State.lookup(name: 'new'), - priority: Ticket::Priority.lookup(name: '2 normal'), - updated_by_id: @agent1.id, - created_by_id: @agent1.id, - ) - article2 = Ticket::Article.create!( - ticket_id: ticket2.id, - from: 'some_sender@example.com', - to: 'some_recipient@example.com', - subject: 'some subject', - message_id: 'some@id', - body: "some message\nFon (GEL): +49 111 366-1111 Mi-Fr -Fon (LIN): +49 222 6112222 Mo-Di -Mob: +49 333 8362222", - internal: false, - sender: Ticket::Article::Sender.where(name: 'Customer').first, - type: Ticket::Article::Type.where(name: 'email').first, - updated_by_id: @customer1.id, - created_by_id: @customer1.id, - ) - assert(ticket2) - - Cti::CallerId.rebuild - - Cti::Log.process( - 'cause' => '', - 'event' => 'newCall', - 'user' => 'user 1', - 'from' => '491113661111', - 'to' => '4930600000000', - 'callId' => '4991155921769858278-1', - 'direction' => 'in', - ) - - log = Cti::Log.log - assert(log[:list]) - assert(log[:assets]) - assert(log[:list][0]) - assert_not(log[:list][1]) - assert(log[:list][0].preferences) - assert(log[:list][0].preferences[:from]) - assert_equal(1, log[:list][0].preferences[:from].count) - assert_equal(@customer1.id, log[:list][0].preferences[:from][0][:user_id]) - assert_equal('maybe', log[:list][0].preferences[:from][0][:level]) - - end - test '4 touch caller log / don\'t touch caller log' do 5.times do |count| travel 2.seconds