diff options
author | Seb Bacon <seb.bacon@gmail.com> | 2011-07-29 10:02:57 +0100 |
---|---|---|
committer | Seb Bacon <seb.bacon@gmail.com> | 2011-07-29 10:10:52 +0100 |
commit | 7573586c3be6318e19ae02ff008dfb2a576b5980 (patch) | |
tree | df84452dada69c64ebe0c09d51a994b9715fd67f | |
parent | 51a74f4b275fab25ac567843ab99b885a8444789 (diff) |
To make tests pass with RawEmails on the filesystem, instead of hacky dependency on existence of text_binary field in raw_emails model, rewrite tests (and their fixtures) to explicitly load data from FS when raw_emails are used.
-rw-r--r-- | app/models/raw_email.rb | 10 | ||||
-rw-r--r-- | db/migrate/100_remove_redundant_raw_email_columns.rb | 2 | ||||
-rw-r--r-- | spec/controllers/request_controller_spec.rb | 49 | ||||
-rw-r--r-- | spec/controllers/request_game_controller_spec.rb | 3 | ||||
-rw-r--r-- | spec/controllers/track_controller_spec.rb | 5 | ||||
-rw-r--r-- | spec/controllers/user_controller_spec.rb | 3 | ||||
-rw-r--r-- | spec/fixtures/files/useless_raw_email.email | 15 | ||||
-rw-r--r-- | spec/fixtures/raw_emails.yml | 17 | ||||
-rw-r--r-- | spec/models/incoming_message_spec.rb | 4 | ||||
-rw-r--r-- | spec/models/outgoing_mailer_spec.rb | 12 | ||||
-rw-r--r-- | spec/models/request_mailer_spec.rb | 3 | ||||
-rw-r--r-- | spec/models/xapian_spec.rb | 18 | ||||
-rw-r--r-- | spec/spec_helper.rb | 27 |
13 files changed, 114 insertions, 54 deletions
diff --git a/app/models/raw_email.rb b/app/models/raw_email.rb index 4e1a69456..1d85cc7d9 100644 --- a/app/models/raw_email.rb +++ b/app/models/raw_email.rb @@ -29,9 +29,13 @@ class RawEmail < ActiveRecord::Base def directory request_id = self.incoming_message.info_request.id.to_s - File.join(MySociety::Config.get('RAW_EMAILS_LOCATION', - 'files/raw_emails'), - request_id[0..2], request_id) + if ENV["RAILS_ENV"] = "test" + return 'files/raw_email_test' + else + return File.join(MySociety::Config.get('RAW_EMAILS_LOCATION', + 'files/raw_emails'), + request_id[0..2], request_id) + end end def filepath diff --git a/db/migrate/100_remove_redundant_raw_email_columns.rb b/db/migrate/100_remove_redundant_raw_email_columns.rb index 2ebffba34..edf6006d7 100644 --- a/db/migrate/100_remove_redundant_raw_email_columns.rb +++ b/db/migrate/100_remove_redundant_raw_email_columns.rb @@ -1,9 +1,9 @@ class RemoveRedundantRawEmailColumns < ActiveRecord::Migration def self.up remove_column :raw_emails, :data_text + remove_column :raw_emails, :data_binary end def self.down - add_column :raw_emails, :data_text, :text end end diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb index 1a190cad6..514eda0b4 100644 --- a/spec/controllers/request_controller_spec.rb +++ b/spec/controllers/request_controller_spec.rb @@ -8,7 +8,7 @@ require 'json' describe RequestController, "when listing recent requests" do - before(:all) do + before(:each) do rebuild_xapian_index end @@ -35,11 +35,14 @@ describe RequestController, "when listing recent requests" do end end - describe RequestController, "when showing one request" do fixtures :info_requests, :info_request_events, :public_bodies, :public_body_translations, :public_body_translations, :users, :incoming_messages, :raw_emails, :outgoing_messages, :comments # all needed as integrating views - + + before(:each) do + load_raw_emails_data(raw_emails) + end + it "should be successful" do get :show, :url_title => 'why_do_you_have_such_a_fancy_dog' response.should be_success @@ -198,6 +201,10 @@ end describe RequestController, "when changing prominence of a request" do fixtures :info_requests, :info_request_events, :public_bodies, :public_body_translations, :users, :incoming_messages, :raw_emails, :outgoing_messages # all needed as integrating views + before(:each) do + load_raw_emails_data(raw_emails) + end + it "should not show hidden requests" do ir = info_requests(:fancy_dog_request) ir.prominence = 'hidden' @@ -457,7 +464,11 @@ end describe RequestController, "when viewing an individual response for reply/followup" do integrate_views fixtures :info_requests, :info_request_events, :public_bodies, :public_body_translations, :users, :incoming_messages, :raw_emails, :outgoing_messages, :comments # all needed as integrating views - + + before(:each) do + load_raw_emails_data(raw_emails) + end + it "should ask for login if you are logged in as wrong person" do session[:user_id] = users(:silly_name_user).id get :show_response, :id => info_requests(:fancy_dog_request).id, :incoming_message_id => incoming_messages(:useless_incoming_message) @@ -489,6 +500,7 @@ describe RequestController, "when classifying an information request" do @dog_request = info_requests(:fancy_dog_request) @dog_request.stub!(:is_old_unclassified?).and_return(false) InfoRequest.stub!(:find).and_return(@dog_request) + load_raw_emails_data(raw_emails) end def post_status(status) @@ -898,7 +910,11 @@ end describe RequestController, "sending overdue request alerts" do integrate_views fixtures :info_requests, :info_request_events, :public_bodies, :public_body_translations, :users, :incoming_messages, :raw_emails, :outgoing_messages # all needed as integrating views - + + before(:each) do + load_raw_emails_data(raw_emails) + end + 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 @@ -982,7 +998,11 @@ end describe RequestController, "sending unclassified new response reminder alerts" do integrate_views fixtures :info_requests, :info_request_events, :public_bodies, :public_body_translations, :users, :incoming_messages, :raw_emails, :outgoing_messages, :comments # all needed as integrating views - + + before(:each) do + load_raw_emails_data(raw_emails) + end + it "should send an alert" do RequestMailer.alert_new_response_reminders @@ -1009,7 +1029,10 @@ end describe RequestController, "clarification required alerts" do integrate_views fixtures :info_requests, :info_request_events, :public_bodies, :public_body_translations, :users, :incoming_messages, :raw_emails, :outgoing_messages # all needed as integrating views - + before(:each) do + load_raw_emails_data(raw_emails) + end + it "should send an alert" do ir = info_requests(:fancy_dog_request) ir.set_described_state('waiting_clarification') @@ -1060,6 +1083,9 @@ end describe RequestController, "comment alerts" do integrate_views fixtures :info_requests, :info_request_events, :public_bodies, :public_body_translations, :users, :incoming_messages, :raw_emails, :outgoing_messages, :comments # all needed as integrating views + before(:each) do + load_raw_emails_data(raw_emails) + end it "should send an alert (once and once only)" do # delete ficture comment and make new one, so is in last month (as @@ -1131,7 +1157,10 @@ end describe RequestController, "when viewing comments" do integrate_views - fixtures :users + fixtures :users, :raw_emails, :incoming_messages, :info_requests + before(:each) do + load_raw_emails_data(raw_emails) + end it "should link to the user who submitted it" do session[:user_id] = users(:bob_smith_user).id @@ -1240,6 +1269,10 @@ describe RequestController, "when showing JSON version for API" do fixtures :info_requests, :info_request_events, :public_bodies, :public_body_translations, :users, :incoming_messages, :raw_emails, :outgoing_messages, :comments + before(:each) do + load_raw_emails_data(raw_emails) + end + it "should return data in JSON form" do get :show, :url_title => 'why_do_you_have_such_a_fancy_dog', :format => 'json' diff --git a/spec/controllers/request_game_controller_spec.rb b/spec/controllers/request_game_controller_spec.rb index ce507fad1..4883bfdd6 100644 --- a/spec/controllers/request_game_controller_spec.rb +++ b/spec/controllers/request_game_controller_spec.rb @@ -3,6 +3,9 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe RequestGameController, "when playing the game" do fixtures :info_requests, :info_request_events, :public_bodies, :public_body_translations, :users, :incoming_messages, :raw_emails, :outgoing_messages # all needed as integrating views + before(:each) do + load_raw_emails_data(raw_emails) + end it "should show the game homepage" do get :play diff --git a/spec/controllers/track_controller_spec.rb b/spec/controllers/track_controller_spec.rb index 0a8919268..f7002a9d4 100644 --- a/spec/controllers/track_controller_spec.rb +++ b/spec/controllers/track_controller_spec.rb @@ -37,6 +37,9 @@ describe TrackController, "when sending alerts for a track" do integrate_views fixtures :info_requests, :outgoing_messages, :incoming_messages, :raw_emails, :info_request_events, :users, :track_things, :track_things_sent_emails, :public_bodies, :public_body_translations include LinkToHelper # for main_url + before(:each) do + load_raw_emails_data(raw_emails) + end before do rebuild_xapian_index @@ -100,6 +103,7 @@ describe TrackController, "when viewing RSS feed for a track" do before do rebuild_xapian_index + load_raw_emails_data(raw_emails) end it "should get the RSS feed" do @@ -125,6 +129,7 @@ describe TrackController, "when viewing JSON version of a track feed" do before do rebuild_xapian_index + load_raw_emails_data(raw_emails) end it "should get the feed" do diff --git a/spec/controllers/user_controller_spec.rb b/spec/controllers/user_controller_spec.rb index c6817fbc6..0b5e96711 100644 --- a/spec/controllers/user_controller_spec.rb +++ b/spec/controllers/user_controller_spec.rb @@ -8,6 +8,9 @@ require 'json' describe UserController, "when showing a user" do integrate_views fixtures :users, :outgoing_messages, :incoming_messages, :raw_emails, :info_requests, :info_request_events, :comments, :public_bodies, :public_body_translations + before(:each) do + load_raw_emails_data(raw_emails) + end it "should be successful" do get :show, :url_name => "bob_smith" diff --git a/spec/fixtures/files/useless_raw_email.email b/spec/fixtures/files/useless_raw_email.email new file mode 100644 index 000000000..2e4585af7 --- /dev/null +++ b/spec/fixtures/files/useless_raw_email.email @@ -0,0 +1,15 @@ +From: "FOI Person" <foiperson@localhost> +To: "Bob Smith" <bob@localhost> +Date: Tue, 13 Nov 2007 11:39:55 +0000 +Bcc: +Subject: Geraldine FOI Code AZXB421 +Reply-To: +In-Reply-To: <471f1eae5d1cb_7347..fdbe67386163@cat.tmail> + +No way! I'm not going to tell you that in a month of Thursdays. + +The Geraldine Quango + +On Wed, Oct 24, 2007 at 11:30:06AM +0100, Bob Smith wrote: +> Why do you have such a fancy dog? + diff --git a/spec/fixtures/raw_emails.yml b/spec/fixtures/raw_emails.yml index d24b61854..8ef9248a3 100644 --- a/spec/fixtures/raw_emails.yml +++ b/spec/fixtures/raw_emails.yml @@ -1,19 +1,2 @@ useless_raw_email: id: 1 - data_binary: | - From: "FOI Person" <foiperson@localhost> - To: "Bob Smith" <bob@localhost> - Date: Tue, 13 Nov 2007 11:39:55 +0000 - NoCc: foi+request-1-4b571715@cat - Bcc: - Subject: Geraldine FOI Code AZXB421 - Reply-To: - In-Reply-To: <471f1eae5d1cb_7347..fdbe67386163@cat.tmail> - - No way! I'm not going to tell you that in a month of Thursdays. - - The Geraldine Quango - - On Wed, Oct 24, 2007 at 11:30:06AM +0100, Bob Smith wrote: - > Why do you have such a fancy dog? - diff --git a/spec/models/incoming_message_spec.rb b/spec/models/incoming_message_spec.rb index abbe94a47..68ef541a4 100644 --- a/spec/models/incoming_message_spec.rb +++ b/spec/models/incoming_message_spec.rb @@ -5,6 +5,7 @@ describe IncomingMessage, " when dealing with incoming mail" do before do @im = incoming_messages(:useless_incoming_message) + load_raw_emails_data(raw_emails) end it "should return the mail Date header date for sent at" do @@ -129,6 +130,8 @@ describe IncomingMessage, " when censoring data" do @censor_rule_2.last_edit_editor = "unknown" @censor_rule_2.last_edit_comment = "none" @im.info_request.censor_rules << @censor_rule_2 + + load_raw_emails_data(raw_emails) end it "should do nothing to a JPEG" do @@ -212,6 +215,7 @@ describe IncomingMessage, " when censoring whole users" do @censor_rule_1.last_edit_editor = "unknown" @censor_rule_1.last_edit_comment = "none" @im.info_request.user.censor_rules << @censor_rule_1 + load_raw_emails_data(raw_emails) end it "should apply censor rules to HTML files" do diff --git a/spec/models/outgoing_mailer_spec.rb b/spec/models/outgoing_mailer_spec.rb index 24143fc9b..d7587cb41 100644 --- a/spec/models/outgoing_mailer_spec.rb +++ b/spec/models/outgoing_mailer_spec.rb @@ -5,6 +5,9 @@ describe OutgoingMailer, " when working out follow up addresses" do # calls TMail. XXX untangle it and make these tests spread out and using # mocks. Put parts of the tests in spec/lib/tmail_extensions.rb fixtures :info_requests, :incoming_messages, :raw_emails, :public_bodies, :public_body_translations + before(:each) do + load_raw_emails_data(raw_emails) + end it "should parse them right" do ir = info_requests(:fancy_dog_request) @@ -69,13 +72,8 @@ end describe OutgoingMailer, "when working out follow up subjects" do fixtures :info_requests, :incoming_messages, :outgoing_messages, :raw_emails - before do - raw_email = raw_emails(:useless_raw_email) - raw_email.data=raw_email.dbdata - end - - after do - raw_emails(:useless_raw_email).destroy_file_representation! + before(:each) do + load_raw_emails_data(raw_emails) end it "should prefix the title with 'Freedom of Information request -' for initial requests" do diff --git a/spec/models/request_mailer_spec.rb b/spec/models/request_mailer_spec.rb index a8311e8f8..a5f59b9bf 100644 --- a/spec/models/request_mailer_spec.rb +++ b/spec/models/request_mailer_spec.rb @@ -2,6 +2,9 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe RequestMailer, " when receiving incoming mail" do fixtures :info_requests, :incoming_messages, :raw_emails, :users, :public_bodies, :public_body_translations + before(:each) do + load_raw_emails_data(raw_emails) + end it "should append it to the appropriate request" do ir = info_requests(:fancy_dog_request) diff --git a/spec/models/xapian_spec.rb b/spec/models/xapian_spec.rb index 79ffc4839..37e68b145 100644 --- a/spec/models/xapian_spec.rb +++ b/spec/models/xapian_spec.rb @@ -35,6 +35,9 @@ end describe PublicBody, " when indexing public bodies with Xapian" do fixtures :public_bodies, :public_body_translations, :incoming_messages, :outgoing_messages, :raw_emails, :comments + before(:each) do + load_raw_emails_data(raw_emails) + end it "should search index the main name field" do rebuild_xapian_index @@ -126,6 +129,9 @@ end describe User, " when indexing requests by user they are from" do fixtures :users, :info_request_events, :info_requests, :incoming_messages, :outgoing_messages, :raw_emails, :comments + before(:each) do + load_raw_emails_data(raw_emails) + end it "should find requests from the user" do rebuild_xapian_index @@ -210,6 +216,9 @@ end describe User, " when indexing comments by user they are by" do fixtures :users, :info_request_events, :info_requests, :comments, :incoming_messages, :outgoing_messages, :raw_emails, :comments + before(:each) do + load_raw_emails_data(raw_emails) + end it "should find requests from the user" do rebuild_xapian_index @@ -244,6 +253,9 @@ end describe InfoRequest, " when indexing requests by their title" do fixtures :info_request_events, :info_requests, :incoming_messages, :raw_emails, :comments + before(:each) do + load_raw_emails_data(raw_emails) + end it "should find events for the request" do rebuild_xapian_index @@ -272,6 +284,9 @@ end describe InfoRequest, " when indexing requests by tag" do fixtures :info_request_events, :info_requests, :incoming_messages, :raw_emails, :comments + before(:each) do + load_raw_emails_data(raw_emails) + end it "should find request by tag, even when changes" do rebuild_xapian_index @@ -291,6 +306,9 @@ end describe PublicBody, " when indexing authorities by tag" do fixtures :public_bodies, :public_body_translations, :incoming_messages, :outgoing_messages, :raw_emails, :comments + before(:each) do + load_raw_emails_data(raw_emails) + end it "should find request by tag, even when changes" do rebuild_xapian_index diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 086def32a..42c5ff6bf 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -13,9 +13,6 @@ config['ADMIN_PASSWORD'] = 'baz' # tests assume 20 days config['REPLY_LATE_AFTER_DAYS'] = 20 -# tests assume 20 days -config['RAW_EMAILS_LOCATION'] = 'files/raw_emails_tests' - # Uncomment the next line to use webrat's matchers #require 'webrat/integrations/rspec-rails' @@ -31,21 +28,6 @@ Spec::Runner.configure do |config| config.fixture_path = RAILS_ROOT + '/spec/fixtures/' - config.before(:each) do - # XXX this is a hack around the fact that our raw_email model - # is in transition to something that doesn't actually live in - # the database at all. The raw_email *fixture* saves to the - # model, the model then needs to be told to save itself on the - # filesystem. - begin - raw_email = raw_emails(:useless_raw_email) - raw_email.data=raw_email.dbdata - rescue NoMethodError - # only do it in tests with raw_emails fixtures - end - end - - # == Fixtures # # You can declare fixtures for each example_group like this: @@ -164,3 +146,12 @@ if $tempfilecount.nil? puts "WARNING: HTML validation script " + $html_validation_script + " not found" end end + +def load_raw_emails_data(raw_emails) + raw_email = raw_emails(:useless_raw_email) + begin + raw_email.destroy_file_representation! + rescue Errno::ENOENT + end + raw_email.data = load_file_fixture("useless_raw_email.email") +end |