diff options
Diffstat (limited to 'spec/lib')
-rw-r--r-- | spec/lib/alaveteli_external_command_spec.rb (renamed from spec/lib/alaveteli_external_command.rb) | 0 | ||||
-rw-r--r-- | spec/lib/attachment_to_html/adapters/could_not_convert_spec.rb | 36 | ||||
-rw-r--r-- | spec/lib/attachment_to_html/adapters/google_docs_viewer_spec.rb | 49 | ||||
-rw-r--r-- | spec/lib/attachment_to_html/adapters/pdf_spec.rb | 100 | ||||
-rw-r--r-- | spec/lib/attachment_to_html/adapters/rtf_spec.rb | 85 | ||||
-rw-r--r-- | spec/lib/attachment_to_html/adapters/text_spec.rb | 70 | ||||
-rw-r--r-- | spec/lib/attachment_to_html/attachment_to_html_spec.rb | 71 | ||||
-rw-r--r-- | spec/lib/attachment_to_html/view_spec.rb | 145 | ||||
-rw-r--r-- | spec/lib/basic_encoding_spec.rb (renamed from spec/lib/basic_encoding_tests.rb) | 20 | ||||
-rw-r--r-- | spec/lib/confidence_intervals_spec.rb (renamed from spec/lib/confidence_intervals.rb) | 0 | ||||
-rw-r--r-- | spec/lib/date_quarter_spec.rb | 31 | ||||
-rw-r--r-- | spec/lib/i18n_interpolation_spec.rb (renamed from spec/lib/i18n_interpolation.rb) | 1 |
12 files changed, 598 insertions, 10 deletions
diff --git a/spec/lib/alaveteli_external_command.rb b/spec/lib/alaveteli_external_command_spec.rb index 18afeda33..18afeda33 100644 --- a/spec/lib/alaveteli_external_command.rb +++ b/spec/lib/alaveteli_external_command_spec.rb 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..da79b2de0 --- /dev/null +++ b/spec/lib/attachment_to_html/adapters/pdf_spec.rb @@ -0,0 +1,100 @@ +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 + + it 'is not successful if the body contains more than 50 images' do + # Sometimes pdftohtml extracts images incorrectly, resulting + # in thousands of PNGs being created for one image. This creates + # a huge request spike when the converted attachment is requested. + # + # See bug report https://bugs.freedesktop.org/show_bug.cgi?id=77932 + + # Construct mocked HTML output with 51 images + invalid = <<-DOC + <!DOCTYPE html> + <HTML xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang=""> + <HEAD> + <TITLE>Microsoft Word - FOI 12-01605 Resp 1.doc</TITLE> + <META http-equiv="Content-Type" content="text/html; charset=UTF-8"/> + <META name="generator" content="pdftohtml 0.36"/> + <META name="author" content="8065"/> + <META name="date" content="2012-09-24T15:37:06+00:00"/> + </HEAD> + <BODY bgcolor="#A0A0A0" vlink="blue" link="blue"> + <A name=1></a><IMG src="FOI 12 01605 Resp 1 PDF-1_1.png"/><br/> + <IMG src="FOI 12 01605 Resp 1 PDF-1_2.png"/><br/> + DOC + + (3..51).each { |i| invalid += %Q(<IMG src="FOI 12 01605 Resp 1 PDF-1_#{i}.png"/><br/>) } + + invalid += <<-DOC +  <br/> + Some Content<br/> + <hr> + </BODY> + </HTML> + DOC + AlaveteliExternalCommand.stub(:run).and_return(invalid) + + 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 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..1cf7debb7 --- /dev/null +++ b/spec/lib/attachment_to_html/attachment_to_html_spec.rb @@ -0,0 +1,71 @@ +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 '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 'passes content injections options when rendering the result' do + html = to_html(attachment, :content_for => { :body_prefix => '<p>prefix</p>' }) + html.should include('<p>prefix</p>') + 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::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, {}).and_call_original + 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 + + describe 'when wrapping the content' do + + it 'uses a the default wrapper' do + attachment = FactoryGirl.build(:pdf_attachment) + to_html(attachment).should include(%Q(<div id="wrapper">)) + 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(<div id="wrapper_google_embed">)) + end + + end + + end + +end 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 => '<p>hello</p>', + :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 +<!DOCTYPE html> +<html> +<head> + <title>An attachment.txt</title> +</head> +<body> + <div id="wrap"> + <div id="view-html-content"> + <p>hello</p> + </div> + </div> +</body> +</html> + HTML + + view.render.gsub(/\s+/, '').should == expected.gsub(/\s+/, '') + end + + it 'allows the dynamic injection of content' do + content = %Q(<meta charset="utf-8">) + result = view.render { inject_content(:head_suffix) { content } } + result.should include(content) + end + + end + +end diff --git a/spec/lib/basic_encoding_tests.rb b/spec/lib/basic_encoding_spec.rb index 35d35fd4a..43a65eab9 100644 --- a/spec/lib/basic_encoding_tests.rb +++ b/spec/lib/basic_encoding_spec.rb @@ -4,8 +4,8 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') def bytes_to_binary_string( bytes, claimed_encoding = nil ) claimed_encoding ||= 'ASCII-8BIT' bytes_string = bytes.pack('c*') - if RUBY_VERSION.to_f >= 1.9 - bytes_string.force_encoding! claimed_encoding + if String.method_defined?(:force_encoding) + bytes_string.force_encoding claimed_encoding end bytes_string end @@ -110,15 +110,15 @@ describe "convert_string_to_utf8_or_binary" do converted = convert_string_to_utf8_or_binary random_string converted.should == random_string - if RUBY_VERSION.to_f >= 1.9 - converted.encoding.should == 'ASCII-8BIT' + if String.method_defined?(:encode) + converted.encoding.to_s.should == 'ASCII-8BIT' end converted = convert_string_to_utf8_or_binary random_string,'UTF-8' converted.should == random_string - if RUBY_VERSION.to_f >= 1.9 - converted.encoding.should == 'ASCII-8BIT' + if String.method_defined?(:encode) + converted.encoding.to_s.should == 'ASCII-8BIT' end end @@ -132,8 +132,8 @@ describe "convert_string_to_utf8_or_binary" do converted.should == "DASH – DASH" - if RUBY_VERSION.to_f >= 1.9 - converted.encoding.should == 'UTF-8' + if String.method_defined?(:encode) + converted.encoding.to_s.should == 'UTF-8' end end @@ -147,8 +147,8 @@ describe "convert_string_to_utf8_or_binary" do converted.should start_with("贵公司负责人") - if RUBY_VERSION.to_f >= 1.9 - converted.encoding.should == 'UTF-8' + if String.method_defined?(:encode) + converted.encoding.to_s.should == 'UTF-8' end end diff --git a/spec/lib/confidence_intervals.rb b/spec/lib/confidence_intervals_spec.rb index cb8717f3d..cb8717f3d 100644 --- a/spec/lib/confidence_intervals.rb +++ b/spec/lib/confidence_intervals_spec.rb diff --git a/spec/lib/date_quarter_spec.rb b/spec/lib/date_quarter_spec.rb new file mode 100644 index 000000000..5af6fa334 --- /dev/null +++ b/spec/lib/date_quarter_spec.rb @@ -0,0 +1,31 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe DateQuarter do + include DateQuarter + + describe :quarters_between do + + it 'returns all the quarters in a year' do + # This is a bit of a convoluted spec, since we have to convert each + # Time in to an Integer to make a reasonable comparison + # See http://makandracards.com/makandra/1057-why-two-ruby-time-objects-are-not-equal-although-they-appear-to-be + with_env_tz 'UTC' do + start = Time.parse('2014-01-01') + finish = Time.parse('2014-12-31') + + expected = [['Wed Jan 01 00:00:00 +0000 2014', 'Mon Mar 31 23:59:59 +0000 2014'], + ['Tue Apr 01 00:00:00 +0000 2014', 'Mon Jun 30 23:59:59 +0000 2014'], + ['Tue Jul 01 00:00:00 +0000 2014', 'Tue Sep 30 23:59:59 +0000 2014'], + ['Wed Oct 01 00:00:00 +0000 2014', 'Wed Dec 31 23:59:59 +0000 2014']]. + map { |pair| [Time.parse(pair[0]).to_i, Time.parse(pair[1]).to_i] } + + quarters_between(start, finish).each_with_index do |pair, i| + pair.map!(&:to_i) + pair.should == expected[i] + end + end + end + + end + +end diff --git a/spec/lib/i18n_interpolation.rb b/spec/lib/i18n_interpolation_spec.rb index b07cf1e9a..47037ecdb 100644 --- a/spec/lib/i18n_interpolation.rb +++ b/spec/lib/i18n_interpolation_spec.rb @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe "when using i18n" do |