BigW Consortium Gitlab

redactor_spec.rb 1.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
require 'spec_helper'

describe Banzai::Redactor do
  let(:user) { build(:user) }
  let(:project) { build(:empty_project) }
  let(:redactor) { described_class.new(project, user) }

  describe '#redact' do
    it 'redacts an Array of documents' do
      doc1 = Nokogiri::HTML.
        fragment('<a class="gfm" data-reference-type="issue">foo</a>')

      doc2 = Nokogiri::HTML.
        fragment('<a class="gfm" data-reference-type="issue">bar</a>')

      expect(redactor).to receive(:nodes_visible_to_user).and_return([])

      expect(redactor.redact([doc1, doc2])).to eq([doc1, doc2])

      expect(doc1.to_html).to eq('foo')
      expect(doc2.to_html).to eq('bar')
    end
  end

  describe '#redact_nodes' do
    it 'redacts an Array of nodes' do
      doc = Nokogiri::HTML.fragment('<a href="foo">foo</a>')
      node = doc.children[0]

      expect(redactor).to receive(:nodes_visible_to_user).
        with([node]).
        and_return(Set.new)

      redactor.redact_nodes([node])

      expect(doc.to_html).to eq('foo')
    end
  end

  describe '#nodes_visible_to_user' do
    it 'returns a Set containing the visible nodes' do
      doc = Nokogiri::HTML.fragment('<a data-reference-type="issue"></a>')
      node = doc.children[0]

      expect_any_instance_of(Banzai::ReferenceParser::IssueParser).
        to receive(:nodes_visible_to_user).
        with(user, [node]).
        and_return([node])

      expect(redactor.nodes_visible_to_user([node])).to eq(Set.new([node]))
    end
  end
end