BigW Consortium Gitlab

redactor_spec.rb 2.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
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([])

18
      redacted_data = redactor.redact([doc1, doc2])
19

20 21
      expect(redacted_data.map { |data| data[:document] }).to eq([doc1, doc2])
      expect(redacted_data.map { |data| data[:visible_reference_count] }).to eq([0, 0])
22 23 24
      expect(doc1.to_html).to eq('foo')
      expect(doc2.to_html).to eq('bar')
    end
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

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

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

      nodes = redactor.document_nodes([doc1, doc2]).map { |x| x[:nodes] }
      expect(redactor).to receive(:nodes_visible_to_user).and_return(nodes.flatten)

      redacted_data = redactor.redact([doc1, doc2])

      expect(redacted_data.map { |data| data[:document] }).to eq([doc1, doc2])
      expect(redacted_data.map { |data| data[:visible_reference_count] }).to eq([1, 1])
      expect(doc1.to_html).to eq(doc1_html)
      expect(doc2.to_html).to eq(doc2_html)
    end
43 44 45 46 47 48 49 50 51 52 53
  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)

54
      redactor.redact_document_nodes([{ document: doc, nodes: [node] }])
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

      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