diff options
Diffstat (limited to 'spec')
54 files changed, 1635 insertions, 264 deletions
diff --git a/spec/controllers/admin_spam_addresses_controller_spec.rb b/spec/controllers/admin_spam_addresses_controller_spec.rb new file mode 100644 index 000000000..da1e9bb5a --- /dev/null +++ b/spec/controllers/admin_spam_addresses_controller_spec.rb @@ -0,0 +1,91 @@ +require 'spec_helper' + +describe AdminSpamAddressesController do + render_views + before { basic_auth_login @request } + + describe :index do + + it 'lists the spam addresses' do + 3.times { FactoryGirl.create(:spam_address) } + get :index + assigns(:spam_addresses).should == SpamAddress.all + end + + it 'creates a new spam address for the form' do + get :index + expect(assigns(:spam_address)).to be_a_new(SpamAddress) + end + + it 'renders the index template' do + get :index + expect(response).to render_template('index') + end + + end + + describe :create do + + let(:spam_params) { FactoryGirl.attributes_for(:spam_address) } + + it 'creates a new spam address with the given parameters' do + post :create, :spam_address => spam_params + assigns(:spam_address).email.should == spam_params[:email] + assigns(:spam_address).should be_persisted + end + + it 'redirects to the index action if successful' do + SpamAddress.any_instance.stub(:save).and_return(true) + post :create, :spam_address => spam_params + expect(response).to redirect_to(spam_addresses_path) + end + + it 'notifies the admin the spam address has been created' do + SpamAddress.any_instance.stub(:save).and_return(true) + post :create, :spam_address => spam_params + msg = "#{ spam_params[:email] } has been added to the spam addresses list" + flash[:notice].should == msg + end + + it 'renders the index action if the address could not be saved' do + SpamAddress.any_instance.stub(:save).and_return(false) + post :create, :spam_address => spam_params + expect(response).to render_template('index') + end + + it 'collects the spam addresses if the address could not be saved' do + 3.times { FactoryGirl.create(:spam_address) } + SpamAddress.any_instance.stub(:save).and_return(false) + post :create, :spam_address => spam_params + assigns(:spam_addresses).should == SpamAddress.all + end + + end + + describe :delete do + + before(:each) do + @spam = FactoryGirl.create(:spam_address) + delete :destroy, :id => @spam.id + end + + it 'finds the spam address to delete' do + assigns(:spam_address).should == @spam + end + + it 'destroys the spam address' do + assigns(:spam_address).should be_destroyed + end + + it 'tells the admin the spam address has been deleted' do + msg = "#{ @spam.email } has been removed from the spam addresses list" + flash[:notice].should == msg + end + + it 'redirects to the index action' do + expect(response).to redirect_to(spam_addresses_path) + end + + end + +end diff --git a/spec/controllers/admin_user_controller_spec.rb b/spec/controllers/admin_user_controller_spec.rb index 99894a414..8b89506f9 100644 --- a/spec/controllers/admin_user_controller_spec.rb +++ b/spec/controllers/admin_user_controller_spec.rb @@ -44,3 +44,72 @@ describe AdminUserController, "when updating a user" do end end + +describe AdminUserController do + + describe :modify_comment_visibility do + + before(:each) do + @user = FactoryGirl.create(:user) + request.env["HTTP_REFERER"] = admin_user_show_path(@user) + end + + it 'redirects to the page the admin was previously on' do + comment = FactoryGirl.create(:visible_comment, :user => @user) + + post :modify_comment_visibility, { :id => @user.id, + :comment_ids => comment.id, + :hide_selected => 'hidden' } + + response.should redirect_to(admin_user_show_path(@user)) + end + + it 'sets the given comments visibility to hidden' do + comments = FactoryGirl.create_list(:visible_comment, 3, :user => @user) + comment_ids = comments.map(&:id) + + post :modify_comment_visibility, { :id => @user.id, + :comment_ids => comment_ids, + :hide_selected => 'hidden' } + + Comment.find(comment_ids).each { |comment| comment.should_not be_visible } + end + + it 'sets the given comments visibility to visible' do + comments = FactoryGirl.create_list(:hidden_comment, 3, :user => @user) + comment_ids = comments.map(&:id) + + post :modify_comment_visibility, { :id => @user.id, + :comment_ids => comment_ids, + :unhide_selected => 'visible' } + + Comment.find(comment_ids).each { |comment| comment.should be_visible } + end + + it 'only modifes the given list of comments' do + unaffected_comment = FactoryGirl.create(:hidden_comment, :user => @user) + affected_comment = FactoryGirl.create(:hidden_comment, :user => @user) + + post :modify_comment_visibility, { :id => @user.id, + :comment_ids => affected_comment.id, + :unhide_selected => 'visible' } + + Comment.find(unaffected_comment).should_not be_visible + Comment.find(affected_comment).should be_visible + end + + it 'preserves the visibility if a comment is already of the requested visibility' do + hidden_comment = FactoryGirl.create(:hidden_comment, :user => @user) + visible_comment = FactoryGirl.create(:visible_comment, :user => @user) + comment_ids = [hidden_comment.id, visible_comment.id] + + post :modify_comment_visibility, { :id => @user.id, + :comment_ids => comment_ids, + :unhide_selected => 'visible' } + + Comment.find(comment_ids).each { |c| c.should be_visible } + end + + end + +end diff --git a/spec/controllers/comment_controller_spec.rb b/spec/controllers/comment_controller_spec.rb index c03615ce2..5e250f689 100644 --- a/spec/controllers/comment_controller_spec.rb +++ b/spec/controllers/comment_controller_spec.rb @@ -53,16 +53,30 @@ describe CommentController, "when commenting on a request" do response.should render_template('new') end - + it "should not allow comments if comments are not allowed" do - session[:user_id] = users(:silly_name_user).id - - expect { - post :new, :url_title => info_requests(:spam_1_request).url_title, - :comment => { :body => "I demand to be heard!" }, - :type => 'request', :submitted_comment => 1, :preview => 0 - }.to raise_error("Comments are not allowed on this request") - + session[:user_id] = users(:silly_name_user).id + info_request = info_requests(:spam_1_request) + + post :new, :url_title => info_request.url_title, + :comment => { :body => "I demand to be heard!" }, + :type => 'request', :submitted_comment => 1, :preview => 0 + + response.should redirect_to(show_request_path(info_request.url_title)) + flash[:notice].should == 'Comments are not allowed on this request' + end + + it "should not allow comments from banned users" do + User.any_instance.stub(:ban_text).and_return('Banned from commenting') + + user = users(:silly_name_user) + session[:user_id] = user.id + + post :new, :url_title => info_requests(:fancy_dog_request).url_title, + :comment => { :body => comments(:silly_comment).body }, + :type => 'request', :submitted_comment => 1, :preview => 0 + + response.should render_template('user/banned') end describe 'when commenting on an external request' do diff --git a/spec/controllers/help_controller_spec.rb b/spec/controllers/help_controller_spec.rb index cc024f840..f92323f50 100644 --- a/spec/controllers/help_controller_spec.rb +++ b/spec/controllers/help_controller_spec.rb @@ -1,48 +1,81 @@ # -*- coding: utf-8 -*- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') -describe HelpController, "when using help" do +describe HelpController do render_views - it "shows the about page" do - get :about - end + describe :about do - it "shows contact form" do - get :contact - end + it 'shows the about page' do + get :about + response.should be_success + response.should render_template('help/about') + end - it "sends a contact message" do - post :contact, { :contact => { - :name => "Vinny Vanilli", - :email => "vinny@localhost", - :subject => "Why do I have such an ace name?", - :message => "You really should know!!!\n\nVinny", - }, :submitted_contact_form => 1 - } - response.should redirect_to(:controller => 'general', :action => 'frontpage') - - deliveries = ActionMailer::Base.deliveries - deliveries.size.should == 1 - deliveries[0].body.should include("really should know") - deliveries.clear end - describe 'when requesting a page in a supported locale ' do + describe 'GET contact' do - before do - # Prepend our fixture templates - fixture_theme_path = File.join(Rails.root, 'spec', 'fixtures', 'theme_views', 'theme_one') - controller.prepend_view_path fixture_theme_path + it 'shows contact form' do + get :contact + response.should be_success + response.should render_template('help/contact') end - it 'should render the locale-specific template if available' do - get :contact, {:locale => 'es'} - response.body.should match('contáctenos theme one') + describe 'when requesting a page in a supported locale' do + + before do + # Prepend our fixture templates + fixture_theme_path = File.join(Rails.root, 'spec', 'fixtures', 'theme_views', 'theme_one') + controller.prepend_view_path fixture_theme_path + end + + it 'should render the locale-specific template if available' do + get :contact, {:locale => 'es'} + response.body.should match('contáctenos theme one') + end + end end + describe 'POST contact' do + + it 'sends a contact message' do + post :contact, { :contact => { + :name => 'Vinny Vanilli', + :email => 'vinny@localhost', + :subject => 'Why do I have such an ace name?', + :comment => '', + :message => "You really should know!!!\n\nVinny", + }, :submitted_contact_form => 1 + } + response.should redirect_to(frontpage_path) + + deliveries = ActionMailer::Base.deliveries + deliveries.size.should == 1 + deliveries[0].body.should include('really should know') + deliveries.clear + end + + it 'has rudimentary spam protection' do + post :contact, { :contact => { + :name => 'Vinny Vanilli', + :email => 'vinny@localhost', + :subject => 'Why do I have such an ace name?', + :comment => 'I AM A SPAMBOT', + :message => "You really should know!!!\n\nVinny", + }, :submitted_contact_form => 1 + } + + response.should redirect_to(frontpage_path) + + deliveries = ActionMailer::Base.deliveries + deliveries.size.should == 0 + deliveries.clear + end + + end end diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb index 1e7df4536..9353efcb3 100644 --- a/spec/controllers/request_controller_spec.rb +++ b/spec/controllers/request_controller_spec.rb @@ -2407,8 +2407,7 @@ describe RequestController, "when caching fragments" do :html_mask_stuff! => nil, :user_can_view? => true, :all_can_view? => true) - attachment = mock(FoiAttachment, :display_filename => long_name, - :body_as_html => ['some text', 'wrapper']) + attachment = FactoryGirl.build(:body_text, :filename => long_name) IncomingMessage.stub!(:find).with("44").and_return(incoming_message) IncomingMessage.stub!(:get_attachment_by_url_part_number_and_filename).and_return(attachment) InfoRequest.stub!(:find).with("132").and_return(info_request) diff --git a/spec/controllers/track_controller_spec.rb b/spec/controllers/track_controller_spec.rb index 40865d2b9..d2b45b6bf 100644 --- a/spec/controllers/track_controller_spec.rb +++ b/spec/controllers/track_controller_spec.rb @@ -5,7 +5,7 @@ describe TrackController, "when making a new track on a request" do @ir = mock_model(InfoRequest, :url_title => 'myrequest', :title => 'My request') @track_thing = mock_model(TrackThing, :save! => true, - :params => {:list_description => 'list description'}, + :params => {}, :track_medium= => nil, :tracking_user_id= => nil) TrackThing.stub!(:create_track_for_request).and_return(@track_thing) @@ -58,7 +58,7 @@ end describe TrackController, "when unsubscribing from a track" do before do - @track_thing = FactoryGirl.create(:track_thing) + @track_thing = FactoryGirl.create(:search_track) end it 'should destroy the track thing' do @@ -78,7 +78,7 @@ describe TrackController, "when unsubscribing from a track" do end it 'should not redirect to a url on another site' do - track_thing = FactoryGirl.create(:track_thing) + track_thing = FactoryGirl.create(:search_track) get :update, {:track_id => @track_thing.id, :track_medium => 'delete', :r => 'http://example.com/'}, diff --git a/spec/factories.rb b/spec/factories.rb deleted file mode 100644 index 8efc53033..000000000 --- a/spec/factories.rb +++ /dev/null @@ -1,165 +0,0 @@ -FactoryGirl.define do - - sequence(:email) { |n| "person#{n}@example.com" } - sequence(:name) { |n| "Example Public Body #{n}" } - sequence(:short_name) { |n| "Example Body #{n}" } - - factory :foi_attachment do - factory :body_text do - content_type 'text/plain' - body { 'hereisthetext' } - end - factory :pdf_attachment do - content_type 'application/pdf' - filename 'interesting.pdf' - body { load_file_fixture('interesting.pdf') } - end - end - - factory :incoming_message do - info_request - raw_email - last_parsed { 1.week.ago } - sent_at { 1.week.ago } - - after_create do |incoming_message, evaluator| - FactoryGirl.create(:body_text, - :incoming_message => incoming_message, - :url_part_number => 1) - end - - factory :plain_incoming_message do - last_parsed { nil } - sent_at { nil } - after_create do |incoming_message, evaluator| - data = load_file_fixture('incoming-request-plain.email') - data.gsub!('EMAIL_FROM', 'Bob Responder <bob@example.com>') - incoming_message.raw_email.data = data - incoming_message.raw_email.save! - end - end - - factory :incoming_message_with_attachments do - # foi_attachments_count is declared as an ignored attribute and available in - # attributes on the factory, as well as the callback via the evaluator - ignore do - foi_attachments_count 2 - end - - # the after(:create) yields two values; the incoming_message instance itself and the - # evaluator, which stores all values from the factory, including ignored - # attributes; - after_create do |incoming_message, evaluator| - evaluator.foi_attachments_count.times do |count| - FactoryGirl.create(:pdf_attachment, - :incoming_message => incoming_message, - :url_part_number => count+2) - end - end - end - end - - factory :raw_email - - factory :outgoing_message do - factory :initial_request do - ignore do - status 'ready' - message_type 'initial_request' - body 'Some information please' - what_doing 'normal_sort' - end - initialize_with { OutgoingMessage.new({ :status => status, - :message_type => message_type, - :body => body, - :what_doing => what_doing }) } - after_create do |outgoing_message| - outgoing_message.send_message - end - end - end - - factory :info_request do - title "Example Title" - public_body - user - - after_create do |info_request, evaluator| - FactoryGirl.create(:initial_request, :info_request => info_request) - end - - factory :info_request_with_incoming do - after_create do |info_request, evaluator| - incoming_message = FactoryGirl.create(:incoming_message, :info_request => info_request) - info_request.log_event("response", {:incoming_message_id => incoming_message.id}) - end - end - - factory :info_request_with_plain_incoming do - after_create do |info_request, evaluator| - incoming_message = FactoryGirl.create(:plain_incoming_message, :info_request => info_request) - info_request.log_event("response", {:incoming_message_id => incoming_message.id}) - end - end - - factory :info_request_with_incoming_attachments do - after_create do |info_request, evaluator| - incoming_message = FactoryGirl.create(:incoming_message_with_attachments, :info_request => info_request) - info_request.log_event("response", {:incoming_message_id => incoming_message.id}) - end - end - - factory :external_request do - user nil - external_user_name 'External User' - external_url 'http://www.example.org/request/external' - end - - end - - factory :user do - name 'Example User' - email - salt "-6116981980.392287733335677" - hashed_password '6b7cd45a5f35fd83febc0452a799530398bfb6e8' # jonespassword - email_confirmed true - ban_text "" - factory :admin_user do - name 'Admin User' - admin_level 'super' - end - end - - factory :public_body do - name - short_name - request_email 'request@example.com' - last_edit_editor "admin user" - last_edit_comment "Making an edit" - end - - factory :track_thing do - association :tracking_user, :factory => :user - track_medium 'email_daily' - track_type 'search_query' - track_query 'Example Query' - end - - factory :public_body_change_request do - user - source_url 'http://www.example.com' - notes 'Please' - public_body_email 'new@example.com' - factory :add_body_request do - public_body_name 'A New Body' - end - factory :update_body_request do - public_body - end - end - factory :info_request_batch do - title "Example title" - user - body "Some text" - end -end diff --git a/spec/factories/comments.rb b/spec/factories/comments.rb new file mode 100644 index 000000000..1e0861dad --- /dev/null +++ b/spec/factories/comments.rb @@ -0,0 +1,19 @@ +FactoryGirl.define do + + factory :comment do + user + info_request + + body 'This a wise and helpful annotation.' + comment_type 'request' + + factory :visible_comment do + visible true + end + + factory :hidden_comment do + visible false + end + end + +end diff --git a/spec/factories/foi_attchments.rb b/spec/factories/foi_attchments.rb new file mode 100644 index 000000000..4e9875a00 --- /dev/null +++ b/spec/factories/foi_attchments.rb @@ -0,0 +1,21 @@ +FactoryGirl.define do + + factory :foi_attachment do + factory :body_text do + content_type 'text/plain' + body { 'hereisthetext' } + filename 'attachment.txt' + end + factory :pdf_attachment do + content_type 'application/pdf' + filename 'interesting.pdf' + body { load_file_fixture('interesting.pdf') } + end + factory :rtf_attachment do + content_type 'application/rtf' + filename 'interesting.rtf' + body { load_file_fixture('interesting.rtf') } + end + end + +end diff --git a/spec/factories/incoming_messages.rb b/spec/factories/incoming_messages.rb new file mode 100644 index 000000000..38ad98394 --- /dev/null +++ b/spec/factories/incoming_messages.rb @@ -0,0 +1,46 @@ +FactoryGirl.define do + + factory :incoming_message do + info_request + raw_email + last_parsed { 1.week.ago } + sent_at { 1.week.ago } + + after_create do |incoming_message, evaluator| + FactoryGirl.create(:body_text, + :incoming_message => incoming_message, + :url_part_number => 1) + end + + factory :plain_incoming_message do + last_parsed { nil } + sent_at { nil } + after_create do |incoming_message, evaluator| + data = load_file_fixture('incoming-request-plain.email') + data.gsub!('EMAIL_FROM', 'Bob Responder <bob@example.com>') + incoming_message.raw_email.data = data + incoming_message.raw_email.save! + end + end + + factory :incoming_message_with_attachments do + # foi_attachments_count is declared as an ignored attribute and available in + # attributes on the factory, as well as the callback via the evaluator + ignore do + foi_attachments_count 2 + end + + # the after(:create) yields two values; the incoming_message instance itself and the + # evaluator, which stores all values from the factory, including ignored + # attributes; + after_create do |incoming_message, evaluator| + evaluator.foi_attachments_count.times do |count| + FactoryGirl.create(:pdf_attachment, + :incoming_message => incoming_message, + :url_part_number => count+2) + end + end + end + end + +end diff --git a/spec/factories/info_request_batches.rb b/spec/factories/info_request_batches.rb new file mode 100644 index 000000000..960db6ec5 --- /dev/null +++ b/spec/factories/info_request_batches.rb @@ -0,0 +1,9 @@ +FactoryGirl.define do + + factory :info_request_batch do + title "Example title" + user + body "Some text" + end + +end diff --git a/spec/factories/info_requests.rb b/spec/factories/info_requests.rb new file mode 100644 index 000000000..8052625cd --- /dev/null +++ b/spec/factories/info_requests.rb @@ -0,0 +1,47 @@ +FactoryGirl.define do + + factory :info_request do + title "Example Title" + public_body + user + + after_create do |info_request, evaluator| + FactoryGirl.create(:initial_request, :info_request => info_request) + end + + factory :info_request_with_incoming do + after_create do |info_request, evaluator| + incoming_message = FactoryGirl.create(:incoming_message, :info_request => info_request) + info_request.log_event("response", {:incoming_message_id => incoming_message.id}) + end + end + + factory :info_request_with_plain_incoming do + after_create do |info_request, evaluator| + incoming_message = FactoryGirl.create(:plain_incoming_message, :info_request => info_request) + info_request.log_event("response", {:incoming_message_id => incoming_message.id}) + end + end + + factory :info_request_with_incoming_attachments do + after_create do |info_request, evaluator| + incoming_message = FactoryGirl.create(:incoming_message_with_attachments, :info_request => info_request) + info_request.log_event("response", {:incoming_message_id => incoming_message.id}) + end + end + + factory :info_request_with_internal_review_request do + after_create do |info_request, evaluator| + outgoing_message = FactoryGirl.create(:internal_review_request, :info_request => info_request) + end + end + + factory :external_request do + user nil + external_user_name 'External User' + external_url 'http://www.example.org/request/external' + end + + end + +end diff --git a/spec/factories/outgoing_messages.rb b/spec/factories/outgoing_messages.rb new file mode 100644 index 000000000..d1ed25093 --- /dev/null +++ b/spec/factories/outgoing_messages.rb @@ -0,0 +1,29 @@ +FactoryGirl.define do + + factory :outgoing_message do + factory :initial_request do + ignore do + status 'ready' + message_type 'initial_request' + body 'Some information please' + what_doing 'normal_sort' + end + end + factory :internal_review_request do + ignore do + status 'ready' + message_type 'followup' + body 'I want a review' + what_doing 'internal_review' + end + end + initialize_with { OutgoingMessage.new({ :status => status, + :message_type => message_type, + :body => body, + :what_doing => what_doing }) } + after_create do |outgoing_message| + outgoing_message.send_message + end + end + +end diff --git a/spec/factories/public_bodies.rb b/spec/factories/public_bodies.rb new file mode 100644 index 000000000..44769f7c2 --- /dev/null +++ b/spec/factories/public_bodies.rb @@ -0,0 +1,12 @@ +FactoryGirl.define do + + factory :public_body do + sequence(:name) { |n| "Example Public Body #{n}" } + sequence(:short_name) { |n| "Example Body #{n}" } + request_email 'request@example.com' + last_edit_editor "admin user" + last_edit_comment "Making an edit" + end + + +end diff --git a/spec/factories/public_body_change_requests.rb b/spec/factories/public_body_change_requests.rb new file mode 100644 index 000000000..2bacb9b9b --- /dev/null +++ b/spec/factories/public_body_change_requests.rb @@ -0,0 +1,16 @@ +FactoryGirl.define do + + factory :public_body_change_request do + user + source_url 'http://www.example.com' + notes 'Please' + public_body_email 'new@example.com' + factory :add_body_request do + public_body_name 'A New Body' + end + factory :update_body_request do + public_body + end + end + +end diff --git a/spec/factories/raw_emails.rb b/spec/factories/raw_emails.rb new file mode 100644 index 000000000..30fb24c37 --- /dev/null +++ b/spec/factories/raw_emails.rb @@ -0,0 +1,5 @@ +FactoryGirl.define do + + factory :raw_email + +end diff --git a/spec/factories/spam_addresses.rb b/spec/factories/spam_addresses.rb new file mode 100644 index 000000000..bafb7cd50 --- /dev/null +++ b/spec/factories/spam_addresses.rb @@ -0,0 +1,5 @@ +FactoryGirl.define do + factory :spam_address do + sequence(:email) { |n| "spam-#{ n }@example.org" } + end +end diff --git a/spec/factories/track_things.rb b/spec/factories/track_things.rb new file mode 100644 index 000000000..cf76b00b3 --- /dev/null +++ b/spec/factories/track_things.rb @@ -0,0 +1,30 @@ +FactoryGirl.define do + + factory :track_thing do + association :tracking_user, :factory => :user + factory :search_track do + track_medium 'email_daily' + track_type 'search_query' + track_query 'Example Query' + end + factory :user_track do + association :tracked_user, :factory => :user + track_type 'user_updates' + end + factory :public_body_track do + association :public_body, :factory => :public_body + track_type 'public_body_updates' + end + factory :request_update_track do + association :info_request, :factory => :info_request + track_type 'request_updates' + end + factory :successful_request_track do + track_type 'all_successful_requests' + end + factory :new_request_track do + track_type 'all_new_requests' + end + end + +end diff --git a/spec/factories/users.rb b/spec/factories/users.rb new file mode 100644 index 000000000..ab782fbf7 --- /dev/null +++ b/spec/factories/users.rb @@ -0,0 +1,16 @@ +FactoryGirl.define do + + factory :user do + name 'Example User' + sequence(:email) { |n| "person#{n}@example.com" } + salt "-6116981980.392287733335677" + hashed_password '6b7cd45a5f35fd83febc0452a799530398bfb6e8' # jonespassword + email_confirmed true + ban_text "" + factory :admin_user do + name 'Admin User' + admin_level 'super' + end + end + +end diff --git a/spec/fixtures/files/attachment_to_html/alternative_template.html.erb b/spec/fixtures/files/attachment_to_html/alternative_template.html.erb new file mode 100644 index 000000000..024565d5a --- /dev/null +++ b/spec/fixtures/files/attachment_to_html/alternative_template.html.erb @@ -0,0 +1,2 @@ +<h1><%= @title %></h1> +<div><%= @body %></div>
\ No newline at end of file diff --git a/spec/fixtures/files/empty.rtf b/spec/fixtures/files/empty.rtf new file mode 100644 index 000000000..82dd2964a --- /dev/null +++ b/spec/fixtures/files/empty.rtf @@ -0,0 +1,5 @@ +{\rtf1\ansi\ansicpg1252\cocoartf1265\cocoasubrtf190 +{\fonttbl} +{\colortbl;\red255\green255\blue255;} +\paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0 +}
\ No newline at end of file diff --git a/spec/fixtures/files/empty.txt b/spec/fixtures/files/empty.txt new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/spec/fixtures/files/empty.txt diff --git a/spec/fixtures/files/interesting.rtf b/spec/fixtures/files/interesting.rtf new file mode 100644 index 000000000..fa95b53b5 --- /dev/null +++ b/spec/fixtures/files/interesting.rtf @@ -0,0 +1,7 @@ +{\rtf1\ansi\ansicpg1252\cocoartf1265\cocoasubrtf190 +{\fonttbl\f0\fswiss\fcharset0 Helvetica;} +{\colortbl;\red255\green255\blue255;} +\paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0 +\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural + +\f0\fs24 \cf0 thisisthebody}
\ No newline at end of file diff --git a/spec/fixtures/info_requests.yml b/spec/fixtures/info_requests.yml index d64807a49..d523236b5 100644 --- a/spec/fixtures/info_requests.yml +++ b/spec/fixtures/info_requests.yml @@ -20,6 +20,7 @@ # external_url :string(255) # attention_requested :boolean default(FALSE) # comments_allowed :boolean default(TRUE), not null +# info_request_batch_id :integer # fancy_dog_request: diff --git a/spec/fixtures/public_bodies.yml b/spec/fixtures/public_bodies.yml index d0eb572b3..16c12d9e2 100644 --- a/spec/fixtures/public_bodies.yml +++ b/spec/fixtures/public_bodies.yml @@ -4,7 +4,7 @@ # # id :integer not null, primary key # name :text not null -# short_name :text not null +# short_name :text default(""), not null # request_email :text not null # version :integer not null # last_edit_editor :string(255) not null diff --git a/spec/fixtures/users.yml b/spec/fixtures/users.yml index 9eb91ee9c..fb0414ff2 100644 --- a/spec/fixtures/users.yml +++ b/spec/fixtures/users.yml @@ -2,24 +2,25 @@ # # Table name: users # -# id :integer not null, primary key -# email :string(255) not null -# name :string(255) not null -# hashed_password :string(255) not null -# salt :string(255) not null -# created_at :datetime not null -# updated_at :datetime not null -# email_confirmed :boolean default(FALSE), not null -# url_name :text not null -# last_daily_track_email :datetime default(2000-01-01 00:00:00 UTC) -# admin_level :string(255) default("none"), not null -# ban_text :text default(""), not null -# about_me :text default(""), not null -# locale :string(255) -# email_bounced_at :datetime -# email_bounce_message :text default(""), not null -# no_limit :boolean default(FALSE), not null -# receive_email_alerts :boolean default(TRUE), not null +# id :integer not null, primary key +# email :string(255) not null +# name :string(255) not null +# hashed_password :string(255) not null +# salt :string(255) not null +# created_at :datetime not null +# updated_at :datetime not null +# email_confirmed :boolean default(FALSE), not null +# url_name :text not null +# last_daily_track_email :datetime default(Sat Jan 01 00:00:00 UTC 2000) +# admin_level :string(255) default("none"), not null +# ban_text :text default(""), not null +# about_me :text default(""), not null +# locale :string(255) +# email_bounced_at :datetime +# email_bounce_message :text default(""), not null +# no_limit :boolean default(FALSE), not null +# receive_email_alerts :boolean default(TRUE), not null +# can_make_batch_requests :boolean default(FALSE), not null # bob_smith_user: diff --git a/spec/helpers/admin_helper_spec.rb b/spec/helpers/admin_helper_spec.rb new file mode 100644 index 000000000..804fcc7fd --- /dev/null +++ b/spec/helpers/admin_helper_spec.rb @@ -0,0 +1,21 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe AdminHelper do + + include AdminHelper + + describe :comment_visibility do + + it 'shows the status of a visible comment' do + comment = Factory.build(:visible_comment) + comment_visibility(comment).should == 'Visible' + end + + it 'shows the status of a hidden comment' do + comment = Factory.build(:hidden_comment) + comment_visibility(comment).should == 'Hidden' + end + + end + +end diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb new file mode 100644 index 000000000..6407eaf3a --- /dev/null +++ b/spec/helpers/application_helper_spec.rb @@ -0,0 +1,34 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe ApplicationHelper do + + include ApplicationHelper + include LinkToHelper + + describe 'when creating an event description' do + + it 'should generate a description for a request' do + @info_request = FactoryGirl.create(:info_request) + @sent_event = @info_request.get_last_event + expected = "Request sent to #{public_body_link_absolute(@info_request.public_body)} by #{request_user_link_absolute(@info_request)}" + event_description(@sent_event).should match(expected) + + end + + it 'should generate a description for a response' do + @info_request_with_incoming = FactoryGirl.create(:info_request_with_incoming) + @response_event = @info_request_with_incoming.get_last_event + expected = "Response by #{public_body_link_absolute(@info_request_with_incoming.public_body)} to #{request_user_link_absolute(@info_request_with_incoming)}" + event_description(@response_event).should match(expected) + end + + it 'should generate a description for a request where an internal review has been requested' do + @info_request_with_internal_review_request = FactoryGirl.create(:info_request_with_internal_review_request) + @response_event = @info_request_with_internal_review_request.get_last_event + expected = "Internal review request sent to #{public_body_link_absolute(@info_request_with_internal_review_request.public_body)} by #{request_user_link_absolute(@info_request_with_internal_review_request)}" + event_description(@response_event).should match(expected) + end + + end + +end diff --git a/spec/helpers/link_to_helper_spec.rb b/spec/helpers/link_to_helper_spec.rb index 2259db6c2..4a01ec683 100644 --- a/spec/helpers/link_to_helper_spec.rb +++ b/spec/helpers/link_to_helper_spec.rb @@ -70,14 +70,50 @@ describe LinkToHelper do end describe 'simple_date' do + + it 'formats a date in html by default' do + time = Time.utc(2012, 11, 07, 21, 30, 26) + self.should_receive(:simple_date_html).with(time) + simple_date(time) + end + + it 'formats a date in the specified format' do + time = Time.utc(2012, 11, 07, 21, 30, 26) + self.should_receive(:simple_date_text).with(time) + simple_date(time, :format => :text) + end + + it 'raises an argument error if given an unrecognized format' do + time = Time.utc(2012, 11, 07, 21, 30, 26) + expect { simple_date(time, :format => :unknown) }.to raise_error(ArgumentError) + end + + end + + describe 'simple_date_html' do + + it 'formats a date in a time tag' do + Time.use_zone('London') do + time = Time.utc(2012, 11, 07, 21, 30, 26) + expected = "<time datetime=\"2012-11-07T21:30:26+00:00\" title=\"2012-11-07 21:30:26 +0000\">November 07, 2012</time>" + simple_date_html(time).should == expected + end + end + + end + + describe 'simple_date_text' do + it 'should respect time zones' do Time.use_zone('Australia/Sydney') do - simple_date(Time.utc(2012, 11, 07, 21, 30, 26)).should == 'November 08, 2012' + simple_date_text(Time.utc(2012, 11, 07, 21, 30, 26)).should == 'November 08, 2012' end end it 'should handle Date objects' do - simple_date(Date.new(2012, 11, 21)).should == 'November 21, 2012' + simple_date_text(Date.new(2012, 11, 21)).should == 'November 21, 2012' end + end + end diff --git a/spec/helpers/track_helper_spec.rb b/spec/helpers/track_helper_spec.rb new file mode 100644 index 000000000..b6252ab39 --- /dev/null +++ b/spec/helpers/track_helper_spec.rb @@ -0,0 +1,204 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe TrackHelper do + + include TrackHelper + include LinkToHelper + + describe 'when displaying notices for a search track' do + + before do + @track_thing = FactoryGirl.build(:search_track) + end + + it 'should create an already subscribed_notice' do + expected = %Q(You are already subscribed to <a href="/search/Example%20Query/newest/advanced">this search</a>.) + already_subscribed_notice(@track_thing).should == expected + end + + it 'should create an email subscription notice' do + expected = %Q(You will now be emailed updates about <a href="/search/Example%20Query/newest/advanced">this search</a>.) + subscribe_email_notice(@track_thing).should == expected + end + + it 'should create a following subscription notice' do + expected = %Q(You are now <a href="#{show_user_wall_path(:url_name => @track_thing.tracking_user.url_name)}">following</a> updates about <a href="/search/Example%20Query/newest/advanced">this search</a>.) + subscribe_follow_notice(@track_thing).should == expected + end + + it 'should create an unsubscribe notice' do + expected = %Q(You are no longer following <a href="/search/Example%20Query/newest/advanced">this search</a>.) + unsubscribe_notice(@track_thing).should == expected + end + + it 'should create a description of the track' do + expected = %Q(<a href="/search/Example%20Query/newest/advanced">anything matching text 'Example Query'</a>) + track_description(@track_thing).should == expected + end + + end + + describe 'when displaying notices for a user track' do + + before do + @track_thing = FactoryGirl.build(:user_track) + end + + it 'should create an already subscribed_notice' do + expected = %Q(You are already subscribed to '#{user_link(@track_thing.tracked_user)}', a person.) + already_subscribed_notice(@track_thing).should == expected + end + + it 'should create an email subscription notice' do + expected = %Q(You will now be emailed updates about '#{user_link(@track_thing.tracked_user)}', a person.) + subscribe_email_notice(@track_thing).should == expected + end + + it 'should create a following subscription notice' do + expected = %Q(You are now <a href="#{show_user_wall_path(:url_name => @track_thing.tracking_user.url_name)}">following</a> updates about '#{user_link(@track_thing.tracked_user)}', a person.) + subscribe_follow_notice(@track_thing).should == expected + end + + it 'should create an unsubscribe notice' do + expected = %Q(You are no longer following '#{user_link(@track_thing.tracked_user)}', a person.) + unsubscribe_notice(@track_thing).should == expected + end + + it 'should create a description of the track' do + expected = %Q('#{user_link(@track_thing.tracked_user)}', a person) + track_description(@track_thing).should == expected + end + + end + + describe 'when displaying notices for a public body track' do + + before do + @track_thing = FactoryGirl.build(:public_body_track) + end + + it 'should create an already subscribed_notice' do + expected = %Q(You are already subscribed to '#{public_body_link(@track_thing.public_body)}', a public authority.) + already_subscribed_notice(@track_thing).should == expected + end + + it 'should create an email subscription notice' do + expected = %Q(You will now be emailed updates about '#{public_body_link(@track_thing.public_body)}', a public authority.) + subscribe_email_notice(@track_thing).should == expected + end + + it 'should create a following subscription notice' do + expected = %Q(You are now <a href="#{show_user_wall_path(:url_name => @track_thing.tracking_user.url_name)}">following</a> updates about '#{public_body_link(@track_thing.public_body)}', a public authority.) + subscribe_follow_notice(@track_thing).should == expected + end + + it 'should create an unsubscribe notice' do + expected = %Q(You are no longer following '#{public_body_link(@track_thing.public_body)}', a public authority.) + unsubscribe_notice(@track_thing).should == expected + end + + it 'should create a description of the track' do + expected = %Q('#{public_body_link(@track_thing.public_body)}', a public authority) + track_description(@track_thing).should == expected + end + end + + describe 'when displaying notices for a successful request track' do + + before do + @track_thing = FactoryGirl.build(:successful_request_track) + end + + it 'should create an already subscribed_notice' do + expected = %Q(You are already subscribed to any <a href="/list/successful">successful requests</a>.) + already_subscribed_notice(@track_thing).should == expected + end + + it 'should create an email subscription notice' do + expected = %Q(You will now be emailed updates about <a href="/list/successful">successful requests</a>.) + subscribe_email_notice(@track_thing).should == expected + end + + it 'should create a following subscription notice' do + expected = %Q(You are now <a href="#{show_user_wall_path(:url_name => @track_thing.tracking_user.url_name)}">following</a> updates about <a href="/list/successful">successful requests</a>.) + subscribe_follow_notice(@track_thing).should == expected + end + + it 'should create an unsubscribe notice' do + expected = %Q(You are no longer following <a href="/list/successful">successful requests</a>.) + unsubscribe_notice(@track_thing).should == expected + end + + it 'should create a description of the track' do + expected = %Q(<a href="/list/successful">successful requests</a>) + track_description(@track_thing).should == expected + end + end + + describe 'when displaying notices for a new request track' do + + before do + @track_thing = FactoryGirl.build(:new_request_track) + end + + it 'should create an already subscribed_notice' do + expected = %Q(You are already subscribed to any <a href="/list">new requests</a>.) + already_subscribed_notice(@track_thing).should == expected + end + + it 'should create an email subscription notice' do + expected = %Q(You will now be emailed updates about any <a href="/list">new requests</a>.) + subscribe_email_notice(@track_thing).should == expected + end + + it 'should create a following subscription notice' do + expected = %Q(You are now <a href="#{show_user_wall_path(:url_name => @track_thing.tracking_user.url_name)}">following</a> updates about <a href="/list">new requests</a>.) + subscribe_follow_notice(@track_thing).should == expected + end + + it 'should create an unsubscribe notice' do + expected = %Q(You are no longer following <a href="/list">new requests</a>.) + unsubscribe_notice(@track_thing).should == expected + end + + it 'should create a description of the track' do + expected = %Q(<a href="/list">new requests</a>) + track_description(@track_thing).should == expected + end + + end + + describe 'when displaying notices for a request update track' do + + before do + @track_thing = FactoryGirl.build(:request_update_track) + end + + it 'should create an already subscribed_notice' do + expected = %Q(You are already subscribed to '#{request_link(@track_thing.info_request)}', a request.) + already_subscribed_notice(@track_thing).should == expected + end + + it 'should create an email subscription notice' do + expected = %Q(You will now be emailed updates about '#{request_link(@track_thing.info_request)}', a request.) + subscribe_email_notice(@track_thing).should == expected + end + + it 'should create a following subscription notice' do + expected = %Q(You are now <a href="#{show_user_wall_path(:url_name => @track_thing.tracking_user.url_name)}">following</a> updates about '#{request_link(@track_thing.info_request)}', a request.) + subscribe_follow_notice(@track_thing).should == expected + end + + it 'should create an unsubscribe notice' do + expected = %Q(You are no longer following '#{request_link(@track_thing.info_request)}', a request.) + unsubscribe_notice(@track_thing).should == expected + end + + it 'should create a description of the track' do + expected = %Q('#{request_link(@track_thing.info_request)}', a request) + track_description(@track_thing).should == expected + end + + end + +end diff --git a/spec/integration/errors_spec.rb b/spec/integration/errors_spec.rb index 17a0153c2..8ceb8243b 100644 --- a/spec/integration/errors_spec.rb +++ b/spec/integration/errors_spec.rb @@ -54,6 +54,21 @@ describe "When errors occur" do end end + it 'should render a 404 when given an invalid page parameter' do + get '/body/list/all', :page => 'xoforvfmy' + response.should render_template('general/exception_caught') + response.code.should == '404' + response.body.should match("Sorry, we couldn't find that page") + response.body.should match(%Q(invalid value for Integer)) + end + + it 'should url encode params' do + get ('/%d3') + response.should render_template('general/exception_caught') + response.code.should == '404' + response.body.should match("Sorry, we couldn't find that page") + end + it "should render a 500 for general errors using the general/exception_caught template" do InfoRequest.stub!(:find_by_url_title!).and_raise("An example error") get("/request/example") diff --git a/spec/integration/ip_spoofing_spec.rb b/spec/integration/ip_spoofing_spec.rb new file mode 100644 index 000000000..073f71ad6 --- /dev/null +++ b/spec/integration/ip_spoofing_spec.rb @@ -0,0 +1,11 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe 'when getting a country message' do + + it 'should not raise an IP spoofing error when given mismatched headers' do + get '/country_message', nil, { 'HTTP_X_FORWARDED_FOR' => '1.2.3.4', + 'HTTP_CLIENT_IP' => '5.5.5.5' } + response.status.should == 200 + end + +end 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 diff --git a/spec/mailers/info_request_batch_mailer.rb b/spec/mailers/info_request_batch_mailer_spec.rb index 19791e163..19791e163 100644 --- a/spec/mailers/info_request_batch_mailer.rb +++ b/spec/mailers/info_request_batch_mailer_spec.rb diff --git a/spec/mailers/request_mailer_spec.rb b/spec/mailers/request_mailer_spec.rb index 516d13127..2c5d6e6a9 100644 --- a/spec/mailers/request_mailer_spec.rb +++ b/spec/mailers/request_mailer_spec.rb @@ -78,6 +78,16 @@ describe RequestMailer, " when receiving incoming mail" do deliveries.clear end + it "should ignore mail sent to known spam addresses" do + @spam_address = FactoryGirl.create(:spam_address) + + receive_incoming_mail('incoming-request-plain.email', @spam_address.email) + + deliveries = ActionMailer::Base.deliveries + deliveries.size.should == 0 + deliveries.clear + end + it "should return incoming mail to sender when a request is stopped fully for spam" do # mark request as anti-spam ir = info_requests(:fancy_dog_request) diff --git a/spec/models/contact_validator_spec.rb b/spec/models/contact_validator_spec.rb index 9ea0fac49..0f5403967 100644 --- a/spec/models/contact_validator_spec.rb +++ b/spec/models/contact_validator_spec.rb @@ -1,8 +1,53 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') -describe ContactValidator, " when blah" do - before do +describe ContactValidator do + + describe :new do + + let(:valid_params) do + { :name => "Vinny Vanilli", + :email => "vinny@localhost", + :subject => "Why do I have such an ace name?", + :message => "You really should know!!!\n\nVinny" } + end + + it 'validates specified attributes' do + ContactValidator.new(valid_params).should be_valid + end + + it 'validates name is present' do + valid_params.except!(:name) + validator = ContactValidator.new(valid_params) + expect(validator).to have(1).error_on(:name) + end + + it 'validates email is present' do + valid_params.except!(:email) + validator = ContactValidator.new(valid_params) + # We have 2 errors on email because of the format validator + expect(validator).to have(2).errors_on(:email) + end + + it 'validates email format' do + valid_params.merge!({:email => 'not-an-email'}) + validator = ContactValidator.new(valid_params) + expect(validator.errors_on(:email)).to include("Email doesn't look like a valid address") + end + + it 'validates subject is present' do + valid_params.except!(:subject) + validator = ContactValidator.new(valid_params) + expect(validator).to have(1).error_on(:subject) + end + + it 'validates message is present' do + valid_params.except!(:message) + validator = ContactValidator.new(valid_params) + expect(validator).to have(1).error_on(:message) + end + end + end diff --git a/spec/models/info_request_batch_spec.rb b/spec/models/info_request_batch_spec.rb index 53158ebe2..2881e7745 100644 --- a/spec/models/info_request_batch_spec.rb +++ b/spec/models/info_request_batch_spec.rb @@ -1,3 +1,16 @@ +# == Schema Information +# +# Table name: info_request_batches +# +# id :integer not null, primary key +# title :text not null +# user_id :integer not null +# created_at :datetime not null +# updated_at :datetime not null +# body :text +# sent_at :datetime +# + require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe InfoRequestBatch, "when validating" do diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb index 9766f928f..12499f50a 100644 --- a/spec/models/info_request_spec.rb +++ b/spec/models/info_request_spec.rb @@ -21,6 +21,7 @@ # external_url :string(255) # attention_requested :boolean default(FALSE) # comments_allowed :boolean default(TRUE), not null +# info_request_batch_id :integer # require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index dc09bdfa6..38e31783d 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -5,7 +5,7 @@ # # id :integer not null, primary key # name :text not null -# short_name :text not null +# short_name :text default(""), not null # request_email :text not null # version :integer not null # last_edit_editor :string(255) not null @@ -205,6 +205,12 @@ describe PublicBody, " when saving" do pb.first_letter.should == 'Å' end + it "should not save if the url_name is already taken" do + existing = FactoryGirl.create(:public_body) + pb = PublicBody.new(existing.attributes) + pb.should have(1).errors_on(:url_name) + end + it "should save the name when renaming an existing public body" do public_body = public_bodies(:geraldine_public_body) public_body.name = "Mark's Public Body" @@ -527,6 +533,19 @@ describe PublicBody, " when loading CSV files" do PublicBody.count.should == original_count + 3 end + it "should handle active record validation errors" do + csv = <<-CSV +#name,request_email,short_name +Foobar,a@example.com,foobar +Foobar Test,b@example.com,foobar +CSV + + csv_contents = normalize_string_to_utf8(csv) + errors, notes = PublicBody.import_csv(csv_contents, '', 'replace', true, 'someadmin') # true means dry run + + errors.should include("error: line 3: Url name URL name is already taken for authority 'Foobar Test'") + end + end describe PublicBody do diff --git a/spec/models/spam_address_spec.rb b/spec/models/spam_address_spec.rb new file mode 100644 index 000000000..f28440121 --- /dev/null +++ b/spec/models/spam_address_spec.rb @@ -0,0 +1,59 @@ +# == Schema Information +# +# Table name: spam_addresses +# +# id :integer not null, primary key +# email :string(255) not null +# created_at :datetime not null +# updated_at :datetime not null +# + +require 'spec_helper' + +describe SpamAddress do + + describe :new do + + it 'requres an email address' do + SpamAddress.new().should_not be_valid + SpamAddress.new(:email => 'spam@example.org').should be_valid + end + + it 'must have a unique email address' do + existing = FactoryGirl.create(:spam_address) + SpamAddress.new(:email => existing.email).should_not be_valid + end + + end + + describe '.spam?' do + + before(:each) do + @spam_address = FactoryGirl.create(:spam_address) + end + + it 'is a spam address if the address is stored' do + SpamAddress.spam?(@spam_address.email).should be_true + end + + it 'is not a spam address if the adress is not stored' do + SpamAddress.spam?('genuine-email@example.com').should be_false + end + + describe 'when accepting an array of emails' do + + it 'is spam if any of the emails are stored' do + emails = ['genuine-email@example.com', @spam_address.email] + SpamAddress.spam?(emails).should be_true + end + + it 'is not spam if none of the emails are stored' do + emails = ['genuine-email@example.com', 'genuine-email@example.org'] + SpamAddress.spam?(emails).should be_false + end + + end + + end + +end diff --git a/spec/models/track_thing_spec.rb b/spec/models/track_thing_spec.rb index 1c582564b..3edf2d1ad 100644 --- a/spec/models/track_thing_spec.rb +++ b/spec/models/track_thing_spec.rb @@ -51,10 +51,11 @@ describe TrackThing, "when tracking changes" do end it "will make some sane descriptions of search-based tracks" do - tests = { 'bob variety:user' => "users matching text 'bob'", - 'bob (variety:sent OR variety:followup_sent OR variety:response OR variety:comment) (latest_status:successful OR latest_status:partially_successful OR latest_status:rejected OR latest_status:not_held)' => "comments or requests which are successful or unsuccessful matching text 'bob'", - '(latest_status:waiting_response OR latest_status:waiting_clarification OR waiting_classification:true)' => 'requests which are awaiting a response', - ' (variety:sent OR variety:followup_sent OR variety:response OR variety:comment)' => 'all requests or comments' } + tests = { ' (variety:sent OR variety:followup_sent OR variety:response OR variety:comment)' => 'all requests or comments', + 'bob (variety:sent OR variety:followup_sent OR variety:response OR variety:comment)' => "all requests or comments matching text 'bob'", + 'bob (latest_status:successful OR latest_status:partially_successful)' => "requests which are successful matching text 'bob'", + '(latest_status:successful OR latest_status:partially_successful)' => 'requests which are successful', + 'bob' => "anything matching text 'bob'" } tests.each do |query, description| track_thing = TrackThing.create_track_for_search_query(query) track_thing.track_query_description.should == description diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index b6f48dad3..c54043092 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -2,24 +2,25 @@ # # Table name: users # -# id :integer not null, primary key -# email :string(255) not null -# name :string(255) not null -# hashed_password :string(255) not null -# salt :string(255) not null -# created_at :datetime not null -# updated_at :datetime not null -# email_confirmed :boolean default(FALSE), not null -# url_name :text not null -# last_daily_track_email :datetime default(2000-01-01 00:00:00 UTC) -# admin_level :string(255) default("none"), not null -# ban_text :text default(""), not null -# about_me :text default(""), not null -# locale :string(255) -# email_bounced_at :datetime -# email_bounce_message :text default(""), not null -# no_limit :boolean default(FALSE), not null -# receive_email_alerts :boolean default(TRUE), not null +# id :integer not null, primary key +# email :string(255) not null +# name :string(255) not null +# hashed_password :string(255) not null +# salt :string(255) not null +# created_at :datetime not null +# updated_at :datetime not null +# email_confirmed :boolean default(FALSE), not null +# url_name :text not null +# last_daily_track_email :datetime default(Sat Jan 01 00:00:00 UTC 2000) +# admin_level :string(255) default("none"), not null +# ban_text :text default(""), not null +# about_me :text default(""), not null +# locale :string(255) +# email_bounced_at :datetime +# email_bounce_message :text default(""), not null +# no_limit :boolean default(FALSE), not null +# receive_email_alerts :boolean default(TRUE), not null +# can_make_batch_requests :boolean default(FALSE), not null # require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index dc5a0d6eb..e391c97d3 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -13,6 +13,7 @@ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[ SimpleCov.start('rails') do add_filter 'commonlib' add_filter 'vendor/plugins' + add_filter 'lib/attachment_to_html' add_filter 'lib/strip_attributes' add_filter 'lib/has_tag_string' add_filter 'lib/acts_as_xapian' |