suggestions_spec.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Queries::KnowledgeBase::Answer::Suggestions, current_user_id: 1, type: :graphql do
  4. include KnowledgeBaseRichTextHelper
  5. context 'when searching' do
  6. let(:agent) { create(:agent) }
  7. let(:customer) { create(:customer) }
  8. let(:knowledge_base_answer) { create(:knowledge_base_answer, :published, :with_video, :with_image, :with_attachment) }
  9. let(:knowledge_base_answer_translation) { knowledge_base_answer.translation }
  10. let(:query) do
  11. <<~QUERY
  12. query knowledgeBaseAnswerSuggestions($query: String!) {
  13. knowledgeBaseAnswerSuggestions(query: $query) {
  14. id
  15. title
  16. categoryTreeTranslation {
  17. id
  18. title
  19. }
  20. content {
  21. body
  22. bodyPrepared
  23. hasAttachments
  24. }
  25. }
  26. }
  27. QUERY
  28. end
  29. let(:variables) { { query: search_query } }
  30. before do
  31. gql.execute(query, variables: variables)
  32. end
  33. context 'with authenticated session', authenticated_as: :agent do
  34. let(:search_query) { knowledge_base_answer_translation.title[0..2] }
  35. let(:category_translation) { knowledge_base_answer.category.translation_preferred(knowledge_base_answer_translation.kb_locale) }
  36. let(:expected_result) do
  37. [
  38. {
  39. 'id' => Gql::ZammadSchema.id_from_object(knowledge_base_answer_translation),
  40. 'title' => knowledge_base_answer_translation.title,
  41. 'categoryTreeTranslation' => [
  42. {
  43. 'id' => Gql::ZammadSchema.id_from_object(category_translation),
  44. 'title' => category_translation.title,
  45. },
  46. ],
  47. 'content' => {
  48. 'body' => knowledge_base_answer_translation.content.body,
  49. 'bodyPrepared' => prepare_rich_text(knowledge_base_answer_translation.content.body_with_urls),
  50. 'hasAttachments' => true,
  51. },
  52. }
  53. ]
  54. end
  55. it 'has data' do
  56. expect(gql.result.data).to eq(expected_result)
  57. end
  58. context 'with no results' do
  59. let(:search_query) { 'foo' }
  60. it 'has no data' do
  61. expect(gql.result.data).to be_empty
  62. end
  63. end
  64. end
  65. context 'with a customer', authenticated_as: :customer do
  66. let(:search_query) { knowledge_base_answer_translation.title[0..2] }
  67. it_behaves_like 'graphql responds with error if unauthenticated'
  68. end
  69. end
  70. end