aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models
diff options
context:
space:
mode:
Diffstat (limited to 'spec/models')
-rw-r--r--spec/models/info_request_batch_spec.rb2
-rw-r--r--spec/models/info_request_event_spec.rb6
-rw-r--r--spec/models/info_request_spec.rb130
-rw-r--r--spec/models/post_redirect_spec.rb10
-rw-r--r--spec/models/public_body_spec.rb27
-rw-r--r--spec/models/widget_vote_spec.rb53
6 files changed, 225 insertions, 3 deletions
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..1ead1e0bf 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
@@ -1221,6 +1332,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 +1425,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/post_redirect_spec.rb b/spec/models/post_redirect_spec.rb
index 73740e914..70b221f10 100644
--- a/spec/models/post_redirect_spec.rb
+++ b/spec/models/post_redirect_spec.rb
@@ -65,11 +65,19 @@ 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
+ puts pr.reason_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