recent_view_spec.rb 5.8 KB

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