ticket_spec.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Queries::AutocompleteSearch::Ticket, authenticated_as: :agent, type: :graphql do
  4. context 'when searching for tickets', searchindex: true do
  5. let(:group) { create(:group) }
  6. let(:agent) { create(:agent, groups: [group]) }
  7. let(:state) { Ticket::State.find_by(name: 'open') }
  8. let(:source_ticket) { tickets.first }
  9. let!(:tickets) do
  10. create_list(:ticket, 3, group: group, state: state).each_with_index do |ticket, i|
  11. ticket.title = "TicketAutoComplete#{i}"
  12. ticket.save!
  13. create(:ticket_article, ticket: ticket)
  14. end
  15. end
  16. let(:query) do
  17. <<~QUERY
  18. query autocompleteSearchTicket($input: AutocompleteSearchTicketInput!) {
  19. autocompleteSearchTicket(input: $input) {
  20. value
  21. label
  22. heading
  23. ticket {
  24. number
  25. }
  26. }
  27. }
  28. QUERY
  29. end
  30. let(:variables) { { input: { query: query_string, limit: limit, exceptTicketInternalId: source_ticket.id } } }
  31. let(:query_string) { 'TicketAutoComplete' }
  32. let(:limit) { nil }
  33. before do
  34. searchindex_model_reload([Ticket])
  35. gql.execute(query, variables: variables)
  36. end
  37. context 'with an agent' do
  38. context 'without limit' do
  39. it 'finds all tickets except the source ticket' do
  40. expect(gql.result.data.length).to eq(tickets.length - 1)
  41. end
  42. context 'with merged tickets' do
  43. let(:state) { Ticket::State.find_by(name: 'merged') }
  44. it 'does not find merged tickets' do
  45. expect(gql.result.data).to be_empty
  46. end
  47. end
  48. end
  49. context 'without change permission' do
  50. let(:agent) { create(:agent, groups: [group], group_names_access_map: { group.name => 'read' }) }
  51. it 'finds no tickets' do
  52. expect(gql.result.data).to be_empty
  53. end
  54. end
  55. context 'with limit' do
  56. let(:limit) { 1 }
  57. it 'respects the limit' do
  58. expect(gql.result.data.length).to eq(limit)
  59. end
  60. end
  61. context 'with exact search' do
  62. let(:second_ticket_payload) do
  63. {
  64. 'value' => gql.id(tickets.second),
  65. 'label' => "#{Setting.get('ticket_hook')}#{tickets.second.number} - #{tickets.second.title}",
  66. 'heading' => tickets.second.customer.fullname,
  67. 'ticket' => { 'number' => tickets.second.number },
  68. }
  69. end
  70. let(:query_string) { tickets.second.number }
  71. it 'has data' do
  72. expect(gql.result.data).to eq([second_ticket_payload])
  73. end
  74. end
  75. context 'when sending an empty search string' do
  76. let(:query_string) { ' ' }
  77. it 'still returns tickets, but not the source ticket' do
  78. expect(gql.result.data.length).to eq(tickets.length - 1)
  79. end
  80. end
  81. end
  82. context 'with a customer', authenticated_as: :customer do
  83. let(:customer) { create(:customer) }
  84. it 'raises authorization error' do
  85. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  86. end
  87. end
  88. it_behaves_like 'graphql responds with error if unauthenticated'
  89. end
  90. end