suggestions_spec.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Queries::TextModule::Suggestions, authenticated_as: :agent, type: :graphql do
  4. context 'when searching for text modules' do
  5. let(:groups) { create_list(:group, 2) }
  6. let(:agent) { create(:agent, groups: groups) }
  7. let(:ticket) { create(:ticket, group: groups.first) }
  8. let(:customer) { create(:customer) }
  9. let(:group) { groups.first }
  10. let(:organization) { create(:organization) }
  11. let(:user) { create(:user) }
  12. let!(:text_modules) do
  13. create_list(:text_module, 4).each_with_index do |tm, i|
  14. tm.name = "TextModuleTest#{i}"
  15. tm.keywords = "KeywordTextModuleTest#{i}"
  16. tm.content = 't:#{ticket.customer.fullname}-c:#{customer.fullname}-u:#{user.fullname}-g:#{group.name}-o:#{organization.name}-m:#{missing.nonexisting}' # rubocop:disable Lint/InterpolationCheck
  17. tm.groups = if i <= 2
  18. groups
  19. elsif i == 3
  20. create_list(:group, 1)
  21. else
  22. []
  23. end
  24. tm.save!
  25. end
  26. end
  27. let(:query) do
  28. <<~QUERY
  29. query textModuleSuggestions($query: String!, $limit: Int, $ticketId: ID, $customerId: ID, $userId: ID, $groupId: ID, $organizationId: ID) {
  30. textModuleSuggestions(query: $query, ticketId: $ticketId, limit: $limit) {
  31. name
  32. keywords
  33. content
  34. renderedContent(templateRenderContext: { ticketId: $ticketId, customerId: $customerId, userId: $userId, groupId: $groupId, organizationId: $organizationId })
  35. }
  36. }
  37. QUERY
  38. end
  39. let(:variables) { { query: query_string, limit: limit, ticketId: gql.id(ticket), customerId: gql.id(customer), userId: gql.id(user), groupId: gql.id(group), organizationId: gql.id(organization) } }
  40. let(:query_string) { 'TextModuleTest' }
  41. let(:limit) { nil }
  42. before do
  43. gql.execute(query, variables: variables)
  44. end
  45. context 'with a ticket' do
  46. it 'calls text modules scope wih read context' do
  47. expect_any_instance_of(TextModulePolicy::Scope)
  48. .to receive(:resolve)
  49. .with(context: :read)
  50. .and_call_original
  51. gql.execute(query, variables: variables)
  52. end
  53. end
  54. context 'without a ticket' do
  55. let(:variables) { { query: query_string, limit: limit, customerId: gql.id(customer), userId: gql.id(user), groupId: gql.id(group), organizationId: gql.id(organization) } }
  56. it 'calls text modules scope wih create context' do
  57. expect_any_instance_of(TextModulePolicy::Scope)
  58. .to receive(:resolve)
  59. .with(context: :create)
  60. .and_call_original
  61. gql.execute(query, variables: variables)
  62. end
  63. end
  64. context 'without limit' do
  65. it 'finds all text modules with permission' do
  66. expect(gql.result.data.length).to eq(3)
  67. end
  68. end
  69. context 'with limit' do
  70. let(:limit) { 1 }
  71. it 'respects the limit' do
  72. expect(gql.result.data.length).to eq(limit)
  73. end
  74. end
  75. context 'with exact search' do
  76. context 'with a ticket present' do
  77. let(:first_text_module_payload) do
  78. {
  79. 'name' => text_modules.first.name,
  80. 'keywords' => text_modules.first.keywords,
  81. 'content' => text_modules.first.content,
  82. 'renderedContent' => "t:#{ticket.customer.fullname}-c:#{customer.fullname}-u:#{user.fullname}-g:#{group.name}-o:#{organization.name}-m:-",
  83. }
  84. end
  85. let(:query_string) { text_modules.first.name }
  86. it 'has data' do
  87. expect(gql.result.data).to eq([first_text_module_payload])
  88. end
  89. end
  90. context 'without a ticket, but with a customer present' do
  91. let(:variables) { { query: query_string, limit: limit, ticketId: nil, customerId: gql.id(customer), userId: gql.id(user), groupId: gql.id(group), organizationId: gql.id(organization) } }
  92. let(:first_text_module_payload) do
  93. {
  94. 'name' => text_modules.first.name,
  95. 'keywords' => text_modules.first.keywords,
  96. 'content' => text_modules.first.content,
  97. 'renderedContent' => "t:#{customer.fullname}-c:#{customer.fullname}-u:#{user.fullname}-g:#{group.name}-o:#{organization.name}-m:-",
  98. }
  99. end
  100. let(:query_string) { text_modules.first.name }
  101. it 'has data' do
  102. expect(gql.result.data).to eq([first_text_module_payload])
  103. end
  104. end
  105. end
  106. context 'when sending an empty search string' do
  107. let(:query_string) { ' ' }
  108. let(:limit) { 3 }
  109. it 'still returns text modules' do
  110. expect(gql.result.data.length).to eq(3)
  111. end
  112. end
  113. it_behaves_like 'graphql responds with error if unauthenticated'
  114. end
  115. end