answers_spec.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'KnowledgeBase public answers', type: :request do
  4. include_context 'basic Knowledge Base'
  5. before do
  6. # Skip asset generation.
  7. allow_any_instance_of(ActionView::Base).to receive(:compute_asset_path).and_return('')
  8. end
  9. describe '#show' do
  10. context 'when visitor is a guest' do
  11. it 'returns OK for published answer' do
  12. get help_answer_path(locale_name, category, published_answer)
  13. expect(response).to have_http_status :ok
  14. end
  15. it 'returns NOT FOUND for draft answer' do
  16. get help_answer_path(locale_name, category, draft_answer)
  17. expect(response).to have_http_status :not_found
  18. end
  19. end
  20. context 'when visitor is an editor' do
  21. before do
  22. published_answer && draft_answer
  23. authenticated_as(create(:admin), via: :browser)
  24. end
  25. it 'returns OK for published answer' do
  26. get help_answer_path(locale_name, category, published_answer)
  27. expect(response).to have_http_status :ok
  28. end
  29. it 'returns OK for draft answer' do
  30. get help_answer_path(locale_name, category, draft_answer)
  31. expect(response).to have_http_status :ok
  32. end
  33. end
  34. context 'when knowledge base is inactive' do
  35. before do
  36. knowledge_base.update! active: false
  37. end
  38. # https://github.com/zammad/zammad/issues/3888
  39. it 'returns route not found error' do
  40. get help_answer_path(locale_name, category, published_answer)
  41. expect(response.body).to include('No route matches')
  42. end
  43. end
  44. end
  45. describe '#render_alternative' do
  46. context 'when a translation is available' do
  47. before { create(:knowledge_base_translation, kb_locale: alternative_locale) }
  48. it 'returns OK for published answer' do
  49. get help_answer_path(alternative_locale.system_locale.locale, category, published_answer)
  50. expect(response).to have_http_status :ok
  51. end
  52. it 'returns NOT FOUND for draft answer' do
  53. get help_answer_path(alternative_locale.system_locale.locale, category, draft_answer)
  54. expect(response).to have_http_status :not_found
  55. end
  56. # https://github.com/zammad/zammad/issues/3931
  57. context 'when the category has been updated' do
  58. let(:new_category) { create(:knowledge_base_category, knowledge_base: knowledge_base) }
  59. it 'returns NOT FOUND for published answer if old category is used' do
  60. published_answer.update! category_id: new_category.id
  61. get help_answer_path(alternative_locale.system_locale.locale, category, published_answer)
  62. expect(response).to have_http_status :not_found
  63. end
  64. it 'returns OK for published answer if new category is used' do
  65. published_answer.update! category_id: new_category.id
  66. get help_answer_path(alternative_locale.system_locale.locale, new_category, published_answer)
  67. expect(response).to have_http_status :ok
  68. end
  69. end
  70. end
  71. end
  72. end