aboutsummaryrefslogtreecommitdiffstats
path: root/spec/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'spec/controllers')
-rw-r--r--spec/controllers/request_controller_spec.rb184
-rw-r--r--spec/controllers/track_controller_spec.rb18
-rw-r--r--spec/controllers/user_controller_spec.rb180
3 files changed, 357 insertions, 25 deletions
diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb
index e4404d4d0..b6d4e4c3f 100644
--- a/spec/controllers/request_controller_spec.rb
+++ b/spec/controllers/request_controller_spec.rb
@@ -263,7 +263,7 @@ describe RequestController, "when creating a new request" do
end
it "should accept a public body parameter" do
- get :new, :info_request => { :public_body_id => @body.id }
+ get :new, :public_body_id => @body.id
assigns[:info_request].public_body.should == @body
response.should render_template('new')
end
@@ -285,6 +285,15 @@ describe RequestController, "when creating a new request" do
response.should render_template('preview')
end
+ it "should allow re-editing of a request" do
+ post :new, :info_request => { :public_body_id => @body.id,
+ :title => "Why is your quango called Geraldine?"},
+ :outgoing_message => { :body => "This is a silly letter. It is too short to be interesting." },
+ :submitted_new_request => 1, :preview => 0,
+ :reedit => "Re-edit this request"
+ response.should render_template('new')
+ end
+
it "should redirect to sign in page when input is good and nobody is logged in" do
params = { :info_request => { :public_body_id => @body.id,
:title => "Why is your quango called Geraldine?"},
@@ -382,7 +391,7 @@ describe RequestController, "when making a new request" do
@user.stub!(:can_file_requests?).and_return(true)
User.stub!(:find).and_return(@user)
- @body = mock_model(PublicBody, :id => 314, :eir_only? => false, :is_requestable? => true)
+ @body = mock_model(PublicBody, :id => 314, :eir_only? => false, :is_requestable? => true, :name => "Test Quango")
PublicBody.stub!(:find).and_return(@body)
end
@@ -683,7 +692,11 @@ describe RequestController, "when classifying an information request" do
def request_url
"request/#{@dog_request.url_title}"
end
-
+
+ def unhappy_url
+ "help/unhappy/#{@dog_request.url_title}"
+ end
+
def expect_redirect(status, redirect_path)
post_status(status)
response.should redirect_to("http://test.host/#{redirect_path}")
@@ -691,16 +704,26 @@ describe RequestController, "when classifying an information request" do
it 'should redirect to the "request url" with a message in the right tense when status is updated to "waiting response" and the response is not overdue' do
@dog_request.stub!(:date_response_required_by).and_return(Time.now.to_date+1)
+ @dog_request.stub!(:date_very_overdue_after).and_return(Time.now.to_date+40)
+
expect_redirect("waiting_response", "request/#{@dog_request.url_title}")
flash[:notice].should match(/should get a response/)
end
it 'should redirect to the "request url" with a message in the right tense when status is updated to "waiting response" and the response is overdue' do
@dog_request.stub!(:date_response_required_by).and_return(Time.now.to_date-1)
+ @dog_request.stub!(:date_very_overdue_after).and_return(Time.now.to_date+40)
expect_redirect('waiting_response', request_url)
flash[:notice].should match(/should have got a response/)
end
-
+
+ it 'should redirect to the "request url" with a message in the right tense when status is updated to "waiting response" and the response is overdue' do
+ @dog_request.stub!(:date_response_required_by).and_return(Time.now.to_date-2)
+ @dog_request.stub!(:date_very_overdue_after).and_return(Time.now.to_date-1)
+ expect_redirect('waiting_response', unhappy_url)
+ flash[:notice].should match(/is long overdue/)
+ end
+
it 'should redirect to the "request url" when status is updated to "not held"' do
expect_redirect('not_held', request_url)
end
@@ -729,7 +752,7 @@ describe RequestController, "when classifying an information request" do
end
it 'should redirect to the "respond to last url" when status is updated to "gone postal"' do
- expect_redirect('gone_postal', "request/#{@dog_request.id}/response/1?gone_postal=1")
+ expect_redirect('gone_postal', "request/#{@dog_request.id}/response/#{@dog_request.get_last_response.id}?gone_postal=1")
end
it 'should redirect to the "request url" when status is updated to "internal review"' do
@@ -744,8 +767,8 @@ describe RequestController, "when classifying an information request" do
expect_redirect('error_message', "help/contact")
end
- it 'should redirect to the "request url" when status is updated to "user_withdrawn"' do
- expect_redirect('user_withdrawn', request_url)
+ it 'should redirect to the "respond to last url url" when status is updated to "user_withdrawn"' do
+ expect_redirect('user_withdrawn', "request/#{@dog_request.id}/response/#{@dog_request.get_last_response.id}")
end
end
@@ -775,7 +798,18 @@ describe RequestController, "when sending a followup message" do
response.should render_template('show_response')
end
+ it "should show preview when input is good" do
+ session[:user_id] = users(:bob_smith_user).id
+ post :show_response, :outgoing_message => { :body => "What a useless response! You suck.", :what_doing => 'normal_sort'}, :id => info_requests(:fancy_dog_request).id, :incoming_message_id => incoming_messages(:useless_incoming_message), :submitted_followup => 1, :preview => 1
+ response.should render_template('followup_preview')
+ end
+ it "should allow re-editing of a preview" do
+ session[:user_id] = users(:bob_smith_user).id
+ post :show_response, :outgoing_message => { :body => "What a useless response! You suck.", :what_doing => 'normal_sort'}, :id => info_requests(:fancy_dog_request).id, :incoming_message_id => incoming_messages(:useless_incoming_message), :submitted_followup => 1, :preview => 0, :reedit => "Re-edit this request"
+ response.should render_template('show_response')
+ end
+
it "should send the follow up message if you are the right user" do
# fake that this is a clarification
info_requests(:fancy_dog_request).set_described_state('waiting_clarification')
@@ -825,12 +859,16 @@ describe RequestController, "sending overdue request alerts" do
fixtures :info_requests, :info_request_events, :public_bodies, :users, :incoming_messages, :raw_emails, :outgoing_messages # all needed as integrating views
it "should send an overdue alert mail to creators of overdue requests" do
+ chicken_request = info_requests(:naughty_chicken_request)
+ chicken_request.outgoing_messages[0].last_sent_at = Time.now() - 30.days
+ chicken_request.outgoing_messages[0].save!
+
RequestMailer.alert_overdue_requests
deliveries = ActionMailer::Base.deliveries
deliveries.size.should == 1
mail = deliveries[0]
- mail.body.should =~ /promptly, as required by law/
+ mail.body.should =~ /promptly, as normally\s+required by law/
mail.to_addrs.to_s.should == info_requests(:naughty_chicken_request).user.name_and_email
mail.body =~ /(http:\/\/.*\/c\/(.*))/
@@ -845,7 +883,24 @@ describe RequestController, "sending overdue request alerts" do
assigns[:info_request].should == info_requests(:naughty_chicken_request)
end
- it "should send not actualy send the overdue alert if the user is banned" do
+ it "should include clause for schools when sending an overdue alert mail to creators of overdue requests" do
+ chicken_request = info_requests(:naughty_chicken_request)
+ chicken_request.outgoing_messages[0].last_sent_at = Time.now() - 30.days
+ chicken_request.outgoing_messages[0].save!
+
+ chicken_request.public_body.tag_string = "school"
+ chicken_request.public_body.save!
+
+ RequestMailer.alert_overdue_requests
+
+ deliveries = ActionMailer::Base.deliveries
+ deliveries.size.should == 1
+ mail = deliveries[0]
+ mail.body.should =~ /promptly, as normally\s+required by law during term time/
+ mail.to_addrs.to_s.should == info_requests(:naughty_chicken_request).user.name_and_email
+ end
+
+ it "should send not actually send the overdue alert if the user is banned" do
user = info_requests(:naughty_chicken_request).user
user.ban_text = 'Banned'
user.save!
@@ -856,6 +911,31 @@ describe RequestController, "sending overdue request alerts" do
deliveries.size.should == 0
end
+ it "should send a very overdue alert mail to creators of very overdue requests" do
+ chicken_request = info_requests(:naughty_chicken_request)
+ chicken_request.outgoing_messages[0].last_sent_at = Time.now() - 60.days
+ chicken_request.outgoing_messages[0].save!
+
+ RequestMailer.alert_overdue_requests
+
+ deliveries = ActionMailer::Base.deliveries
+ deliveries.size.should == 1
+ mail = deliveries[0]
+ mail.body.should =~ /required by law/
+ mail.to_addrs.to_s.should == info_requests(:naughty_chicken_request).user.name_and_email
+
+ mail.body =~ /(http:\/\/.*\/c\/(.*))/
+ mail_url = $1
+ mail_token = $2
+
+ session[:user_id].should be_nil
+ controller.test_code_redirect_by_email_token(mail_token, self) # XXX hack to avoid having to call User controller for email link
+ session[:user_id].should == info_requests(:naughty_chicken_request).user.id
+
+ response.should render_template('show_response')
+ assigns[:info_request].should == info_requests(:naughty_chicken_request)
+ end
+
end
describe RequestController, "sending unclassified new response reminder alerts" do
@@ -1031,6 +1111,92 @@ describe RequestController, "when viewing comments" do
end
+describe RequestController, "authority uploads a response from the web interface" do
+ fixtures :info_requests, :info_request_events, :public_bodies, :users
+
+ before(:all) do
+ # domain after the @ is used for authentication of FOI officers, so to test it
+ # we need a user which isn't at localhost.
+ @normal_user = User.new(:name => "Mr. Normal", :email => "normal-user@flourish.org",
+ :password => PostRedirect.generate_random_token)
+ @normal_user.save!
+
+ @foi_officer_user = User.new(:name => "The Geraldine Quango", :email => "geraldine-requests@localhost",
+ :password => PostRedirect.generate_random_token)
+ @foi_officer_user.save!
+ end
+
+ it "should require login to view the form to upload" do
+ @ir = info_requests(:fancy_dog_request)
+ @ir.public_body.is_foi_officer?(@normal_user).should == false
+ session[:user_id] = @normal_user.id
+
+ get :upload_response, :url_title => 'why_do_you_have_such_a_fancy_dog'
+ response.should render_template('user/wrong_user')
+ end
+
+ it "should let you view upload form if you are an FOI officer" do
+ @ir = info_requests(:fancy_dog_request)
+ @ir.public_body.is_foi_officer?(@foi_officer_user).should == true
+ session[:user_id] = @foi_officer_user.id
+
+ get :upload_response, :url_title => 'why_do_you_have_such_a_fancy_dog'
+ response.should render_template('request/upload_response')
+ end
+
+ it "should prevent uploads if you are not a requester" do
+ @ir = info_requests(:fancy_dog_request)
+ incoming_before = @ir.incoming_messages.size
+ session[:user_id] = @normal_user.id
+
+ # post up a photo of the parrot
+ parrot_upload = fixture_file_upload('parrot.png','image/png')
+ post :upload_response, :url_title => 'why_do_you_have_such_a_fancy_dog',
+ :body => "Find attached a picture of a parrot",
+ :file_1 => parrot_upload,
+ :submitted_upload_response => 1
+ response.should render_template('user/wrong_user')
+ end
+
+ it "should prevent entirely blank uploads" do
+ session[:user_id] = @foi_officer_user.id
+
+ post :upload_response, :url_title => 'why_do_you_have_such_a_fancy_dog', :body => "", :submitted_upload_response => 1
+ response.should render_template('request/upload_response')
+ flash[:error].should match(/Please type a message/)
+ end
+
+ # How do I test a file upload in rails?
+ # http://stackoverflow.com/questions/1178587/how-do-i-test-a-file-upload-in-rails
+ it "should let the requester upload a file" do
+ @ir = info_requests(:fancy_dog_request)
+ incoming_before = @ir.incoming_messages.size
+ session[:user_id] = @foi_officer_user.id
+
+ # post up a photo of the parrot
+ parrot_upload = fixture_file_upload('parrot.png','image/png')
+ post :upload_response, :url_title => 'why_do_you_have_such_a_fancy_dog',
+ :body => "Find attached a picture of a parrot",
+ :file_1 => parrot_upload,
+ :submitted_upload_response => 1
+
+ response.should redirect_to(:action => 'show', :url_title => 'why_do_you_have_such_a_fancy_dog')
+ flash[:notice].should match(/Thank you for responding to this FOI request/)
+
+ # check there is a new attachment
+ incoming_after = @ir.incoming_messages.size
+ incoming_after.should == incoming_before + 1
+
+ # check new attachment looks vaguely OK
+ new_im = @ir.incoming_messages[-1]
+ new_im.mail.body.should match(/Find attached a picture of a parrot/)
+ attachments = new_im.get_attachments_for_display
+ attachments.size.should == 1
+ attachments[0].filename.should == "parrot.png"
+ attachments[0].display_size.should == "94K"
+ end
+end
+
diff --git a/spec/controllers/track_controller_spec.rb b/spec/controllers/track_controller_spec.rb
index 7ab1c0a70..5959c2bc1 100644
--- a/spec/controllers/track_controller_spec.rb
+++ b/spec/controllers/track_controller_spec.rb
@@ -31,6 +31,11 @@ describe TrackController, "when sending alerts for a track" do
include LinkToHelper # for main_url
it "should send alerts" do
+ # set the time the comment event happened at to within the last week
+ ire = info_request_events(:silly_comment_event)
+ ire.created_at = Time.now - 3.days
+ ire.save!
+
TrackMailer.alert_tracks
deliveries = ActionMailer::Base.deliveries
@@ -41,12 +46,19 @@ describe TrackController, "when sending alerts for a track" do
mail.body =~ /(http:\/\/.*\/c\/(.*))/
mail_url = $1
mail_token = $2
-
+
mail.body.should_not =~ /&/
+ mail.body.should_not include('sent a request') # request not included
+ mail.body.should_not include('sent a response') # response not included
+ mail.body.should include('added an annotation') # comment included
+
+ mail.body.should =~ /This a the daftest comment the world has ever seen/ # comment text included
+
# Check subscription managing link
-# XXX We can't do this, as it is redirecting to another control, so this is a
-# functional test. Bah, I so don't care, bit of an obsessive constraint.
+# XXX We can't do this, as it is redirecting to another controller. I'm
+# apparently meant to be writing controller unit tests here, not functional
+# tests. Bah, I so don't care, bit of an obsessive constraint.
# session[:user_id].should be_nil
# controller.test_code_redirect_by_email_token(mail_token, self) # XXX hack to avoid having to call User controller for email link
# session[:user_id].should == users(:silly_name_user).id
diff --git a/spec/controllers/user_controller_spec.rb b/spec/controllers/user_controller_spec.rb
index 109a95cbc..7fc9dbf98 100644
--- a/spec/controllers/user_controller_spec.rb
+++ b/spec/controllers/user_controller_spec.rb
@@ -84,6 +84,15 @@ describe UserController, "when signing in" do
response.should_not send_email
end
+# No idea how to test this in the test framework :(
+# it "should have set a long lived cookie if they picked remember me, session cookie if they didn't" do
+# get :signin, :r => "/list"
+# response.should render_template('sign')
+# post :signin, { :user_signin => { :email => 'bob@localhost', :password => 'jonespassword' } }
+# session[:user_id].should == users(:bob_smith_user).id
+# raise session.options.to_yaml # check cookie lasts a month
+# end
+
it "should ask you to confirm your email if it isn't confirmed, after log in" do
get :signin, :r => "/list"
response.should render_template('sign')
@@ -231,14 +240,14 @@ describe UserController, "when changing password" do
fixtures :users
it "should show the email form when not logged in" do
- get :signchange
- response.should render_template('signchange_send_confirm')
+ get :signchangepassword
+ response.should render_template('signchangepassword_send_confirm')
end
it "should send a confirmation email when logged in normally" do
session[:user_id] = users(:bob_smith_user).id
- get :signchange
- response.should render_template('signchange_confirm')
+ get :signchangepassword
+ response.should render_template('signchangepassword_confirm')
deliveries = ActionMailer::Base.deliveries
deliveries.size.should == 1
@@ -249,15 +258,15 @@ describe UserController, "when changing password" do
it "should send a confirmation email when have wrong login circumstance" do
session[:user_id] = users(:bob_smith_user).id
session[:user_circumstance] = "bogus"
- get :signchange
- response.should render_template('signchange_confirm')
+ get :signchangepassword
+ response.should render_template('signchangepassword_confirm')
end
it "should show the password change screen when logged in as special password change mode" do
session[:user_id] = users(:bob_smith_user).id
session[:user_circumstance] = "change_password"
- get :signchange
- response.should render_template('signchange')
+ get :signchangepassword
+ response.should render_template('signchangepassword')
end
it "should change the password, if you have right to do so" do
@@ -265,8 +274,8 @@ describe UserController, "when changing password" do
session[:user_circumstance] = "change_password"
old_hash = users(:bob_smith_user).hashed_password
- post :signchange, { :user => { :password => 'ooo', :password_confirmation => 'ooo' },
- :submitted_signchange_password => 1
+ post :signchangepassword, { :user => { :password => 'ooo', :password_confirmation => 'ooo' },
+ :submitted_signchangepassword_do => 1
}
users(:bob_smith_user).hashed_password.should != old_hash
@@ -297,16 +306,161 @@ describe UserController, "when changing password" do
end
+describe UserController, "when changing email address" do
+ integrate_views
+ fixtures :users
+
+ it "should require login" do
+ get :signchangeemail
+
+ post_redirect = PostRedirect.get_last_post_redirect
+ response.should redirect_to(:controller => 'user', :action => 'signin', :token => post_redirect.token)
+ end
+
+ it "should show form for changing email if logged in" do
+ @user = users(:bob_smith_user)
+ session[:user_id] = @user.id
+
+ get :signchangeemail
+
+ response.should render_template('signchangeemail')
+ end
+
+ it "should be an error if the password is wrong, everything else right" do
+ @user = users(:bob_smith_user)
+ session[:user_id] = @user.id
+
+ post :signchangeemail, { :signchangeemail => { :old_email => 'bob@localhost',
+ :password => 'donotknowpassword', :new_email => 'newbob@localhost' },
+ :submitted_signchangeemail_do => 1
+ }
+
+ @user.reload
+ @user.email.should == 'bob@localhost'
+ response.should render_template('signchangeemail')
+ assigns[:signchangeemail].errors[:password].should_not be_nil
+
+ deliveries = ActionMailer::Base.deliveries
+ deliveries.size.should == 0
+ end
+
+ it "should be an error if old email is wrong, everything else right" do
+ @user = users(:bob_smith_user)
+ session[:user_id] = @user.id
+
+ post :signchangeemail, { :signchangeemail => { :old_email => 'bob@moo',
+ :password => 'jonespassword', :new_email => 'newbob@localhost' },
+ :submitted_signchangeemail_do => 1
+ }
+
+ @user.reload
+ @user.email.should == 'bob@localhost'
+ response.should render_template('signchangeemail')
+ assigns[:signchangeemail].errors[:old_email].should_not be_nil
+
+ deliveries = ActionMailer::Base.deliveries
+ deliveries.size.should == 0
+ end
+
+ it "should work even if the old email had a case difference" do
+ @user = users(:bob_smith_user)
+ session[:user_id] = @user.id
+
+ post :signchangeemail, { :signchangeemail => { :old_email => 'BOB@localhost',
+ :password => 'jonespassword', :new_email => 'newbob@localhost' },
+ :submitted_signchangeemail_do => 1
+ }
+
+ response.should render_template('signchangeemail_confirm')
+ end
+
+ it "should send confirmation email if you get all the details right" do
+ @user = users(:bob_smith_user)
+ session[:user_id] = @user.id
+
+ post :signchangeemail, { :signchangeemail => { :old_email => 'bob@localhost',
+ :password => 'jonespassword', :new_email => 'newbob@localhost' },
+ :submitted_signchangeemail_do => 1
+ }
+
+ @user.reload
+ @user.email.should == 'bob@localhost'
+ @user.email_confirmed.should == true
+
+ response.should render_template('signchangeemail_confirm')
+
+ deliveries = ActionMailer::Base.deliveries
+ deliveries.size.should == 1
+ mail = deliveries[0]
+ mail.body.should include("confirm that you want to change")
+ mail.to.should == [ 'newbob@localhost' ]
+
+ mail.body =~ /(http:\/\/.*(\/c\/(.*)))/
+ mail_url = $1
+ mail_path = $2
+ mail_token = $3
+
+ # Check confirmation URL works
+ session[:user_id] = nil
+ session[:user_circumstance].should == nil
+ get :confirm, :email_token => mail_token
+ session[:user_id].should == users(:bob_smith_user).id
+ session[:user_circumstance].should == 'change_email'
+ response.should redirect_to(:controller => 'user', :action => 'signchangeemail', :post_redirect => 1)
+
+ # Would be nice to do a follow_redirect! here, but rspec-rails doesn't
+ # have one. Instead do an equivalent manually.
+ post_redirect = PostRedirect.find_by_email_token(mail_token)
+ post_redirect.circumstance.should == 'change_email'
+ post_redirect.user.should == users(:bob_smith_user)
+ post_redirect.post_params.should == {"submitted_signchangeemail_do"=>"1",
+ "action"=>"signchangeemail",
+ "signchangeemail"=>{
+ "old_email"=>"bob@localhost",
+ "new_email"=>"newbob@localhost",
+ "password"=>"jonespassword"},
+ "controller"=>"user"}
+ post :signchangeemail, post_redirect.post_params
+
+ response.should redirect_to(:controller => 'user', :action => 'show', :url_name => 'bob_smith')
+ flash[:notice].should match(/You have now changed your email address/)
+ @user.reload
+ @user.email.should == 'newbob@localhost'
+ @user.email_confirmed.should == true
+ end
+
+ it "should send special 'already signed up' mail if you try to change your email to one already used" do
+ @user = users(:bob_smith_user)
+ session[:user_id] = @user.id
+
+ post :signchangeemail, { :signchangeemail => { :old_email => 'bob@localhost',
+ :password => 'jonespassword', :new_email => 'silly@localhost' },
+ :submitted_signchangeemail_do => 1
+ }
+
+ @user.reload
+ @user.email.should == 'bob@localhost'
+ @user.email_confirmed.should == true
+
+ response.should render_template('signchangeemail_confirm')
+
+ deliveries = ActionMailer::Base.deliveries
+ deliveries.size.should == 1
+ mail = deliveries[0]
+
+ mail.body.should include("perhaps you, just tried to change their")
+ mail.to.should == [ 'silly@localhost' ]
+ end
+end
+
describe UserController, "when using profile photos" do
integrate_views
fixtures :users
- it "should not let you change profile photo if you're not logged in as the user"
+ it "should not let you change profile photo if you're not logged in as the user" do
user = users(:bob_smith_user)
data = load_file_fixture("parrot.png")
post :profile_photo, { :id => user.id, :data => data }
end
-
end
-