search_spec.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Queries::Search, type: :graphql do
  4. context 'when performing generic searches' do
  5. let(:group) { create(:group) }
  6. let(:organization) { create(:organization, name: search) }
  7. let(:agent) { create(:agent, firstname: search, groups: [ticket.group]) }
  8. let!(:ticket) do
  9. create(:ticket, title: search, organization: organization).tap do |ticket|
  10. # Article required to find ticket via SQL
  11. create(:ticket_article, ticket: ticket)
  12. end
  13. end
  14. let(:search) { SecureRandom.uuid }
  15. let(:query) do
  16. <<~QUERY
  17. query search($search: String!, $onlyIn: EnumSearchableModels) {
  18. search(search: $search, onlyIn: $onlyIn) {
  19. ... on Ticket {
  20. __typename
  21. number
  22. title
  23. }
  24. ... on User {
  25. __typename
  26. firstname
  27. lastname
  28. }
  29. ... on Organization {
  30. __typename
  31. name
  32. }
  33. }
  34. }
  35. QUERY
  36. end
  37. let(:variables) { { search: search } }
  38. let(:es_setup) do
  39. Setting.set('es_url', nil)
  40. end
  41. before do
  42. es_setup
  43. gql.execute(query, variables: variables)
  44. end
  45. shared_examples 'test search query' do
  46. context 'with an agent', authenticated_as: :agent do
  47. context 'without model limit' do
  48. let(:expected_result) do
  49. [
  50. { '__typename' => 'Ticket', 'number' => ticket.number, 'title' => ticket.title },
  51. { '__typename' => 'User', 'firstname' => agent.firstname, 'lastname' => agent.lastname },
  52. { '__typename' => 'Organization', 'name' => organization.name },
  53. ]
  54. end
  55. it 'finds expected objects across models' do
  56. expect(gql.result.data).to eq(expected_result)
  57. end
  58. end
  59. context 'with model restriction' do
  60. let(:variables) { { search: search, onlyIn: 'User' } }
  61. let(:expected_result) do
  62. [
  63. { '__typename' => 'User', 'firstname' => agent.firstname, 'lastname' => agent.lastname },
  64. ]
  65. end
  66. it 'finds expected objects only from selected model' do
  67. expect(gql.result.data).to eq(expected_result)
  68. end
  69. end
  70. end
  71. context 'with a customer', authenticated_as: :customer do
  72. let(:customer) { create(:customer, firstname: search, organization: organization) }
  73. let(:expected_result) do
  74. [
  75. { '__typename' => 'Organization', 'name' => organization.name },
  76. ]
  77. end
  78. it 'finds only objects available to the customer' do
  79. expect(gql.result.data).to eq(expected_result)
  80. end
  81. end
  82. end
  83. context 'without search index' do
  84. include_examples 'test search query'
  85. end
  86. context 'with search index', searchindex: true do
  87. let(:es_setup) do
  88. searchindex_model_reload([Ticket, User, Organization])
  89. end
  90. include_examples 'test search query'
  91. end
  92. it_behaves_like 'graphql responds with error if unauthenticated'
  93. end
  94. end