From e7d0f9a8b350ffe3c17451d6bb18051c7230ca61 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Wed, 2 Apr 2014 12:11:32 +0100 Subject: Simpler AttachmentToHTML::Adapters::PDF interface TODO: We really should be testing the full output of PDF#body, but inconsistencies between pdftohtml prevent sensible means of doing this. For example: adapter.body.should == %Q(\nthisisthebody
\n
\n) Fails because some versions (correctly!) use lower case tag names. --- spec/lib/attachment_to_html/adapters/pdf_spec.rb | 77 +++++++----------------- 1 file changed, 21 insertions(+), 56 deletions(-) (limited to 'spec/lib/attachment_to_html') diff --git a/spec/lib/attachment_to_html/adapters/pdf_spec.rb b/spec/lib/attachment_to_html/adapters/pdf_spec.rb index 65c376043..c02b157e4 100644 --- a/spec/lib/attachment_to_html/adapters/pdf_spec.rb +++ b/spec/lib/attachment_to_html/adapters/pdf_spec.rb @@ -3,94 +3,59 @@ require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') describe AttachmentToHTML::Adapters::PDF do let(:attachment) { FactoryGirl.build(:pdf_attachment) } - let(:pdf_adapter) { AttachmentToHTML::Adapters::PDF.new(attachment) } - - describe :wrapper do - - it 'defaults to wrapper' do - pdf_adapter.wrapper.should == 'wrapper' - end - - it 'accepts a wrapper option' do - pdf_adapter = AttachmentToHTML::Adapters::PDF.new(attachment, :wrapper => 'wrap') - pdf_adapter.wrapper.should == 'wrap' - end - - end + let(:adapter) { AttachmentToHTML::Adapters::PDF.new(attachment) } describe :tmpdir do it 'defaults to the rails tmp directory' do - pdf_adapter.tmpdir.should == Rails.root.join('tmp') + adapter.tmpdir.should == Rails.root.join('tmp') end it 'allows a tmpdir to be specified to store the converted document' do - pdf_adapter = AttachmentToHTML::Adapters::PDF.new(attachment, :tmpdir => '/tmp') - pdf_adapter.tmpdir.should == '/tmp' + adapter = AttachmentToHTML::Adapters::PDF.new(attachment, :tmpdir => '/tmp') + adapter.tmpdir.should == '/tmp' end end - describe :to_html do + describe :title do - it 'should be a valid html document' do - parsed = Nokogiri::HTML.parse(pdf_adapter.to_html) do |config| - config.strict - end - parsed.errors.any?.should be_false - end - - it 'contains the attachment filename in the title tag' do - parsed = Nokogiri::HTML.parse(pdf_adapter.to_html) do |config| - config.strict - end - parsed.css('title').inner_html.should == attachment.display_filename + it 'uses the attachment filename for the title' do + adapter.title.should == attachment.display_filename end + + end - it 'contains the wrapper div in the body tag' do - pdf_adapter = AttachmentToHTML::Adapters::PDF.new(attachment, :wrapper => 'wrap') - parsed = Nokogiri::HTML.parse(pdf_adapter.to_html) do |config| - config.strict - end - parsed.css('body div').first.attributes['id'].value.should == 'wrap' - end + describe :body do - it 'contains the attachment body in the wrapper div' do - pdf_adapter = AttachmentToHTML::Adapters::PDF.new(attachment, :wrapper => 'wrap') - parsed = Nokogiri::HTML.parse(pdf_adapter.to_html) do |config| - config.strict - end - parsed.css('div#wrap div#view-html-content').inner_html.should include('thisisthebody') + it 'extracts the body from the document' do + adapter.body.should include('thisisthebody') end it 'operates in the context of the supplied tmpdir' do - pdf_adapter = AttachmentToHTML::Adapters::PDF.new(attachment, :tmpdir => '/tmp') + adapter = AttachmentToHTML::Adapters::PDF.new(attachment, :tmpdir => '/tmp') Dir.should_receive(:chdir).with('/tmp').and_call_original - pdf_adapter.to_html + adapter.body end end + describe :success? do it 'is successful if the body has content excluding the tags' do - pdf_adapter.to_html - pdf_adapter.success?.should be_true + adapter.stub(:body).and_return('

some content

') + adapter.success?.should be_true end it 'is successful if the body contains images' do - mocked_return = %Q() - pdf_adapter = AttachmentToHTML::Adapters::PDF.new(attachment) - pdf_adapter.stub(:to_html).and_return(mocked_return) - pdf_adapter.success?.should be_true + adapter.stub(:body).and_return(%Q()) + adapter.success?.should be_true end it 'is not successful if the body has no content other than tags' do - # TODO: Add and use spec/fixtures/files/empty.pdf - attachment = FactoryGirl.build(:body_text, :body => '') - pdf_adapter = AttachmentToHTML::Adapters::PDF.new(attachment) - pdf_adapter.to_html - pdf_adapter.success?.should be_false + adapter.stub(:body).and_return('

') + adapter.success?.should be_false end end -- cgit v1.2.3