diff options
author | Gareth Rees <gareth@mysociety.org> | 2014-04-14 17:14:01 +0100 |
---|---|---|
committer | Gareth Rees <gareth@mysociety.org> | 2014-04-14 17:14:01 +0100 |
commit | 0c54aa3bc1bda24fa6cca97e52753a5ea07e7638 (patch) | |
tree | 0b3dc1c6aae0e277e51c1e43c8519f21e36401cd /spec/lib/attachment_to_html/adapters | |
parent | fb0742f39fc9f5ba9e45ef08a4e4312ea10660f1 (diff) | |
parent | 9f283e2e48e859d1ba6a31baa783feb177cccb17 (diff) |
Merge branch 'issues/337-attachment-title' into rails-3-develop
Diffstat (limited to 'spec/lib/attachment_to_html/adapters')
5 files changed, 303 insertions, 0 deletions
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..afdc5c552 --- /dev/null +++ b/spec/lib/attachment_to_html/adapters/could_not_convert_spec.rb @@ -0,0 +1,36 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') + +describe AttachmentToHTML::Adapters::CouldNotConvert do + + let(:attachment) { FactoryGirl.build(:pdf_attachment) } + let(:adapter) do + AttachmentToHTML::Adapters::CouldNotConvert.new(attachment) + end + + describe :title do + + it 'uses the attachment filename for the title' do + adapter.title.should == attachment.display_filename + end + + end + + describe :body do + + it 'contains a message asking the user to download the file directly' do + expected = "<p>Sorry, we were unable to convert this file to HTML. " \ + "Please use the download link at the top right.</p>" + adapter.body.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..e7aafb40d --- /dev/null +++ b/spec/lib/attachment_to_html/adapters/google_docs_viewer_spec.rb @@ -0,0 +1,49 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') + +describe AttachmentToHTML::Adapters::GoogleDocsViewer do + + let(:attachment) { FactoryGirl.build(:pdf_attachment) } + let(:adapter) do + AttachmentToHTML::Adapters::GoogleDocsViewer.new(attachment, :attachment_url => 'http://example.com/test.pdf') + end + + describe :title do + + it 'uses the attachment filename for the title' do + adapter.title.should == attachment.display_filename + end + + end + + describe :body do + + it 'contains the google docs viewer iframe' do + expected = %Q(<iframe src="http://docs.google.com/viewer?url=http://example.com/test.pdf&embedded=true" width="100%" height="100%" style="border: none;"></iframe>) + 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) + adapter.body.should include('https://docs.google.com') + end + + it 'http if force_ssl is off' do + AlaveteliConfiguration.stub(:force_ssl).and_return(false) + adapter.body.should include('http://docs.google.com') + end + + 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/pdf_spec.rb b/spec/lib/attachment_to_html/adapters/pdf_spec.rb new file mode 100644 index 000000000..c02b157e4 --- /dev/null +++ b/spec/lib/attachment_to_html/adapters/pdf_spec.rb @@ -0,0 +1,63 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') + +describe AttachmentToHTML::Adapters::PDF do + + let(:attachment) { FactoryGirl.build(:pdf_attachment) } + let(:adapter) { AttachmentToHTML::Adapters::PDF.new(attachment) } + + describe :tmpdir do + + it 'defaults to the rails tmp directory' do + adapter.tmpdir.should == Rails.root.join('tmp') + end + + it 'allows a tmpdir to be specified to store the converted document' do + adapter = AttachmentToHTML::Adapters::PDF.new(attachment, :tmpdir => '/tmp') + adapter.tmpdir.should == '/tmp' + end + + end + + describe :title do + + it 'uses the attachment filename for the title' do + adapter.title.should == attachment.display_filename + end + + end + + describe :body do + + it 'extracts the body from the document' do + adapter.body.should include('thisisthebody') + end + + it 'operates in the context of the supplied tmpdir' do + adapter = AttachmentToHTML::Adapters::PDF.new(attachment, :tmpdir => '/tmp') + Dir.should_receive(:chdir).with('/tmp').and_call_original + adapter.body + end + + end + + + describe :success? do + + it 'is successful if the body has content excluding the tags' do + adapter.stub(:body).and_return('<p>some content</p>') + adapter.success?.should be_true + end + + it 'is successful if the body contains images' do + adapter.stub(:body).and_return(%Q(<img src="logo.png" />)) + adapter.success?.should be_true + end + + it 'is not successful if the body has no content other than tags' do + adapter.stub(:body).and_return('<p></p>') + 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..a3bf0e27e --- /dev/null +++ b/spec/lib/attachment_to_html/adapters/rtf_spec.rb @@ -0,0 +1,85 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') + +describe AttachmentToHTML::Adapters::RTF do + + let(:attachment) { FactoryGirl.build(:rtf_attachment) } + let(:adapter) { AttachmentToHTML::Adapters::RTF.new(attachment) } + + describe :tmpdir do + + it 'defaults to the rails tmp directory' do + adapter.tmpdir.should == Rails.root.join('tmp') + end + + it 'allows a tmpdir to be specified to store the converted document' do + adapter = AttachmentToHTML::Adapters::RTF.new(attachment, :tmpdir => '/tmp') + adapter.tmpdir.should == '/tmp' + end + + end + + describe :title do + + it 'uses the attachment filename for the title' do + adapter.title.should == attachment.display_filename + end + + end + + describe :body do + + it 'extracts the body from the document' do + adapter.body.should include('thisisthebody') + end + + it 'operates in the context of the supplied tmpdir' do + adapter = AttachmentToHTML::Adapters::RTF.new(attachment, :tmpdir => '/tmp') + Dir.should_receive(:chdir).with('/tmp').and_call_original + adapter.body + end + + it 'does not result in incorrect conversion when unrtf returns an invalid doctype' do + # Doctype public identifier is unquoted + # Valid doctype would be: + # <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> + # See bug report http://savannah.gnu.org/bugs/?42015 + invalid = <<-DOC + <!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN> + <html> + <head> + <meta http-equiv="content-type" content="text/html; charset=utf-8"> + <!-- Translation from RTF performed by UnRTF, version 0.21.5 --> + <!--font table contains 0 fonts total--> + <!--invalid font number 0--> + </head> + <body><font size="3"><font color="#000000">thisisthebody</font></font></body> + </html> + DOC + AlaveteliExternalCommand.stub(:run).and_return(invalid) + + 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 + adapter.stub(:body).and_return('<p>some content</p>') + adapter.success?.should be_true + end + + it 'is successful if the body contains images' do + adapter.stub(:body).and_return(%Q(<img src="logo.png" />)) + adapter.success?.should be_true + end + + it 'is not successful if the body has no content other than tags' do + adapter.stub(:body).and_return('<p></p>') + 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..b2e8141e0 --- /dev/null +++ b/spec/lib/attachment_to_html/adapters/text_spec.rb @@ -0,0 +1,70 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') + +describe AttachmentToHTML::Adapters::Text do + + let(:attachment) { FactoryGirl.build(:body_text) } + let(:adapter) { AttachmentToHTML::Adapters::Text.new(attachment) } + + describe :title do + + it 'uses the attachment filename for the title' do + adapter.title.should == attachment.display_filename + end + + end + + describe :body do + + it 'extracts the body from the document' do + adapter.body.should == attachment.body + end + + it 'strips the body of trailing whitespace' do + attachment = FactoryGirl.build(:body_text, :body => ' Hello ') + adapter = AttachmentToHTML::Adapters::Text.new(attachment) + adapter.body.should == 'Hello' + end + + it 'escapes special characters' do + attachment = FactoryGirl.build(:body_text, :body => 'Usage: foo "bar" >baz<') + adapter = AttachmentToHTML::Adapters::Text.new(attachment) + expected = %Q(Usage: foo "bar" >baz<) + 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') + adapter = AttachmentToHTML::Adapters::Text.new(attachment) + expected = %Q(<a href='http://www.whatdotheyknow.com'>http://www.whatdotheyknow.com</a>) + adapter.body.should == expected + end + + it 'substitutes newlines for br tags' do + attachment = FactoryGirl.build(:body_text, :body => "A\nNewline") + adapter = AttachmentToHTML::Adapters::Text.new(attachment) + expected = %Q(A<br>Newline) + adapter.body.should == expected + end + + end + + describe :success? do + + it 'is successful if the body has content excluding the tags' do + adapter.stub(:body).and_return('<p>some content</p>') + adapter.success?.should be_true + end + + it 'is successful if the body contains images' do + adapter.stub(:body).and_return(%Q(<img src="logo.png" />)) + adapter.success?.should be_true + end + + it 'is not successful if the body has no content other than tags' do + adapter.stub(:body).and_return('<p></p>') + adapter.success?.should be_false + end + + end + +end |