ticket_duplicate_detection_spec.rb 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Assist agent/customer to prevent duplicate ticket creation #4592', authenticated_as: :authenticate, type: :system do
  4. let(:permitted_group1) { create(:group) }
  5. let(:permitted_group2) { create(:group) }
  6. let(:non_permitted_group) { create(:group) }
  7. let(:current_user) { create(:agent, groups: [permitted_group1, permitted_group2]) }
  8. let(:customer) { create(:customer) }
  9. let(:ticket1) { create(:ticket, customer: customer, group: permitted_group1, title: '123') }
  10. let(:ticket2) { create(:ticket, customer: customer, group: permitted_group2, title: 'ABC') }
  11. let(:ticket3) { create(:ticket, group: non_permitted_group, title: '123') }
  12. let(:ticket4) { create(:ticket, group: non_permitted_group, title: 'XXX') }
  13. let(:attributes) { %w[group_id title] }
  14. let(:show_tickets) { true }
  15. let(:permission_level) { 'user' }
  16. let(:role_ids) { [Role.find_by(name: 'Agent').id] }
  17. let(:route) { 'ticket/create' }
  18. def authenticate
  19. create_text_attribute if defined?(create_text_attribute)
  20. ticket1 && ticket2 && ticket3 && ticket4
  21. Setting.set('ticket_duplicate_detection', true)
  22. Setting.set('ticket_duplicate_detection_attributes', attributes)
  23. Setting.set('ticket_duplicate_detection_show_tickets', show_tickets)
  24. Setting.set('ticket_duplicate_detection_permission_level', permission_level)
  25. Setting.set('ticket_duplicate_detection_role_ids', role_ids)
  26. current_user
  27. end
  28. before do
  29. visit route
  30. end
  31. shared_examples 'showing a warning' do
  32. it 'shows a warning' do
  33. find('[name="title"]').send_keys title, :tab if defined?(title)
  34. set_tree_select_value('group_id', group.name) if defined?(group)
  35. find('[name="text_attribute"]').send_keys text_attribute, :tab if defined?(text_attribute)
  36. expect(page).to have_text 'Similar tickets found'
  37. expect(page).to have_text 'Tickets with the same attributes were found.'
  38. shown_tickets.each do |ticket|
  39. expect(page).to have_text ticket.number
  40. end
  41. hidden_tickets.each do |ticket|
  42. expect(page).to have_no_text ticket.number
  43. end
  44. end
  45. end
  46. shared_examples 'showing no warning' do
  47. it 'shows no warning' do
  48. find('[name="title"]').send_keys title, :tab
  49. set_tree_select_value('group_id', group.name)
  50. expect(page).to have_no_text 'Similar tickets found'
  51. end
  52. end
  53. context 'with matching attributes shows a warning and accessible tickets only' do
  54. let(:title) { '123' }
  55. let(:group) { permitted_group1 }
  56. let(:shown_tickets) { [ticket1] }
  57. let(:hidden_tickets) { [ticket2, ticket3, ticket4] }
  58. it_behaves_like 'showing a warning'
  59. end
  60. context 'with non-matching attributes shows no warning' do
  61. let(:title) { '123' }
  62. let(:group) { permitted_group2 }
  63. it_behaves_like 'showing no warning'
  64. end
  65. context 'with matching attributes but no access to the tickets shows no warning' do
  66. let(:title) { 'XXX' }
  67. let(:group) { permitted_group1 }
  68. it_behaves_like 'showing no warning'
  69. end
  70. context 'with no tickets shown in the warning' do
  71. let(:show_tickets) { false }
  72. context 'with matching attributes shows a warning without tickets' do
  73. let(:title) { '123' }
  74. let(:group) { permitted_group1 }
  75. let(:shown_tickets) { [] }
  76. let(:hidden_tickets) { [ticket1, ticket2, ticket3, ticket4] }
  77. it_behaves_like 'showing a warning'
  78. end
  79. end
  80. context 'with permission level system' do
  81. let(:attributes) { ['title'] }
  82. let(:permission_level) { 'system' }
  83. context 'with matching attributes shows a warning and accessible tickets only' do
  84. let(:title) { '123' }
  85. let(:shown_tickets) { [ticket1] }
  86. let(:hidden_tickets) { [ticket2, ticket3, ticket4] }
  87. it_behaves_like 'showing a warning'
  88. end
  89. end
  90. context 'with permission level user' do
  91. let(:attributes) { ['title'] }
  92. context 'with matching attributes shows a warning and accessible tickets only' do
  93. let(:title) { '123' }
  94. let(:shown_tickets) { [ticket1] }
  95. let(:hidden_tickets) { [ticket2, ticket3, ticket4] }
  96. it_behaves_like 'showing a warning'
  97. end
  98. end
  99. context 'with customer user' do
  100. let(:current_user) { customer }
  101. let(:role_ids) { [Role.find_by(name: 'Customer').id] }
  102. let(:route) { 'customer_ticket_new' }
  103. context 'with matching attributes shows a warning and accessible tickets only' do
  104. let(:title) { '123' }
  105. let(:group) { permitted_group1 }
  106. let(:shown_tickets) { [ticket1] }
  107. let(:hidden_tickets) { [ticket2, ticket3, ticket4] }
  108. it_behaves_like 'showing a warning'
  109. end
  110. end
  111. context 'with custom object attribute', db_strategy: :reset do
  112. let(:attributes) { ['text_attribute'] }
  113. let(:ticket1) { create(:ticket, customer: customer, group: permitted_group1, title: '123', text_attribute: text_attribute) }
  114. let(:ticket2) { create(:ticket, customer: customer, group: permitted_group2, title: 'ABC', text_attribute: 'baz qux') }
  115. let(:ticket3) { create(:ticket, group: non_permitted_group, title: '123', text_attribute: text_attribute) }
  116. let(:ticket4) { create(:ticket, group: non_permitted_group, title: '123', text_attribute: text_attribute) }
  117. def create_text_attribute
  118. create(:object_manager_attribute_text, :required_screen, name: 'text_attribute', display: 'Text Attribute', data_option: {
  119. 'type' => 'text',
  120. 'maxlength' => 255,
  121. 'null' => true,
  122. 'translate' => false,
  123. 'default' => '',
  124. 'options' => {},
  125. 'relation' => '',
  126. })
  127. ObjectManager::Attribute.migration_execute
  128. end
  129. context 'with matching attributes shows a warning and accessible tickets only' do
  130. let(:text_attribute) { 'foobar' }
  131. let(:shown_tickets) { [ticket1] }
  132. let(:hidden_tickets) { [ticket2, ticket3, ticket4] }
  133. it_behaves_like 'showing a warning'
  134. end
  135. end
  136. end