ticket_overview_list_spec.rb 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. require 'rails_helper'
  2. RSpec.describe Sessions::Backend::TicketOverviewList do
  3. it 'inherits #asset_needed? from Sessions::Backend::Base' do
  4. expect(described_class.instance_method(:asset_needed?).owner)
  5. .to be(described_class.superclass)
  6. end
  7. it 'inherits #asset_push from Sessions::Backend::Base' do
  8. expect(described_class.instance_method(:asset_push).owner)
  9. .to be(described_class.superclass)
  10. end
  11. describe '.push' do
  12. let(:admin) { create(:admin, groups: [group]) }
  13. let(:group) { create(:group) }
  14. let(:client_id) { '12345' }
  15. let(:ttl) { 3 } # seconds
  16. context 'when 3rd argument ("client") is false' do
  17. subject(:collection) { described_class.new(admin, {}, false, client_id, ttl) }
  18. it 'returns an array of hashes with :event and :data keys' do
  19. expect(collection.push)
  20. .to be_an(Array)
  21. .and have_attributes(length: Ticket::Overviews.all(current_user: admin).count)
  22. .and all(match({ event: 'ticket_overview_list', data: hash_including(:assets) }))
  23. end
  24. it 'returns one hash for each of the user’s ticket overviews' do
  25. expect(collection.push.map { |hash| hash[:data][:overview][:name] })
  26. .to match_array(Ticket::Overviews.all(current_user: admin).map(&:name))
  27. end
  28. it 'is optimized to not send duplicate asset entries over all events' do
  29. collection_assets = collection.push.map { |hash| hash[:data][:assets] }
  30. # match all event assets against the assets of the other events
  31. # and make sure that each asset entry is unique over all events assets
  32. unique_asssets = collection_assets.each_with_index.all? do |lookup_assets, lookup_index|
  33. collection_assets.each_with_index.none? do |comparison_assets, comparison_index|
  34. # skip assets comparison for same event
  35. next if comparison_index == lookup_index
  36. # check that none of the lookup_assets assets is present
  37. # in the comparison_assets
  38. lookup_assets.keys.any? do |model|
  39. # skip Models that are only present in our lookup_assets entry
  40. next if !comparison_assets.key?(model)
  41. # check if there are no intersect Model record IDs
  42. # aka IDs present in both hashes
  43. intersection_ids = lookup_assets[model].keys & comparison_assets[model].keys
  44. intersection_ids.present?
  45. end
  46. end
  47. end
  48. expect(unique_asssets).to be(true)
  49. end
  50. it 'includes FE assets for all overviews and tickets not pushed in the last two hours' do
  51. # ATTENTION: we can't compare the arrays of assets directly
  52. # because the Ticket::Overviews backend contain an optimization logic that sends an asset entry only once
  53. # while the Sessions::Backend::* classes results contain all assets for each entry.
  54. # Therefore we merge all assets for each of the both arrays to have two big Hashes that contains all assets.
  55. # See previous example for the matching spec.
  56. collection_assets = collection.push.map { |hash| hash[:data][:assets] }
  57. collection_assets_merged = collection_assets.each_with_object({}) { |assets, result| result.deep_merge!(assets) }
  58. overviews_all_assets = Ticket::Overviews.all(current_user: admin).map { |overview| overview.assets({}) }
  59. overviews_all_assets_merged = overviews_all_assets.each_with_object({}) { |assets, result| result.deep_merge!(assets) }
  60. expect(collection_assets_merged).to eq(overviews_all_assets_merged)
  61. end
  62. context 'when called twice, with no changes to Ticket and Overview tables' do
  63. let!(:first_call) { collection.push }
  64. it 'returns nil' do
  65. expect(collection.push).to eq(nil)
  66. end
  67. context 'even after the TTL has passed' do
  68. before { travel(ttl + 1) }
  69. it 'returns nil' do
  70. expect(collection.push).to eq(nil)
  71. end
  72. end
  73. context 'even after .reset with the user’s id' do
  74. before { described_class.reset(admin.id) }
  75. it 'returns nil' do
  76. expect(collection.push).to eq(nil)
  77. end
  78. end
  79. end
  80. context 'when called twice, after changes have occurred to the Ticket table' do
  81. let!(:ticket) { create(:ticket, group: group) }
  82. let!(:first_call) { collection.push }
  83. context 'before the TTL has passed' do
  84. it 'returns nil' do
  85. expect(collection.push).to eq(nil)
  86. end
  87. context 'after .reset with the user’s id' do
  88. before { described_class.reset(admin.id) }
  89. it 'returns nil because no ticket and no overview has changed' do
  90. expect(collection.push).to eq(nil)
  91. end
  92. end
  93. end
  94. context 'after the TTL has passed' do
  95. before { travel(ttl + 1) }
  96. it 'returns an empty result' do
  97. expect(collection.push).to eq(nil)
  98. end
  99. end
  100. context 'after two hours have passed' do
  101. before { travel(2.hours + 1.second) }
  102. it 'returns an empty result' do
  103. expect(collection.push).to eq(nil)
  104. end
  105. end
  106. end
  107. context 'when called twice, after changes have occurred to the Overviews table' do
  108. let!(:first_call) { collection.push }
  109. before { Overview.first.touch }
  110. context 'before the TTL has passed' do
  111. it 'returns nil' do
  112. expect(collection.push).to be(nil)
  113. end
  114. context 'after .reset with the user’s id' do
  115. before { described_class.reset(admin.id) }
  116. it 'returns an updated set of results' do
  117. expect(collection.push)
  118. .to be_an(Array)
  119. .and have_attributes(length: 1)
  120. .and all(match({ event: 'ticket_overview_list', data: hash_including(:assets) }))
  121. end
  122. end
  123. end
  124. context 'after two hours have passed' do
  125. before { travel(2.hours + 1.second) }
  126. it 'returns an empty result' do
  127. expect(collection.push)
  128. .to be_an(Array)
  129. .and have_attributes(length: 1)
  130. .and all(match({ event: 'ticket_overview_list', data: hash_including(:assets) }))
  131. end
  132. end
  133. end
  134. end
  135. end
  136. end