feed_spec.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'KnowledgeBase feed', authenticated_as: :user, type: :request do
  4. include_context 'basic Knowledge Base'
  5. let(:user) { create(:admin) }
  6. before do
  7. published_answer
  8. travel 1.minute
  9. published_answer_in_other_category
  10. end
  11. describe '#root' do
  12. before do
  13. get feed_knowledge_base_path(knowledge_base, locale_name)
  14. end
  15. it 'lists entries' do
  16. answer_index = response.body.index published_answer.translations.first.title
  17. answer_index2 = response.body.index published_answer_in_other_category.translations.first.title
  18. expect(answer_index > answer_index2).to be_truthy
  19. end
  20. it 'uses KB title' do
  21. expect(response.body).to include(knowledge_base.translations.first.title)
  22. end
  23. end
  24. describe '#category' do
  25. before do
  26. get feed_knowledge_base_category_path(knowledge_base, category, locale_name)
  27. end
  28. it 'lists entries', :aggregate_failures do
  29. expect(response.body).to include(published_answer.translations.first.title)
  30. expect(response.body).not_to include(published_answer_in_other_category.translations.first.title)
  31. end
  32. it 'uses category title' do
  33. expect(response.body).to include(category.translations.first.title)
  34. end
  35. end
  36. context 'with complex answers' do
  37. it 'sanitizes bodies', :aggregate_failures do
  38. published_answer_with_video
  39. published_answer_with_image
  40. get feed_knowledge_base_path(knowledge_base, locale_name)
  41. expect(response.body).not_to include('img')
  42. expect(response.body).not_to include('widget:')
  43. end
  44. end
  45. context 'with no answers' do
  46. before do
  47. Ticket.destroy_all
  48. end
  49. it 'loads' do
  50. get feed_knowledge_base_path(knowledge_base, locale_name)
  51. expect(response).to have_http_status :ok
  52. end
  53. end
  54. context 'with granular permissions', :aggregate_failures do
  55. before do
  56. admin = create(:admin)
  57. internal_answer
  58. published_answer
  59. KnowledgeBase::PermissionsUpdate.new(category, admin).update_using_params!(granular_permissions)
  60. end
  61. let(:role_admin) { Role.find_by(name: 'Admin') }
  62. let(:role_agent) { Role.find_by(name: 'Agent') }
  63. let(:granular_permissions) do
  64. {
  65. permissions: {
  66. role_admin.id => 'editor',
  67. role_agent.id => 'none'
  68. }
  69. }
  70. end
  71. context 'with admin user' do
  72. let(:user) { create(:admin) }
  73. it 'shows answer in root' do
  74. get feed_knowledge_base_path(knowledge_base, locale_name)
  75. expect(response).to have_http_status :ok
  76. expect(response.body).to include internal_answer.translations.first.title
  77. expect(response.body).to include published_answer.translations.first.title
  78. end
  79. it 'shows answer in category' do
  80. get feed_knowledge_base_category_path(knowledge_base, category, locale_name)
  81. expect(response).to have_http_status :ok
  82. expect(response.body).to include internal_answer.translations.first.title
  83. expect(response.body).to include published_answer.translations.first.title
  84. end
  85. context 'with a token', authenticated_as: false do
  86. let(:token) { Token.ensure_token! 'KnowledgeBaseFeed', user.id }
  87. it 'shows answer in root' do
  88. get feed_knowledge_base_path(knowledge_base, locale_name, token: token)
  89. expect(response).to have_http_status :ok
  90. expect(response.body).to include internal_answer.translations.first.title
  91. expect(response.body).to include published_answer.translations.first.title
  92. end
  93. it 'shows answer in category' do
  94. get feed_knowledge_base_category_path(knowledge_base, category, locale_name, token: token)
  95. expect(response).to have_http_status :ok
  96. expect(response.body).to include internal_answer.translations.first.title
  97. expect(response.body).to include published_answer.translations.first.title
  98. end
  99. end
  100. end
  101. context 'with agent user' do
  102. let(:user) { create(:agent) }
  103. let(:token) { Token.ensure_token! 'KnowledgeBaseFeed', user.id }
  104. it 'does not show answer in root' do
  105. get feed_knowledge_base_path(knowledge_base, locale_name)
  106. expect(response).to have_http_status :ok
  107. expect(response.body).not_to include internal_answer.translations.first.title
  108. expect(response.body).to include published_answer.translations.first.title
  109. end
  110. it 'shows answer in category' do
  111. get feed_knowledge_base_category_path(knowledge_base, category, locale_name)
  112. expect(response).to have_http_status :ok
  113. expect(response.body).not_to include internal_answer.translations.first.title
  114. expect(response.body).to include published_answer.translations.first.title
  115. end
  116. context 'with a token', authenticated_as: false do
  117. it 'does not show answer in root' do
  118. get feed_knowledge_base_path(knowledge_base, locale_name, token: token)
  119. expect(response).to have_http_status :ok
  120. expect(response.body).not_to include internal_answer.translations.first.title
  121. expect(response.body).to include published_answer.translations.first.title
  122. end
  123. it 'shows answer in category' do
  124. get feed_knowledge_base_category_path(knowledge_base, category, locale_name, token: token)
  125. expect(response).to have_http_status :ok
  126. expect(response.body).not_to include internal_answer.translations.first.title
  127. expect(response.body).to include published_answer.translations.first.title
  128. end
  129. end
  130. end
  131. end
  132. end