From d57ca2a22579df4c634d554989c0ee9e4ebb5165 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Mon, 17 Mar 2014 11:15:40 +0000 Subject: Add AttachmentToHTML library Extracts the attachment processing from FoiAttachment#body_to_html AttachmentToHTML contains adapters which convert - text/plain - application/pdf - application/rtf Results are returned as an AttachmentHTML::HTML instance which contains the raw HTML and other metadata about the conversion. --- .../adapters/could_not_convert_spec.rb | 67 ++++++++++++ .../adapters/google_docs_viewer_spec.rb | 81 ++++++++++++++ spec/lib/attachment_to_html/adapters/pdf_spec.rb | 98 +++++++++++++++++ spec/lib/attachment_to_html/adapters/rtf_spec.rb | 98 +++++++++++++++++ spec/lib/attachment_to_html/adapters/text_spec.rb | 121 +++++++++++++++++++++ .../attachment_to_html/attachment_to_html_spec.rb | 48 ++++++++ spec/lib/attachment_to_html/html_spec.rb | 24 ++++ 7 files changed, 537 insertions(+) create mode 100644 spec/lib/attachment_to_html/adapters/could_not_convert_spec.rb create mode 100644 spec/lib/attachment_to_html/adapters/google_docs_viewer_spec.rb create mode 100644 spec/lib/attachment_to_html/adapters/pdf_spec.rb create mode 100644 spec/lib/attachment_to_html/adapters/rtf_spec.rb create mode 100644 spec/lib/attachment_to_html/adapters/text_spec.rb create mode 100644 spec/lib/attachment_to_html/attachment_to_html_spec.rb create mode 100644 spec/lib/attachment_to_html/html_spec.rb (limited to 'spec/lib/attachment_to_html') diff --git a/spec/lib/attachment_to_html/adapters/could_not_convert_spec.rb b/spec/lib/attachment_to_html/adapters/could_not_convert_spec.rb new file mode 100644 index 000000000..aee68e986 --- /dev/null +++ b/spec/lib/attachment_to_html/adapters/could_not_convert_spec.rb @@ -0,0 +1,67 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') + +describe AttachmentToHTML::Adapters::CouldNotConvert do + + let(:attachment) { FactoryGirl.build(:pdf_attachment) } + let(:adapter) { AttachmentToHTML::Adapters::CouldNotConvert.new(attachment) } + + describe :wrapper do + + it 'defaults to wrapper' do + adapter.wrapper.should == 'wrapper' + end + + it 'accepts a wrapper option' do + adapter = AttachmentToHTML::Adapters::CouldNotConvert.new(attachment, :wrapper => 'wrap') + adapter.wrapper.should == 'wrap' + end + + end + + describe :to_html do + + it 'should be a valid html document' do + parsed = Nokogiri::HTML.parse(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(adapter.to_html) do |config| + config.strict + end + parsed.css('title').inner_html.should == attachment.display_filename + end + + it 'contains the wrapper div in the body tag' do + adapter = AttachmentToHTML::Adapters::CouldNotConvert.new(attachment, :wrapper => 'wrap') + parsed = Nokogiri::HTML.parse(adapter.to_html) do |config| + config.strict + end + parsed.css('body div').first.attributes['id'].value.should == 'wrap' + end + + it 'should contain text about the conversion failure' do + adapter = AttachmentToHTML::Adapters::CouldNotConvert.new(attachment, :wrapper => 'wrap') + parsed = Nokogiri::HTML.parse(adapter.to_html) do |config| + config.strict + end + + expected = "

Sorry, we were unable to convert this file to HTML. " \ + "Please use the download link at the top right.

" + + parsed.css('div#wrap div#view-html-content').inner_html.should == expected + end + + end + + describe :success? do + + it 'is always true' do + adapter.success?.should be_true + end + + end + +end diff --git a/spec/lib/attachment_to_html/adapters/google_docs_viewer_spec.rb b/spec/lib/attachment_to_html/adapters/google_docs_viewer_spec.rb new file mode 100644 index 000000000..166ca3241 --- /dev/null +++ b/spec/lib/attachment_to_html/adapters/google_docs_viewer_spec.rb @@ -0,0 +1,81 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') + +describe AttachmentToHTML::Adapters::GoogleDocsViewer do + + let(:attachment) { FactoryGirl.build(:pdf_attachment) } + let(:google_adapter) do + AttachmentToHTML::Adapters::GoogleDocsViewer.new(attachment, :attachment_url => 'http://example.com/test.pdf') + end + + describe :wrapper do + + it 'defaults to wrapper_google_embed' do + google_adapter.wrapper.should == 'wrapper_google_embed' + end + + it 'accepts a wrapper option' do + google_adapter = AttachmentToHTML::Adapters::GoogleDocsViewer.new(attachment, :wrapper => 'wrap') + google_adapter.wrapper.should == 'wrap' + end + + end + + describe :to_html do + + it 'should be a valid html document' do + parsed = Nokogiri::HTML.parse(google_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(google_adapter.to_html) do |config| + config.strict + end + parsed.css('title').inner_html.should == attachment.display_filename + end + + it 'contains the wrapper div in the body tag' do + google_adapter = AttachmentToHTML::Adapters::GoogleDocsViewer.new(attachment, :wrapper => 'wrap') + parsed = Nokogiri::HTML.parse(google_adapter.to_html) do |config| + config.strict + end + parsed.css('body div').first.attributes['id'].value.should == 'wrap' + end + + it 'contains the google docs viewer url in the wrapper div' do + options = { :wrapper => 'wrap', :attachment_url => 'http://example.com/test.pdf' } + google_adapter = AttachmentToHTML::Adapters::GoogleDocsViewer.new(attachment, options) + parsed = Nokogiri::HTML.parse(google_adapter.to_html) do |config| + config.strict + end + expected = %Q() + parsed.css('div#wrap div#view-html-content').inner_html.should include(expected) + end + + describe 'uses the confugured alaveteli protocol' do + + it 'https if force_ssl is on' do + AlaveteliConfiguration.stub(:force_ssl).and_return(true) + google_adapter.to_html.should include('https://docs.google.com') + end + + it 'http if force_ssl is off' do + AlaveteliConfiguration.stub(:force_ssl).and_return(false) + google_adapter.to_html.should include('http://docs.google.com') + end + + end + + end + + describe :success? do + + it 'is always true' do + google_adapter.success?.should be_true + end + + end + +end diff --git a/spec/lib/attachment_to_html/adapters/pdf_spec.rb b/spec/lib/attachment_to_html/adapters/pdf_spec.rb new file mode 100644 index 000000000..65c376043 --- /dev/null +++ b/spec/lib/attachment_to_html/adapters/pdf_spec.rb @@ -0,0 +1,98 @@ +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 + + describe :tmpdir do + + it 'defaults to the rails tmp directory' do + pdf_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' + end + + end + + describe :to_html 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 + 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 + + 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') + end + + it 'operates in the context of the supplied tmpdir' do + pdf_adapter = AttachmentToHTML::Adapters::PDF.new(attachment, :tmpdir => '/tmp') + Dir.should_receive(:chdir).with('/tmp').and_call_original + pdf_adapter.to_html + 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 + 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 + 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 + end + + end + +end diff --git a/spec/lib/attachment_to_html/adapters/rtf_spec.rb b/spec/lib/attachment_to_html/adapters/rtf_spec.rb new file mode 100644 index 000000000..f84073c51 --- /dev/null +++ b/spec/lib/attachment_to_html/adapters/rtf_spec.rb @@ -0,0 +1,98 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') + +describe AttachmentToHTML::Adapters::RTF do + + let(:attachment) { FactoryGirl.build(:rtf_attachment) } + let(:rtf_adapter) { AttachmentToHTML::Adapters::RTF.new(attachment) } + + describe :wrapper do + + it 'defaults to wrapper' do + rtf_adapter.wrapper.should == 'wrapper' + end + + it 'accepts a wrapper option' do + rtf_adapter = AttachmentToHTML::Adapters::RTF.new(attachment, :wrapper => 'wrap') + rtf_adapter.wrapper.should == 'wrap' + end + + end + + describe :tmpdir do + + it 'defaults to the rails tmp directory' do + rtf_adapter.tmpdir.should == Rails.root.join('tmp') + end + + it 'allows a tmpdir to be specified to store the converted document' do + rtf_adapter = AttachmentToHTML::Adapters::RTF.new(attachment, :tmpdir => '/tmp') + rtf_adapter.tmpdir.should == '/tmp' + end + + end + + describe :to_html do + + it 'should be a valid html document' do + parsed = Nokogiri::HTML.parse(rtf_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(rtf_adapter.to_html) do |config| + config.strict + end + parsed.css('head title').inner_html.should == attachment.display_filename + end + + it 'contains the wrapper div in the body tag' do + rtf_adapter = AttachmentToHTML::Adapters::RTF.new(attachment, :wrapper => 'wrap') + parsed = Nokogiri::HTML.parse(rtf_adapter.to_html) do |config| + config.strict + end + parsed.css('body div').first.attributes['id'].value.should == 'wrap' + end + + it 'contains the attachment body in the wrapper div' do + rtf_adapter = AttachmentToHTML::Adapters::RTF.new(attachment, :wrapper => 'wrap') + parsed = Nokogiri::HTML.parse(rtf_adapter.to_html) do |config| + config.strict + end + parsed.css('div#wrap div#view-html-content').inner_text.should include('thisisthebody') + end + + it 'operates in the context of the supplied tmpdir' do + rtf_adapter = AttachmentToHTML::Adapters::RTF.new(attachment, :tmpdir => '/tmp') + Dir.should_receive(:chdir).with('/tmp').and_call_original + rtf_adapter.to_html + end + + end + + describe :success? do + + it 'is successful if the body has content excluding the tags' do + rtf_adapter.to_html + rtf_adapter.success?.should be_true + end + + it 'is successful if the body contains images' do + mocked_return = %Q() + rtf_adapter = AttachmentToHTML::Adapters::RTF.new(attachment) + rtf_adapter.stub(:to_html).and_return(mocked_return) + rtf_adapter.success?.should be_true + end + + it 'is not successful if the body has no content other than tags' do + empty_rtf = load_file_fixture('empty.rtf') + attachment = FactoryGirl.build(:rtf_attachment, :body => empty_rtf) + rtf_adapter = AttachmentToHTML::Adapters::RTF.new(attachment) + rtf_adapter.to_html + rtf_adapter.success?.should be_false + end + + end + +end diff --git a/spec/lib/attachment_to_html/adapters/text_spec.rb b/spec/lib/attachment_to_html/adapters/text_spec.rb new file mode 100644 index 000000000..599670603 --- /dev/null +++ b/spec/lib/attachment_to_html/adapters/text_spec.rb @@ -0,0 +1,121 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') + +describe AttachmentToHTML::Adapters::Text do + + let(:attachment) { FactoryGirl.build(:body_text) } + let(:text_adapter) { AttachmentToHTML::Adapters::Text.new(attachment) } + + describe :wrapper do + + it 'defaults to wrapper' do + text_adapter.wrapper.should == 'wrapper' + end + + it 'accepts a wrapper option' do + text_adapter = AttachmentToHTML::Adapters::Text.new(attachment, :wrapper => 'wrap') + text_adapter.wrapper.should == 'wrap' + end + + end + + describe :to_html do + + it 'should be a valid html document' do + parsed = Nokogiri::HTML.parse(text_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(text_adapter.to_html) do |config| + config.strict + end + parsed.css('title').inner_html.should == attachment.display_filename + end + + it 'contains the wrapper div in the body tag' do + text_adapter = AttachmentToHTML::Adapters::Text.new(attachment, :wrapper => 'wrap') + parsed = Nokogiri::HTML.parse(text_adapter.to_html) do |config| + config.strict + end + parsed.css('body').children.first.attributes['id'].value.should == 'wrap' + end + + it 'contains the attachment body in the wrapper div' do + text_adapter = AttachmentToHTML::Adapters::Text.new(attachment, :wrapper => 'wrap') + parsed = Nokogiri::HTML.parse(text_adapter.to_html) do |config| + config.strict + end + parsed.css('div#wrap div#view-html-content').inner_html.should == attachment.body + end + + it 'strips the body of trailing whitespace' do + attachment = FactoryGirl.build(:body_text, :body => ' Hello ') + text_adapter = AttachmentToHTML::Adapters::Text.new(attachment) + parsed = Nokogiri::HTML.parse(text_adapter.to_html) do |config| + config.strict + end + parsed.css('div#wrapper div#view-html-content').inner_html.should == 'Hello' + end + + # NOTE: Can't parse this spec with Nokogiri at the moment because even + # in strict mode Nokogiri tampers with the HTML returned: + # Failure/Error: parsed.css('div#wrapper div#view-html-content'). + # inner_html.should == expected + # expected: "Usage: foo "bar" >baz<" + # got: "Usage: foo \"bar\" >baz<" (using ==) + it 'escapes special characters' do + attachment = FactoryGirl.build(:body_text, :body => 'Usage: foo "bar" >baz<') + text_adapter = AttachmentToHTML::Adapters::Text.new(attachment) + expected = %Q(Usage: foo "bar" >baz<) + text_adapter.to_html.should include(expected) + end + + it 'creates hyperlinks for text that looks like a url' do + attachment = FactoryGirl.build(:body_text, :body => 'http://www.whatdotheyknow.com') + text_adapter = AttachmentToHTML::Adapters::Text.new(attachment) + parsed = Nokogiri::HTML.parse(text_adapter.to_html) do |config| + config.strict + end + parsed.css('div#wrapper div#view-html-content a').first.text.should == 'http://www.whatdotheyknow.com' + parsed.css('div#wrapper div#view-html-content a').first['href'].should == 'http://www.whatdotheyknow.com' + end + + it 'substitutes newlines for br tags' do + attachment = FactoryGirl.build(:body_text, :body => "A\nNewline") + text_adapter = AttachmentToHTML::Adapters::Text.new(attachment) + parsed = Nokogiri::HTML.parse(text_adapter.to_html) do |config| + config.strict + end + expected = %Q(A
Newline) + parsed.css('div#wrapper div#view-html-content').inner_html.should == expected + end + + end + + describe :success? do + + it 'is successful if the body has content excluding the tags' do + text_adapter.to_html + text_adapter.success?.should be_true + end + + it 'is successful if the body contains images' do + mocked_return = %Q() + text_adapter = AttachmentToHTML::Adapters::Text.new(attachment) + text_adapter.stub(:to_html).and_return(mocked_return) + text_adapter.success?.should be_true + end + + it 'is not successful if the body has no content other than tags' do + empty_txt = load_file_fixture('empty.txt') + attachment = FactoryGirl.build(:body_text, :body => empty_txt) + text_adapter = AttachmentToHTML::Adapters::Text.new(attachment) + text_adapter.to_html + text_adapter.success?.should be_false + end + + end + +end diff --git a/spec/lib/attachment_to_html/attachment_to_html_spec.rb b/spec/lib/attachment_to_html/attachment_to_html_spec.rb new file mode 100644 index 000000000..f7df06f87 --- /dev/null +++ b/spec/lib/attachment_to_html/attachment_to_html_spec.rb @@ -0,0 +1,48 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +describe AttachmentToHTML do + include AttachmentToHTML + + let(:attachment) { FactoryGirl.build(:body_text) } + + describe :to_html do + + it 'sends the attachment to the correct adapter for conversion' do + AttachmentToHTML::Adapters::Text.should_receive(:new).with(attachment, {}).and_call_original + to_html(attachment) + end + + it 'returns the results in a HTML class' do + expected = AttachmentToHTML::Adapters::Text.new(attachment).to_html + to_html(attachment).should be_instance_of(AttachmentToHTML::HTML) + end + + it 'accepts a hash of options to pass to the adapter' do + options = { :wrapper => 'wrap' } + AttachmentToHTML::Adapters::Text.should_receive(:new).with(attachment, options).and_call_original + to_html(attachment, options) + end + + it 'converts an attachment that has an adapter, fails to convert, but has a google viewer' do + attachment = FactoryGirl.build(:pdf_attachment) + AttachmentToHTML::HTML.any_instance.stub(:success?).and_return(false) + AttachmentToHTML::Adapters::PDF.should_receive(:new).with(attachment, {}).and_call_original + AttachmentToHTML::Adapters::GoogleDocsViewer.should_receive(:new).with(attachment, {}) + to_html(attachment) + end + + it 'converts an attachment that doesnt have an adapter, but has a google viewer' do + attachment = FactoryGirl.build(:body_text, :content_type => 'application/vnd.ms-word') + AttachmentToHTML::Adapters::GoogleDocsViewer.should_receive(:new).with(attachment, {}).and_call_original + to_html(attachment) + end + + it 'converts an attachment that has no adapter or google viewer' do + attachment = FactoryGirl.build(:body_text, :content_type => 'application/json') + AttachmentToHTML::Adapters::CouldNotConvert.should_receive(:new).with(attachment, {}).and_call_original + to_html(attachment) + end + + end + +end diff --git a/spec/lib/attachment_to_html/html_spec.rb b/spec/lib/attachment_to_html/html_spec.rb new file mode 100644 index 000000000..65b63d383 --- /dev/null +++ b/spec/lib/attachment_to_html/html_spec.rb @@ -0,0 +1,24 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +describe AttachmentToHTML::HTML do + + let(:adapter) { OpenStruct.new(:to_html => '

hello

', :success? => true) } + let(:html) { AttachmentToHTML::HTML.new(adapter) } + + describe :to_s do + + it 'returns the raw html' do + html.to_s.should == '

hello

' + end + + end + + describe :success? do + + it 'returns whether the conversion succeeded' do + html.success?.should be_true + end + + end + +end -- cgit v1.2.3 From 08572fe8d0ad97c01ecc5c0f0ee39e610de383a3 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Tue, 1 Apr 2014 11:59:26 +0100 Subject: Work around a bug in unrtf --- spec/lib/attachment_to_html/adapters/rtf_spec.rb | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'spec/lib/attachment_to_html') diff --git a/spec/lib/attachment_to_html/adapters/rtf_spec.rb b/spec/lib/attachment_to_html/adapters/rtf_spec.rb index f84073c51..75fd467f6 100644 --- a/spec/lib/attachment_to_html/adapters/rtf_spec.rb +++ b/spec/lib/attachment_to_html/adapters/rtf_spec.rb @@ -69,6 +69,31 @@ describe AttachmentToHTML::Adapters::RTF do rtf_adapter.to_html end + it 'does not result in incorrect conversion when unrtf returns an invalid doctype' do + # Doctype public identifier is unquoted + # Valid doctype would be: + # + # See bug report http://savannah.gnu.org/bugs/?42015 + invalid = <<-DOC + + + + + + + + + thisisthebody + + DOC + AlaveteliExternalCommand.stub(:run).and_return(invalid) + + parsed = Nokogiri::HTML.parse(rtf_adapter.to_html) do |config| + config.strict + end + parsed.css('body').inner_text.should_not include('//W3C//DTD HTML 4.01 Transitional//EN') + end + end describe :success? do -- cgit v1.2.3 From ea1e040780f00938331e92472780c91b7e0f43a2 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Wed, 2 Apr 2014 11:28:45 +0100 Subject: Add an AttachmentToHTML::View to deal with rendering --- spec/lib/attachment_to_html/view_spec.rb | 145 +++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 spec/lib/attachment_to_html/view_spec.rb (limited to 'spec/lib/attachment_to_html') diff --git a/spec/lib/attachment_to_html/view_spec.rb b/spec/lib/attachment_to_html/view_spec.rb new file mode 100644 index 000000000..65eff4cad --- /dev/null +++ b/spec/lib/attachment_to_html/view_spec.rb @@ -0,0 +1,145 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +describe AttachmentToHTML::View do + + let(:adapter) do + OpenStruct.new( + :body => '

hello

', + :title => 'An attachment.txt', + :success? => true) + end + + let(:view) { AttachmentToHTML::View.new(adapter) } + + let(:default_template) do + "#{ Rails.root }/lib/attachment_to_html/template.html.erb" + end + + describe '.template' do + + after(:each) do + AttachmentToHTML::View.template = nil + end + + it 'has a default template location' do + AttachmentToHTML::View.template.should == default_template + end + + end + + describe '.template=' do + + after(:each) do + AttachmentToHTML::View.template = nil + end + + it 'allows a global template to be set' do + template = file_fixture_name('attachment_to_html/alternative_template.html.erb') + AttachmentToHTML::View.template = template + AttachmentToHTML::View.template.should == template + end + + end + + describe :new do + + it 'sets the title on initialization' do + view.title.should == adapter.title + end + + it 'sets the body on initialization' do + view.body.should == adapter.body + end + + it 'sets a default template if none is specified' do + view.template.should == default_template + end + + it 'allows a template to be set through an option' do + template = file_fixture_name('attachment_to_html/alternative_template.html.erb') + opts = { :template => template } + view = AttachmentToHTML::View.new(adapter, opts) + view.template.should == template + end + + end + + describe :title= do + + it 'allows the title to be set' do + view.title = adapter.title + view.title.should == adapter.title + end + + end + + describe :body= do + + it 'allows the body to be set' do + view.body = adapter.body + view.body.should == adapter.body + end + + end + + describe :template= do + + it 'allows the template to be set' do + template = file_fixture_name('attachment_to_html/alternative_template.html.erb') + view.template = template + view.template.should == template + end + + end + + describe :wrapper do + + it 'is set to wrapper by default' do + view.wrapper.should == 'wrapper' + end + + end + + describe :wrapper= do + + it 'allows the wrapper div to be customised' do + view.wrapper = 'wrap' + view.wrapper.should == 'wrap' + end + + end + + # Need to remove all whitespace to assert equal because + # ERB adds additional indentation after ERB tags + describe :render do + + it 'renders the contents in to the template' do + view.wrapper = 'wrap' + expected = <<-HTML + + + + An attachment.txt + + +
+
+

hello

+
+
+ + + HTML + + view.render.gsub(/\s+/, '').should == expected.gsub(/\s+/, '') + end + + it 'allows the dynamic injection of content' do + content = %Q() + result = view.render { inject_content(:head_suffix) { content } } + result.should include(content) + end + + end + +end -- cgit v1.2.3 From 44eff43ee8024a03fe4c327638ac0dbc1b47f4fd Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Wed, 2 Apr 2014 11:31:55 +0100 Subject: Simpler AttachmentToHTML::Adapters::Text interface --- spec/lib/attachment_to_html/adapters/text_spec.rb | 95 ++++++----------------- 1 file changed, 22 insertions(+), 73 deletions(-) (limited to 'spec/lib/attachment_to_html') diff --git a/spec/lib/attachment_to_html/adapters/text_spec.rb b/spec/lib/attachment_to_html/adapters/text_spec.rb index 599670603..b2e8141e0 100644 --- a/spec/lib/attachment_to_html/adapters/text_spec.rb +++ b/spec/lib/attachment_to_html/adapters/text_spec.rb @@ -3,93 +3,47 @@ require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') describe AttachmentToHTML::Adapters::Text do let(:attachment) { FactoryGirl.build(:body_text) } - let(:text_adapter) { AttachmentToHTML::Adapters::Text.new(attachment) } + let(:adapter) { AttachmentToHTML::Adapters::Text.new(attachment) } - describe :wrapper do + describe :title do - it 'defaults to wrapper' do - text_adapter.wrapper.should == 'wrapper' - end - - it 'accepts a wrapper option' do - text_adapter = AttachmentToHTML::Adapters::Text.new(attachment, :wrapper => 'wrap') - text_adapter.wrapper.should == 'wrap' + it 'uses the attachment filename for the title' do + adapter.title.should == attachment.display_filename end end - describe :to_html do - - it 'should be a valid html document' do - parsed = Nokogiri::HTML.parse(text_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(text_adapter.to_html) do |config| - config.strict - end - parsed.css('title').inner_html.should == attachment.display_filename - end + describe :body do - it 'contains the wrapper div in the body tag' do - text_adapter = AttachmentToHTML::Adapters::Text.new(attachment, :wrapper => 'wrap') - parsed = Nokogiri::HTML.parse(text_adapter.to_html) do |config| - config.strict - end - parsed.css('body').children.first.attributes['id'].value.should == 'wrap' + it 'extracts the body from the document' do + adapter.body.should == attachment.body end - it 'contains the attachment body in the wrapper div' do - text_adapter = AttachmentToHTML::Adapters::Text.new(attachment, :wrapper => 'wrap') - parsed = Nokogiri::HTML.parse(text_adapter.to_html) do |config| - config.strict - end - parsed.css('div#wrap div#view-html-content').inner_html.should == attachment.body - end - it 'strips the body of trailing whitespace' do attachment = FactoryGirl.build(:body_text, :body => ' Hello ') - text_adapter = AttachmentToHTML::Adapters::Text.new(attachment) - parsed = Nokogiri::HTML.parse(text_adapter.to_html) do |config| - config.strict - end - parsed.css('div#wrapper div#view-html-content').inner_html.should == 'Hello' + adapter = AttachmentToHTML::Adapters::Text.new(attachment) + adapter.body.should == 'Hello' end - # NOTE: Can't parse this spec with Nokogiri at the moment because even - # in strict mode Nokogiri tampers with the HTML returned: - # Failure/Error: parsed.css('div#wrapper div#view-html-content'). - # inner_html.should == expected - # expected: "Usage: foo "bar" >baz<" - # got: "Usage: foo \"bar\" >baz<" (using ==) it 'escapes special characters' do attachment = FactoryGirl.build(:body_text, :body => 'Usage: foo "bar" >baz<') - text_adapter = AttachmentToHTML::Adapters::Text.new(attachment) + adapter = AttachmentToHTML::Adapters::Text.new(attachment) expected = %Q(Usage: foo "bar" >baz<) - text_adapter.to_html.should include(expected) + adapter.body.should == expected end it 'creates hyperlinks for text that looks like a url' do attachment = FactoryGirl.build(:body_text, :body => 'http://www.whatdotheyknow.com') - text_adapter = AttachmentToHTML::Adapters::Text.new(attachment) - parsed = Nokogiri::HTML.parse(text_adapter.to_html) do |config| - config.strict - end - parsed.css('div#wrapper div#view-html-content a').first.text.should == 'http://www.whatdotheyknow.com' - parsed.css('div#wrapper div#view-html-content a').first['href'].should == 'http://www.whatdotheyknow.com' + adapter = AttachmentToHTML::Adapters::Text.new(attachment) + expected = %Q(http://www.whatdotheyknow.com) + adapter.body.should == expected end it 'substitutes newlines for br tags' do attachment = FactoryGirl.build(:body_text, :body => "A\nNewline") - text_adapter = AttachmentToHTML::Adapters::Text.new(attachment) - parsed = Nokogiri::HTML.parse(text_adapter.to_html) do |config| - config.strict - end + adapter = AttachmentToHTML::Adapters::Text.new(attachment) expected = %Q(A
Newline) - parsed.css('div#wrapper div#view-html-content').inner_html.should == expected + adapter.body.should == expected end end @@ -97,23 +51,18 @@ describe AttachmentToHTML::Adapters::Text do describe :success? do it 'is successful if the body has content excluding the tags' do - text_adapter.to_html - text_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() - text_adapter = AttachmentToHTML::Adapters::Text.new(attachment) - text_adapter.stub(:to_html).and_return(mocked_return) - text_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 - empty_txt = load_file_fixture('empty.txt') - attachment = FactoryGirl.build(:body_text, :body => empty_txt) - text_adapter = AttachmentToHTML::Adapters::Text.new(attachment) - text_adapter.to_html - text_adapter.success?.should be_false + adapter.stub(:body).and_return('

') + adapter.success?.should be_false end end -- cgit v1.2.3 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 From 0532eeee63f06e796f0e967f39dfa5f23d4821f7 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Wed, 2 Apr 2014 12:11:56 +0100 Subject: Simpler AttachmentToHTML::Adapters::RTF interface TODO: We really should be testing the full output of RTF#body, but we currently want to remain consistent with Adapters::PDF as many methods are shared between the Adapters. A more correct spec might be: expected = %Q(thisisthebody) adapter.body.should == expected --- spec/lib/attachment_to_html/adapters/rtf_spec.rb | 82 +++++++----------------- 1 file changed, 22 insertions(+), 60 deletions(-) (limited to 'spec/lib/attachment_to_html') diff --git a/spec/lib/attachment_to_html/adapters/rtf_spec.rb b/spec/lib/attachment_to_html/adapters/rtf_spec.rb index 75fd467f6..a3bf0e27e 100644 --- a/spec/lib/attachment_to_html/adapters/rtf_spec.rb +++ b/spec/lib/attachment_to_html/adapters/rtf_spec.rb @@ -3,70 +3,39 @@ require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') describe AttachmentToHTML::Adapters::RTF do let(:attachment) { FactoryGirl.build(:rtf_attachment) } - let(:rtf_adapter) { AttachmentToHTML::Adapters::RTF.new(attachment) } - - describe :wrapper do - - it 'defaults to wrapper' do - rtf_adapter.wrapper.should == 'wrapper' - end - - it 'accepts a wrapper option' do - rtf_adapter = AttachmentToHTML::Adapters::RTF.new(attachment, :wrapper => 'wrap') - rtf_adapter.wrapper.should == 'wrap' - end - - end + let(:adapter) { AttachmentToHTML::Adapters::RTF.new(attachment) } describe :tmpdir do it 'defaults to the rails tmp directory' do - rtf_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 - rtf_adapter = AttachmentToHTML::Adapters::RTF.new(attachment, :tmpdir => '/tmp') - rtf_adapter.tmpdir.should == '/tmp' + adapter = AttachmentToHTML::Adapters::RTF.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(rtf_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(rtf_adapter.to_html) do |config| - config.strict - end - parsed.css('head 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 - rtf_adapter = AttachmentToHTML::Adapters::RTF.new(attachment, :wrapper => 'wrap') - parsed = Nokogiri::HTML.parse(rtf_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 - rtf_adapter = AttachmentToHTML::Adapters::RTF.new(attachment, :wrapper => 'wrap') - parsed = Nokogiri::HTML.parse(rtf_adapter.to_html) do |config| - config.strict - end - parsed.css('div#wrap div#view-html-content').inner_text.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 - rtf_adapter = AttachmentToHTML::Adapters::RTF.new(attachment, :tmpdir => '/tmp') + adapter = AttachmentToHTML::Adapters::RTF.new(attachment, :tmpdir => '/tmp') Dir.should_receive(:chdir).with('/tmp').and_call_original - rtf_adapter.to_html + adapter.body end it 'does not result in incorrect conversion when unrtf returns an invalid doctype' do @@ -88,34 +57,27 @@ describe AttachmentToHTML::Adapters::RTF do DOC AlaveteliExternalCommand.stub(:run).and_return(invalid) - parsed = Nokogiri::HTML.parse(rtf_adapter.to_html) do |config| - config.strict - end - parsed.css('body').inner_text.should_not include('//W3C//DTD HTML 4.01 Transitional//EN') + adapter.body.should_not include('//W3C//DTD HTML 4.01 Transitional//EN') end end + describe :success? do it 'is successful if the body has content excluding the tags' do - rtf_adapter.to_html - rtf_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() - rtf_adapter = AttachmentToHTML::Adapters::RTF.new(attachment) - rtf_adapter.stub(:to_html).and_return(mocked_return) - rtf_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 - empty_rtf = load_file_fixture('empty.rtf') - attachment = FactoryGirl.build(:rtf_attachment, :body => empty_rtf) - rtf_adapter = AttachmentToHTML::Adapters::RTF.new(attachment) - rtf_adapter.to_html - rtf_adapter.success?.should be_false + adapter.stub(:body).and_return('

') + adapter.success?.should be_false end end -- cgit v1.2.3 From 50ed310bed98ea23f813a5abd4210e34711dfb4c Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Wed, 2 Apr 2014 12:20:02 +0100 Subject: Simpler AttachmentToHTML::Adapters::GoogleDocsViewer interface --- .../adapters/google_docs_viewer_spec.rb | 52 +++++----------------- 1 file changed, 10 insertions(+), 42 deletions(-) (limited to 'spec/lib/attachment_to_html') diff --git a/spec/lib/attachment_to_html/adapters/google_docs_viewer_spec.rb b/spec/lib/attachment_to_html/adapters/google_docs_viewer_spec.rb index 166ca3241..e7aafb40d 100644 --- a/spec/lib/attachment_to_html/adapters/google_docs_viewer_spec.rb +++ b/spec/lib/attachment_to_html/adapters/google_docs_viewer_spec.rb @@ -3,67 +3,35 @@ require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') describe AttachmentToHTML::Adapters::GoogleDocsViewer do let(:attachment) { FactoryGirl.build(:pdf_attachment) } - let(:google_adapter) do + let(:adapter) do AttachmentToHTML::Adapters::GoogleDocsViewer.new(attachment, :attachment_url => 'http://example.com/test.pdf') end - describe :wrapper do + describe :title do - it 'defaults to wrapper_google_embed' do - google_adapter.wrapper.should == 'wrapper_google_embed' - end - - it 'accepts a wrapper option' do - google_adapter = AttachmentToHTML::Adapters::GoogleDocsViewer.new(attachment, :wrapper => 'wrap') - google_adapter.wrapper.should == 'wrap' + it 'uses the attachment filename for the title' do + adapter.title.should == attachment.display_filename end end - describe :to_html do - - it 'should be a valid html document' do - parsed = Nokogiri::HTML.parse(google_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(google_adapter.to_html) do |config| - config.strict - end - parsed.css('title').inner_html.should == attachment.display_filename - end - - it 'contains the wrapper div in the body tag' do - google_adapter = AttachmentToHTML::Adapters::GoogleDocsViewer.new(attachment, :wrapper => 'wrap') - parsed = Nokogiri::HTML.parse(google_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 google docs viewer url in the wrapper div' do - options = { :wrapper => 'wrap', :attachment_url => 'http://example.com/test.pdf' } - google_adapter = AttachmentToHTML::Adapters::GoogleDocsViewer.new(attachment, options) - parsed = Nokogiri::HTML.parse(google_adapter.to_html) do |config| - config.strict - end + it 'contains the google docs viewer iframe' do expected = %Q() - parsed.css('div#wrap div#view-html-content').inner_html.should include(expected) + adapter.body.should == expected end describe 'uses the confugured alaveteli protocol' do it 'https if force_ssl is on' do AlaveteliConfiguration.stub(:force_ssl).and_return(true) - google_adapter.to_html.should include('https://docs.google.com') + adapter.body.should include('https://docs.google.com') end it 'http if force_ssl is off' do AlaveteliConfiguration.stub(:force_ssl).and_return(false) - google_adapter.to_html.should include('http://docs.google.com') + adapter.body.should include('http://docs.google.com') end end @@ -73,7 +41,7 @@ describe AttachmentToHTML::Adapters::GoogleDocsViewer do describe :success? do it 'is always true' do - google_adapter.success?.should be_true + adapter.success?.should be_true end end -- cgit v1.2.3 From 7a30de942b03156bc8220471c23aca52143a14a1 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Wed, 2 Apr 2014 12:25:27 +0100 Subject: Simpler AttachmentToHTML::Adapters::CouldNotConvert interface --- .../adapters/could_not_convert_spec.rb | 49 ++++------------------ 1 file changed, 9 insertions(+), 40 deletions(-) (limited to 'spec/lib/attachment_to_html') diff --git a/spec/lib/attachment_to_html/adapters/could_not_convert_spec.rb b/spec/lib/attachment_to_html/adapters/could_not_convert_spec.rb index aee68e986..afdc5c552 100644 --- a/spec/lib/attachment_to_html/adapters/could_not_convert_spec.rb +++ b/spec/lib/attachment_to_html/adapters/could_not_convert_spec.rb @@ -3,55 +3,24 @@ require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') describe AttachmentToHTML::Adapters::CouldNotConvert do let(:attachment) { FactoryGirl.build(:pdf_attachment) } - let(:adapter) { AttachmentToHTML::Adapters::CouldNotConvert.new(attachment) } - - describe :wrapper do + let(:adapter) do + AttachmentToHTML::Adapters::CouldNotConvert.new(attachment) + end - it 'defaults to wrapper' do - adapter.wrapper.should == 'wrapper' - end + describe :title do - it 'accepts a wrapper option' do - adapter = AttachmentToHTML::Adapters::CouldNotConvert.new(attachment, :wrapper => 'wrap') - adapter.wrapper.should == 'wrap' + it 'uses the attachment filename for the title' do + adapter.title.should == attachment.display_filename end end - describe :to_html do - - it 'should be a valid html document' do - parsed = Nokogiri::HTML.parse(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(adapter.to_html) do |config| - config.strict - end - parsed.css('title').inner_html.should == attachment.display_filename - end - - it 'contains the wrapper div in the body tag' do - adapter = AttachmentToHTML::Adapters::CouldNotConvert.new(attachment, :wrapper => 'wrap') - parsed = Nokogiri::HTML.parse(adapter.to_html) do |config| - config.strict - end - parsed.css('body div').first.attributes['id'].value.should == 'wrap' - end - - it 'should contain text about the conversion failure' do - adapter = AttachmentToHTML::Adapters::CouldNotConvert.new(attachment, :wrapper => 'wrap') - parsed = Nokogiri::HTML.parse(adapter.to_html) do |config| - config.strict - end + describe :body do + it 'contains a message asking the user to download the file directly' do expected = "

Sorry, we were unable to convert this file to HTML. " \ "Please use the download link at the top right.

" - - parsed.css('div#wrap div#view-html-content').inner_html.should == expected + adapter.body.should == expected end end -- cgit v1.2.3 From ce9fa9e77a60d06e3ed968ccb48c0fa30fb9f1f2 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Wed, 2 Apr 2014 13:38:25 +0100 Subject: Update AttachmentToHTML for new View class --- .../attachment_to_html/attachment_to_html_spec.rb | 30 +++++++++++++++++----- 1 file changed, 24 insertions(+), 6 deletions(-) (limited to 'spec/lib/attachment_to_html') diff --git a/spec/lib/attachment_to_html/attachment_to_html_spec.rb b/spec/lib/attachment_to_html/attachment_to_html_spec.rb index f7df06f87..41e1d67e5 100644 --- a/spec/lib/attachment_to_html/attachment_to_html_spec.rb +++ b/spec/lib/attachment_to_html/attachment_to_html_spec.rb @@ -12,11 +12,12 @@ describe AttachmentToHTML do to_html(attachment) end - it 'returns the results in a HTML class' do - expected = AttachmentToHTML::Adapters::Text.new(attachment).to_html - to_html(attachment).should be_instance_of(AttachmentToHTML::HTML) + it 'renders the attachment as html' do + adapter = AttachmentToHTML::Adapters::Text.new(attachment) + expected = AttachmentToHTML::View.new(adapter).render + to_html(attachment).should == expected end - + it 'accepts a hash of options to pass to the adapter' do options = { :wrapper => 'wrap' } AttachmentToHTML::Adapters::Text.should_receive(:new).with(attachment, options).and_call_original @@ -25,9 +26,9 @@ describe AttachmentToHTML do it 'converts an attachment that has an adapter, fails to convert, but has a google viewer' do attachment = FactoryGirl.build(:pdf_attachment) - AttachmentToHTML::HTML.any_instance.stub(:success?).and_return(false) + AttachmentToHTML::Adapters::PDF.any_instance.stub(:success?).and_return(false) AttachmentToHTML::Adapters::PDF.should_receive(:new).with(attachment, {}).and_call_original - AttachmentToHTML::Adapters::GoogleDocsViewer.should_receive(:new).with(attachment, {}) + AttachmentToHTML::Adapters::GoogleDocsViewer.should_receive(:new).with(attachment, {}).and_call_original to_html(attachment) end @@ -43,6 +44,23 @@ describe AttachmentToHTML do to_html(attachment) end + describe 'when wrapping the content' do + + it 'uses a the default wrapper' do + attachment = FactoryGirl.build(:pdf_attachment) + to_html(attachment).should include(%Q(
)) + end + + it 'uses a custom wrapper for GoogleDocsViewer attachments' do + attachment = FactoryGirl.build(:pdf_attachment) + # TODO: Add a document that will always render in a + # GoogleDocsViewer for testing + AttachmentToHTML::Adapters::PDF.any_instance.stub(:success?).and_return(false) + to_html(attachment).should include(%Q(
)) + end + + end + end end -- cgit v1.2.3 From cb06289e9fc04bf2ea9430828358a7cf304eae8f Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Wed, 2 Apr 2014 13:39:36 +0100 Subject: Remove redundant AttachmentToHTML::HTML --- spec/lib/attachment_to_html/html_spec.rb | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 spec/lib/attachment_to_html/html_spec.rb (limited to 'spec/lib/attachment_to_html') diff --git a/spec/lib/attachment_to_html/html_spec.rb b/spec/lib/attachment_to_html/html_spec.rb deleted file mode 100644 index 65b63d383..000000000 --- a/spec/lib/attachment_to_html/html_spec.rb +++ /dev/null @@ -1,24 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') - -describe AttachmentToHTML::HTML do - - let(:adapter) { OpenStruct.new(:to_html => '

hello

', :success? => true) } - let(:html) { AttachmentToHTML::HTML.new(adapter) } - - describe :to_s do - - it 'returns the raw html' do - html.to_s.should == '

hello

' - end - - end - - describe :success? do - - it 'returns whether the conversion succeeded' do - html.success?.should be_true - end - - end - -end -- cgit v1.2.3 From 3954e78a4d330e3e9d82be0d239d5bf25d2f6a04 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Wed, 2 Apr 2014 17:15:41 +0100 Subject: Dynamically inject AttachmentToHTML::View content --- spec/lib/attachment_to_html/attachment_to_html_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'spec/lib/attachment_to_html') diff --git a/spec/lib/attachment_to_html/attachment_to_html_spec.rb b/spec/lib/attachment_to_html/attachment_to_html_spec.rb index 41e1d67e5..1cf7debb7 100644 --- a/spec/lib/attachment_to_html/attachment_to_html_spec.rb +++ b/spec/lib/attachment_to_html/attachment_to_html_spec.rb @@ -18,6 +18,11 @@ describe AttachmentToHTML do to_html(attachment).should == expected end + it 'passes content injections options when rendering the result' do + html = to_html(attachment, :content_for => { :body_prefix => '

prefix

' }) + html.should include('

prefix

') + end + it 'accepts a hash of options to pass to the adapter' do options = { :wrapper => 'wrap' } AttachmentToHTML::Adapters::Text.should_receive(:new).with(attachment, options).and_call_original -- cgit v1.2.3