suggestions_spec.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # Copyright (C) 2012-2024 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, 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 'without limit' do
  46. it 'finds all text modules with permission' do
  47. expect(gql.result.data.length).to eq(3)
  48. end
  49. end
  50. context 'with inactive text modules' do
  51. let(:limit) do
  52. text_modules.each do |tm|
  53. tm.active = false
  54. tm.save!
  55. end
  56. nil
  57. end
  58. it 'finds none' do
  59. expect(gql.result.data.length).to eq(0)
  60. end
  61. end
  62. context 'with limit' do
  63. let(:limit) { 1 }
  64. it 'respects the limit' do
  65. expect(gql.result.data.length).to eq(limit)
  66. end
  67. end
  68. context 'with exact search' do
  69. context 'with a ticket present' do
  70. let(:first_text_module_payload) do
  71. {
  72. 'name' => text_modules.first.name,
  73. 'keywords' => text_modules.first.keywords,
  74. 'content' => text_modules.first.content,
  75. 'renderedContent' => "t:#{ticket.customer.fullname}-c:#{customer.fullname}-u:#{user.fullname}-g:#{group.name}-o:#{organization.name}-m:-",
  76. }
  77. end
  78. let(:query_string) { text_modules.first.name }
  79. it 'has data' do
  80. expect(gql.result.data).to eq([first_text_module_payload])
  81. end
  82. end
  83. context 'without a ticket, but with a customer present' do
  84. 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) } }
  85. let(:first_text_module_payload) do
  86. {
  87. 'name' => text_modules.first.name,
  88. 'keywords' => text_modules.first.keywords,
  89. 'content' => text_modules.first.content,
  90. 'renderedContent' => "t:#{customer.fullname}-c:#{customer.fullname}-u:#{user.fullname}-g:#{group.name}-o:#{organization.name}-m:-",
  91. }
  92. end
  93. let(:query_string) { text_modules.first.name }
  94. it 'has data' do
  95. expect(gql.result.data).to eq([first_text_module_payload])
  96. end
  97. end
  98. end
  99. context 'when sending an empty search string' do
  100. let(:query_string) { ' ' }
  101. let(:limit) { 3 }
  102. it 'still returns text modules' do
  103. expect(gql.result.data.length).to eq(3)
  104. end
  105. end
  106. it_behaves_like 'graphql responds with error if unauthenticated'
  107. end
  108. end