translation_spec.rb 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. require 'rails_helper'
  2. require 'models/contexts/factory_context'
  3. RSpec.describe KnowledgeBase::Answer::Translation, type: :model, current_user_id: 1 do
  4. subject { create(:knowledge_base_answer_translation) }
  5. include_context 'factory'
  6. it { is_expected.to validate_presence_of(:title) }
  7. it { is_expected.to validate_uniqueness_of(:kb_locale_id).scoped_to(:answer_id).with_message(//) }
  8. it { is_expected.to belong_to(:answer) }
  9. it { is_expected.to belong_to(:kb_locale) }
  10. describe '.search' do
  11. include_context 'basic Knowledge Base'
  12. shared_examples 'verify given search backend' do |trait:, user_id:, is_visible:, elasticsearch:|
  13. prefix = is_visible ? 'lists' : 'does not list'
  14. it "#{prefix} #{trait} answer to #{user_id} when ES=#{elasticsearch}", searchindex: elasticsearch do
  15. user = create(user_id)
  16. object = create(:knowledge_base_answer, trait, knowledge_base: knowledge_base)
  17. configure_elasticsearch(required: true, rebuild: true) if elasticsearch
  18. expect(described_class.search({ query: object.translations.first.title, current_user: user }))
  19. .to is_visible ? be_present : be_blank
  20. end
  21. end
  22. shared_examples 'verify given user' do |trait:, user_id:, is_visible:|
  23. include_examples 'verify given search backend', trait: trait, user_id: user_id, is_visible: is_visible, elasticsearch: true
  24. include_examples 'verify given search backend', trait: trait, user_id: user_id, is_visible: is_visible, elasticsearch: false
  25. end
  26. shared_examples 'verify given permissions' do |trait:, admin:, agent:, customer:|
  27. include_examples 'verify given user', trait: trait, user_id: :admin, is_visible: admin
  28. include_examples 'verify given user', trait: trait, user_id: :agent, is_visible: agent
  29. include_examples 'verify given user', trait: trait, user_id: :customer, is_visible: customer
  30. end
  31. include_examples 'verify given permissions', trait: :published, admin: true, agent: true, customer: false
  32. include_examples 'verify given permissions', trait: :internal, admin: true, agent: true, customer: false
  33. include_examples 'verify given permissions', trait: :draft, admin: true, agent: false, customer: false
  34. include_examples 'verify given permissions', trait: :archived, admin: true, agent: false, customer: false
  35. end
  36. end