aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rw-r--r--spec/controllers/application_controller_spec.rb9
-rw-r--r--spec/controllers/general_controller_spec.rb6
-rw-r--r--spec/controllers/public_body_controller_spec.rb18
-rw-r--r--spec/controllers/request_controller_spec.rb2
-rw-r--r--spec/controllers/track_controller_spec.rb14
-rw-r--r--spec/controllers/user_controller_spec.rb70
-rw-r--r--spec/integration/create_request_spec.rb35
-rw-r--r--spec/integration/search_request_spec.rb2
-rw-r--r--spec/lib/mail_handler/mail_handler_spec.rb23
-rw-r--r--spec/lib/tmail_extensions_spec.rb45
-rw-r--r--spec/models/application_mailer_spec.rb6
-rw-r--r--spec/models/incoming_message_spec.rb56
-rw-r--r--spec/models/info_request_event_spec.rb47
-rw-r--r--spec/models/info_request_spec.rb54
-rw-r--r--spec/models/request_mailer_spec.rb2
-rw-r--r--spec/models/track_mailer_spec.rb2
-rw-r--r--spec/models/xapian_spec.rb22
-rw-r--r--spec/script/handle-mail-replies_spec.rb10
-rw-r--r--spec/spec_helper.rb56
19 files changed, 263 insertions, 216 deletions
diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb
index 08e68d85e..18341ae6f 100644
--- a/spec/controllers/application_controller_spec.rb
+++ b/spec/controllers/application_controller_spec.rb
@@ -39,7 +39,14 @@ describe ApplicationController, "when caching fragments" do
it "should not fail with long filenames" do
long_name = "blahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah.txt"
- path = self.controller.send(:foi_fragment_cache_path, long_name)
+ params = { :only_path => true,
+ :file_name => [long_name],
+ :controller => "request",
+ :action => "get_attachment_as_html",
+ :id => "132",
+ :incoming_message_id => "44",
+ :part => "2" }
+ path = self.controller.send(:foi_fragment_cache_path, params)
self.controller.send(:foi_fragment_cache_write, path, "whassap")
end
diff --git a/spec/controllers/general_controller_spec.rb b/spec/controllers/general_controller_spec.rb
index 935f8eab6..830486493 100644
--- a/spec/controllers/general_controller_spec.rb
+++ b/spec/controllers/general_controller_spec.rb
@@ -144,7 +144,7 @@ describe GeneralController, "when showing the front page with fixture data" do
describe 'when constructing the list of recent requests' do
before(:each) do
- rebuild_xapian_index
+ get_fixtures_xapian_index
end
describe 'when there are fewer than five successful requests' do
@@ -189,8 +189,8 @@ describe GeneralController, 'when using xapian search' do
# rebuild xapian index after fixtures loaded
before(:each) do
- load_raw_emails_data
- rebuild_xapian_index
+ load_raw_emails_data
+ get_fixtures_xapian_index
end
it "should redirect from search query URL to pretty URL" do
diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb
index d12818a1c..29ece18cb 100644
--- a/spec/controllers/public_body_controller_spec.rb
+++ b/spec/controllers/public_body_controller_spec.rb
@@ -6,7 +6,7 @@ describe PublicBodyController, "when showing a body" do
before(:each) do
load_raw_emails_data
- rebuild_xapian_index
+ get_fixtures_xapian_index
end
it "should be successful" do
@@ -29,14 +29,14 @@ describe PublicBodyController, "when showing a body" do
assigns[:xapian_requests].results.map{|x|x[:model].info_request}.should =~ InfoRequest.all(
:conditions => ["public_body_id = ?", public_bodies(:geraldine_public_body).id])
end
-
+
it "should assign the requests (2)" do
get :show, :url_name => "tgq", :view => 'successful'
assigns[:xapian_requests].results.map{|x|x[:model].info_request}.should =~ InfoRequest.all(
:conditions => ["described_state = ? and public_body_id = ?",
"successful", public_bodies(:geraldine_public_body).id])
end
-
+
it "should assign the requests (3)" do
get :show, :url_name => "dfh", :view => 'all'
assigns[:xapian_requests].results.map{|x|x[:model].info_request}.should =~ InfoRequest.all(
@@ -66,7 +66,7 @@ describe PublicBodyController, "when showing a body" do
ActionController::Routing::Routes.filters = old_filters
end
-
+
it "should redirect to newest name if you use historic name of public body in URL" do
get :show, :url_name => "hdink", :view => 'all'
response.should redirect_to(:controller => 'public_body', :action => 'show', :url_name => "dfh")
@@ -148,7 +148,7 @@ describe PublicBodyController, "when listing bodies" do
get :list, :tag => "other"
response.should render_template('list')
assigns[:public_bodies].should =~ PublicBody.all(:conditions => "id not in (#{public_bodies(:humpadink_public_body).id}, #{PublicBody.internal_admin_body.id})")
-
+
get :list
response.should render_template('list')
assigns[:public_bodies].should =~ PublicBody.all(:conditions => "id <> #{PublicBody.internal_admin_body.id}")
@@ -194,10 +194,10 @@ end
describe PublicBodyController, "when doing type ahead searches" do
integrate_views
-
+
before(:each) do
load_raw_emails_data
- rebuild_xapian_index
+ get_fixtures_xapian_index
end
it "should return nothing for the empty query string" do
@@ -205,7 +205,7 @@ describe PublicBodyController, "when doing type ahead searches" do
response.should render_template('public_body/_search_ahead')
assigns[:xapian_requests].should be_nil
end
-
+
it "should return a body matching the given keyword, but not users with a matching description" do
get :search_typeahead, :query => "Geraldine"
response.should render_template('public_body/_search_ahead')
@@ -230,7 +230,7 @@ describe PublicBodyController, "when doing type ahead searches" do
end
it "should not return matches for short words" do
- get :search_typeahead, :query => "b"
+ get :search_typeahead, :query => "b"
response.should render_template('public_body/_search_ahead')
assigns[:xapian_requests].should be_nil
end
diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb
index 625bf17e7..61415661c 100644
--- a/spec/controllers/request_controller_spec.rb
+++ b/spec/controllers/request_controller_spec.rb
@@ -5,7 +5,7 @@ describe RequestController, "when listing recent requests" do
before(:each) do
load_raw_emails_data
- rebuild_xapian_index
+ get_fixtures_xapian_index
end
it "should be successful" do
diff --git a/spec/controllers/track_controller_spec.rb b/spec/controllers/track_controller_spec.rb
index 7daa23769..c785960b5 100644
--- a/spec/controllers/track_controller_spec.rb
+++ b/spec/controllers/track_controller_spec.rb
@@ -38,7 +38,7 @@ describe TrackController, "when making a new track on a request" do
get :track_request, :url_title => @ir.url_title, :feed => 'track'
response.should redirect_to(:controller => 'request', :action => 'show', :url_title => @ir.url_title)
end
-
+
it "should 404 for non-existent requests" do
session[:user_id] = @user.id
lambda {
@@ -61,9 +61,9 @@ describe TrackController, "when sending alerts for a track" do
before(:each) do
load_raw_emails_data
- rebuild_xapian_index
+ get_fixtures_xapian_index
end
-
+
it "should send alerts" do
# Don't do clever locale-insertion-unto-URL stuff
old_filters = ActionController::Routing::Routes.filters
@@ -138,7 +138,7 @@ describe TrackController, "when viewing RSS feed for a track" do
before(:each) do
load_raw_emails_data
- rebuild_xapian_index
+ get_fixtures_xapian_index
end
it "should get the RSS feed" do
@@ -168,7 +168,7 @@ describe TrackController, "when viewing JSON version of a track feed" do
before(:each) do
load_raw_emails_data
- rebuild_xapian_index
+ get_fixtures_xapian_index
end
it "should get the feed" do
@@ -210,9 +210,9 @@ describe TrackController, "when tracking a public body" do
before(:each) do
load_raw_emails_data
- rebuild_xapian_index
+ get_fixtures_xapian_index
end
-
+
it "should work" do
geraldine = public_bodies(:geraldine_public_body)
get :track_public_body, :feed => 'feed', :url_name => geraldine.url_name
diff --git a/spec/controllers/user_controller_spec.rb b/spec/controllers/user_controller_spec.rb
index 386d1b04b..23006803b 100644
--- a/spec/controllers/user_controller_spec.rb
+++ b/spec/controllers/user_controller_spec.rb
@@ -8,9 +8,9 @@ describe UserController, "when showing a user" do
integrate_views
before(:each) do
load_raw_emails_data
- rebuild_xapian_index
+ get_fixtures_xapian_index
end
-
+
it "should be successful" do
get :show, :url_name => "bob_smith"
response.should be_success
@@ -45,7 +45,7 @@ describe UserController, "when showing a user" do
get :show, :url_name => "bob_smith"
assigns[:xapian_requests].results.map{|x|x[:model].info_request}.should =~ InfoRequest.all(
:conditions => "user_id = #{users(:bob_smith_user).id}")
-
+
get :show, :url_name => "bob_smith", :user_query => "money"
assigns[:xapian_requests].results.map{|x|x[:model].info_request}.should =~ [
info_requests(:naughty_chicken_request),
@@ -218,7 +218,7 @@ describe UserController, "when signing in" do
# Get the confirmation URL, and check we’re still Joe
get :confirm, :email_token => post_redirect.email_token
session[:user_id].should == users(:admin_user).id
-
+
# And the redirect should still work, of course
response.should redirect_to(:controller => 'request', :action => 'list', :post_redirect => 1)
@@ -232,21 +232,21 @@ describe UserController, "when signing up" do
it "should be an error if you type the password differently each time" do
post :signup, { :user_signup => { :email => 'new@localhost', :name => 'New Person',
- :password => 'sillypassword', :password_confirmation => 'sillypasswordtwo' }
+ :password => 'sillypassword', :password_confirmation => 'sillypasswordtwo' }
}
assigns[:user_signup].errors[:password].should == 'Please enter the same password twice'
end
it "should be an error to sign up with a misformatted email" do
post :signup, { :user_signup => { :email => 'malformed-email', :name => 'Mr Malformed',
- :password => 'sillypassword', :password_confirmation => 'sillypassword' }
+ :password => 'sillypassword', :password_confirmation => 'sillypassword' }
}
assigns[:user_signup].errors[:email].should_not be_nil
end
it "should send confirmation mail if you fill in the form right" do
post :signup, { :user_signup => { :email => 'new@localhost', :name => 'New Person',
- :password => 'sillypassword', :password_confirmation => 'sillypassword' }
+ :password => 'sillypassword', :password_confirmation => 'sillypassword' }
}
response.should render_template('confirm')
@@ -270,13 +270,13 @@ describe UserController, "when signing up" do
it "should send special 'already signed up' mail if you fill the form in with existing registered email" do
post :signup, { :user_signup => { :email => 'silly@localhost', :name => 'New Person',
- :password => 'sillypassword', :password_confirmation => 'sillypassword' }
+ :password => 'sillypassword', :password_confirmation => 'sillypassword' }
}
response.should render_template('confirm')
deliveries = ActionMailer::Base.deliveries
deliveries.size.should == 1
-
+
# This text may span a line break, depending on the length of the SITE_NAME
deliveries[0].body.should match(/when\s+you\s+already\s+have\s+an/)
end
@@ -377,7 +377,7 @@ describe UserController, "when changing password" do
get :signchangepassword
response.should render_template('signchangepassword')
end
-
+
it "should change the password, if you have right to do so" do
session[:user_id] = users(:bob_smith_user).id
session[:user_circumstance] = "change_password"
@@ -437,8 +437,8 @@ describe UserController, "when changing email address" do
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',
+
+ post :signchangeemail, { :signchangeemail => { :old_email => 'bob@localhost',
:password => 'donotknowpassword', :new_email => 'newbob@localhost' },
:submitted_signchangeemail_do => 1
}
@@ -455,8 +455,8 @@ describe UserController, "when changing email address" do
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',
+
+ post :signchangeemail, { :signchangeemail => { :old_email => 'bob@moo',
:password => 'jonespassword', :new_email => 'newbob@localhost' },
:submitted_signchangeemail_do => 1
}
@@ -473,8 +473,8 @@ describe UserController, "when changing email address" do
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',
+
+ post :signchangeemail, { :signchangeemail => { :old_email => 'BOB@localhost',
:password => 'jonespassword', :new_email => 'newbob@localhost' },
:submitted_signchangeemail_do => 1
}
@@ -485,8 +485,8 @@ describe UserController, "when changing email address" do
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',
+
+ post :signchangeemail, { :signchangeemail => { :old_email => 'bob@localhost',
:password => 'jonespassword', :new_email => 'newbob@localhost' },
:submitted_signchangeemail_do => 1
}
@@ -521,16 +521,16 @@ describe UserController, "when changing email address" do
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",
+ post_redirect.post_params.should == {"submitted_signchangeemail_do"=>"1",
+ "action"=>"signchangeemail",
"signchangeemail"=>{
- "old_email"=>"bob@localhost",
- "new_email"=>"newbob@localhost"},
+ "old_email"=>"bob@localhost",
+ "new_email"=>"newbob@localhost"},
"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/)
+ flash[:notice].should match(/You have now changed your email address/)
@user.reload
@user.email.should == 'newbob@localhost'
@user.email_confirmed.should == true
@@ -539,8 +539,8 @@ describe UserController, "when changing email address" do
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',
+
+ post :signchangeemail, { :signchangeemail => { :old_email => 'bob@localhost',
:password => 'jonespassword', :new_email => 'silly@localhost' },
:submitted_signchangeemail_do => 1
}
@@ -572,9 +572,9 @@ describe UserController, "when using profile photos" do
@uploadedfile_2 = File.open(file_fixture_name("parrot.jpg"))
@uploadedfile_2.stub!(:original_filename).and_return('parrot.jpg')
end
-
+
it "should not let you change profile photo if you're not logged in as the user" do
- post :set_profile_photo, { :id => @user.id, :file => @uploadedfile, :submitted_draft_profile_photo => 1, :automatically_crop => 1 }
+ post :set_profile_photo, { :id => @user.id, :file => @uploadedfile, :submitted_draft_profile_photo => 1, :automatically_crop => 1 }
end
it "should return a 404 not a 500 when a profile photo has not been set" do
@@ -588,10 +588,10 @@ describe UserController, "when using profile photos" do
@user.profile_photo.should be_nil
session[:user_id] = @user.id
- post :set_profile_photo, { :id => @user.id, :file => @uploadedfile, :submitted_draft_profile_photo => 1, :automatically_crop => 1 }
+ post :set_profile_photo, { :id => @user.id, :file => @uploadedfile, :submitted_draft_profile_photo => 1, :automatically_crop => 1 }
response.should redirect_to(:controller => 'user', :action => 'show', :url_name => "bob_smith")
- flash[:notice].should match(/Thank you for updating your profile photo/)
+ flash[:notice].should match(/Thank you for updating your profile photo/)
@user.reload
@user.profile_photo.should_not be_nil
@@ -601,13 +601,13 @@ describe UserController, "when using profile photos" do
@user.profile_photo.should be_nil
session[:user_id] = @user.id
- post :set_profile_photo, { :id => @user.id, :file => @uploadedfile, :submitted_draft_profile_photo => 1, :automatically_crop => 1 }
+ post :set_profile_photo, { :id => @user.id, :file => @uploadedfile, :submitted_draft_profile_photo => 1, :automatically_crop => 1 }
response.should redirect_to(:controller => 'user', :action => 'show', :url_name => "bob_smith")
- flash[:notice].should match(/Thank you for updating your profile photo/)
+ flash[:notice].should match(/Thank you for updating your profile photo/)
- post :set_profile_photo, { :id => @user.id, :file => @uploadedfile_2, :submitted_draft_profile_photo => 1, :automatically_crop => 1 }
+ post :set_profile_photo, { :id => @user.id, :file => @uploadedfile_2, :submitted_draft_profile_photo => 1, :automatically_crop => 1 }
response.should redirect_to(:controller => 'user', :action => 'show', :url_name => "bob_smith")
- flash[:notice].should match(/Thank you for updating your profile photo/)
+ flash[:notice].should match(/Thank you for updating your profile photo/)
@user.reload
@user.profile_photo.should_not be_nil
@@ -617,7 +617,7 @@ describe UserController, "when using profile photos" do
end
describe UserController, "when showing JSON version for API" do
-
+
it "should be successful" do
get :show, :url_name => "bob_smith", :format => "json"
@@ -634,7 +634,7 @@ describe UserController, "when viewing the wall" do
integrate_views
before(:each) do
- rebuild_xapian_index
+ get_fixtures_xapian_index
end
it "should show users stuff on their wall, most recent first" do
diff --git a/spec/integration/create_request_spec.rb b/spec/integration/create_request_spec.rb
index 56757c7e0..4efbf94ee 100644
--- a/spec/integration/create_request_spec.rb
+++ b/spec/integration/create_request_spec.rb
@@ -1,23 +1,30 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe "When creating requests" do
- it "should associate the request with the requestor, even if it is approved by an admin" do
- # This is a test for https://github.com/mysociety/alaveteli/issues/446
- params = { :info_request => { :public_body_id => public_bodies(:geraldine_public_body).id,
- :title => "Why is your quango called Geraldine?", :tag_string => "" },
- :outgoing_message => { :body => "This is a silly letter. It is too short to be interesting." },
- :submitted_new_request => 1, :preview => 0
- }
+ def create_request_unregistered
+ params = { :info_request => { :public_body_id => public_bodies(:geraldine_public_body).id,
+ :title => "Why is your quango called Geraldine?",
+ :tag_string => "" },
+ :outgoing_message => { :body => "This is a silly letter. It is too short to be interesting." },
+ :submitted_new_request => 1,
+ :preview => 0
+ }
- # Initially we are not logged in. Try to create a new request.
- post "/new", params
- # We expect to be redirected to the login page
- post_redirect = PostRedirect.get_last_post_redirect
- response.should redirect_to(:controller => 'user', :action => 'signin', :token => post_redirect.token)
- follow_redirect!
- response.should render_template("user/sign")
+ # Initially we are not logged in. Try to create a new request.
+ post "/new", params
+ # We expect to be redirected to the login page
+ post_redirect = PostRedirect.get_last_post_redirect
+ response.should redirect_to(:controller => 'user', :action => 'signin', :token => post_redirect.token)
+ follow_redirect!
+ response.should render_template("user/sign")
+ response.body.should match(/To send your FOI request, please sign in or make a new account./)
+ end
+ it "should associate the request with the requestor, even if it is approved by an admin" do
+ # This is a test for https://github.com/mysociety/alaveteli/issues/446
+ create_request_unregistered
+ post_redirect = PostRedirect.get_last_post_redirect
# Now log in as an unconfirmed user.
post "/profile/sign_in", :user_signin => {:email => users(:unconfirmed_user).email, :password => "jonespassword"}, :token => post_redirect.token
# This will trigger a confirmation mail. Get the PostRedirect for later.
diff --git a/spec/integration/search_request_spec.rb b/spec/integration/search_request_spec.rb
index 17a7b4aaa..c564032a6 100644
--- a/spec/integration/search_request_spec.rb
+++ b/spec/integration/search_request_spec.rb
@@ -4,7 +4,7 @@ describe "When searching" do
before(:each) do
load_raw_emails_data
- rebuild_xapian_index
+ get_fixtures_xapian_index
end
it "should not strip quotes from quoted query" do
diff --git a/spec/lib/mail_handler/mail_handler_spec.rb b/spec/lib/mail_handler/mail_handler_spec.rb
new file mode 100644
index 000000000..a3fba0698
--- /dev/null
+++ b/spec/lib/mail_handler/mail_handler_spec.rb
@@ -0,0 +1,23 @@
+# coding: utf-8
+require File.expand_path(File.dirname(__FILE__) + '../../../spec_helper')
+
+describe 'when creating a mail object from raw data' do
+
+ it 'should correctly parse a multipart email with a linebreak in the boundary' do
+ mail = get_fixture_mail('space-boundary.email')
+ mail.parts.size.should == 2
+ mail.multipart?.should == true
+ end
+
+ it 'should parse multiple to addresses with unqoted display names' do
+ mail = get_fixture_mail('multiple-unquoted-display-names.email')
+ mail.to.should == ["request-66666-caa77777@whatdotheyknow.com", "foi@example.com"]
+ end
+
+ it 'should convert an iso8859 email to utf8' do
+ mail = get_fixture_mail('iso8859_2_raw_email.email')
+ mail.subject.should have_text(/gjatë/u)
+ mail.body.is_utf8?.should == true
+ end
+
+end
diff --git a/spec/lib/tmail_extensions_spec.rb b/spec/lib/tmail_extensions_spec.rb
deleted file mode 100644
index bd89e6a84..000000000
--- a/spec/lib/tmail_extensions_spec.rb
+++ /dev/null
@@ -1,45 +0,0 @@
-# coding: utf-8
-# This is a test of the set_content_type monkey patch in
-# lib/tmail_extensions.rb
-
-require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
-
-describe "when using TMail" do
-
- before(:each) do
- ActionMailer::Base.deliveries.clear
- end
-
- it "should load an email with funny MIME settings" do
- # just send it to the holding pen
- InfoRequest.holding_pen_request.incoming_messages.size.should == 0
- receive_incoming_mail("humberside-police-odd-mime-type.email", 'dummy')
- InfoRequest.holding_pen_request.incoming_messages.size.should == 1
-
- # clear the notification of new message in holding pen
- deliveries = ActionMailer::Base.deliveries
- deliveries.size.should == 1
- deliveries.clear
-
- incoming_message = InfoRequest.holding_pen_request.incoming_messages[0]
-
- # This will raise an error if the bug in TMail hasn't been fixed
- incoming_message.get_body_for_html_display()
- end
-
- it 'should parse multiple to addresses with unqoted display names' do
- mail = TMail::Mail.parse(load_file_fixture('multiple-unquoted-display-names.email'))
- mail.to.should == ["request-66666-caa77777@whatdotheyknow.com", "foi@example.com"]
- end
-
- it 'should convert to utf8' do
- # NB this isn't actually a TMail extension, but is core TMail;
- # this was just a convenient place to assert the UTF8
- # conversion is working
- mail = TMail::Mail.parse(load_file_fixture('iso8859_2_raw_email.email'))
- mail.subject.should have_text(/gjatë/u)
- mail.body.is_utf8?.should == true
- end
-
-end
-
diff --git a/spec/models/application_mailer_spec.rb b/spec/models/application_mailer_spec.rb
index a90f79c01..acf5f43bc 100644
--- a/spec/models/application_mailer_spec.rb
+++ b/spec/models/application_mailer_spec.rb
@@ -80,7 +80,8 @@ describe ApplicationMailer do
it 'should raise an error if the template is in neither core nor theme' do
prepend_theme_views('theme_one')
- lambda{ ApplicationMailer.create_neither() }.should raise_error('Missing template application_mailer/neither.erb in view path spec/fixtures/theme_views/theme_one:spec/fixtures/theme_views/core')
+ expected_error = 'Missing template application_mailer/neither.erb in view path'
+ lambda{ ApplicationMailer.create_neither() }.should raise_error(/#{expected_error}/)
end
it 'should render a multipart email using a theme template' do
@@ -125,7 +126,8 @@ describe ApplicationMailer do
it 'should raise an error if the template is in neither core nor theme' do
append_theme_views('theme_one')
- lambda{ ApplicationMailer.create_neither() }.should raise_error('Missing template application_mailer/neither.erb in view path spec/fixtures/theme_views/core:spec/fixtures/theme_views/theme_one')
+ expected_error = 'Missing template application_mailer/neither.erb in view path'
+ lambda{ ApplicationMailer.create_neither() }.should raise_error(/#{expected_error}/)
end
it 'should render a multipart email using a core template' do
diff --git a/spec/models/incoming_message_spec.rb b/spec/models/incoming_message_spec.rb
index b038c43d9..fdbcd1e23 100644
--- a/spec/models/incoming_message_spec.rb
+++ b/spec/models/incoming_message_spec.rb
@@ -85,6 +85,26 @@ describe IncomingMessage, " when dealing with incoming mail" do
end
end
+
+ it "should load an email with funny MIME settings" do
+ ActionMailer::Base.deliveries.clear
+ # just send it to the holding pen
+ InfoRequest.holding_pen_request.incoming_messages.size.should == 0
+ receive_incoming_mail("humberside-police-odd-mime-type.email", 'dummy')
+ InfoRequest.holding_pen_request.incoming_messages.size.should == 1
+
+ # clear the notification of new message in holding pen
+ deliveries = ActionMailer::Base.deliveries
+ deliveries.size.should == 1
+ deliveries.clear
+
+ incoming_message = InfoRequest.holding_pen_request.incoming_messages[0]
+
+ # This will raise an error if the bug in TMail hasn't been fixed
+ incoming_message.get_body_for_html_display()
+ end
+
+
end
describe IncomingMessage, "when parsing HTML mail" do
@@ -399,14 +419,8 @@ end
describe IncomingMessage, " when uudecoding bad messages" do
- before(:each) do
- load_raw_emails_data
- end
-
it "should be able to do it at all" do
- mail_body = load_file_fixture('incoming-request-bad-uuencoding.email')
- mail = TMail::Mail.parse(mail_body)
- mail.base64_decode
+ mail = get_fixture_mail('incoming-request-bad-uuencoding.email')
im = incoming_messages(:useless_incoming_message)
im.stub!(:mail).and_return(mail)
im.extract_attachments!
@@ -418,9 +432,7 @@ describe IncomingMessage, " when uudecoding bad messages" do
end
it "should apply censor rules" do
- mail_body = load_file_fixture('incoming-request-bad-uuencoding.email')
- mail = TMail::Mail.parse(mail_body)
- mail.base64_decode
+ mail = get_fixture_mail('incoming-request-bad-uuencoding.email')
im = incoming_messages(:useless_incoming_message)
im.stub!(:mail).and_return(mail)
@@ -443,14 +455,8 @@ end
describe IncomingMessage, "when messages are attached to messages" do
- before(:each) do
- load_raw_emails_data
- end
-
it "should flatten all the attachments out" do
- mail_body = load_file_fixture('incoming-request-attach-attachments.email')
- mail = TMail::Mail.parse(mail_body)
- mail.base64_decode
+ mail = get_fixture_mail('incoming-request-attach-attachments.email')
im = incoming_messages(:useless_incoming_message)
im.stub!(:mail).and_return(mail)
@@ -468,14 +474,8 @@ end
describe IncomingMessage, "when Outlook messages are attached to messages" do
- before(:each) do
- load_raw_emails_data
- end
-
it "should flatten all the attachments out" do
- mail_body = load_file_fixture('incoming-request-oft-attachments.email')
- mail = TMail::Mail.parse(mail_body)
- mail.base64_decode
+ mail = get_fixture_mail('incoming-request-oft-attachments.email')
im = incoming_messages(:useless_incoming_message)
im.stub!(:mail).and_return(mail)
@@ -490,14 +490,8 @@ end
describe IncomingMessage, "when TNEF attachments are attached to messages" do
- before(:each) do
- load_raw_emails_data
- end
-
it "should flatten all the attachments out" do
- mail_body = load_file_fixture('incoming-request-tnef-attachments.email')
- mail = TMail::Mail.parse(mail_body)
- mail.base64_decode
+ mail = get_fixture_mail('incoming-request-tnef-attachments.email')
im = incoming_messages(:useless_incoming_message)
im.stub!(:mail).and_return(mail)
diff --git a/spec/models/info_request_event_spec.rb b/spec/models/info_request_event_spec.rb
index 7352f3be0..796f8b840 100644
--- a/spec/models/info_request_event_spec.rb
+++ b/spec/models/info_request_event_spec.rb
@@ -54,36 +54,71 @@ describe InfoRequestEvent do
end
- describe "doing search/index stuff" do
+ describe "doing search/index stuff" do
before(:each) do
load_raw_emails_data
parse_all_incoming_messages
end
- it 'should get search text for outgoing messages' do
+ it 'should get search text for outgoing messages' do
event = info_request_events(:useless_outgoing_message_event)
message = outgoing_messages(:useless_outgoing_message).body
event.search_text_main.should == message + "\n\n"
end
- it 'should get search text for incoming messages' do
+ it 'should get search text for incoming messages' do
event = info_request_events(:useless_incoming_message_event)
event.search_text_main.strip.should == "No way! I'm not going to tell you that in a month of Thursdays.\n\nThe Geraldine Quango"
end
- it 'should get clipped text for incoming messages, and cache it too' do
+ it 'should get clipped text for incoming messages, and cache it too' do
event = info_request_events(:useless_incoming_message_event)
-
+
event.incoming_message_selective_columns("cached_main_body_text_folded").cached_main_body_text_folded = nil
event.search_text_main(true).strip.should == "No way! I'm not going to tell you that in a month of Thursdays.\n\nThe Geraldine Quango"
event.incoming_message_selective_columns("cached_main_body_text_folded").cached_main_body_text_folded.should_not == nil
end
-
end
+ describe 'when asked if it has the same email as a previous send' do
+
+ before do
+ @info_request_event = InfoRequestEvent.new
+ end
+
+ it 'should return true if the email in its params and the previous email the request was sent to are both nil' do
+ @info_request_event.stub!(:params).and_return({})
+ @info_request_event.stub_chain(:info_request, :get_previous_email_sent_to).and_return(nil)
+ @info_request_event.same_email_as_previous_send?.should be_true
+ end
+
+ it 'should return false if one email address exists and the other does not' do
+ @info_request_event.stub!(:params).and_return(:email => 'test@example.com')
+ @info_request_event.stub_chain(:info_request, :get_previous_email_sent_to).and_return(nil)
+ @info_request_event.same_email_as_previous_send?.should be_false
+ end
+ it 'should return true if the addresses are identical' do
+ @info_request_event.stub!(:params).and_return(:email => 'test@example.com')
+ @info_request_event.stub_chain(:info_request, :get_previous_email_sent_to).and_return('test@example.com')
+ @info_request_event.same_email_as_previous_send?.should be_true
+ end
+
+ it 'should return false if the addresses are different' do
+ @info_request_event.stub!(:params).and_return(:email => 'test@example.com')
+ @info_request_event.stub_chain(:info_request, :get_previous_email_sent_to).and_return('different@example.com')
+ @info_request_event.same_email_as_previous_send?.should be_false
+ end
+
+ it 'should return true if the addresses have different formats' do
+ @info_request_event.stub!(:params).and_return(:email => 'A Test <test@example.com>')
+ @info_request_event.stub_chain(:info_request, :get_previous_email_sent_to).and_return('test@example.com')
+ @info_request_event.same_email_as_previous_send?.should be_true
+ end
+
+ end
end
diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb
index 2aeac2fec..58d62f29c 100644
--- a/spec/models/info_request_spec.rb
+++ b/spec/models/info_request_spec.rb
@@ -438,6 +438,33 @@ describe InfoRequest do
end
+ describe 'when an instance is asked if all can view it' do
+
+ before do
+ @info_request = InfoRequest.new
+ end
+
+ it 'should return true if its prominence is normal' do
+ @info_request.prominence = 'normal'
+ @info_request.all_can_view?.should == true
+ end
+
+ it 'should return true if its prominence is backpage' do
+ @info_request.prominence = 'backpage'
+ @info_request.all_can_view?.should == true
+ end
+
+ it 'should return false if its prominence is hidden' do
+ @info_request.prominence = 'hidden'
+ @info_request.all_can_view?.should == false
+ end
+
+ it 'should return false if its prominence is requester_only' do
+ @info_request.prominence = 'requester_only'
+ @info_request.all_can_view?.should == false
+ end
+ end
+
describe 'when applying censor rules' do
before do
@@ -521,32 +548,5 @@ describe InfoRequest do
end
- describe 'when an instance is asked if all can view it' do
-
- before do
- @info_request = InfoRequest.new
- end
-
- it 'should return true if its prominence is normal' do
- @info_request.prominence = 'normal'
- @info_request.all_can_view?.should == true
- end
-
- it 'should return true if its prominence is backpage' do
- @info_request.prominence = 'backpage'
- @info_request.all_can_view?.should == true
- end
-
- it 'should return false if its prominence is hidden' do
- @info_request.prominence = 'hidden'
- @info_request.all_can_view?.should == false
- end
-
- it 'should return false if its prominence is requester_only' do
- @info_request.prominence = 'requester_only'
- @info_request.all_can_view?.should == false
- end
-
- end
end
diff --git a/spec/models/request_mailer_spec.rb b/spec/models/request_mailer_spec.rb
index 906756784..0f09e6926 100644
--- a/spec/models/request_mailer_spec.rb
+++ b/spec/models/request_mailer_spec.rb
@@ -98,7 +98,7 @@ describe RequestMailer, " when receiving incoming mail" do
mail.multipart?.should == true
mail.parts.size.should == 2
message_part = mail.parts[0].to_s
- bounced_mail = TMail::Mail.parse(mail.parts[1].body)
+ bounced_mail = MailHandler.mail_from_raw_email(mail.parts[1].body, decode=false)
bounced_mail.to.should == [ ir.incoming_email ]
bounced_mail.from.should == [ 'geraldinequango@localhost' ]
bounced_mail.body.include?("That's so totally a rubbish question").should be_true
diff --git a/spec/models/track_mailer_spec.rb b/spec/models/track_mailer_spec.rb
index 1bf77dab5..9bf03c3d0 100644
--- a/spec/models/track_mailer_spec.rb
+++ b/spec/models/track_mailer_spec.rb
@@ -169,7 +169,7 @@ describe TrackMailer do
it 'should deliver one email, with right headers' do
@user = mock_model(User,
- :name_and_email => TMail::Address.address_from_name_and_email('Tippy Test', 'tippy@localhost'),
+ :name_and_email => MailHandler.address_from_name_and_email('Tippy Test', 'tippy@localhost'),
:url_name => 'tippy_test'
)
diff --git a/spec/models/xapian_spec.rb b/spec/models/xapian_spec.rb
index 195b39eee..8c99d550f 100644
--- a/spec/models/xapian_spec.rb
+++ b/spec/models/xapian_spec.rb
@@ -4,9 +4,9 @@ describe User, " when indexing users with Xapian" do
before(:each) do
load_raw_emails_data
- rebuild_xapian_index
+ get_fixtures_xapian_index
end
-
+
it "should search by name" do
# def InfoRequest.full_search(models, query, order, ascending, collapse, per_page, page)
xapian_object = InfoRequest.full_search([User], "Silly", 'created_at', true, nil, 100, 1)
@@ -21,7 +21,7 @@ describe User, " when indexing users with Xapian" do
xapian_object = InfoRequest.full_search([User], "stuff", 'created_at', true, nil, 100, 1)
xapian_object.results.size.should == 1
xapian_object.results[0][:model].should == user
-
+
user.about_me = "I am really an aardvark, true story."
user.save!
update_xapian_index
@@ -38,7 +38,7 @@ end
describe PublicBody, " when indexing public bodies with Xapian" do
before(:each) do
load_raw_emails_data
- rebuild_xapian_index
+ get_fixtures_xapian_index
end
it "should search index the main name field" do
@@ -71,7 +71,7 @@ describe PublicBody, " when indexing requests by body they are to" do
before(:each) do
load_raw_emails_data
- rebuild_xapian_index
+ get_fixtures_xapian_index
end
it "should find requests to the body" do
@@ -126,7 +126,7 @@ end
describe User, " when indexing requests by user they are from" do
before(:each) do
load_raw_emails_data
- rebuild_xapian_index
+ get_fixtures_xapian_index
end
it "should find requests from the user" do
@@ -204,7 +204,7 @@ end
describe User, " when indexing comments by user they are by" do
before(:each) do
load_raw_emails_data
- rebuild_xapian_index
+ get_fixtures_xapian_index
end
it "should find requests from the user" do
@@ -239,7 +239,7 @@ end
describe InfoRequest, " when indexing requests by their title" do
before(:each) do
load_raw_emails_data
- rebuild_xapian_index
+ get_fixtures_xapian_index
end
it "should find events for the request" do
@@ -268,7 +268,7 @@ end
describe InfoRequest, " when indexing requests by tag" do
before(:each) do
load_raw_emails_data
- rebuild_xapian_index
+ get_fixtures_xapian_index
end
it "should find request by tag, even when changes" do
@@ -289,7 +289,7 @@ end
describe PublicBody, " when indexing authorities by tag" do
before(:each) do
load_raw_emails_data
- rebuild_xapian_index
+ get_fixtures_xapian_index
end
it "should find request by tag, even when changes" do
@@ -313,7 +313,7 @@ end
describe PublicBody, " when only indexing selected things on a rebuild" do
before(:each) do
load_raw_emails_data
- rebuild_xapian_index
+ get_fixtures_xapian_index
end
it "should only index what we ask it to" do
diff --git a/spec/script/handle-mail-replies_spec.rb b/spec/script/handle-mail-replies_spec.rb
index 406af9ee3..90a8de27c 100644
--- a/spec/script/handle-mail-replies_spec.rb
+++ b/spec/script/handle-mail-replies_spec.rb
@@ -5,7 +5,7 @@ def mail_reply_test(email_filename)
Dir.chdir Rails.root do
xc = ExternalCommand.new("script/handle-mail-replies", "--test")
xc.run(load_file_fixture(email_filename))
-
+
xc.err.should == ""
return xc
end
@@ -14,7 +14,7 @@ end
describe "When filtering" do
it "should not fail when not in test mode" do
xc = ExternalCommand.new("script/handle-mail-replies")
- xc.run(load_file_fixture("track-response-exim-bounce.email"))
+ xc.run(load_file_fixture("track-response-exim-bounce.email"))
xc.err.should == ""
end
@@ -23,19 +23,19 @@ describe "When filtering" do
r.status.should == 1
r.out.should == "user@example.com\n"
end
-
+
it "should detect a WebShield delivery error message" do
r = mail_reply_test("track-response-webshield-bounce.email")
r.status.should == 1
r.out.should == "failed.user@example.co.uk\n"
end
-
+
it "should detect a MS Exchange non-permanent delivery error message" do
r = mail_reply_test("track-response-ms-bounce.email")
r.status.should == 1
r.out.should == ""
end
-
+
it "should pass on a non-bounce message" do
r = mail_reply_test("incoming-request-bad-uuencoding.email")
r.status.should == 0
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 248dff70e..d4dad591d 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -23,7 +23,6 @@ FakeWeb.register_uri(:purge, %r|varnish.localdomain|, :body => "OK")
# Use test-specific translations
FastGettext.add_text_domain 'app', :path => File.join(File.dirname(__FILE__), 'fixtures', 'locale'), :type => :po
FastGettext.default_text_domain = 'app'
-
Spec::Runner.configure do |config|
# If you're not using ActiveRecord you should remove these
# lines, delete config/database.yml and disable :active_record
@@ -47,6 +46,7 @@ Spec::Runner.configure do |config|
:holidays,
:track_things_sent_emails
+
# == Fixtures
#
# You can declare fixtures for each example_group like this:
@@ -99,6 +99,19 @@ def load_file_fixture(file_name)
return content
end
+def parse_all_incoming_messages
+ IncomingMessage.find(:all).each{ |x| x.parse_raw_email! }
+end
+
+def load_raw_emails_data
+ raw_emails_yml = File.join(Spec::Runner.configuration.fixture_path, "raw_emails.yml")
+ for raw_email_id in YAML::load_file(raw_emails_yml).map{|k,v| v["id"]} do
+ raw_email = RawEmail.find(raw_email_id)
+ raw_email.data = load_file_fixture("raw_emails/%d.email" % [raw_email_id])
+ end
+end
+
+# Rebuild the current xapian index
def rebuild_xapian_index(terms = true, values = true, texts = true, dropfirst = true)
if dropfirst
begin
@@ -110,16 +123,35 @@ def rebuild_xapian_index(terms = true, values = true, texts = true, dropfirst =
ActsAsXapian.writable_db.close
end
parse_all_incoming_messages
- verbose = false
# safe_rebuild=true, which involves forking to avoid memory leaks, doesn't work well with rspec.
# unsafe is significantly faster, and we can afford possible memory leaks while testing.
- safe_rebuild = false
- ActsAsXapian.rebuild_index(["PublicBody", "User", "InfoRequestEvent"].map{|m| m.constantize}, verbose, terms, values, texts, safe_rebuild)
+ models = [PublicBody, User, InfoRequestEvent]
+ ActsAsXapian.rebuild_index(models, verbose=false, terms, values, texts, safe_rebuild=false)
+end
+
+# 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
def update_xapian_index
- verbose = false
- ActsAsXapian.update_index(flush_to_disk=false, verbose)
+ ActsAsXapian.update_index(flush_to_disk=false, verbose=false)
+end
+
+# Copy the xapian index created in create_fixtures_xapian_index to a temporary
+# copy at the same level and point xapian at the copy
+def get_fixtures_xapian_index()
+ # Create a base index for the fixtures if not already created
+ $existing_xapian_db ||= create_fixtures_xapian_index
+ # Store whatever the xapian db path is originally
+ $original_xapian_path ||= ActsAsXapian.db_path
+ path_array = $original_xapian_path.split(File::Separator)
+ path_array.pop
+ temp_path = File.join(path_array, 'test.temp')
+ FileUtils.remove_entry_secure(temp_path, force=true)
+ FileUtils.cp_r($original_xapian_path, temp_path)
+ ActsAsXapian.db_path = temp_path
end
# Validate an entire HTML page
@@ -200,16 +232,8 @@ def safe_mock_model(model, args = {})
mock
end
-def load_raw_emails_data
- raw_emails_yml = File.join(Spec::Runner.configuration.fixture_path, "raw_emails.yml")
- for raw_email_id in YAML::load_file(raw_emails_yml).map{|k,v| v["id"]} do
- raw_email = RawEmail.find(raw_email_id)
- raw_email.data = load_file_fixture("raw_emails/%d.email" % [raw_email_id])
- end
-end
-
-def parse_all_incoming_messages
- IncomingMessage.find(:all).each{|x| x.parse_raw_email!}
+def get_fixture_mail(filename)
+ MailHandler.mail_from_raw_email(load_file_fixture(filename))
end
def load_test_categories