diff options
Diffstat (limited to 'spec')
23 files changed, 670 insertions, 52 deletions
diff --git a/spec/controllers/admin_public_body_categories_controller_spec.rb b/spec/controllers/admin_public_body_categories_controller_spec.rb index 1131b3c0b..c15ee77f1 100644 --- a/spec/controllers/admin_public_body_categories_controller_spec.rb +++ b/spec/controllers/admin_public_body_categories_controller_spec.rb @@ -310,7 +310,7 @@ describe AdminPublicBodyCategoriesController do post :update, :id => category.id, :public_body_category => category.serializable_hash.except(:title, :description) - expect(assigns(:tagged_public_bodies)).to eq(expected_bodies) + expect(assigns(:tagged_public_bodies)).to match_array(expected_bodies) end it "saves edits to a public body category's heading associations" do diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb index 02237b29d..c4f3c847e 100644 --- a/spec/controllers/request_controller_spec.rb +++ b/spec/controllers/request_controller_spec.rb @@ -2764,4 +2764,3 @@ describe RequestController, "#select_authorities" do end end - diff --git a/spec/controllers/widgets_controller_spec.rb b/spec/controllers/widgets_controller_spec.rb new file mode 100644 index 000000000..6a58c7c5c --- /dev/null +++ b/spec/controllers/widgets_controller_spec.rb @@ -0,0 +1,181 @@ +# coding: utf-8 +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe WidgetsController do + + include LinkToHelper + + describe "#show" do + + before do + @info_request = FactoryGirl.create(:info_request) + AlaveteliConfiguration.stub!(:enable_widgets).and_return(true) + end + + it 'should render the widget template' do + get :show, :request_id => @info_request.id + expect(response).to render_template('show') + end + + it 'should find the info request' do + get :show, :request_id => @info_request.id + assigns[:info_request].should == @info_request + end + + it 'should create a track thing for the request' do + get :show, :request_id => @info_request.id + assigns[:track_thing].info_request.should == @info_request + end + + it 'should assign the request status' do + get :show, :request_id => @info_request.id + assigns[:status].should == @info_request.calculate_status + end + + it 'should not send an x-frame-options header' do + get :show, :request_id => @info_request.id + response.headers["X-Frame-Options"].should be_nil + end + + context 'for a non-logged-in user' do + + context 'if no widget-vote cookie is set' do + + it 'should set a widget-vote cookie' do + cookies[:widget_vote].should be_nil + get :show, :request_id => @info_request.id + cookies[:widget_vote].should_not be_nil + end + + end + + end + + context 'when widgets are not enabled' do + + it 'should return a 404' do + AlaveteliConfiguration.stub!(:enable_widgets).and_return(false) + lambda{ get :show, :request_id => @info_request.id }.should + raise_error(ActiveRecord::RecordNotFound) + end + + end + + context "when the request's prominence is not 'normal'" do + + it 'should return a 403' do + @info_request.prominence = 'hidden' + @info_request.save! + get :show, :request_id => @info_request.id + response.code.should == "403" + end + + end + + end + + describe "#new" do + + before do + @info_request = FactoryGirl.create(:info_request) + AlaveteliConfiguration.stub!(:enable_widgets).and_return(true) + end + + it 'should render the create widget template' do + get :new, :request_id => @info_request.id + expect(response).to render_template('new') + end + + it 'should find the info request' do + get :new, :request_id => @info_request.id + assigns[:info_request].should == @info_request + end + + context 'when widgets are not enabled' do + + it 'should return a 404' do + AlaveteliConfiguration.stub!(:enable_widgets).and_return(false) + lambda{ get :new, :request_id => @info_request.id }.should + raise_error(ActiveRecord::RecordNotFound) + end + + end + + context "when the request's prominence is not 'normal'" do + + it 'should return a 403' do + @info_request.prominence = 'hidden' + @info_request.save! + get :show, :request_id => @info_request.id + response.code.should == "403" + end + + end + + end + + describe :update do + + before do + @info_request = FactoryGirl.create(:info_request) + AlaveteliConfiguration.stub!(:enable_widgets).and_return(true) + end + + it 'should find the info request' do + get :update, :request_id => @info_request.id + assigns[:info_request].should == @info_request + end + + it 'should redirect to the track path for the info request' do + get :update, :request_id => @info_request.id + track_thing = TrackThing.create_track_for_request(@info_request) + expect(response).to redirect_to(do_track_path(track_thing)) + end + + context 'when there is no logged-in user and a widget vote cookie' do + + before do + @cookie_value = 'x' * 20 + end + + it 'should create a widget vote if none exists for the info request and cookie' do + @info_request.widget_votes.where(:cookie => @cookie_value).size.should == 0 + request.cookies['widget_vote'] = @cookie_value + get :update, :request_id => @info_request.id + @info_request.widget_votes.where(:cookie => @cookie_value).size.should == 1 + end + + it 'should not create a widget vote if one exists for the info request and cookie' do + @info_request.widget_votes.create(:cookie => @cookie_value) + request.cookies['widget_vote'] = @cookie_value + get :update, :request_id => @info_request.id + @info_request.widget_votes.where(:cookie => @cookie_value).size.should == 1 + end + + end + + context 'when widgets are not enabled' do + + it 'should raise ActiveRecord::RecordNotFound' do + AlaveteliConfiguration.stub!(:enable_widgets).and_return(false) + lambda{ get :update, :request_id => @info_request.id }.should + raise_error(ActiveRecord::RecordNotFound) + end + + end + + context "when the request's prominence is not 'normal'" do + + it 'should return a 403' do + @info_request.prominence = 'hidden' + @info_request.save! + get :show, :request_id => @info_request.id + response.code.should == "403" + end + + end + + end + +end + diff --git a/spec/factories/info_requests.rb b/spec/factories/info_requests.rb index 8052625cd..ee96bfa89 100644 --- a/spec/factories/info_requests.rb +++ b/spec/factories/info_requests.rb @@ -1,7 +1,7 @@ FactoryGirl.define do factory :info_request do - title "Example Title" + sequence(:title) { |n| "Example Title #{n}" } public_body user diff --git a/spec/factories/widget_votes.rb b/spec/factories/widget_votes.rb new file mode 100644 index 000000000..964bbb20d --- /dev/null +++ b/spec/factories/widget_votes.rb @@ -0,0 +1,7 @@ +require 'securerandom' +FactoryGirl.define do + factory :widget_vote do + info_request + cookie { SecureRandom.hex(10) } + end +end
\ No newline at end of file diff --git a/spec/helpers/health_checks_helper_spec.rb b/spec/helpers/health_checks_helper_spec.rb index 7d4083da5..7dbfaf06e 100644 --- a/spec/helpers/health_checks_helper_spec.rb +++ b/spec/helpers/health_checks_helper_spec.rb @@ -10,6 +10,11 @@ describe HealthChecksHelper do expect(check_status(check)).to include('red') end + it 'sets style to a blank string if ok' do + check = double(:message => '', :ok? => true) + expect(check_status(check)).to include('style=""') + end + end end diff --git a/spec/helpers/widget_helper_spec.rb b/spec/helpers/widget_helper_spec.rb new file mode 100644 index 000000000..c8c41b14f --- /dev/null +++ b/spec/helpers/widget_helper_spec.rb @@ -0,0 +1,28 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe WidgetHelper do + + include WidgetHelper + + describe :status_description do + + before do + @info_request = FactoryGirl.build(:info_request) + end + + it 'should return "Awaiting classification" for "waiting_classification' do + expect(status_description(@info_request, 'waiting_classification')).to eq('Awaiting classification') + end + + it 'should call theme_display_status for a theme status' do + @info_request.stub!(:theme_display_status).and_return("Special status") + expect(status_description(@info_request, 'special_status')).to eq('Special status') + end + + it 'should return unknown for an unknown status' do + expect(status_description(@info_request, 'special_status')).to eq('Unknown') + end + + end + +end
\ No newline at end of file diff --git a/spec/integration/admin_public_body_edit_spec.rb b/spec/integration/admin_public_body_edit_spec.rb index aeec3e65a..21011b172 100644 --- a/spec/integration/admin_public_body_edit_spec.rb +++ b/spec/integration/admin_public_body_edit_spec.rb @@ -39,7 +39,7 @@ describe 'Editing a Public Body' do end end - it 'can add a translation for multiple locales', :focus => true do + it 'can add a translation for multiple locales' do @admin.visit edit_admin_body_path(@body) @admin.fill_in 'public_body_name__en', :with => 'New Quango EN' @admin.click_button 'Save' diff --git a/spec/integration/download_request_spec.rb b/spec/integration/download_request_spec.rb index 48b42b11d..c73b85866 100644 --- a/spec/integration/download_request_spec.rb +++ b/spec/integration/download_request_spec.rb @@ -143,7 +143,8 @@ describe 'when making a zipfile available' do it "should update the contents of the zipfile when the request changes" do - info_request = FactoryGirl.create(:info_request_with_incoming) + info_request = FactoryGirl.create(:info_request_with_incoming, + :title => 'Example Title') request_owner = login(info_request.user) inspect_zip_download(request_owner, info_request) do |zip| zip.count.should == 1 # just the message diff --git a/spec/integration/xapian_search_highlighting_spec.rb b/spec/integration/xapian_search_highlighting_spec.rb index 65a34cf91..a91df341f 100644 --- a/spec/integration/xapian_search_highlighting_spec.rb +++ b/spec/integration/xapian_search_highlighting_spec.rb @@ -1,8 +1,14 @@ +# encoding: utf-8 + require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe 'highlighting search results' do include HighlightHelper + before do + get_fixtures_xapian_index + end + it 'ignores stopwords' do phrase = 'department of humpadinking' search = ActsAsXapian::Search.new([PublicBody], phrase, :limit => 1) @@ -36,4 +42,13 @@ describe 'highlighting search results' do highlight_matches(phrase, matches).should == '<mark>boring</mark>' end + it 'handles macrons correctly' do + phrase = 'Māori' + + search = ActsAsXapian::Search.new([PublicBody], phrase, :limit => 1) + matches = search.words_to_highlight(:regex => true, :include_original => true) + + highlight_matches(phrase, matches).should == '<mark>Māori</mark>' + end + end diff --git a/spec/lib/alaveteli_text_masker_spec.rb b/spec/lib/alaveteli_text_masker_spec.rb index 1a4782a83..102d2582e 100644 --- a/spec/lib/alaveteli_text_masker_spec.rb +++ b/spec/lib/alaveteli_text_masker_spec.rb @@ -92,6 +92,23 @@ describe AlaveteliTextMasker do pdf.should_not == "" end + it 'should keep the uncensored original if uncompression of a PDF fails' do + orig_pdf = load_file_fixture('tfl.pdf') + pdf = orig_pdf.dup + stub!(:uncompress_pdf).and_return nil + apply_masks!(pdf, "application/pdf") + pdf.should == orig_pdf + end + + it 'should use the uncompressed PDF text if re-compression of a compressed PDF fails' do + orig_pdf = load_file_fixture('tfl.pdf') + pdf = orig_pdf.dup + stub!(:uncompress_pdf).and_return "something about foi@tfl.gov.uk" + stub!(:compress_pdf).and_return nil + apply_masks!(pdf, "application/pdf") + pdf.should match "something about xxx@xxx.xxx.xx" + end + it "should apply hard-coded privacy rules to HTML files" do data = "http://test.host/c/cheese" apply_masks!(data, 'text/html') diff --git a/spec/lib/mail_handler/mail_handler_spec.rb b/spec/lib/mail_handler/mail_handler_spec.rb index e7ad93300..ea7a99b05 100644 --- a/spec/lib/mail_handler/mail_handler_spec.rb +++ b/spec/lib/mail_handler/mail_handler_spec.rb @@ -9,7 +9,7 @@ end describe 'when creating a mail object from raw data' do - it "should be able to parse a large email without raising an exception", :focus => true do + it "should be able to parse a large email without raising an exception" do m = Mail.new m.add_file(:filename => "attachment.data", :content => "a" * (8 * 1024 * 1024)) raw_email = "From jamis_buck@byu.edu Mon May 2 16:07:05 2005\r\n#{m.to_s}" @@ -22,7 +22,7 @@ describe 'when creating a mail object from raw data' do mail.multipart?.should == true end - it "should not fail on invalid byte sequence in content-disposition header", :focus => true do + it "should not fail on invalid byte sequence in content-disposition header" do part = Mail::Part.new("Content-Disposition: inline; filename=a\xB8z\r\n\r\nThis is the body text.") lambda { part.inline? }.should_not raise_error end diff --git a/spec/mailers/request_mailer_spec.rb b/spec/mailers/request_mailer_spec.rb index a26be866e..f70bf8675 100644 --- a/spec/mailers/request_mailer_spec.rb +++ b/spec/mailers/request_mailer_spec.rb @@ -1,6 +1,8 @@ # encoding: utf-8 require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +# TODO: Combine all these separate "describe" blocks to tidy things up + describe RequestMailer, " when receiving incoming mail" do before(:each) do load_raw_emails_data @@ -435,6 +437,10 @@ describe RequestMailer, 'when sending a new response email' do @mail = RequestMailer.new_response(@info_request, @incoming_message) end + it 'should not create HTML entities in the subject line' do + mail = RequestMailer.new_response(FactoryGirl.create(:info_request, :title => "Here's a request"), FactoryGirl.create(:incoming_message)) + expect(mail.subject).to eq "New response to your FOI request - Here's a request" + end end describe RequestMailer, 'requires_admin' do @@ -443,7 +449,7 @@ describe RequestMailer, 'requires_admin' do :name => 'Bruce Jones') @info_request = mock_model(InfoRequest, :user => user, :described_state => 'error_message', - :title => 'Test request', + :title => "It's a Test request", :url_title => 'test_request', :law_used_short => 'FOI', :id => 123) @@ -459,4 +465,42 @@ describe RequestMailer, 'requires_admin' do mail.body.should include 'Something has gone wrong' end + it 'should not create HTML entities in the subject line' do + expect(RequestMailer.requires_admin(@info_request).subject).to eq "FOI response requires admin (error_message) - It's a Test request" + end +end + +describe RequestMailer, "overdue_alert" do + it 'should not create HTML entities in the subject line' do + mail = RequestMailer.overdue_alert(FactoryGirl.create(:info_request, :title => "Here's a request"), FactoryGirl.create(:user)) + expect(mail.subject).to eq "Delayed response to your FOI request - Here's a request" + end +end + +describe RequestMailer, "very_overdue_alert" do + it 'should not create HTML entities in the subject line' do + mail = RequestMailer.very_overdue_alert(FactoryGirl.create(:info_request, :title => "Here's a request"), FactoryGirl.create(:user)) + expect(mail.subject).to eq "You're long overdue a response to your FOI request - Here's a request" + end +end + +describe RequestMailer, "not_clarified_alert" do + it 'should not create HTML entities in the subject line' do + mail = RequestMailer.not_clarified_alert(FactoryGirl.create(:info_request, :title => "Here's a request"), FactoryGirl.create(:incoming_message)) + expect(mail.subject).to eq "Clarify your FOI request - Here's a request" + end +end + +describe RequestMailer, "comment_on_alert" do + it 'should not create HTML entities in the subject line' do + mail = RequestMailer.comment_on_alert(FactoryGirl.create(:info_request, :title => "Here's a request"), FactoryGirl.create(:comment)) + expect(mail.subject).to eq "Somebody added a note to your FOI request - Here's a request" + end +end + +describe RequestMailer, "comment_on_alert_plural" do + it 'should not create HTML entities in the subject line' do + mail = RequestMailer.comment_on_alert_plural(FactoryGirl.create(:info_request, :title => "Here's a request"), 2, FactoryGirl.create(:comment)) + expect(mail.subject).to eq "Some notes have been added to your FOI request - Here's a request" + end end diff --git a/spec/models/censor_rule_spec.rb b/spec/models/censor_rule_spec.rb index 4ecd2d3e1..77b8cd07a 100644 --- a/spec/models/censor_rule_spec.rb +++ b/spec/models/censor_rule_spec.rb @@ -17,6 +17,42 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +describe CensorRule do + + describe :apply_to_text do + + it 'applies the rule to the text' do + rule = FactoryGirl.build(:censor_rule, :text => 'secret') + text = 'Some secret text' + expect(rule.apply_to_text(text)).to eq('Some [REDACTED] text') + end + + it 'does not mutate the input' do + rule = FactoryGirl.build(:censor_rule, :text => 'secret') + text = 'Some secret text' + rule.apply_to_text(text) + expect(text).to eq('Some secret text') + end + + it 'returns the text if the rule is unmatched' do + rule = FactoryGirl.build(:censor_rule, :text => 'secret') + text = 'Some text' + expect(rule.apply_to_text(text)).to eq('Some text') + end + end + + describe :apply_to_text! do + + it 'mutates the input' do + rule = FactoryGirl.build(:censor_rule, :text => 'secret') + text = 'Some secret text' + rule.apply_to_text!(text) + expect(text).to eq('Some [REDACTED] text') + end + + end +end + describe CensorRule, "substituting things" do describe 'when using a text rule' do diff --git a/spec/models/info_request_batch_spec.rb b/spec/models/info_request_batch_spec.rb index 2881e7745..701422037 100644 --- a/spec/models/info_request_batch_spec.rb +++ b/spec/models/info_request_batch_spec.rb @@ -80,7 +80,7 @@ describe InfoRequestBatch, "when finding an existing batch" do end end -describe InfoRequestBatch, "when creating a batch", :focus => true do +describe InfoRequestBatch, "when creating a batch" do before do @title = 'A test title' diff --git a/spec/models/info_request_event_spec.rb b/spec/models/info_request_event_spec.rb index 53c83bd46..1299dfb63 100644 --- a/spec/models/info_request_event_spec.rb +++ b/spec/models/info_request_event_spec.rb @@ -30,6 +30,12 @@ describe InfoRequestEvent do ire.params.should == example_params end + it "should restore UTF8-heavy params stored under ruby 1.8 as UTF-8" do + ire = InfoRequestEvent.new + utf8_params = "--- \n:foo: !binary |\n 0KLQvtCz0LDRiCDR\n" + ire.params_yaml = utf8_params + ire.params[:foo].encoding.to_s.should == 'UTF-8' if ire.params[:foo].respond_to?(:encoding) + end end describe 'when deciding if it is indexed by search' do diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb index 70947584b..941561c81 100644 --- a/spec/models/info_request_spec.rb +++ b/spec/models/info_request_spec.rb @@ -28,6 +28,117 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe InfoRequest do + describe :new do + + it 'sets the default law used' do + expect(InfoRequest.new().law_used).to eq('foi') + end + + it 'sets the default law used if a body is eir-only' do + body = FactoryGirl.create(:public_body, :tag_string => 'eir_only') + expect(body.info_requests.build.law_used).to eq('eir') + end + + it 'does not try to set the law used for existing requests' do + info_request = FactoryGirl.create(:info_request) + body = FactoryGirl.create(:public_body, :tag_string => 'eir_only') + info_request.update_attributes(:public_body_id => body.id) + InfoRequest.any_instance.should_not_receive(:law_used=).and_call_original + InfoRequest.find(info_request.id) + end + end + + describe :move_to_public_body do + + context 'with no options' do + + it 'requires an :editor option' do + request = FactoryGirl.create(:info_request) + new_body = FactoryGirl.create(:public_body) + expect { + request.move_to_public_body(new_body) + }.to raise_error IndexError + end + + end + + context 'with the :editor option' do + + it 'moves the info request to the new public body' do + request = FactoryGirl.create(:info_request) + new_body = FactoryGirl.create(:public_body) + user = FactoryGirl.create(:user) + request.move_to_public_body(new_body, :editor => user) + request.reload + expect(request.public_body).to eq(new_body) + end + + it 'logs the move' do + request = FactoryGirl.create(:info_request) + old_body = request.public_body + new_body = FactoryGirl.create(:public_body) + user = FactoryGirl.create(:user) + request.move_to_public_body(new_body, :editor => user) + request.reload + event = request.info_request_events.last + + expect(event.event_type).to eq('move_request') + expect(event.params[:editor]).to eq(user) + expect(event.params[:public_body_url_name]).to eq(new_body.url_name) + expect(event.params[:old_public_body_url_name]).to eq(old_body.url_name) + end + + it 'updates the law_used to the new body law' do + request = FactoryGirl.create(:info_request) + new_body = FactoryGirl.create(:public_body, :tag_string => 'eir_only') + user = FactoryGirl.create(:user) + request.move_to_public_body(new_body, :editor => user) + request.reload + expect(request.law_used).to eq('eir') + end + + it 'returns the new public body' do + request = FactoryGirl.create(:info_request) + new_body = FactoryGirl.create(:public_body) + user = FactoryGirl.create(:user) + expect(request.move_to_public_body(new_body, :editor => user)).to eq(new_body) + end + + it 'retains the existing body if the new body does not exist' do + request = FactoryGirl.create(:info_request) + user = FactoryGirl.create(:user) + existing_body = request.public_body + request.move_to_public_body(nil, :editor => user) + request.reload + expect(request.public_body).to eq(existing_body) + end + + it 'returns nil if the body cannot be updated' do + request = FactoryGirl.create(:info_request) + user = FactoryGirl.create(:user) + expect(request.move_to_public_body(nil, :editor => user)).to eq(nil) + end + + it 'reindexes the info request' do + request = FactoryGirl.create(:info_request) + new_body = FactoryGirl.create(:public_body) + user = FactoryGirl.create(:user) + reindex_job = ActsAsXapian::ActsAsXapianJob. + where(:model => 'InfoRequestEvent'). + delete_all + + request.move_to_public_body(new_body, :editor => user) + request.reload + + reindex_job = ActsAsXapian::ActsAsXapianJob. + where(:model => 'InfoRequestEvent'). + last + expect(reindex_job.model_id).to eq(request.info_request_events.last.id) + end + + end + end + describe 'when validating' do it 'should accept a summary with ascii characters' do @@ -42,7 +153,7 @@ describe InfoRequest do info_request.errors[:title].should be_empty end - it 'should not accept a summary with no ascii or unicode characters' do + it 'should not accept a summary with no ascii or unicode characters' do info_request = InfoRequest.new(:title => '55555') info_request.valid? info_request.errors[:title].should_not be_empty @@ -547,17 +658,22 @@ describe InfoRequest do before do Time.stub!(:now).and_return(Time.utc(2007, 11, 9, 23, 59)) - @mock_comment_event = mock_model(InfoRequestEvent, :created_at => Time.now - 23.days, - :event_type => 'comment', - :response? => false) - mock_incoming_message = mock_model(IncomingMessage, :all_can_view? => true) - @mock_response_event = mock_model(InfoRequestEvent, :created_at => Time.now - 22.days, - :event_type => 'response', - :response? => true, - :incoming_message => mock_incoming_message) - @info_request = InfoRequest.new(:prominence => 'normal', - :awaiting_description => true, - :info_request_events => [@mock_response_event, @mock_comment_event]) + @info_request = FactoryGirl.create(:info_request, + :prominence => 'normal', + :awaiting_description => true) + @comment_event = FactoryGirl.create(:info_request_event, + :created_at => Time.now - 23.days, + :event_type => 'comment', + :info_request => @info_request) + @incoming_message = FactoryGirl.create(:incoming_message, + :prominence => 'normal', + :info_request => @info_request) + @response_event = FactoryGirl.create(:info_request_event, + :info_request => @info_request, + :created_at => Time.now - 22.days, + :event_type => 'response', + :incoming_message => @incoming_message) + @info_request.update_attribute(:awaiting_description, true) end it 'should return false if it is the holding pen' do @@ -571,7 +687,7 @@ describe InfoRequest do end it 'should return false if its last response event occurred less than 21 days ago' do - @mock_response_event.stub!(:created_at).and_return(Time.now - 20.days) + @response_event.update_attribute(:created_at, Time.now - 20.days) @info_request.is_old_unclassified?.should be_false end @@ -1221,6 +1337,7 @@ describe InfoRequest do describe InfoRequest, "when constructing a list of requests by query" do before(:each) do + load_raw_emails_data get_fixtures_xapian_index end @@ -1313,4 +1430,20 @@ describe InfoRequest do end + + describe 'when destroying a message' do + + it 'can destroy a request with comments and censor rules' do + info_request = FactoryGirl.create(:info_request) + censor_rule = FactoryGirl.create(:censor_rule, :info_request => info_request) + comment = FactoryGirl.create(:comment, :info_request => info_request) + info_request.reload + info_request.fully_destroy + + InfoRequest.where(:id => info_request.id).should be_empty + CensorRule.where(:id => censor_rule.id).should be_empty + Comment.where(:id => comment.id).should be_empty + end + + end end diff --git a/spec/models/outgoing_message_spec.rb b/spec/models/outgoing_message_spec.rb index a3e2d1c68..051f263d2 100644 --- a/spec/models/outgoing_message_spec.rb +++ b/spec/models/outgoing_message_spec.rb @@ -18,6 +18,93 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +describe OutgoingMessage do + + describe :initialize do + + it 'does not censor the #body' do + attrs = { :status => 'ready', + :message_type => 'initial_request', + :body => 'abc', + :what_doing => 'normal_sort' } + + message = FactoryGirl.create(:outgoing_message, attrs) + + OutgoingMessage.any_instance.should_not_receive(:body).and_call_original + OutgoingMessage.find(message.id) + end + + end + + describe :body do + + it 'returns the body attribute' do + attrs = { :status => 'ready', + :message_type => 'initial_request', + :body => 'abc', + :what_doing => 'normal_sort' } + + message = FactoryGirl.build(:outgoing_message, attrs) + expect(message.body).to eq('abc') + end + + it 'strips the body of leading and trailing whitespace' do + attrs = { :status => 'ready', + :message_type => 'initial_request', + :body => ' abc ', + :what_doing => 'normal_sort' } + + message = FactoryGirl.build(:outgoing_message, attrs) + expect(message.body).to eq('abc') + end + + it 'removes excess linebreaks that unnecessarily space it out' do + attrs = { :status => 'ready', + :message_type => 'initial_request', + :body => "ab\n\nc\n\n", + :what_doing => 'normal_sort' } + + message = FactoryGirl.build(:outgoing_message, attrs) + expect(message.body).to eq("ab\n\nc") + end + + it "applies the associated request's censor rules to the text" do + attrs = { :status => 'ready', + :message_type => 'initial_request', + :body => 'This sensitive text contains secret info!', + :what_doing => 'normal_sort' } + message = FactoryGirl.build(:outgoing_message, attrs) + + rules = [FactoryGirl.build(:censor_rule, :text => 'secret'), + FactoryGirl.build(:censor_rule, :text => 'sensitive')] + InfoRequest.any_instance.stub(:censor_rules).and_return(rules) + + expected = 'This [REDACTED] text contains [REDACTED] info!' + expect(message.body).to eq(expected) + end + + it "applies the given censor rules to the text" do + attrs = { :status => 'ready', + :message_type => 'initial_request', + :body => 'This sensitive text contains secret info!', + :what_doing => 'normal_sort' } + message = FactoryGirl.build(:outgoing_message, attrs) + + request_rules = [FactoryGirl.build(:censor_rule, :text => 'secret'), + FactoryGirl.build(:censor_rule, :text => 'sensitive')] + InfoRequest.any_instance.stub(:censor_rules).and_return(request_rules) + + censor_rules = [FactoryGirl.build(:censor_rule, :text => 'text'), + FactoryGirl.build(:censor_rule, :text => 'contains')] + + expected = 'This sensitive [REDACTED] [REDACTED] secret info!' + expect(message.body(:censor_rules => censor_rules)).to eq(expected) + end + + end + +end + describe OutgoingMessage, " when making an outgoing message" do before do @@ -57,6 +144,7 @@ describe OutgoingMessage, " when making an outgoing message" do info_request = mock_model(InfoRequest, :public_body => public_body, :url_title => 'a_test_title', :title => 'A test title', + :applicable_censor_rules => [], :apply_censor_rules_to_text! => nil, :is_batch_request_template? => false) outgoing_message = OutgoingMessage.new({ @@ -155,27 +243,6 @@ describe OutgoingMessage, " when making an outgoing message" do end end - -describe OutgoingMessage, " when censoring data" do - - before do - @om = outgoing_messages(:useless_outgoing_message) - - @censor_rule = CensorRule.new() - @censor_rule.text = "dog" - @censor_rule.replacement = "cat" - @censor_rule.last_edit_editor = "unknown" - @censor_rule.last_edit_comment = "none" - - @om.info_request.censor_rules << @censor_rule - end - - it "should apply censor rules to outgoing messages" do - @om.read_attribute(:body).should match(/fancy dog/) - @om.body.should match(/fancy cat/) - end -end - describe OutgoingMessage, "when validating the format of the message body" do it 'should handle a salutation with a bracket in it' do diff --git a/spec/models/post_redirect_spec.rb b/spec/models/post_redirect_spec.rb index 73740e914..750e47cc3 100644 --- a/spec/models/post_redirect_spec.rb +++ b/spec/models/post_redirect_spec.rb @@ -65,11 +65,18 @@ describe PostRedirect, " when accessing values" do end it "should convert reason parameters into YAML and back successfully" do - pr = PostRedirect.new + pr = PostRedirect.new example_reason_params = { :foo => 'this is stuff', :bar => 83, :humbug => "yikes!!!" } pr.reason_params = example_reason_params pr.reason_params_yaml.should == example_reason_params.to_yaml pr.reason_params.should == example_reason_params end + + it "should restore UTF8-heavy params stored under ruby 1.8 as UTF-8" do + pr = PostRedirect.new + utf8_params = "--- \n:foo: !binary |\n 0KLQvtCz0LDRiCDR\n" + pr.reason_params_yaml = utf8_params + pr.reason_params[:foo].encoding.to_s.should == 'UTF-8' if pr.reason_params[:foo].respond_to?(:encoding) + end end diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index 7b55efda1..cce017424 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -1237,6 +1237,33 @@ describe PublicBody do end + describe :request_email do + context "when the email is set" do + subject(:public_body) { FactoryGirl.create(:public_body, :request_email => "request@example.com") } + + it "should return the set email address" do + expect(public_body.request_email).to eq("request@example.com") + end + + it "should return a different email address when overridden in configuration" do + AlaveteliConfiguration.stub!(:override_all_public_body_request_emails).and_return("tester@example.com") + expect(public_body.request_email).to eq("tester@example.com") + end + end + + context "when no email is set" do + subject(:public_body) { FactoryGirl.create(:public_body, :request_email => "") } + + it "should return a blank email address" do + expect(public_body.request_email).to be_blank + end + + it "should still return a blank email address when overridden in configuration" do + AlaveteliConfiguration.stub!(:override_all_public_body_request_emails).and_return("tester@example.com") + expect(public_body.request_email).to be_blank + end + end + end end describe PublicBody::Translation do diff --git a/spec/models/widget_vote_spec.rb b/spec/models/widget_vote_spec.rb new file mode 100644 index 000000000..b9f990eac --- /dev/null +++ b/spec/models/widget_vote_spec.rb @@ -0,0 +1,53 @@ +# == Schema Information +# +# Table name: widget_votes +# +# id :integer not null, primary key +# cookie :string(255) +# info_request_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# + +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe WidgetVote do + + describe :new do + + it 'requires an info request' do + widget_vote = WidgetVote.new + widget_vote.should_not be_valid + widget_vote.errors[:info_request].should == ["can't be blank"] + end + + it 'validates the cookie length' do + widget_vote = WidgetVote.new + widget_vote.should_not be_valid + widget_vote.errors[:cookie].should == ["is the wrong length (should be 20 characters)"] + end + + it 'is valid with a cookie and info request' do + widget_vote = FactoryGirl.create(:widget_vote) + widget_vote.should be_valid + end + + it 'enforces uniqueness of cookie per info request' do + info_request = FactoryGirl.create(:info_request) + widget_vote = info_request.widget_votes.create(:cookie => 'x' * 20) + duplicate_vote = info_request.widget_votes.build(:cookie => 'x' * 20) + duplicate_vote.should_not be_valid + duplicate_vote.errors[:cookie].should == ["has already been taken"] + end + + it 'allows the same cookie to be used across info requests' do + info_request = FactoryGirl.create(:info_request) + second_info_request = FactoryGirl.create(:info_request) + widget_vote = info_request.widget_votes.create(:cookie => 'x' * 20) + second_request_vote = second_info_request.widget_votes.build(:cookie => 'x' * 20) + second_request_vote.should be_valid + end + + end + +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 93bcfa1ba..4df1b5649 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -124,13 +124,6 @@ Spork.prefork do end end - # TODO: No idea what namespace/class/module to put this in - # Create a clean xapian index based on the fixture files and the raw_email data. - def create_fixtures_xapian_index - load_raw_emails_data - rebuild_xapian_index - end - # Use the before create job hook to simulate a race condition with # another process by creating an acts_as_xapian_job record for the # same model: diff --git a/spec/views/public_body/show.html.erb_spec.rb b/spec/views/public_body/show.html.erb_spec.rb index 6ebc39caa..2a4c21d04 100644 --- a/spec/views/public_body/show.html.erb_spec.rb +++ b/spec/views/public_body/show.html.erb_spec.rb @@ -13,7 +13,6 @@ describe "public_body/show" do :publication_scheme => '', :disclosure_log => '', :calculated_home_page => '') - @pb.stub!(:override_request_email).and_return(nil) @pb.stub!(:is_requestable?).and_return(true) @pb.stub!(:special_not_requestable_reason?).and_return(false) @pb.stub!(:has_notes?).and_return(false) |