knowledge_base_spec.rb 2.4 KB

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