knowledge_base_spec.rb 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. require 'rails_helper'
  2. require 'models/concerns/checks_kb_client_notification_examples'
  3. require 'models/contexts/factory_context'
  4. RSpec.describe KnowledgeBase, type: :model do
  5. subject(:knowledge_base) { create(:knowledge_base) }
  6. # make sure there's no KBs from seed data
  7. before { described_class.all.each(&:full_destroy!) }
  8. include_context 'factory'
  9. it_behaves_like 'ChecksKbClientNotification'
  10. it { is_expected.to validate_presence_of(:color_highlight) }
  11. it { is_expected.to validate_presence_of(:color_header) }
  12. it { is_expected.to validate_presence_of(:iconset).with_message(//) }
  13. it { is_expected.to validate_inclusion_of(:iconset).in_array(KnowledgeBase::ICONSETS) }
  14. it { is_expected.to validate_inclusion_of(:category_layout).in_array(KnowledgeBase::LAYOUTS) }
  15. it { is_expected.to validate_inclusion_of(:homepage_layout).in_array(KnowledgeBase::LAYOUTS) }
  16. context 'activation' do
  17. it 'on by default' do
  18. expect(knowledge_base).to be_active
  19. end
  20. it 'switcing off changes kb_active setting to false' do
  21. knowledge_base # trigger KB creation to set initial setting value
  22. expect { knowledge_base.update(active: false) }.to change { Setting.get('kb_active') }.from(true).to(false)
  23. end
  24. context 'with inactive' do
  25. let!(:knowledge_base_inactive) { create(:knowledge_base, active: false) }
  26. it 'switching on changes kb_active setting to true' do
  27. expect { knowledge_base_inactive.update(active: true) }.to change { Setting.get('kb_active') }.from(false).to(true)
  28. end
  29. context 'including active' do
  30. before { knowledge_base }
  31. it 'ensure 2 knowledge bases are created' do
  32. expect(described_class.count).to eq(2)
  33. end
  34. it 'filter by activity' do
  35. expect(described_class.active).to contain_exactly(knowledge_base)
  36. end
  37. it 'skip activity check for editors when filtering by activity' do
  38. user = create(:admin_user)
  39. expect(described_class.check_active_unless_editor(user).count).to eq(2)
  40. end
  41. it 'check activity if user is not editor when filtering by activity' do
  42. user = create(:agent_user)
  43. expect(described_class.check_active_unless_editor(user)).to contain_exactly(knowledge_base)
  44. end
  45. it 'skip activity check for guests when filtering by activity' do
  46. expect(described_class.check_active_unless_editor(nil)).to contain_exactly(knowledge_base)
  47. end
  48. end
  49. end
  50. end
  51. context 'acceptable colors' do
  52. let(:allowed_values) { ['#aaa', '#ff0000', 'rgb(0,100,100)', 'hsl(0,100%,50%)'] }
  53. let(:not_allowed_values) { ['aaa', '#aa', '#ff000', 'rgb(0,100,100', 'def(0,100%,0.5)', 'test'] }
  54. it { is_expected.to allow_values(*allowed_values).for(:color_header) }
  55. it { is_expected.to allow_values(*allowed_values).for(:color_highlight) }
  56. it { is_expected.not_to allow_values(*not_allowed_values).for(:color_header) }
  57. it { is_expected.not_to allow_values(*not_allowed_values).for(:color_highlight) }
  58. end
  59. end