recent_view_spec.rb 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. require 'rails_helper'
  2. RSpec.describe RecentView, type: :model do
  3. let(:admin) { create(:admin) }
  4. let(:agent) { create(:agent) }
  5. let(:customer) { create(:customer) }
  6. let(:ticket) { create(:ticket, owner: owner, customer: customer) }
  7. let(:tickets) { create_list(:ticket, 15, owner: owner, customer: customer) }
  8. let(:owner) { admin }
  9. describe '::list' do
  10. it 'returns a sample of recently viewed objects (e.g., tickets/users/organizations)' do
  11. tickets.each { |t| described_class.log('Ticket', t.id, admin) }
  12. expect(described_class.list(admin).map(&:o_id)).to include(*tickets.last(10).map(&:id))
  13. end
  14. it 'returns up to 10 results by default' do
  15. tickets.each { |t| described_class.log('Ticket', t.id, admin) }
  16. expect(described_class.list(admin).length).to eq(10)
  17. end
  18. context 'with a `limit` argument (optional)' do
  19. it 'returns up to that number of results' do
  20. tickets.each { |t| described_class.log('Ticket', t.id, admin) }
  21. expect(described_class.list(admin, 12).length).to eq(12)
  22. end
  23. end
  24. context 'with an `object_name` argument (optional)' do
  25. it 'includes only the specified model class' do
  26. described_class.log('Ticket', ticket.id, admin)
  27. described_class.log('Organization', 1, admin)
  28. expect(described_class.list(admin, 10, 'Organization').length).to eq(1)
  29. end
  30. it 'does not include merged tickets in results' do
  31. described_class.log('Ticket', ticket.id, admin)
  32. ticket.update(state: Ticket::State.find_by(name: 'merged'))
  33. expect(described_class.list(admin, 10, 'Ticket').length).to eq(0)
  34. end
  35. it 'does not include removed tickets in results' do
  36. described_class.log('Ticket', ticket.id, admin)
  37. ticket.update(state: Ticket::State.find_by(name: 'removed'))
  38. expect(described_class.list(admin, 10, 'Ticket').length).to eq(0)
  39. end
  40. end
  41. it 'does not include duplicate results' do
  42. 5.times { described_class.log('Ticket', ticket.id, admin) }
  43. expect(described_class.list(admin).length).to eq(1)
  44. end
  45. it 'does not include deleted tickets in results' do
  46. described_class.log('Ticket', ticket.id, admin)
  47. ticket.destroy
  48. expect(described_class.list(admin).length).to eq(0)
  49. end
  50. describe 'access privileges' do
  51. context 'when given user is agent' do
  52. let(:owner) { agent }
  53. it 'includes own tickets in results' do
  54. described_class.log('Ticket', ticket.id, agent)
  55. expect(described_class.list(agent).length).to eq(1)
  56. end
  57. it 'does not include other agents’ tickets in results' do
  58. described_class.log('Ticket', ticket.id, agent)
  59. ticket.update(owner: User.first)
  60. expect(described_class.list(agent).length).to eq(0)
  61. end
  62. it 'includes any organizations in results' do
  63. agent.update(organization: nil)
  64. described_class.log('Organization', 1, agent)
  65. expect(described_class.list(agent).length).to eq(1)
  66. end
  67. end
  68. context 'when given user is customer' do
  69. it 'includes own tickets in results' do
  70. described_class.log('Ticket', ticket.id, customer)
  71. expect(described_class.list(customer).length).to eq(1)
  72. end
  73. it 'does not include other customers’ tickets in results' do
  74. described_class.log('Ticket', ticket.id, customer)
  75. ticket.update(customer: User.first)
  76. expect(described_class.list(customer).length).to eq(0)
  77. end
  78. it 'includes own organization in results' do
  79. customer.update(organization: Organization.first)
  80. described_class.log('Organization', 1, customer)
  81. expect(described_class.list(customer).length).to eq(1)
  82. end
  83. it 'does not include other organizations in results' do
  84. customer.update(organization: Organization.first)
  85. described_class.log('Organization', 1, customer)
  86. customer.update(organization: nil)
  87. expect(described_class.list(customer).length).to eq(0)
  88. end
  89. end
  90. end
  91. end
  92. describe '::user_log_destroy' do
  93. it 'deletes all RecentView records for a given user' do
  94. create_list(:recent_view, 10, created_by_id: admin.id)
  95. expect { described_class.user_log_destroy(admin) }
  96. .to change { described_class.exists?(created_by_id: admin.id) }.to(false)
  97. end
  98. end
  99. describe '::log' do
  100. let(:viewed_object) { ticket }
  101. let(:viewed_object_class_id) { ObjectLookup.by_name(viewed_object.class.name) }
  102. it 'wraps RecentView.create' do
  103. expect do
  104. described_class.log(viewed_object.class.name, viewed_object.id, admin)
  105. end.to change(described_class, :count).by(1)
  106. end
  107. describe 'access privileges' do
  108. let(:owner) { agent }
  109. it 'does not create RecentView for records the given user cannot read' do
  110. ticket.update(owner: User.first, # read access may come from ticket ownership,
  111. customer: User.first, # customer status,
  112. organization: nil) # organization's 'shared' status,
  113. agent.update(groups: []) # and membership in the Ticket's group
  114. expect do
  115. described_class.log(viewed_object.class.name, viewed_object.id, agent)
  116. end.not_to change(described_class, :count)
  117. end
  118. end
  119. context 'when given an invalid object' do
  120. it 'does not create RecentView for non-existent record' do
  121. expect do
  122. described_class.log('User', 99_999_999, admin)
  123. end.not_to change(described_class, :count)
  124. end
  125. it 'does not create RecentView for instance of non-ObjectLookup class' do
  126. expect do
  127. described_class.log('Overview', 1, admin)
  128. end.not_to change(described_class, :count)
  129. end
  130. it 'does not create RecentView for instance of non-existent class' do
  131. expect do
  132. described_class.log('NonExistentClass', 1, admin)
  133. end.not_to change(described_class, :count)
  134. end
  135. end
  136. end
  137. end