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('foo') doc2 = Nokogiri::HTML. fragment('bar') expect(redactor).to receive(:nodes_visible_to_user).and_return([]) 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([0, 0]) expect(doc1.to_html).to eq('foo') expect(doc2.to_html).to eq('bar') end it 'does not redact an Array of documents' do doc1_html = 'foo' doc1 = Nokogiri::HTML.fragment(doc1_html) doc2_html = 'bar' 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 end describe '#redact_nodes' do it 'redacts an Array of nodes' do doc = Nokogiri::HTML.fragment('foo') node = doc.children[0] expect(redactor).to receive(:nodes_visible_to_user). with([node]). and_return(Set.new) redactor.redact_document_nodes([{ document: doc, 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('') 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