123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
- require 'rails_helper'
- RSpec.describe 'KnowledgeBase feed', authenticated_as: :user, type: :request do
- include_context 'basic Knowledge Base'
- let(:user) { create(:admin) }
- before do
- published_answer
- travel 1.minute
- published_answer_in_other_category
- end
- describe '#root' do
- before do
- get feed_knowledge_base_path(knowledge_base, locale_name)
- end
- it 'lists entries' do
- answer_index = response.body.index published_answer.translations.first.title
- answer_index2 = response.body.index published_answer_in_other_category.translations.first.title
- expect(answer_index > answer_index2).to be_truthy
- end
- it 'uses KB title' do
- expect(response.body).to include(knowledge_base.translations.first.title)
- end
- end
- describe '#category' do
- before do
- get feed_knowledge_base_category_path(knowledge_base, category, locale_name)
- end
- it 'lists entries', :aggregate_failures do
- expect(response.body).to include(published_answer.translations.first.title)
- expect(response.body).not_to include(published_answer_in_other_category.translations.first.title)
- end
- it 'uses category title' do
- expect(response.body).to include(category.translations.first.title)
- end
- end
- context 'with complex answers' do
- it 'sanitizes bodies', :aggregate_failures do
- published_answer_with_video
- published_answer_with_image
- get feed_knowledge_base_path(knowledge_base, locale_name)
- expect(response.body).not_to include('img')
- expect(response.body).not_to include('widget:')
- end
- end
- context 'with no answers' do
- before do
- Ticket.destroy_all
- end
- it 'loads' do
- get feed_knowledge_base_path(knowledge_base, locale_name)
- expect(response).to have_http_status :ok
- end
- end
- context 'with granular permissions', :aggregate_failures do
- before do
- admin = create(:admin)
- internal_answer
- published_answer
- KnowledgeBase::PermissionsUpdate.new(category, admin).update_using_params!(granular_permissions)
- end
- let(:role_admin) { Role.find_by(name: 'Admin') }
- let(:role_agent) { Role.find_by(name: 'Agent') }
- let(:granular_permissions) do
- {
- permissions: {
- role_admin.id => 'editor',
- role_agent.id => 'none'
- }
- }
- end
- context 'with admin user' do
- let(:user) { create(:admin) }
- it 'shows answer in root' do
- get feed_knowledge_base_path(knowledge_base, locale_name)
- expect(response).to have_http_status :ok
- expect(response.body).to include internal_answer.translations.first.title
- expect(response.body).to include published_answer.translations.first.title
- end
- it 'shows answer in category' do
- get feed_knowledge_base_category_path(knowledge_base, category, locale_name)
- expect(response).to have_http_status :ok
- expect(response.body).to include internal_answer.translations.first.title
- expect(response.body).to include published_answer.translations.first.title
- end
- context 'with a token', authenticated_as: false do
- let(:token) { Token.ensure_token! 'KnowledgeBaseFeed', user.id }
- it 'shows answer in root' do
- get feed_knowledge_base_path(knowledge_base, locale_name, token: token)
- expect(response).to have_http_status :ok
- expect(response.body).to include internal_answer.translations.first.title
- expect(response.body).to include published_answer.translations.first.title
- end
- it 'shows answer in category' do
- get feed_knowledge_base_category_path(knowledge_base, category, locale_name, token: token)
- expect(response).to have_http_status :ok
- expect(response.body).to include internal_answer.translations.first.title
- expect(response.body).to include published_answer.translations.first.title
- end
- end
- end
- context 'with agent user' do
- let(:user) { create(:agent) }
- let(:token) { Token.ensure_token! 'KnowledgeBaseFeed', user.id }
- it 'does not show answer in root' do
- get feed_knowledge_base_path(knowledge_base, locale_name)
- expect(response).to have_http_status :ok
- expect(response.body).not_to include internal_answer.translations.first.title
- expect(response.body).to include published_answer.translations.first.title
- end
- it 'shows answer in category' do
- get feed_knowledge_base_category_path(knowledge_base, category, locale_name)
- expect(response).to have_http_status :ok
- expect(response.body).not_to include internal_answer.translations.first.title
- expect(response.body).to include published_answer.translations.first.title
- end
- context 'with a token', authenticated_as: false do
- it 'does not show answer in root' do
- get feed_knowledge_base_path(knowledge_base, locale_name, token: token)
- expect(response).to have_http_status :ok
- expect(response.body).not_to include internal_answer.translations.first.title
- expect(response.body).to include published_answer.translations.first.title
- end
- it 'shows answer in category' do
- get feed_knowledge_base_category_path(knowledge_base, category, locale_name, token: token)
- expect(response).to have_http_status :ok
- expect(response.body).not_to include internal_answer.translations.first.title
- expect(response.body).to include published_answer.translations.first.title
- end
- end
- end
- end
- end
|