diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/controllers/general_controller_spec.rb | 16 | ||||
-rw-r--r-- | spec/controllers/request_controller_spec.rb | 63 | ||||
-rw-r--r-- | spec/fixtures/public_body_translations.yml | 7 | ||||
-rw-r--r-- | spec/lib/timezone_fixes_spec.rb | 18 | ||||
-rw-r--r-- | spec/models/mail_server_log_spec.rb | 166 | ||||
-rw-r--r-- | spec/views/public_body/show.rhtml_spec.rb | 1 |
6 files changed, 258 insertions, 13 deletions
diff --git a/spec/controllers/general_controller_spec.rb b/spec/controllers/general_controller_spec.rb index de8dd8422..cebcaf6ab 100644 --- a/spec/controllers/general_controller_spec.rb +++ b/spec/controllers/general_controller_spec.rb @@ -17,6 +17,22 @@ describe GeneralController, "when trying to show the blog" do end end +describe GeneralController, 'when getting the blog feed' do + + it 'should add a lang param correctly to a url with no querystring' do + Configuration.stub!(:blog_feed).and_return("http://blog.example.com") + get :blog + assigns[:feed_url].should == "http://blog.example.com?lang=en" + end + + it 'should add a lang param correctly to a url with an existing querystring' do + Configuration.stub!(:blog_feed).and_return("http://blog.example.com?alt=rss") + get :blog + assigns[:feed_url].should == "http://blog.example.com?alt=rss&lang=en" + end + +end + describe GeneralController, "when searching" do integrate_views diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb index 77f43b618..b0223588e 100644 --- a/spec/controllers/request_controller_spec.rb +++ b/spec/controllers/request_controller_spec.rb @@ -245,7 +245,7 @@ describe RequestController, "when showing one request" do response.should have_tag('#anyone_actions', /Add an annotation/) end end - + describe 'when the request does not allow comments' do it 'should not have a comment link' do get :show, { :url_title => 'spam_1' }, @@ -253,7 +253,7 @@ describe RequestController, "when showing one request" do response.should_not have_tag('#anyone_actions', /Add an annotation/) end end - + describe 'when the request is being viewed by an admin' do describe 'if the request is awaiting description' do @@ -1709,15 +1709,17 @@ describe RequestController, "sending overdue request alerts" do mail.to_addrs.first.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 + it "should send not actually send the overdue alert if the user is banned but should + record it as sent" do user = info_requests(:naughty_chicken_request).user user.ban_text = 'Banned' user.save! - + UserInfoRequestSentAlert.find_all_by_user_id(user.id).count.should == 0 RequestMailer.alert_overdue_requests deliveries = ActionMailer::Base.deliveries deliveries.size.should == 0 + UserInfoRequestSentAlert.find_all_by_user_id(user.id).count.should > 0 end it "should send a very overdue alert mail to creators of very overdue requests" do @@ -1746,6 +1748,59 @@ describe RequestController, "sending overdue request alerts" do assigns[:info_request].should == info_requests(:naughty_chicken_request) end + it "should not resend alerts to people who've already received them" 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 + chicken_mails = ActionMailer::Base.deliveries.select{|x| x.body =~ /chickens/} + chicken_mails.size.should == 1 + RequestMailer.alert_overdue_requests + chicken_mails = ActionMailer::Base.deliveries.select{|x| x.body =~ /chickens/} + chicken_mails.size.should == 1 + end + + it 'should send alerts for requests where the last event forming the initial request is a followup + being sent following a request for clarification' 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 + chicken_mails = ActionMailer::Base.deliveries.select{|x| x.body =~ /chickens/} + chicken_mails.size.should == 1 + + # Request is waiting clarification + chicken_request.set_described_state('waiting_clarification') + + # Followup message is sent + outgoing_message = OutgoingMessage.new(:status => 'ready', + :message_type => 'followup', + :info_request_id => chicken_request.id, + :body => 'Some text', + :what_doing => 'normal_sort') + outgoing_message.send_message + outgoing_message.save! + + chicken_request = InfoRequest.find(chicken_request.id) + + # Last event forming the request is now the followup + chicken_request.last_event_forming_initial_request.event_type.should == 'followup_sent' + + # This isn't overdue, so no email + RequestMailer.alert_overdue_requests + chicken_mails = ActionMailer::Base.deliveries.select{|x| x.body =~ /chickens/} + chicken_mails.size.should == 1 + + # Make the followup older + outgoing_message.last_sent_at = Time.now() - 60.days + outgoing_message.save! + + # Now it should be alerted on + RequestMailer.alert_overdue_requests + chicken_mails = ActionMailer::Base.deliveries.select{|x| x.body =~ /chickens/} + chicken_mails.size.should == 2 + end + end describe RequestController, "sending unclassified new response reminder alerts" do diff --git a/spec/fixtures/public_body_translations.yml b/spec/fixtures/public_body_translations.yml index f3453e853..24b14c470 100644 --- a/spec/fixtures/public_body_translations.yml +++ b/spec/fixtures/public_body_translations.yml @@ -9,6 +9,7 @@ geraldine_es_public_body_translation: locale: es notes: "" publication_scheme: "" + disclosure_log: "" geraldine_en_public_body_translation: name: Geraldine Quango @@ -21,6 +22,7 @@ geraldine_en_public_body_translation: locale: en notes: "" publication_scheme: "" + disclosure_log: "" humpadink_es_public_body_translation: name: "El Department for Humpadinking" @@ -33,6 +35,7 @@ humpadink_es_public_body_translation: locale: es notes: Baguette publication_scheme: "" + disclosure_log: "" humpadink_en_public_body_translation: name: "Department for Humpadinking" @@ -45,6 +48,7 @@ humpadink_en_public_body_translation: locale: en notes: An albatross told me!!! publication_scheme: "" + disclosure_log: "" forlorn_en_public_body_translation: name: "Department of Loneliness" @@ -57,6 +61,7 @@ forlorn_en_public_body_translation: locale: en notes: A very lonely public body that no one has corresponded with publication_scheme: "" + disclosure_log: "" silly_walks_en_public_body_translation: id: 6 @@ -69,6 +74,7 @@ silly_walks_en_public_body_translation: url_name: msw notes: You know the one. publication_scheme: "" + disclosure_log: "" sensible_walks_en_public_body_translation: id: 7 @@ -81,3 +87,4 @@ sensible_walks_en_public_body_translation: url_name: sensible_walks notes: I bet you’ve never heard of it. publication_scheme: "" + disclosure_log: "" diff --git a/spec/lib/timezone_fixes_spec.rb b/spec/lib/timezone_fixes_spec.rb index eef7e865c..8dd1ff480 100644 --- a/spec/lib/timezone_fixes_spec.rb +++ b/spec/lib/timezone_fixes_spec.rb @@ -1,6 +1,6 @@ # This is a test of the monkey patches in timezone_fixes.rb -# We use EximLogDone here just as a totally random model that has a datetime type. +# We use MailServerLogDone here just as a totally random model that has a datetime type. require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') @@ -10,8 +10,8 @@ describe "when doing things with timezones" do with_env_tz 'America/New_York' do with_active_record_default_timezone :utc do time = Time.local(2000) - exim_log_done = EximLogDone.create('last_stat' => time, 'filename' => 'dummy') - saved_time = EximLogDone.find(exim_log_done.id).last_stat + mail_server_log_done = MailServerLogDone.create('last_stat' => time, 'filename' => 'dummy') + saved_time = MailServerLogDone.find(mail_server_log_done.id).last_stat assert_equal time, saved_time assert_equal [0, 0, 0, 1, 1, 2000, 6, 1, false, "EST"], time.to_a assert_equal [0, 0, 5, 1, 1, 2000, 6, 1, false, "UTC"], saved_time.to_a @@ -24,8 +24,8 @@ describe "when doing things with timezones" do with_active_record_default_timezone :utc do Time.use_zone 'Central Time (US & Canada)' do time = Time.zone.local(2000) - exim_log_done = EximLogDone.create('last_stat' => time, 'filename' => 'dummy') - saved_time = EximLogDone.find(exim_log_done.id).last_stat + mail_server_log_done = MailServerLogDone.create('last_stat' => time, 'filename' => 'dummy') + saved_time = MailServerLogDone.find(mail_server_log_done.id).last_stat assert_equal time, saved_time assert_equal [0, 0, 0, 1, 1, 2000, 6, 1, false, "CST"], time.to_a assert_equal [0, 0, 6, 1, 1, 2000, 6, 1, false, "UTC"], saved_time.to_a @@ -39,8 +39,8 @@ describe "when doing things with timezones" do #it "should preserve time objects with UTC time conversion to default timezone local" do # with_env_tz 'America/New_York' do # time = Time.utc(2000) - # exim_log_done = EximLogDone.create('last_stat' => time, 'filename' => 'dummy') - # saved_time = EximLogDone.find(exim_log_done.id).last_stat + # mail_server_log_done = MailServerLogDone.create('last_stat' => time, 'filename' => 'dummy') + # saved_time = MailServerLogDone.find(mail_server_log_done.id).last_stat # assert_equal time, saved_time # assert_equal [0, 0, 0, 1, 1, 2000, 6, 1, false, "UTC"], time.to_a # assert_equal [0, 0, 19, 31, 12, 1999, 5, 365, false, "EST"], saved_time.to_a @@ -52,8 +52,8 @@ describe "when doing things with timezones" do with_active_record_default_timezone :local do Time.use_zone 'Central Time (US & Canada)' do time = Time.zone.local(2000) - exim_log_done = EximLogDone.create('last_stat' => time, 'filename' => 'dummy') - saved_time = EximLogDone.find(exim_log_done.id).last_stat + mail_server_log_done = MailServerLogDone.create('last_stat' => time, 'filename' => 'dummy') + saved_time = MailServerLogDone.find(mail_server_log_done.id).last_stat assert_equal time, saved_time assert_equal [0, 0, 0, 1, 1, 2000, 6, 1, false, "CST"], time.to_a assert_equal [0, 0, 1, 1, 1, 2000, 6, 1, false, "EST"], saved_time.to_a diff --git a/spec/models/mail_server_log_spec.rb b/spec/models/mail_server_log_spec.rb new file mode 100644 index 000000000..d0a1d202f --- /dev/null +++ b/spec/models/mail_server_log_spec.rb @@ -0,0 +1,166 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe MailServerLog do + describe ".load_file" do + before :each do + Configuration.stub!(:incoming_email_domain).and_return("example.com") + File.stub_chain(:stat, :mtime).and_return(DateTime.new(2012, 10, 10)) + end + + let(:log) {[ + "This is a line of a logfile relevant to foi+request-1234@example.com", + "This is the second line for the same foi+request-1234@example.com email address" + ]} + + let(:ir) { info_requests(:fancy_dog_request) } + + it "loads relevant lines of an uncompressed exim log file" do + File.should_receive(:open).with("/var/log/exim4/exim-mainlog-2012-10-10", "r").and_return(log) + InfoRequest.should_receive(:find_by_incoming_email).with("request-1234@example.com").twice.and_return(ir) + MailServerLog.load_file("/var/log/exim4/exim-mainlog-2012-10-10") + + ir.mail_server_logs.count.should == 2 + log = ir.mail_server_logs[0] + log.order.should == 1 + log.line.should == "This is a line of a logfile relevant to foi+request-1234@example.com" + + log = ir.mail_server_logs[1] + log.order.should == 2 + log.line.should == "This is the second line for the same foi+request-1234@example.com email address" + end + + it "doesn't load the log file twice if it's unchanged" do + File.should_receive(:open).with("/var/log/exim4/exim-mainlog-2012-10-10", "r").once.and_return([]) + + MailServerLog.load_file("/var/log/exim4/exim-mainlog-2012-10-10") + MailServerLog.load_file("/var/log/exim4/exim-mainlog-2012-10-10") + end + + it "loads the log file again if it's changed" do + File.should_receive(:open).with("/var/log/exim4/exim-mainlog-2012-10-10", "r").twice.and_return([]) + MailServerLog.load_file("/var/log/exim4/exim-mainlog-2012-10-10") + File.stub_chain(:stat, :mtime).and_return(DateTime.new(2012, 10, 11)) + MailServerLog.load_file("/var/log/exim4/exim-mainlog-2012-10-10") + end + + it "doesn't end up with two copies of each line when the same file is actually loaded twice" do + File.should_receive(:open).with("/var/log/exim4/exim-mainlog-2012-10-10", "r").twice.and_return(log) + InfoRequest.should_receive(:find_by_incoming_email).with("request-1234@example.com").any_number_of_times.and_return(ir) + + MailServerLog.load_file("/var/log/exim4/exim-mainlog-2012-10-10") + ir.mail_server_logs.count.should == 2 + + File.stub_chain(:stat, :mtime).and_return(DateTime.new(2012, 10, 11)) + MailServerLog.load_file("/var/log/exim4/exim-mainlog-2012-10-10") + ir.mail_server_logs.count.should == 2 + end + + it "easily handles gzip compress log files" do + File.should_not_receive(:open) + Zlib::GzipReader.should_receive(:open).with("/var/log/exim4/exim-mainlog-2012-10-10.gz").and_return([]) + MailServerLog.load_file("/var/log/exim4/exim-mainlog-2012-10-10.gz") + end + end + + describe ".email_addresses_on_line" do + before :each do + Configuration.stub!(:incoming_email_domain).and_return("example.com") + Configuration.stub!(:incoming_email_prefix).and_return("foi+") + end + + it "recognises a single incoming email" do + MailServerLog.email_addresses_on_line("a random log line foi+request-14-e0e09f97@example.com has an email").should == + ["foi+request-14-e0e09f97@example.com"] + end + + it "recognises two email addresses on the same line" do + MailServerLog.email_addresses_on_line("two email addresses here foi+request-10-1234@example.com and foi+request-14-e0e09f97@example.com").should == + ["foi+request-10-1234@example.com", "foi+request-14-e0e09f97@example.com"] + end + + it "returns an empty array when there is an email address from a different domain" do + MailServerLog.email_addresses_on_line("other foi+request-10-1234@foo.com").should be_empty + end + + it "ignores an email with a different prefix" do + MailServerLog.email_addresses_on_line("foitest+request-14-e0e09f97@example.com").should be_empty + end + + it "ignores an email where the . is substituted for something else" do + MailServerLog.email_addresses_on_line("foi+request-14-e0e09f97@exampledcom").should be_empty + end + end + + context "Postfix" do + let(:log) {[ +"Oct 3 16:39:35 host postfix/pickup[2257]: CB55836EE58C: uid=1003 from=<foitest+request-14-e0e09f97@example.com>", +"Oct 3 16:39:35 host postfix/cleanup[7674]: CB55836EE58C: message-id=<ogm-15+506bdda7a4551-20ee@example.com>", +"Oct 3 16:39:35 host postfix/qmgr[1673]: 9634B16F7F7: from=<foitest+request-10-1234@example.com>, size=368, nrcpt=1 (queue active)", +"Oct 3 16:39:35 host postfix/qmgr[15615]: CB55836EE58C: from=<foitest+request-14-e0e09f97@example.com>, size=1695, nrcpt=1 (queue active)", +"Oct 3 16:39:38 host postfix/smtp[7676]: CB55836EE58C: to=<foi@some.gov.au>, relay=aspmx.l.google.com[74.125.25.27]:25, delay=2.5, delays=0.13/0.02/1.7/0.59, dsn=2.0.0, status=sent (250 2.0.0 OK 1349246383 j9si1676296paw.328)", +"Oct 3 16:39:38 host postfix/smtp[1681]: 9634B16F7F7: to=<kdent@example.com>, relay=none, delay=46, status=deferred (connect to 216.150.150.131[216.150.150.131]: No route to host)", +"Oct 3 16:39:38 host postfix/qmgr[15615]: CB55836EE58C: removed", + ]} + + describe ".load_postfix_log_data" do + # Postfix logs for a single email go over multiple lines. They are all tied together with the Queue ID. + # See http://onlamp.com/onlamp/2004/01/22/postfix.html + it "loads the postfix log and untangles seperate email transactions using the queue ID" do + Configuration.stub!(:incoming_email_domain).and_return("example.com") + log.stub!(:rewind) + ir1 = info_requests(:fancy_dog_request) + ir2 = info_requests(:naughty_chicken_request) + InfoRequest.should_receive(:find_by_incoming_email).with("request-14-e0e09f97@example.com").any_number_of_times.and_return(ir1) + InfoRequest.should_receive(:find_by_incoming_email).with("request-10-1234@example.com").any_number_of_times.and_return(ir2) + MailServerLog.load_postfix_log_data(log, MailServerLogDone.new(:filename => "foo", :last_stat => DateTime.now)) + # TODO: Check that each log line is attached to the correct request + ir1.mail_server_logs.count.should == 5 + ir1.mail_server_logs[0].order.should == 1 + ir1.mail_server_logs[0].line.should == "Oct 3 16:39:35 host postfix/pickup[2257]: CB55836EE58C: uid=1003 from=<foitest+request-14-e0e09f97@example.com>" + ir1.mail_server_logs[1].order.should == 2 + ir1.mail_server_logs[1].line.should == "Oct 3 16:39:35 host postfix/cleanup[7674]: CB55836EE58C: message-id=<ogm-15+506bdda7a4551-20ee@example.com>" + ir1.mail_server_logs[2].order.should == 4 + ir1.mail_server_logs[2].line.should == "Oct 3 16:39:35 host postfix/qmgr[15615]: CB55836EE58C: from=<foitest+request-14-e0e09f97@example.com>, size=1695, nrcpt=1 (queue active)" + ir1.mail_server_logs[3].order.should == 5 + ir1.mail_server_logs[3].line.should == "Oct 3 16:39:38 host postfix/smtp[7676]: CB55836EE58C: to=<foi@some.gov.au>, relay=aspmx.l.google.com[74.125.25.27]:25, delay=2.5, delays=0.13/0.02/1.7/0.59, dsn=2.0.0, status=sent (250 2.0.0 OK 1349246383 j9si1676296paw.328)" + ir1.mail_server_logs[4].order.should == 7 + ir1.mail_server_logs[4].line.should == "Oct 3 16:39:38 host postfix/qmgr[15615]: CB55836EE58C: removed" + ir2.mail_server_logs.count.should == 2 + ir2.mail_server_logs[0].order.should == 3 + ir2.mail_server_logs[0].line.should == "Oct 3 16:39:35 host postfix/qmgr[1673]: 9634B16F7F7: from=<foitest+request-10-1234@example.com>, size=368, nrcpt=1 (queue active)" + ir2.mail_server_logs[1].order.should == 6 + ir2.mail_server_logs[1].line.should == "Oct 3 16:39:38 host postfix/smtp[1681]: 9634B16F7F7: to=<kdent@example.com>, relay=none, delay=46, status=deferred (connect to 216.150.150.131[216.150.150.131]: No route to host)" + end + end + + describe ".scan_for_postfix_queue_ids" do + it "returns the queue ids of interest with the connected email addresses" do + Configuration.stub!(:incoming_email_domain).and_return("example.com") + MailServerLog.scan_for_postfix_queue_ids(log).should == { + "CB55836EE58C" => ["request-14-e0e09f97@example.com"], + "9634B16F7F7" => ["request-10-1234@example.com"] + } + end + end + + describe ".extract_postfix_queue_id_from_syslog_line" do + it "returns nil if there is no queue id" do + MailServerLog.extract_postfix_queue_id_from_syslog_line("Oct 7 07:16:48 kedumba postfix/smtp[14294]: connect to mail.neilcopp.com.au[110.142.151.66]:25: Connection refused").should be_nil + end + end + + describe ".request_postfix_sent?" do + it "returns true when the logs say the message was sent" do + ir = info_requests(:fancy_dog_request) + ir.mail_server_logs.create!(:line => "Oct 10 16:58:38 kedumba postfix/smtp[26358]: A664436F218D: to=<contact@openaustraliafoundation.org.au>, relay=aspmx.l.google.com[74.125.25.26]:25, delay=2.7, delays=0.16/0.02/1.8/0.67, dsn=2.0.0, status=sent (250 2.0.0 OK 1349848723 e6si653316paw.346)", :order => 1) + MailServerLog.request_postfix_sent?(ir).should be_true + end + + it "returns false when the logs say the message hasn't been sent" do + ir = info_requests(:fancy_dog_request) + ir.mail_server_logs.create!(:line => "Oct 10 13:22:49 kedumba postfix/smtp[11876]: 6FB9036F1307: to=<foo@example.com>, relay=mta7.am0.yahoodns.net[74.6.136.244]:25, delay=1.5, delays=0.03/0/0.48/1, dsn=5.0.0, status=bounced (host mta7.am0.yahoodns.net[74.6.136.244] said: 554 delivery error: dd Sorry your message to foo@example.com cannot be delivered. This account has been disabled or discontinued [#102]. - mta1272.mail.sk1.yahoo.com (in reply to end of DATA command))", :order => 1) + MailServerLog.request_postfix_sent?(ir).should be_false + end + end + end +end diff --git a/spec/views/public_body/show.rhtml_spec.rb b/spec/views/public_body/show.rhtml_spec.rb index a42516d72..b68b3f43b 100644 --- a/spec/views/public_body/show.rhtml_spec.rb +++ b/spec/views/public_body/show.rhtml_spec.rb @@ -11,6 +11,7 @@ describe "when viewing a body" do :eir_only? => nil, :info_requests => [1, 2, 3, 4], # out of sync with Xapian :publication_scheme => '', + :disclosure_log => '', :calculated_home_page => '') @pb.stub!(:override_request_email).and_return(nil) @pb.stub!(:is_requestable?).and_return(true) |