Refactoring: Migrate session_basic_ticket_test to RSpec.

This commit is contained in:
Ryan Lue 2019-06-28 11:10:05 +02:00 committed by Thorsten Eckel
parent 5c71350eb6
commit 7930ffdf03
3 changed files with 275 additions and 183 deletions

View file

@ -0,0 +1,98 @@
require 'rails_helper'
RSpec.describe Sessions::Backend::Base do
subject(:backend) { described_class.new(agent, {}, false, client_id, ttl) }
let(:agent) { create(:agent_user) }
let(:client_id) { '123-1' }
let(:ttl) { 3 } # seconds
let!(:ticket) { Ticket.first || create(:ticket) }
describe '#asset_needed?' do
context 'before #asset_push has ever been called for the given record' do
it 'returns true' do
expect(backend.asset_needed?(ticket)).to be(true)
end
end
context 'when #asset_push was previously called for the given record' do
before { backend.asset_push(ticket, {}) }
it 'returns false' do
expect(backend.asset_needed?(ticket)).to be(false)
end
context 'within two hours of the backends #time_now value' do
before { backend.time_now = (2.hours - 1.second).from_now.to_i }
it 'returns false' do
expect(backend.asset_needed?(ticket)).to be(false)
end
end
context 'over two hours before the backends #time_now value' do
before { backend.time_now = (2.hours + 1.second).from_now.to_i }
it 'returns true' do
expect(backend.asset_needed?(ticket)).to be(true)
end
end
context 'prior to the records last update' do
before { ticket.touch }
it 'returns true' do
expect(backend.asset_needed?(ticket)).to be(true)
end
end
end
end
describe '#asset_needed_by_updated_at?' do
let(:method_args) { [ticket.class.name, ticket.id, ticket.updated_at] }
context 'before #asset_push has ever been called for the given record' do
it 'returns true' do
expect(backend.asset_needed_by_updated_at?(*method_args)).to be(true)
end
end
context 'when #asset_push was previously called for the given record' do
before { backend.asset_push(ticket, {}) }
it 'returns false' do
expect(backend.asset_needed_by_updated_at?(*method_args)).to be(false)
end
context 'within two hours of the backends #time_now value' do
before { backend.time_now = (2.hours - 1.second).from_now.to_i }
it 'returns false' do
expect(backend.asset_needed_by_updated_at?(*method_args)).to be(false)
end
end
context 'over two hours before the backends #time_now value' do
before { backend.time_now = (2.hours + 1.second).from_now.to_i }
it 'returns true' do
expect(backend.asset_needed_by_updated_at?(*method_args)).to be(true)
end
end
context 'prior to the records last update' do
before { ticket.touch }
it 'returns true' do
expect(backend.asset_needed_by_updated_at?(*method_args)).to be(true)
end
end
end
end
describe '#asset_push' do
it 'returns the assets for the given record' do
expect(backend.asset_push(ticket, {})).to eq(ticket.assets({}))
end
end
end

View file

@ -0,0 +1,177 @@
require 'rails_helper'
RSpec.describe Sessions::Backend::TicketOverviewList do
it 'inherits #asset_needed? from Sessions::Backend::Base' do
expect(described_class.instance_method(:asset_needed?).owner)
.to be(described_class.superclass)
end
it 'inherits #asset_push from Sessions::Backend::Base' do
expect(described_class.instance_method(:asset_push).owner)
.to be(described_class.superclass)
end
describe '.push' do
let(:admin) { create(:admin_user, groups: [group]) }
let(:group) { create(:group) }
let(:client_id) { '12345' }
let(:ttl) { 3 } # seconds
context 'when 3rd argument ("client") is false' do
subject(:collection) { described_class.new(admin, {}, false, client_id, ttl) }
it 'returns an array of hashes with :event and :data keys' do
expect(collection.push)
.to be_an(Array)
.and have_attributes(length: Ticket::Overviews.all(current_user: admin).count)
.and all(match({ event: 'ticket_overview_list', data: hash_including(:assets) }))
end
it 'returns one hash for each of the users ticket overviews' do
expect(collection.push.map { |hash| hash[:data][:overview][:name] })
.to match_array(Ticket::Overviews.all(current_user: admin).map(&:name))
end
it 'includes FE assets for all overviews and tickets not pushed in the last two hours' do
expect(collection.push.map { |hash| hash[:data][:assets] })
.to match_array(Ticket::Overviews.all(current_user: admin).map { |overview| overview.assets({}) })
end
context 'when called twice, with no changes to Ticket and Overview tables' do
let!(:first_call) { collection.push }
it 'returns nil' do
expect(collection.push).to be(nil)
end
context 'even after the TTL has passed' do
before { travel(ttl + 1) }
it 'returns nil' do
expect(collection.push).to be(nil)
end
end
context 'even after .reset with the users id' do
before { described_class.reset(admin.id) }
it 'returns nil' do
expect(collection.push).to be(nil)
end
end
end
context 'when called twice, after changes have occurred to the Ticket table' do
let!(:first_call) { collection.push }
let!(:ticket) { create(:ticket, group: group, owner: admin) }
context 'before the TTL has passed' do
it 'returns nil' do
expect(collection.push).to be(nil)
end
context 'after .reset with the users id' do
before { described_class.reset(admin.id) }
it 'returns an updated set of results' do
expect(collection.push)
.to be_an(Array)
.and have_attributes(length: Ticket::Overviews.all(current_user: admin).count)
.and all(match({ event: 'ticket_overview_list', data: hash_including(:assets) }))
end
it 'includes FE assets for the new ticket' do
expect(collection.push.first[:data][:assets]) # first overview is "My assigned tickets",
.to eq(ticket.assets({})) # and newly created ticket was assigned to admin
end
end
end
context 'after the TTL has passed' do
before { travel(ttl + 1) }
it 'returns an updated set of results' do
expect(collection.push)
.to be_an(Array)
.and have_attributes(length: Ticket::Overviews.all(current_user: admin).count)
.and all(match({ event: 'ticket_overview_list', data: hash_including(:assets) }))
end
it 'includes FE assets for the new ticket' do
expect(collection.push.first[:data][:assets]) # first overview is "My assigned tickets",
.to eq(ticket.assets({})) # and newly created ticket was assigned to admin
end
end
context 'after two hours have passed' do
before { travel(2.hours + 1.second) }
it 'returns an updated set of results' do
expect(collection.push)
.to be_an(Array)
.and have_attributes(length: Ticket::Overviews.all(current_user: admin).count)
.and all(match({ event: 'ticket_overview_list', data: hash_including(:assets) }))
end
it 'includes FE assets for old AND new tickets/overviews' do
expect(collection.push.first[:data][:assets]) # first overview is "My assigned tickets",
.to eq(Overview.first.assets(ticket.assets({}))) # and newly created ticket was assigned to admin
end
end
end
context 'when called twice, after changes have occurred to the Overviews table' do
let!(:first_call) { collection.push }
before { Overview.first.touch }
context 'before the TTL has passed' do
it 'returns nil' do
expect(collection.push).to be(nil)
end
context 'after .reset with the users id' do
before { described_class.reset(admin.id) }
it 'returns an updated set of results' do
expect(collection.push)
.to be_an(Array)
.and have_attributes(length: Ticket::Overviews.all(current_user: admin).count)
.and all(match({ event: 'ticket_overview_list', data: hash_including(:assets) }))
end
it 'includes FE assets for the touched overview' do
expect(collection.push.first[:data][:assets])
.to eq(Overview.first.assets({}))
end
end
end
context 'after the TTL has passed' do
before { travel(ttl + 1) }
it 'includes FE assets for the touched overview' do
expect(collection.push.first[:data][:assets])
.to eq(Overview.first.assets({}))
end
end
context 'after two hours have passed' do
before { travel(2.hours + 1.second) }
it 'returns an updated set of results' do
expect(collection.push)
.to be_an(Array)
.and have_attributes(length: Ticket::Overviews.all(current_user: admin).count)
.and all(match({ event: 'ticket_overview_list', data: hash_including(:assets) }))
end
it 'includes FE assets for old AND new tickets/overviews' do
expect(collection.push.first[:data][:assets])
.to eq(Overview.first.assets({}))
end
end
end
end
end
end

View file

@ -1,183 +0,0 @@
require 'test_helper'
class SessionBasicTicketTest < ActiveSupport::TestCase
setup do
UserInfo.current_user_id = 1
roles = Role.where(name: ['Agent'])
groups = Group.all
@agent1 = User.create_or_update(
login: 'session-basic-ticket-agent-1',
firstname: 'Session',
lastname: 'session basic ' + rand(99_999).to_s,
email: 'session-basic-ticket-agent-1@example.com',
password: 'agentpw',
active: true,
roles: roles,
groups: groups,
)
@agent2 = User.create_or_update(
login: 'session-basic-ticket-agent-2',
firstname: 'Session',
lastname: 'session basic ' + rand(99_999).to_s,
email: 'session-basic-ticket-agent-2@example.com',
password: 'agentpw',
active: true,
roles: roles,
groups: groups,
)
Overview.destroy_all
load Rails.root.join('db', 'seeds', 'overviews.rb')
end
test 'asset needed' do
client1 = Sessions::Backend::TicketOverviewList.new(@agent1, {}, false, '123-1', 2)
client2 = Sessions::Backend::TicketOverviewList.new(@agent2, {}, false, '123-2', 2)
ticket = Ticket.create!(title: 'default overview test', group_id: 1, priority_id: 1, state_id: 1, customer_id: 1)
assert(client1.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at))
client1.asset_push(ticket, {})
assert_not(client1.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at))
assert(client2.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at))
client2.asset_push(ticket, {})
assert_not(client2.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at))
travel 30.minutes
assert_not(client1.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at))
assert_not(client2.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at))
travel 120.minutes
client1.time_now = Time.zone.now.to_i
assert(client1.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at))
client1.asset_push(ticket, {})
client2.time_now = Time.zone.now.to_i
assert(client2.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at))
client2.asset_push(ticket, {})
client1.time_now = Time.zone.now.to_i
assert_not(client1.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at))
client1.asset_push(ticket, {})
client2.time_now = Time.zone.now.to_i
assert_not(client2.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at))
client2.asset_push(ticket, {})
ticket.touch
client1.time_now = Time.zone.now.to_i
assert(client1.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at))
client1.asset_push(ticket, {})
client2.time_now = Time.zone.now.to_i
assert(client2.asset_needed_by_updated_at?(ticket.class.to_s, ticket.id, ticket.updated_at))
client2.asset_push(ticket, {})
assert_not(client1.asset_needed?(ticket))
assert_not(client2.asset_needed?(ticket))
travel 125.minutes
client1.time_now = Time.zone.now.to_i
assert(client1.asset_needed?(ticket))
client1.asset_push(ticket, {})
client2.time_now = Time.zone.now.to_i
assert(client2.asset_needed?(ticket))
client2.asset_push(ticket, {})
assert_not(client1.asset_needed?(ticket))
assert_not(client2.asset_needed?(ticket))
travel_back
end
test 'ticket_overview_List' do
ticket1 = Ticket.create!(title: 'default overview test 1', group_id: 1, priority_id: 1, state_id: 1, customer_id: 1)
ticket2 = Ticket.create!(title: 'default overview test 2', group_id: 1, priority_id: 1, state_id: 1, customer_id: 1)
client1 = Sessions::Backend::TicketOverviewList.new(@agent1, {}, false, '123-1', 2)
result1 = client1.push
assert(result1, 'check ticket_overview_List')
assert(result1[1][:data][:assets])
assert(result1[1][:data][:assets][:Overview])
assert(result1[1][:data][:assets][:User])
assert_equal(result1[1][:data][:assets][:Ticket][ticket1.id]['title'], ticket1.title)
assert_equal(result1[1][:data][:assets][:Ticket][ticket2.id]['title'], ticket2.title)
# next check should be empty / no changes
result1 = client1.push
assert_not(result1, 'check ticket_overview_index - recall')
# next check should be empty / no changes
travel 3.seconds
result1 = client1.push
assert_not(result1, 'check ticket_overview_index - recall 2')
# create ticket
ticket3 = Ticket.create!(title: '12323', group_id: 1, priority_id: 1, state_id: 1, customer_id: 1)
travel 3.seconds
result1 = client1.push
assert(result1, 'check ticket_overview_index - recall 3')
assert(result1[1][:data][:assets])
assert_not(result1[1][:data][:assets][:Ticket][ticket1.id])
assert_not(result1[1][:data][:assets][:Ticket][ticket2.id])
assert_equal(result1[1][:data][:assets][:Ticket][ticket3.id]['title'], ticket3.title)
travel 3.seconds
# chnage overview
overviews = Ticket::Overviews.all(
current_user: @agent1,
)
overviews.first.touch
result1 = client1.push
assert(result1, 'check ticket_overview_index - recall 4')
assert(result1[1][:data][:assets])
assert_not(result1[1][:data][:assets][:Ticket])
result1 = client1.push
assert_not(result1, 'check ticket_overview_index - recall 5')
Sessions::Backend::TicketOverviewList.reset(@agent1.id)
result1 = client1.push
assert_not(result1, 'check ticket_overview_index - recall 6')
ticket4 = Ticket.create!(title: '12323 - 2', group_id: 1, priority_id: 1, state_id: 1, customer_id: 1)
Sessions::Backend::TicketOverviewList.reset(@agent1.id)
result1 = client1.push
assert(result1, 'check ticket_overview_index - recall 7')
assert(result1[1][:data][:assets])
assert_not(result1[1][:data][:assets][:Ticket][ticket1.id])
assert_not(result1[1][:data][:assets][:Ticket][ticket2.id])
assert_not(result1[1][:data][:assets][:Ticket][ticket3.id])
assert_equal(result1[1][:data][:assets][:Ticket][ticket4.id]['title'], ticket4.title)
travel 125.minutes
ticket1.touch
result1 = client1.push
assert(result1, 'check ticket_overview_index - recall 8')
assert(result1[1][:data][:assets])
assert_equal(result1[1][:data][:assets][:Ticket][ticket1.id]['title'], ticket1.title)
assert_equal(result1[1][:data][:assets][:Ticket][ticket2.id]['title'], ticket2.title)
assert_equal(result1[1][:data][:assets][:Ticket][ticket3.id]['title'], ticket3.title)
assert_equal(result1[1][:data][:assets][:Ticket][ticket4.id]['title'], ticket4.title)
travel 10.seconds
Sessions.destroy_idle_sessions(3)
travel_back
end
end