aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rw-r--r--spec/controllers/public_body_change_requests_controller_spec.rb95
-rw-r--r--spec/models/public_body_change_request_spec.rb110
2 files changed, 205 insertions, 0 deletions
diff --git a/spec/controllers/public_body_change_requests_controller_spec.rb b/spec/controllers/public_body_change_requests_controller_spec.rb
new file mode 100644
index 000000000..00d9c7d40
--- /dev/null
+++ b/spec/controllers/public_body_change_requests_controller_spec.rb
@@ -0,0 +1,95 @@
+# -*- coding: utf-8 -*-
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe PublicBodyChangeRequestsController, "making a new change request" do
+
+ it "should show the form" do
+ get :new
+ response.should render_template("new")
+ end
+
+end
+
+describe PublicBodyChangeRequestsController, "creating a change request" do
+
+ context 'when handling a request for a new authority' do
+
+ before do
+ @email = "test@example.com"
+ name = "Test User"
+ @change_request_params = {:user_email => @email,
+ :user_name => name,
+ :public_body_name => 'New Body',
+ :public_body_email => 'new_body@example.com',
+ :notes => 'Please',
+ :source => 'http://www.example.com'}
+ end
+
+ it "should send an email to the site contact address" do
+ post :create, {:public_body_change_request => @change_request_params}
+ deliveries = ActionMailer::Base.deliveries
+ deliveries.size.should == 1
+ mail = deliveries[0]
+ mail.subject.should =~ /Add authority - New Body/
+ mail.from.should include(@email)
+ mail.to.should include('postmaster@localhost')
+ mail.body.should include('new_body@example.com')
+ mail.body.should include('New Body')
+ mail.body.should include("Please")
+ end
+
+ it 'should show a notice' do
+ post :create, {:public_body_change_request => @change_request_params}
+ expected_text = "Your request to add an authority has been sent. Thank you for getting in touch! We'll get back to you soon."
+ flash[:notice].should == expected_text
+ end
+
+ it 'should redirect to the frontpage' do
+ post :create, {:public_body_change_request => @change_request_params}
+ response.should redirect_to frontpage_url
+ end
+
+ end
+
+ context 'when handling a request for an update to an existing authority' do
+
+ before do
+ @email = "test@example.com"
+ name = "Test User"
+ @public_body = FactoryGirl.create(:public_body)
+ @change_request_params = {:user_email => @email,
+ :user_name => name,
+ :public_body_id => @public_body.id,
+ :public_body_email => 'new_body@example.com',
+ :notes => 'Please',
+ :source => 'http://www.example.com'}
+ end
+
+ it 'should send an email to the site contact address' do
+ post :create, {:public_body_change_request => @change_request_params}
+ deliveries = ActionMailer::Base.deliveries
+ deliveries.size.should == 1
+ mail = deliveries[0]
+ mail.subject.should =~ /Update email address - #{@public_body.name}/
+ mail.from.should include(@email)
+ mail.to.should include('postmaster@localhost')
+ mail.body.should include('new_body@example.com')
+ mail.body.should include(@public_body.name)
+ mail.body.should include("Please")
+ end
+
+ it 'should show a notice' do
+ post :create, {:public_body_change_request => @change_request_params}
+ expected_text = "Your request to update the address for #{@public_body.name} has been sent. Thank you for getting in touch! We'll get back to you soon."
+ flash[:notice].should == expected_text
+ end
+
+ it 'should redirect to the frontpage' do
+ post :create, {:public_body_change_request => @change_request_params}
+ response.should redirect_to frontpage_url
+ end
+
+ end
+
+
+end
diff --git a/spec/models/public_body_change_request_spec.rb b/spec/models/public_body_change_request_spec.rb
new file mode 100644
index 000000000..653a218e3
--- /dev/null
+++ b/spec/models/public_body_change_request_spec.rb
@@ -0,0 +1,110 @@
+# == Schema Information
+#
+# Table name: public_body_change_requests
+#
+# id :integer not null, primary key
+# user_email :string(255)
+# user_name :string(255)
+# user_id :integer
+# public_body_name :text
+# public_body_id :integer
+# public_body_email :string(255)
+# source_url :text
+# notes :text
+# is_open :boolean default(TRUE), not null
+# created_at :datetime not null
+# updated_at :datetime not null
+#
+
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe PublicBodyChangeRequest, 'when validating' do
+
+ it 'should not be valid without a public body name' do
+ change_request = PublicBodyChangeRequest.new()
+ change_request.valid?.should be_false
+ change_request.errors[:public_body_name].should == ['Please enter the name of the authority']
+ end
+
+ it 'should not be valid without a user name if there is no user' do
+ change_request = PublicBodyChangeRequest.new(:public_body_name => 'New Body')
+ change_request.valid?.should be_false
+ change_request.errors[:user_name].should == ['Please enter your name']
+ end
+
+ it 'should not be valid without a user email address if there is no user' do
+ change_request = PublicBodyChangeRequest.new(:public_body_name => 'New Body')
+ change_request.valid?.should be_false
+ change_request.errors[:user_email].should == ['Please enter your email address']
+ end
+
+ it 'should be valid with a user and no name or email address' do
+ user = FactoryGirl.build(:user)
+ change_request = PublicBodyChangeRequest.new(:user => user,
+ :public_body_name => 'New Body')
+ change_request.valid?.should be_true
+ end
+
+ it 'should validate the format of a user email address entered' do
+ change_request = PublicBodyChangeRequest.new(:public_body_name => 'New Body',
+ :user_email => '@example.com')
+ change_request.valid?.should be_false
+ change_request.errors[:user_email].should == ["Your email doesn't look like a valid address"]
+ end
+
+ it 'should validate the format of a public body email address entered' do
+ change_request = PublicBodyChangeRequest.new(:public_body_name => 'New Body',
+ :public_body_email => '@example.com')
+ change_request.valid?.should be_false
+ change_request.errors[:public_body_email].should == ["The authority email doesn't look like a valid address"]
+ end
+
+end
+
+describe PublicBodyChangeRequest, 'get_user_name' do
+
+ it 'should return the user_name field if there is no user association' do
+ change_request = PublicBodyChangeRequest.new(:user_name => 'Test User')
+ change_request.get_user_name.should == 'Test User'
+ end
+
+ it 'should return the name of the associated user if there is one' do
+ user = FactoryGirl.build(:user)
+ change_request = PublicBodyChangeRequest.new(:user => user)
+ change_request.get_user_name.should == user.name
+ end
+
+end
+
+
+describe PublicBodyChangeRequest, 'get_user_email' do
+
+ it 'should return the user_email field if there is no user association' do
+ change_request = PublicBodyChangeRequest.new(:user_email => 'user@example.com')
+ change_request.get_user_email.should == 'user@example.com'
+ end
+
+ it 'should return the email of the associated user if there is one' do
+ user = FactoryGirl.build(:user)
+ change_request = PublicBodyChangeRequest.new(:user => user)
+ change_request.get_user_email.should == user.email
+ end
+
+end
+
+
+describe PublicBodyChangeRequest, 'get_public_body_name' do
+
+ it 'should return the public_body_name field if there is no public body association' do
+ change_request = PublicBodyChangeRequest.new(:public_body_name => 'Test Authority')
+ change_request.get_public_body_name.should == 'Test Authority'
+ end
+
+ it 'should return the name of the associated public body if there is one' do
+ public_body = FactoryGirl.build(:public_body)
+ change_request = PublicBodyChangeRequest.new(:public_body => public_body)
+ change_request.get_public_body_name.should == public_body.name
+ end
+
+end
+