diff options
Diffstat (limited to 'spec/models')
-rw-r--r-- | spec/models/application_mailer_spec.rb | 162 | ||||
-rw-r--r-- | spec/models/censor_rule_spec.rb | 22 | ||||
-rw-r--r-- | spec/models/contact_mailer_spec.rb | 8 | ||||
-rw-r--r-- | spec/models/foi_attachment_spec.rb | 2 | ||||
-rw-r--r-- | spec/models/incoming_message_spec.rb | 97 | ||||
-rw-r--r-- | spec/models/info_request_event_spec.rb | 10 | ||||
-rw-r--r-- | spec/models/info_request_spec.rb | 261 | ||||
-rw-r--r-- | spec/models/mail_server_log_spec.rb | 10 | ||||
-rw-r--r-- | spec/models/outgoing_mailer_spec.rb | 140 | ||||
-rw-r--r-- | spec/models/outgoing_message_spec.rb | 19 | ||||
-rw-r--r-- | spec/models/public_body_spec.rb | 10 | ||||
-rw-r--r-- | spec/models/request_mailer_spec.rb | 375 | ||||
-rw-r--r-- | spec/models/track_mailer_spec.rb | 213 | ||||
-rw-r--r-- | spec/models/user_mailer_spec.rb | 8 | ||||
-rw-r--r-- | spec/models/user_spec.rb | 25 | ||||
-rw-r--r-- | spec/models/xapian_spec.rb | 134 |
16 files changed, 502 insertions, 994 deletions
diff --git a/spec/models/application_mailer_spec.rb b/spec/models/application_mailer_spec.rb deleted file mode 100644 index acf5f43bc..000000000 --- a/spec/models/application_mailer_spec.rb +++ /dev/null @@ -1,162 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') - - -describe ApplicationMailer do - - context 'when using plugins' do - - def set_base_views - ApplicationMailer.class_eval do - @previous_view_paths = self.view_paths.dup - self.view_paths.clear - self.view_paths << File.join(Rails.root, 'spec', 'fixtures', 'theme_views', 'core') - end - end - - def add_mail_methods(method_names) - method_names.each{ |method_name| ApplicationMailer.send(:define_method, method_name){} } - end - - def remove_mail_methods(method_names) - method_names.each do |method_name| - if ApplicationMailer.respond_to?(method_name) - ApplicationMailer.send(:remove_method, method_name) - end - end - end - - def prepend_theme_views(theme_name) - ApplicationMailer.class_eval do - view_paths.unshift File.join(Rails.root, 'spec', 'fixtures', 'theme_views', theme_name) - end - end - - def append_theme_views(theme_name) - ApplicationMailer.class_eval do - view_paths << File.join(Rails.root, 'spec', 'fixtures', 'theme_views', theme_name) - end - end - - def reset_views - ApplicationMailer.class_eval do - self.view_paths = @previous_view_paths - end - end - - def create_multipart_method(method_name) - ApplicationMailer.send(:define_method, method_name) do - attachment :content_type => 'message/rfc822', - :body => 'xxx', - :filename => "original.eml", - :transfer_encoding => '7bit', - :content_disposition => 'inline' - end - end - - before do - set_base_views - add_mail_methods(['simple', 'theme_only', 'core_only', 'neither']) - end - - describe 'when a plugin prepends its mail templates to the view paths' do - - it 'should render a theme template in preference to a core template' do - prepend_theme_views('theme_one') - @mail = ApplicationMailer.create_simple() - @mail.body.should match('Theme simple') - end - - it 'should render the template provided by the theme if no template is available in core' do - prepend_theme_views('theme_one') - @mail = ApplicationMailer.create_theme_only() - @mail.body.should match('Theme only') - end - - it 'should render the template provided by core if there is no theme template' do - prepend_theme_views('theme_one') - @mail = ApplicationMailer.create_core_only() - @mail.body.should match('Core only') - end - - it 'should raise an error if the template is in neither core nor theme' do - prepend_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 theme template' do - prepend_theme_views('theme_one') - create_multipart_method('multipart_theme_only') - @mail = ApplicationMailer.create_multipart_theme_only() - @mail.parts.size.should == 2 - message_part = @mail.parts[0].to_s - message_part.should match("Theme multipart") - end - - it 'should render a multipart email using a core template' do - prepend_theme_views('theme_one') - create_multipart_method('multipart_core_only') - @mail = ApplicationMailer.create_multipart_core_only() - @mail.parts.size.should == 2 - message_part = @mail.parts[0].to_s - message_part.should match("Core multipart") - end - - end - - describe 'when a plugin appends its mail templates to the view paths' do - - it 'should render a core template in preference to a theme template' do - append_theme_views('theme_one') - @mail = ApplicationMailer.create_simple() - @mail.body.should match('Core simple') - end - - it 'should render the template provided by the theme if no template is available in core' do - append_theme_views('theme_one') - @mail = ApplicationMailer.create_theme_only() - @mail.body.should match('Theme only') - end - - it 'should render the template provided by core if there is no theme template' do - append_theme_views('theme_one') - @mail = ApplicationMailer.create_core_only() - @mail.body.should match('Core only') - end - - it 'should raise an error if the template is in neither core nor theme' do - append_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 - append_theme_views('theme_one') - create_multipart_method('multipart_core_only') - @mail = ApplicationMailer.create_multipart_core_only() - @mail.parts.size.should == 2 - message_part = @mail.parts[0].to_s - message_part.should match("Core multipart") - end - - it 'should render a multipart email using a theme template' do - append_theme_views('theme_one') - create_multipart_method('multipart_theme_only') - @mail = ApplicationMailer.create_multipart_theme_only() - @mail.parts.size.should == 2 - message_part = @mail.parts[0].to_s - message_part.should match("Theme multipart") - end - - end - - after do - reset_views - remove_mail_methods(['simple', 'theme_only', 'core_only', 'neither', 'multipart']) - end - end - -end - - - diff --git a/spec/models/censor_rule_spec.rb b/spec/models/censor_rule_spec.rb index c11b05a03..3782cc630 100644 --- a/spec/models/censor_rule_spec.rb +++ b/spec/models/censor_rule_spec.rb @@ -73,10 +73,10 @@ end describe 'when validating rules' do - describe 'should be invalid without text' do + it 'should be invalid without text' do censor_rule = CensorRule.new censor_rule.valid?.should == false - censor_rule.errors.on(:text).should == "can't be blank" + censor_rule.errors[:text].should == ["can't be blank"] end describe 'when validating a regexp rule' do @@ -96,7 +96,7 @@ describe 'when validating rules' do it 'should add an error message to the text field with the regexp error message' do Regexp.stub!(:new).and_raise(RegexpError.new("very bad regexp")) @censor_rule.valid?.should == false - @censor_rule.errors.on(:text).should == "very bad regexp" + @censor_rule.errors[:text].should == ["very bad regexp"] end end @@ -106,7 +106,7 @@ describe 'when validating rules' do it 'should not add any error message to the text field' do Regexp.stub!(:new) @censor_rule.valid? - @censor_rule.errors.on(:text).should == nil + @censor_rule.errors[:text].should == [] end end @@ -134,15 +134,21 @@ describe 'when validating rules' do it 'should not allow a global text censor rule (without user_id, request_id or public_body_id)' do @censor_rule.valid?.should == false - @expected_error = 'Censor must apply to an info request a user or a body; is invalid' - @censor_rule.errors.full_messages.should == [@expected_error] + + expected_error = ["Rule must apply to an info request, a user or a body"] + @censor_rule.errors[:user].should == expected_error + @censor_rule.errors[:info_request].should == expected_error + @censor_rule.errors[:public_body].should == expected_error end it 'should not allow a global regex censor rule (without user_id, request_id or public_body_id)' do @censor_rule.regexp = true @censor_rule.valid?.should == false - @expected_error = 'Censor must apply to an info request a user or a body; is invalid' - @censor_rule.errors.full_messages.should == [@expected_error] + + expected_error = ["Rule must apply to an info request, a user or a body"] + @censor_rule.errors[:user].should == expected_error + @censor_rule.errors[:info_request].should == expected_error + @censor_rule.errors[:public_body].should == expected_error end end diff --git a/spec/models/contact_mailer_spec.rb b/spec/models/contact_mailer_spec.rb deleted file mode 100644 index 202e45758..000000000 --- a/spec/models/contact_mailer_spec.rb +++ /dev/null @@ -1,8 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') - -describe ContactMailer, " when blah" do - before do - end -end - - diff --git a/spec/models/foi_attachment_spec.rb b/spec/models/foi_attachment_spec.rb index 537a3363c..9b0115c44 100644 --- a/spec/models/foi_attachment_spec.rb +++ b/spec/models/foi_attachment_spec.rb @@ -1,6 +1,6 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') -describe FoiAttachment, " when calculating due date" do +describe FoiAttachment do before(:each) do load_raw_emails_data diff --git a/spec/models/incoming_message_spec.rb b/spec/models/incoming_message_spec.rb index f53a5856a..ff6a8e34e 100644 --- a/spec/models/incoming_message_spec.rb +++ b/spec/models/incoming_message_spec.rb @@ -27,7 +27,7 @@ describe IncomingMessage, " when dealing with incoming mail" do end it "should correctly fold various types of footer" do - Dir.glob(File.join(Spec::Runner.configuration.fixture_path, "files", "email-folding-example-*.txt")).each do |file| + Dir.glob(File.join(RSpec.configuration.fixture_path, "files", "email-folding-example-*.txt")).each do |file| message = File.read(file) parsed = IncomingMessage.remove_quoted_sections(message) expected = File.read("#{file}.expected") @@ -59,12 +59,19 @@ describe IncomingMessage, " when dealing with incoming mail" do message.subject.should == "Câmara Responde: Banco de ideias" end - it 'should not error on display of a message which has no charset set on the body part and - is not good utf-8' do + it 'should deal with GB18030 text even if the charset is missing' do ir = info_requests(:fancy_dog_request) receive_incoming_mail('no-part-charset-bad-utf8.email', ir.incoming_email) message = ir.incoming_messages[1] message.parse_raw_email! + message.get_main_body_text_internal.should include("贵公司负责人") + end + + it 'should not error on display of a message which has no charset set on the body part and is not good UTF-8' do + ir = info_requests(:fancy_dog_request) + receive_incoming_mail('no-part-charset-random-data.email', ir.incoming_email) + message = ir.incoming_messages[1] + message.parse_raw_email! message.get_main_body_text_internal.should include("The above text was badly encoded") end @@ -108,6 +115,16 @@ describe IncomingMessage, " when dealing with incoming mail" do end + it 'should handle a main body part that is just quoted content in an email that has + no subject' do + i = IncomingMessage.new + i.stub!(:get_main_body_text_unfolded).and_return("some quoting") + i.stub!(:get_main_body_text_folded).and_return("FOLDED_QUOTED_SECTION") + i.stub!(:subject).and_return(nil) + i.get_body_for_html_display + end + + end describe IncomingMessage, " display attachments" do @@ -311,7 +328,18 @@ describe IncomingMessage, " when censoring data" do data.should == "His email was x\000x\000x\000@\000x\000x\000x\000.\000x\000x\000x\000, indeed" end - + it 'should handle multibyte characters correctly', :focus => true do + orig_data = 'á' + data = orig_data.dup + @regex_censor_rule = CensorRule.new() + @regex_censor_rule.text = 'á' + @regex_censor_rule.regexp = true + @regex_censor_rule.replacement = 'cat' + @regex_censor_rule.last_edit_editor = 'unknown' + @regex_censor_rule.last_edit_comment = 'none' + @im.info_request.censor_rules << @regex_censor_rule + lambda{ @im.binary_mask_stuff!(data, "text/plain") }.should_not raise_error + end def pdf_replacement_test(use_ghostscript_compression) config = MySociety::Config.load_default() @@ -353,7 +381,7 @@ describe IncomingMessage, " when censoring data" do end it "should apply hard-coded privacy rules to HTML files" do - data = "http://#{Configuration::domain}/c/cheese" + data = "http://#{AlaveteliConfiguration::domain}/c/cheese" @im.html_mask_stuff!(data) data.should == "[WDTK login link]" end @@ -405,12 +433,24 @@ describe IncomingMessage, " when uudecoding bad messages" do im.stub!(:mail).and_return(mail) im.extract_attachments! + im.reload attachments = im.foi_attachments attachments.size.should == 2 attachments[1].filename.should == 'moo.txt' im.get_attachments_for_display.size.should == 1 end + it "should still work when parsed from the raw email" do + raw_email = load_file_fixture 'inline-uuencode.email' + mail = MailHandler.mail_from_raw_email(raw_email) + im = incoming_messages :useless_incoming_message + im.stub!(:raw_email).and_return(raw_email) + im.stub!(:mail).and_return(mail) + im.parse_raw_email! + attachments = im.foi_attachments + attachments.size.should == 2 + end + it "should apply censor rules" do mail = get_fixture_mail('incoming-request-bad-uuencoding.email') @@ -523,3 +563,50 @@ describe IncomingMessage, "when TNEF attachments are attached to messages" do end end +describe IncomingMessage, "when extracting attachments" do + + before do + load_raw_emails_data + end + + it 'handles the case where reparsing changes the body of the main part + and the cached attachment has been deleted' do + # original set of attachment attributes + attachment_attributes = { :url_part_number => 1, + :within_rfc822_subject => nil, + :content_type => "text/plain", + :charset => nil, + :body => "No way!\n", + :hexdigest => "0c8b1b0f5cb9c94ed15a180e73b5c7d1", + :filename => nil } + + # Make a small change in the body returned for the attachment + new_attachment_attributes = attachment_attributes.merge(:body => "No way!", + :hexdigest => "74d2c0a41e074f9cebe49324d5b47414") + + + # Simulate parsing with the original attachments + MailHandler.stub!(:get_attachment_attributes).and_return([attachment_attributes]) + incoming_message = incoming_messages(:useless_incoming_message) + + # Extract the attachments + incoming_message.extract_attachments! + + # delete the cached file for the main body part + main = incoming_message.get_main_body_text_part + main.delete_cached_file! + + # Simulate reparsing with the slightly changed body + MailHandler.stub!(:get_attachment_attributes).and_return([new_attachment_attributes]) + + # Re-extract the attachments + incoming_message.extract_attachments! + + attachments = incoming_message.foi_attachments + attachments.size.should == 1 + attachments.first.hexdigest.should == "74d2c0a41e074f9cebe49324d5b47414" + attachments.first.body.should == 'No way!' + end + +end + diff --git a/spec/models/info_request_event_spec.rb b/spec/models/info_request_event_spec.rb index 796f8b840..eb0de8c86 100644 --- a/spec/models/info_request_event_spec.rb +++ b/spec/models/info_request_event_spec.rb @@ -1,3 +1,4 @@ +# coding: utf-8 require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe InfoRequestEvent do @@ -21,7 +22,7 @@ describe InfoRequestEvent do :event_type => 'sent', :params => {}) event.should_receive(:xapian_mark_needs_index) - event.run_callbacks(:after_save) + event.run_callbacks(:save) end end @@ -118,6 +119,13 @@ describe InfoRequestEvent do @info_request_event.same_email_as_previous_send?.should be_true end + it 'should handle non-ascii characters in the name input' do + address = "\"Someone’s name\" <test@example.com>" + @info_request_event.stub!(:params).and_return(:email => address) + @info_request_event.stub_chain(:info_request, :get_previous_email_sent_to).and_return(address) + @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 728a538f9..c9ee57c74 100644 --- a/spec/models/info_request_spec.rb +++ b/spec/models/info_request_spec.rb @@ -156,6 +156,7 @@ describe InfoRequest do end it "should cope with indexing after item is deleted" do + load_raw_emails_data IncomingMessage.find(:all).each{|x| x.parse_raw_email!} rebuild_xapian_index # delete event from underneath indexing; shouldn't cause error @@ -426,8 +427,8 @@ describe InfoRequest do before do Time.stub!(:now).and_return(Time.utc(2007, 11, 9, 23, 59)) - @mock_comment_event = safe_mock_model(InfoRequestEvent, :created_at => Time.now - 23.days, :event_type => 'comment', :response? => false) - @mock_response_event = safe_mock_model(InfoRequestEvent, :created_at => Time.now - 22.days, :event_type => 'response', :response? => true) + @mock_comment_event = mock_model(InfoRequestEvent, :created_at => Time.now - 23.days, :event_type => 'comment', :response? => false) + @mock_response_event = mock_model(InfoRequestEvent, :created_at => Time.now - 22.days, :event_type => 'response', :response? => true) @info_request = InfoRequest.new(:prominence => 'normal', :awaiting_description => true, :info_request_events => [@mock_response_event, @mock_comment_event]) @@ -457,16 +458,16 @@ describe InfoRequest do describe 'when applying censor rules' do before do - @global_rule = safe_mock_model(CensorRule, :apply_to_text! => nil, + @global_rule = mock_model(CensorRule, :apply_to_text! => nil, :apply_to_binary! => nil) - @user_rule = safe_mock_model(CensorRule, :apply_to_text! => nil, + @user_rule = mock_model(CensorRule, :apply_to_text! => nil, :apply_to_binary! => nil) - @request_rule = safe_mock_model(CensorRule, :apply_to_text! => nil, + @request_rule = mock_model(CensorRule, :apply_to_text! => nil, :apply_to_binary! => nil) - @body_rule = safe_mock_model(CensorRule, :apply_to_text! => nil, + @body_rule = mock_model(CensorRule, :apply_to_text! => nil, :apply_to_binary! => nil) - @user = safe_mock_model(User, :censor_rules => [@user_rule]) - @body = safe_mock_model(PublicBody, :censor_rules => [@body_rule]) + @user = mock_model(User, :censor_rules => [@user_rule]) + @body = mock_model(PublicBody, :censor_rules => [@body_rule]) @info_request = InfoRequest.new(:prominence => 'normal', :awaiting_description => true, :title => 'title') @@ -562,7 +563,251 @@ describe InfoRequest do @info_request.prominence = 'requester_only' @info_request.all_can_view?.should == false end + end + + describe 'when generating json for the api' do + + before do + @user = mock_model(User, :json_for_api => { :id => 20, + :url_name => 'alaveteli_user', + :name => 'Alaveteli User', + :ban_text => '', + :about_me => 'Hi' }) + end + it 'should return full user info for an internal request' do + @info_request = InfoRequest.new(:user => @user) + @info_request.user_json_for_api.should == { :id => 20, + :url_name => 'alaveteli_user', + :name => 'Alaveteli User', + :ban_text => '', + :about_me => 'Hi' } + end end + describe 'when working out a subject for a followup emails' do + + it "should not be confused by an nil subject in the incoming message" do + ir = info_requests(:fancy_dog_request) + im = mock_model(IncomingMessage, + :subject => nil, + :valid_to_reply_to? => true) + subject = ir.email_subject_followup im + subject.should match(/^Re: Freedom of Information request.*fancy dog/) + end + + it "should return a hash with the user's name for an external request" do + @info_request = InfoRequest.new(:external_url => 'http://www.example.com', + :external_user_name => 'External User') + @info_request.user_json_for_api.should == {:name => 'External User'} + end + + it 'should return "Anonymous user" for an anonymous external user' do + @info_request = InfoRequest.new(:external_url => 'http://www.example.com') + @info_request.user_json_for_api.should == {:name => 'Anonymous user'} + end + end + describe "#set_described_state and #log_event" do + context "a request" do + let(:request) { InfoRequest.create!(:title => "my request", + :public_body => public_bodies(:geraldine_public_body), + :user => users(:bob_smith_user)) } + + context "a series of events on a request" do + it "should have sensible events after the initial request has been made" do + # An initial request is sent + # The logic that changes the status when a message is sent is mixed up + # in OutgoingMessage#send_message. So, rather than extract it (or call it) + # let's just duplicate what it does here for the time being. + request.log_event('sent', {}) + request.set_described_state('waiting_response') + + events = request.info_request_events + events.count.should == 1 + events[0].event_type.should == "sent" + events[0].described_state.should == "waiting_response" + events[0].calculated_state.should == "waiting_response" + end + + it "should have sensible events after a response is received to a request" do + # An initial request is sent + request.log_event('sent', {}) + request.set_described_state('waiting_response') + # A response is received + # This is normally done in InfoRequest#receive + request.awaiting_description = true + request.log_event("response", {}) + + events = request.info_request_events + events.count.should == 2 + events[0].event_type.should == "sent" + events[0].described_state.should == "waiting_response" + events[0].calculated_state.should == "waiting_response" + events[1].event_type.should == "response" + events[1].described_state.should be_nil + # TODO: Should calculated_status in this situation be "waiting_classification"? + # This would allow searches like "latest_status: waiting_classification" to be + # available to the user in "Advanced search" + events[1].calculated_state.should be_nil + end + + it "should have sensible events after a request is classified by the requesting user" do + # An initial request is sent + request.log_event('sent', {}) + request.set_described_state('waiting_response') + # A response is received + request.awaiting_description = true + request.log_event("response", {}) + # The request is classified by the requesting user + # This is normally done in RequestController#describe_state + request.log_event("status_update", {}) + request.set_described_state("waiting_response") + + events = request.info_request_events + events.count.should == 3 + events[0].event_type.should == "sent" + events[0].described_state.should == "waiting_response" + events[0].calculated_state.should == "waiting_response" + events[1].event_type.should == "response" + events[1].described_state.should be_nil + events[1].calculated_state.should be_nil + events[2].event_type.should == "status_update" + events[2].described_state.should == "waiting_response" + events[2].calculated_state.should == "waiting_response" + end + + it "should have sensible events after a normal followup is sent" do + # An initial request is sent + request.log_event('sent', {}) + request.set_described_state('waiting_response') + # A response is received + request.awaiting_description = true + request.log_event("response", {}) + # The request is classified by the requesting user + request.log_event("status_update", {}) + request.set_described_state("waiting_response") + # A normal follow up is sent + # This is normally done in OutgoingMessage#send_message + request.log_event('followup_sent', {}) + request.set_described_state('waiting_response') + + events = request.info_request_events + events.count.should == 4 + events[0].event_type.should == "sent" + events[0].described_state.should == "waiting_response" + events[0].calculated_state.should == "waiting_response" + events[1].event_type.should == "response" + events[1].described_state.should be_nil + events[1].calculated_state.should be_nil + events[2].event_type.should == "status_update" + events[2].described_state.should == "waiting_response" + events[2].calculated_state.should == "waiting_response" + events[3].event_type.should == "followup_sent" + events[3].described_state.should == "waiting_response" + events[3].calculated_state.should == "waiting_response" + end + + it "should have sensible events after a user classifies the request after a follow up" do + # An initial request is sent + request.log_event('sent', {}) + request.set_described_state('waiting_response') + # A response is received + request.awaiting_description = true + request.log_event("response", {}) + # The request is classified by the requesting user + request.log_event("status_update", {}) + request.set_described_state("waiting_response") + # A normal follow up is sent + request.log_event('followup_sent', {}) + request.set_described_state('waiting_response') + # The request is classified by the requesting user + request.log_event("status_update", {}) + request.set_described_state("waiting_response") + + events = request.info_request_events + events.count.should == 5 + events[0].event_type.should == "sent" + events[0].described_state.should == "waiting_response" + events[0].calculated_state.should == "waiting_response" + events[1].event_type.should == "response" + events[1].described_state.should be_nil + events[1].calculated_state.should be_nil + events[2].event_type.should == "status_update" + events[2].described_state.should == "waiting_response" + events[2].calculated_state.should == "waiting_response" + events[3].event_type.should == "followup_sent" + events[3].described_state.should == "waiting_response" + events[3].calculated_state.should == "waiting_response" + events[4].event_type.should == "status_update" + events[4].described_state.should == "waiting_response" + events[4].calculated_state.should == "waiting_response" + end + end + + context "another series of events on a request" do + it "should have sensible event states" do + # An initial request is sent + request.log_event('sent', {}) + request.set_described_state('waiting_response') + # An internal review is requested + request.log_event('followup_sent', {}) + request.set_described_state('internal_review') + + events = request.info_request_events + events.count.should == 2 + events[0].event_type.should == "sent" + events[0].described_state.should == "waiting_response" + events[0].calculated_state.should == "waiting_response" + events[1].event_type.should == "followup_sent" + events[1].described_state.should == "internal_review" + events[1].calculated_state.should == "internal_review" + end + + it "should have sensible event states" do + # An initial request is sent + request.log_event('sent', {}) + request.set_described_state('waiting_response') + # An internal review is requested + request.log_event('followup_sent', {}) + request.set_described_state('internal_review') + # The user marks the request as rejected + request.log_event("status_update", {}) + request.set_described_state("rejected") + + events = request.info_request_events + events.count.should == 3 + events[0].event_type.should == "sent" + events[0].described_state.should == "waiting_response" + events[0].calculated_state.should == "waiting_response" + events[1].event_type.should == "followup_sent" + events[1].described_state.should == "internal_review" + events[1].calculated_state.should == "internal_review" + events[2].event_type.should == "status_update" + events[2].described_state.should == "rejected" + events[2].calculated_state.should == "rejected" + end + end + + context "another series of events on a request" do + it "should have sensible event states" do + # An initial request is sent + request.log_event('sent', {}) + request.set_described_state('waiting_response') + # The user marks the request as successful (I know silly but someone did + # this in https://www.whatdotheyknow.com/request/family_support_worker_redundanci) + request.log_event("status_update", {}) + request.set_described_state("successful") + + events = request.info_request_events + events.count.should == 2 + events[0].event_type.should == "sent" + events[0].described_state.should == "waiting_response" + events[0].calculated_state.should == "waiting_response" + events[1].event_type.should == "status_update" + events[1].described_state.should == "successful" + events[1].calculated_state.should == "successful" + end + end + end + end end diff --git a/spec/models/mail_server_log_spec.rb b/spec/models/mail_server_log_spec.rb index d0a1d202f..2b44a4559 100644 --- a/spec/models/mail_server_log_spec.rb +++ b/spec/models/mail_server_log_spec.rb @@ -3,7 +3,7 @@ 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") + AlaveteliConfiguration.stub!(:incoming_email_domain).and_return("example.com") File.stub_chain(:stat, :mtime).and_return(DateTime.new(2012, 10, 10)) end @@ -64,8 +64,8 @@ describe MailServerLog do 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+") + AlaveteliConfiguration.stub!(:incoming_email_domain).and_return("example.com") + AlaveteliConfiguration.stub!(:incoming_email_prefix).and_return("foi+") end it "recognises a single incoming email" do @@ -106,7 +106,7 @@ describe MailServerLog 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") + AlaveteliConfiguration.stub!(:incoming_email_domain).and_return("example.com") log.stub!(:rewind) ir1 = info_requests(:fancy_dog_request) ir2 = info_requests(:naughty_chicken_request) @@ -135,7 +135,7 @@ describe MailServerLog do 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") + AlaveteliConfiguration.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"] diff --git a/spec/models/outgoing_mailer_spec.rb b/spec/models/outgoing_mailer_spec.rb deleted file mode 100644 index 5d1ea2dfb..000000000 --- a/spec/models/outgoing_mailer_spec.rb +++ /dev/null @@ -1,140 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') - -describe OutgoingMailer, " when working out follow up addresses" do - # This is done with fixtures as the code is a bit tangled with the way it - # 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 - before(:each) do - load_raw_emails_data - end - - it "should parse them right" do - ir = info_requests(:fancy_dog_request) - im = ir.incoming_messages[0] - - # check the basic entry in the fixture is fine - OutgoingMailer.name_and_email_for_followup(ir, im).should == "FOI Person <foiperson@localhost>" - OutgoingMailer.name_for_followup(ir, im).should == "FOI Person" - OutgoingMailer.email_for_followup(ir, im).should == "foiperson@localhost" - end - - it "should work when there is only an email address" do - ir = info_requests(:fancy_dog_request) - im = ir.incoming_messages[0] - - im.raw_email.data = im.raw_email.data.sub("\"FOI Person\" <foiperson@localhost>", "foiperson@localhost") - im.parse_raw_email! true - - # check the basic entry in the fixture is fine - OutgoingMailer.name_and_email_for_followup(ir, im).should == "foiperson@localhost" - OutgoingMailer.name_for_followup(ir, im).should == "Geraldine Quango" - OutgoingMailer.email_for_followup(ir, im).should == "foiperson@localhost" - end - - it "should quote funny characters" do - ir = info_requests(:fancy_dog_request) - im = ir.incoming_messages[0] - - im.raw_email.data = im.raw_email.data.sub("FOI Person", "FOI [ Person") - im.parse_raw_email! true - - # check the basic entry in the fixture is fine - OutgoingMailer.name_and_email_for_followup(ir, im).should == "\"FOI [ Person\" <foiperson@localhost>" - OutgoingMailer.name_for_followup(ir, im).should == "FOI [ Person" - OutgoingMailer.email_for_followup(ir, im).should == "foiperson@localhost" - end - - it "should quote quotes" do - ir = info_requests(:fancy_dog_request) - im = ir.incoming_messages[0] - - im.raw_email.data = im.raw_email.data.sub("FOI Person", "FOI \\\" Person") - im.parse_raw_email! true - - # check the basic entry in the fixture is fine - OutgoingMailer.name_and_email_for_followup(ir, im).should == "\"FOI \\\" Person\" <foiperson@localhost>" - OutgoingMailer.name_for_followup(ir, im).should == "FOI \" Person" - OutgoingMailer.email_for_followup(ir, im).should == "foiperson@localhost" - end - - it "should quote @ signs" do - ir = info_requests(:fancy_dog_request) - im = ir.incoming_messages[0] - - im.raw_email.data = im.raw_email.data.sub("FOI Person", "FOI @ Person") - im.parse_raw_email! true - - # check the basic entry in the fixture is fine - OutgoingMailer.name_and_email_for_followup(ir, im).should == "\"FOI @ Person\" <foiperson@localhost>" - OutgoingMailer.name_for_followup(ir, im).should == "FOI @ Person" - OutgoingMailer.email_for_followup(ir, im).should == "foiperson@localhost" - end - -end - -describe OutgoingMailer, "when working out follow up subjects" do - - before(:each) do - load_raw_emails_data - end - - it "should prefix the title with 'Freedom of Information request -' for initial requests" do - ir = info_requests(:fancy_dog_request) - im = ir.incoming_messages[0] - - ir.email_subject_request.should == "Freedom of Information request - Why do you have & such a fancy dog?" - end - - it "should use 'Re:' and inital request subject for followups which aren't replies to particular messages" do - ir = info_requests(:fancy_dog_request) - om = outgoing_messages(:useless_outgoing_message) - - OutgoingMailer.subject_for_followup(ir, om).should == "Re: Freedom of Information request - Why do you have & such a fancy dog?" - end - - it "should prefix with Re: the subject of the message being replied to" do - ir = info_requests(:fancy_dog_request) - im = ir.incoming_messages[0] - om = outgoing_messages(:useless_outgoing_message) - om.incoming_message_followup = im - - OutgoingMailer.subject_for_followup(ir, om).should == "Re: Geraldine FOI Code AZXB421" - end - - it "should not add Re: prefix if there already is such a prefix" do - ir = info_requests(:fancy_dog_request) - im = ir.incoming_messages[0] - om = outgoing_messages(:useless_outgoing_message) - om.incoming_message_followup = im - - im.raw_email.data = im.raw_email.data.sub("Subject: Geraldine FOI Code AZXB421", "Subject: Re: Geraldine FOI Code AZXB421") - OutgoingMailer.subject_for_followup(ir, om).should == "Re: Geraldine FOI Code AZXB421" - end - - it "should not add Re: prefix if there already is a lower case re: prefix" do - ir = info_requests(:fancy_dog_request) - im = ir.incoming_messages[0] - om = outgoing_messages(:useless_outgoing_message) - om.incoming_message_followup = im - - im.raw_email.data = im.raw_email.data.sub("Subject: Geraldine FOI Code AZXB421", "Subject: re: Geraldine FOI Code AZXB421") - im.parse_raw_email! true - - OutgoingMailer.subject_for_followup(ir, om).should == "re: Geraldine FOI Code AZXB421" - end - - it "should use 'Re:' and initial request subject when replying to failed delivery notifications" do - ir = info_requests(:fancy_dog_request) - im = ir.incoming_messages[0] - om = outgoing_messages(:useless_outgoing_message) - om.incoming_message_followup = im - - im.raw_email.data = im.raw_email.data.sub("foiperson@localhost", "postmaster@localhost") - im.raw_email.data = im.raw_email.data.sub("Subject: Geraldine FOI Code AZXB421", "Subject: Delivery Failed") - im.parse_raw_email! true - - OutgoingMailer.subject_for_followup(ir, om).should == "Re: Freedom of Information request - Why do you have & such a fancy dog?" - end -end - - diff --git a/spec/models/outgoing_message_spec.rb b/spec/models/outgoing_message_spec.rb index 51bb6fdf5..60164fb31 100644 --- a/spec/models/outgoing_message_spec.rb +++ b/spec/models/outgoing_message_spec.rb @@ -16,7 +16,7 @@ describe OutgoingMessage, " when making an outgoing message" do it "should not index the email addresses" do # also used for track emails @outgoing_message.get_text_for_indexing.should_not include("foo@bar.com") - end + end it "should not display email addresses on page" do @outgoing_message.get_body_for_html_display.should_not include("foo@bar.com") @@ -33,6 +33,23 @@ describe OutgoingMessage, " when making an outgoing message" do it "should work out a salutation" do @om.get_salutation.should == "Dear Geraldine Quango," end + + it 'should produce the expected text for an internal review request' do + public_body = mock_model(PublicBody, :name => 'A test public body') + info_request = mock_model(InfoRequest, :public_body => public_body, + :url_title => 'a_test_title', + :title => 'A test title', + :apply_censor_rules_to_text! => nil) + outgoing_message = OutgoingMessage.new({ + :status => 'ready', + :message_type => 'followup', + :what_doing => 'internal_review', + :info_request => info_request + }) + expected_text = "I am writing to request an internal review of A test public body's handling of my FOI request 'A test title'." + outgoing_message.body.should include(expected_text) + end + end diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index 0b1bfccd7..34a91a2c9 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -169,6 +169,14 @@ describe PublicBody, " when saving" do @public_body.save! @public_body.first_letter.should == 'T' end + + it "should save the name when renaming an existing public body" do + public_body = public_bodies(:geraldine_public_body) + public_body.name = "Mark's Public Body" + public_body.save! + + public_body.name.should == "Mark's Public Body" + end end describe PublicBody, "when searching" do @@ -456,7 +464,7 @@ end describe PublicBody, " when override all public body request emails set" do it "should return the overridden request email" do - MySociety::Config.should_receive(:get).with("OVERRIDE_ALL_PUBLIC_BODY_REQUEST_EMAILS", "").twice.and_return("catch_all_test_email@foo.com") + AlaveteliConfiguration.should_receive(:override_all_public_body_request_emails).twice.and_return("catch_all_test_email@foo.com") @geraldine = public_bodies(:geraldine_public_body) @geraldine.request_email.should == "catch_all_test_email@foo.com" end diff --git a/spec/models/request_mailer_spec.rb b/spec/models/request_mailer_spec.rb deleted file mode 100644 index 40133eedc..000000000 --- a/spec/models/request_mailer_spec.rb +++ /dev/null @@ -1,375 +0,0 @@ -# encoding: utf-8 -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') - -describe RequestMailer, " when receiving incoming mail" do - before(:each) do - load_raw_emails_data - ActionMailer::Base.deliveries = [] - end - - it "should append it to the appropriate request" do - ir = info_requests(:fancy_dog_request) - ir.incoming_messages.size.should == 1 # in the fixture - receive_incoming_mail('incoming-request-plain.email', ir.incoming_email) - ir.incoming_messages.size.should == 2 # one more arrives - ir.info_request_events[-1].incoming_message_id.should_not be_nil - - deliveries = ActionMailer::Base.deliveries - deliveries.size.should == 1 - mail = deliveries[0] - mail.to.should == [ 'bob@localhost' ] # to the user who sent fancy_dog_request - deliveries.clear - end - - it "should store mail in holding pen and send to admin when the email is not to any information request" do - ir = info_requests(:fancy_dog_request) - ir.incoming_messages.size.should == 1 - InfoRequest.holding_pen_request.incoming_messages.size.should == 0 - receive_incoming_mail('incoming-request-plain.email', 'dummy@localhost') - ir.incoming_messages.size.should == 1 - InfoRequest.holding_pen_request.incoming_messages.size.should == 1 - last_event = InfoRequest.holding_pen_request.incoming_messages[0].info_request.info_request_events.last - last_event.params[:rejected_reason].should == "Could not identify the request from the email address" - - deliveries = ActionMailer::Base.deliveries - deliveries.size.should == 1 - mail = deliveries[0] - mail.to.should == [ Configuration::contact_email ] - deliveries.clear - end - - it "should store mail in holding pen and send to admin when the from email is empty and only authorites can reply" do - ir = info_requests(:fancy_dog_request) - ir.allow_new_responses_from = 'authority_only' - ir.handle_rejected_responses = 'holding_pen' - ir.save! - ir.incoming_messages.size.should == 1 - InfoRequest.holding_pen_request.incoming_messages.size.should == 0 - receive_incoming_mail('incoming-request-plain.email', ir.incoming_email, "") - ir.incoming_messages.size.should == 1 - InfoRequest.holding_pen_request.incoming_messages.size.should == 1 - last_event = InfoRequest.holding_pen_request.incoming_messages[0].info_request.info_request_events.last - last_event.params[:rejected_reason].should =~ /there is no "From" address/ - - deliveries = ActionMailer::Base.deliveries - deliveries.size.should == 1 - mail = deliveries[0] - mail.to.should == [ Configuration::contact_email ] - deliveries.clear - end - - it "should store mail in holding pen and send to admin when the from email is unknown and only authorites can reply" do - ir = info_requests(:fancy_dog_request) - ir.allow_new_responses_from = 'authority_only' - ir.handle_rejected_responses = 'holding_pen' - ir.save! - ir.incoming_messages.size.should == 1 - InfoRequest.holding_pen_request.incoming_messages.size.should == 0 - receive_incoming_mail('incoming-request-plain.email', ir.incoming_email, "frob@nowhere.com") - ir.incoming_messages.size.should == 1 - InfoRequest.holding_pen_request.incoming_messages.size.should == 1 - last_event = InfoRequest.holding_pen_request.incoming_messages[0].info_request.info_request_events.last - last_event.params[:rejected_reason].should =~ /Only the authority can reply/ - - deliveries = ActionMailer::Base.deliveries - deliveries.size.should == 1 - mail = deliveries[0] - mail.to.should == [ Configuration::contact_email ] - deliveries.clear - end - - it "should return incoming mail to sender when a request is stopped fully for spam" do - # mark request as anti-spam - ir = info_requests(:fancy_dog_request) - ir.allow_new_responses_from = 'nobody' - ir.handle_rejected_responses = 'bounce' - ir.save! - - # test what happens if something arrives - ir.incoming_messages.size.should == 1 # in the fixture - receive_incoming_mail('incoming-request-plain.email', ir.incoming_email) - ir.incoming_messages.size.should == 1 # nothing should arrive - - # should be a message back to sender - deliveries = ActionMailer::Base.deliveries - deliveries.size.should == 1 - mail = deliveries[0] - mail.to.should == [ 'geraldinequango@localhost' ] - # check attached bounce is good copy of incoming-request-plain.email - mail.multipart?.should == true - mail.parts.size.should == 2 - message_part = mail.parts[0].to_s - bounced_mail = MailHandler.mail_from_raw_email(mail.parts[1].body) - 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 - message_part.include?("marked to no longer receive responses").should be_true - deliveries.clear - end - - it "should return incoming mail to sender if not authority when a request is stopped for non-authority spam" do - # mark request as anti-spam - ir = info_requests(:fancy_dog_request) - ir.allow_new_responses_from = 'authority_only' - ir.handle_rejected_responses = 'bounce' - ir.save! - - # Test what happens if something arrives from authority domain (@localhost) - ir.incoming_messages.size.should == 1 # in the fixture - receive_incoming_mail('incoming-request-plain.email', ir.incoming_email, "Geraldine <geraldinequango@localhost>") - ir.incoming_messages.size.should == 2 # one more arrives - - # ... should get "responses arrived" message for original requester - deliveries = ActionMailer::Base.deliveries - deliveries.size.should == 1 - mail = deliveries[0] - mail.to.should == [ 'bob@localhost' ] # to the user who sent fancy_dog_request - deliveries.clear - - # Test what happens if something arrives from another domain - ir.incoming_messages.size.should == 2 # in fixture and above - receive_incoming_mail('incoming-request-plain.email', ir.incoming_email, "dummy-address@dummy.localhost") - ir.incoming_messages.size.should == 2 # nothing should arrive - - # ... should be a bounce message back to sender - deliveries = ActionMailer::Base.deliveries - deliveries.size.should == 1 - mail = deliveries[0] - mail.to.should == [ 'dummy-address@dummy.localhost' ] - deliveries.clear - end - - it "should send all new responses to holding pen if a request is marked to do so" do - # mark request as anti-spam - ir = info_requests(:fancy_dog_request) - ir.allow_new_responses_from = 'nobody' - ir.handle_rejected_responses = 'holding_pen' - ir.save! - - # test what happens if something arrives - ir = info_requests(:fancy_dog_request) - ir.incoming_messages.size.should == 1 - InfoRequest.holding_pen_request.incoming_messages.size.should == 0 - receive_incoming_mail('incoming-request-plain.email', ir.incoming_email) - ir.incoming_messages.size.should == 1 - InfoRequest.holding_pen_request.incoming_messages.size.should == 1 # arrives in holding pen - last_event = InfoRequest.holding_pen_request.incoming_messages[0].info_request.info_request_events.last - last_event.params[:rejected_reason].should =~ /allow new responses from nobody/ - - # should be a message to admin regarding holding pen - deliveries = ActionMailer::Base.deliveries - deliveries.size.should == 1 - mail = deliveries[0] - mail.to.should == [ Configuration::contact_email ] - deliveries.clear - end - - it "should destroy the messages sent to a request if marked to do so" do - ActionMailer::Base.deliveries.clear - # mark request as anti-spam - ir = info_requests(:fancy_dog_request) - ir.allow_new_responses_from = 'nobody' - ir.handle_rejected_responses = 'blackhole' - ir.save! - - # test what happens if something arrives - should be nothing - ir = info_requests(:fancy_dog_request) - ir.incoming_messages.size.should == 1 - InfoRequest.holding_pen_request.incoming_messages.size.should == 0 - receive_incoming_mail('incoming-request-plain.email', ir.incoming_email) - ir.incoming_messages.size.should == 1 - InfoRequest.holding_pen_request.incoming_messages.size.should == 0 - - # should be no messages to anyone - deliveries = ActionMailer::Base.deliveries - deliveries.size.should == 0 - end - - - it "should not mutilate long URLs when trying to word wrap them" do - long_url = 'http://www.this.is.quite.a.long.url.flourish.org/there.is.no.way.it.is.short.whatsoever' - body = "This is a message with quite a long URL in it. It also has a paragraph, being this one that has quite a lot of text in it to. Enough to test the wrapping of itself. - -#{long_url} - -And a paragraph afterwards." - wrapped = MySociety::Format.wrap_email_body_by_paragraphs(body) - wrapped.should include(long_url) - end -end - - -describe RequestMailer, "when sending reminders to requesters to classify a response to their request" do - - before do - Time.stub!(:now).and_return(Time.utc(2007, 11, 12, 23, 59)) - @mock_event = mock_model(InfoRequestEvent) - @mock_response = mock_model(IncomingMessage) - @mock_user = mock_model(User) - @mock_request = mock_model(InfoRequest, :get_last_response_event_id => @mock_event.id, - :get_last_response => @mock_response, - :user_id => 2, - :url_title => 'test_title', - :user => @mock_user) - InfoRequest.stub!(:find).and_return([@mock_request]) - RequestMailer.stub!(:deliver_new_response_reminder_alert) - @sent_alert = mock_model(UserInfoRequestSentAlert, :user= =>nil, - :info_request= => nil, - :alert_type= => nil, - :info_request_event_id= => nil, - :save! => true) - UserInfoRequestSentAlert.stub!(:new).and_return(@sent_alert) - end - - def send_alerts - RequestMailer.alert_new_response_reminders_internal(7, 'new_response_reminder_1') - end - - it 'should ask for all requests that are awaiting description and whose latest response is older - than the number of days given and that are not the holding pen' do - expected_conditions = [ "awaiting_description = ? - AND (SELECT created_at - FROM info_request_events - WHERE info_request_events.info_request_id = info_requests.id - AND info_request_events.event_type = 'response' - ORDER BY created_at desc LIMIT 1) < ? - AND url_title != 'holding_pen' - AND user_id IS NOT NULL".split(' ').join(' '), - true, Time.now() - 7.days ] - - # compare the query string ignoring any spacing differences - InfoRequest.should_receive(:find) do |all, query_params| - query_string = query_params[:conditions][0] - query_params[:conditions][0] = query_string.split(' ').join(' ') - query_params[:conditions].should == expected_conditions - query_params[:include].should == [ :user ] - query_params[:order].should == 'info_requests.id' - end - - send_alerts - end - - it 'should raise an error if a request does not have a last response event id' do - @mock_request.stub!(:get_last_response_event_id).and_return(nil) - expected_message = "internal error, no last response while making alert new response reminder, request id #{@mock_request.id}" - lambda{ send_alerts }.should raise_error(expected_message) - end - - it 'should check to see if an alert matching the attributes of the one to be sent has already been sent' do - expected_params = {:conditions => [ "alert_type = ? and user_id = ? and info_request_id = ? and info_request_event_id = ?", - 'new_response_reminder_1', 2, @mock_request.id, @mock_event.id]} - UserInfoRequestSentAlert.should_receive(:find).with(:first, expected_params) - send_alerts - end - - describe 'if an alert matching the attributes of the reminder to be sent has already been sent' do - - before do - UserInfoRequestSentAlert.stub!(:find).and_return(mock_model(UserInfoRequestSentAlert)) - end - - it 'should not send the reminder' do - RequestMailer.should_not_receive(:deliver_new_response_reminder_alert) - send_alerts - end - - end - - describe 'if no alert matching the attributes of the reminder to be sent has already been sent' do - - before do - UserInfoRequestSentAlert.stub!(:find).and_return(nil) - end - - it 'should store the information that the reminder has been sent' do - mock_sent_alert = mock_model(UserInfoRequestSentAlert) - UserInfoRequestSentAlert.stub!(:new).and_return(mock_sent_alert) - mock_sent_alert.should_receive(:info_request=).with(@mock_request) - mock_sent_alert.should_receive(:user=).with(@mock_user) - mock_sent_alert.should_receive(:alert_type=).with('new_response_reminder_1') - mock_sent_alert.should_receive(:info_request_event_id=).with(@mock_request.get_last_response_event_id) - mock_sent_alert.should_receive(:save!) - send_alerts - end - - it 'should send the reminder' do - RequestMailer.should_receive(:deliver_new_response_reminder_alert) - send_alerts - end - end - -end - -describe RequestMailer, 'when sending mail when someone has updated an old unclassified request' do - - before do - @user = mock_model(User, :name_and_email => 'test name and email') - @public_body = mock_model(PublicBody, :name => 'Test public body') - @info_request = mock_model(InfoRequest, :user => @user, - :law_used_full => 'Freedom of Information', - :title => 'Test request', - :public_body => @public_body, - :display_status => 'Refused.', - :url_title => 'test_request') - @mail = RequestMailer.create_old_unclassified_updated(@info_request) - end - - it 'should have the subject "Someone has updated the status of your request"' do - @mail.subject.should == 'Someone has updated the status of your request' - end - - it 'should tell them what status was picked' do - @mail.body.should match(/"refused."/) - end - - it 'should contain the request path' do - @mail.body.should match(/request\/test_request/) - end - -end - - -describe RequestMailer, 'when sending a new response email' do - - before do - @user = mock_model(User, :name_and_email => 'test name and email') - @public_body = mock_model(PublicBody, :name => 'Test public body') - @info_request = mock_model(InfoRequest, :user => @user, - :law_used_full => 'Freedom of Information', - :title => 'Here is a character that needs quoting …', - :public_body => @public_body, - :display_status => 'Refused.', - :url_title => 'test_request') - @incoming_message = mock_model(IncomingMessage, :info_request => @info_request) - end - - it 'should not error when sending mails requests with characters requiring quoting in the subject' do - @mail = RequestMailer.create_new_response(@info_request, @incoming_message) - end - -end - -describe RequestMailer, 'requires_admin' do - before(:each) do - user = mock_model(User, :name_and_email => 'Bruce Jones', - :name => 'Bruce Jones') - @info_request = mock_model(InfoRequest, :user => user, - :described_state => 'error_message', - :title => 'Test request', - :url_title => 'test_request', - :law_used_short => 'FOI', - :id => 123) - end - - it 'body should contain the full admin URL' do - mail = RequestMailer.deliver_requires_admin(@info_request) - - mail.body.should include('http://test.host/en/admin/request/show/123') - end - - it "body should contain the message from the user" do - mail = RequestMailer.deliver_requires_admin(@info_request, nil, "Something has gone wrong") - mail.body.should include 'Something has gone wrong' - end - -end diff --git a/spec/models/track_mailer_spec.rb b/spec/models/track_mailer_spec.rb deleted file mode 100644 index 97f12b8f5..000000000 --- a/spec/models/track_mailer_spec.rb +++ /dev/null @@ -1,213 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') - -describe TrackMailer do - - describe 'when sending email alerts for tracked things' do - - before do - TrackMailer.stub!(:deliver_event_digest) - Time.stub!(:now).and_return(Time.utc(2007, 11, 12, 23, 59)) - end - - it 'should ask for all the users whose last daily track email was sent more than a day ago' do - expected_conditions = [ "last_daily_track_email < ?", Time.utc(2007, 11, 11, 23, 59)] - User.should_receive(:find).with(:all, :conditions => expected_conditions).and_return([]) - TrackMailer.alert_tracks - end - - describe 'for each user' do - - before do - @user = mock_model(User, :no_xapian_reindex= => false, - :last_daily_track_email= => true, - :save! => true, - :url_name => 'test-name', - :get_locale => 'en', - :should_be_emailed? => true) - User.stub!(:find).and_return([@user]) - @user.stub!(:receive_email_alerts).and_return(true) - @user.stub!(:no_xapian_reindex=) - end - - it 'should ask for any daily track things for the user' do - expected_conditions = [ "tracking_user_id = ? and track_medium = ?", @user.id, 'email_daily' ] - TrackThing.should_receive(:find).with(:all, :conditions => expected_conditions).and_return([]) - TrackMailer.alert_tracks - end - - - it 'should set the no_xapian_reindex flag on the user' do - @user.should_receive(:no_xapian_reindex=).with(true) - TrackMailer.alert_tracks - end - - it 'should update the time of the user\'s last daily tracking email' do - @user.should_receive(:last_daily_track_email=).with(Time.now) - @user.should_receive(:save!) - TrackMailer.alert_tracks - end - it 'should return true' do - TrackMailer.alert_tracks.should == true - end - - - describe 'for each tracked thing' do - - before do - @track_things_sent_emails_array = [] - @track_things_sent_emails_array.stub!(:find).and_return([]) # this is for the date range find (created in last 14 days) - @track_thing = mock_model(TrackThing, :track_query => 'test query', - :track_things_sent_emails => @track_things_sent_emails_array, - :created_at => Time.utc(2007, 11, 9, 23, 59)) - TrackThing.stub!(:find).and_return([@track_thing]) - @track_things_sent_email = mock_model(TrackThingsSentEmail, :save! => true, - :track_thing_id= => true, - :info_request_event_id= => true) - TrackThingsSentEmail.stub!(:new).and_return(@track_things_sent_email) - @xapian_search = mock('xapian search', :results => []) - @found_event = mock_model(InfoRequestEvent, :described_at => @track_thing.created_at + 1.day) - @search_result = {:model => @found_event} - InfoRequest.stub!(:full_search).and_return(@xapian_search) - end - - it 'should ask for the events returned by the tracking query' do - InfoRequest.should_receive(:full_search).with([InfoRequestEvent], 'test query', 'described_at', true, nil, 100, 1).and_return(@xapian_search) - TrackMailer.alert_tracks - end - - it 'should not include in the email any events that the user has already been sent a tracking email about' do - sent_email = mock_model(TrackThingsSentEmail, :info_request_event_id => @found_event.id) - @track_things_sent_emails_array.stub!(:find).and_return([sent_email]) # this is for the date range find (created in last 14 days) - @xapian_search.stub!(:results).and_return([@search_result]) - TrackMailer.should_not_receive(:deliver_event_digest) - TrackMailer.alert_tracks - end - - it 'should not include in the email any events not sent in a previous tracking email that were described before the track was set up' do - @found_event.stub!(:described_at).and_return(@track_thing.created_at - 1.day) - @xapian_search.stub!(:results).and_return([@search_result]) - TrackMailer.should_not_receive(:deliver_event_digest) - TrackMailer.alert_tracks - end - - it 'should include in the email any events that the user has not been sent a tracking email on that have been described since the track was set up' do - @found_event.stub!(:described_at).and_return(@track_thing.created_at + 1.day) - @xapian_search.stub!(:results).and_return([@search_result]) - TrackMailer.should_receive(:deliver_event_digest) - TrackMailer.alert_tracks - end - - it 'should raise an error if a non-event class is returned by the tracking query' do - @xapian_search.stub!(:results).and_return([{:model => 'string class'}]) - lambda{ TrackMailer.alert_tracks }.should raise_error('need to add other types to TrackMailer.alert_tracks (unalerted)') - end - - it 'should record that a tracking email has been sent for each event that has been included in the email' do - @xapian_search.stub!(:results).and_return([@search_result]) - sent_email = mock_model(TrackThingsSentEmail) - TrackThingsSentEmail.should_receive(:new).and_return(sent_email) - sent_email.should_receive(:track_thing_id=).with(@track_thing.id) - sent_email.should_receive(:info_request_event_id=).with(@found_event.id) - sent_email.should_receive(:save!) - TrackMailer.alert_tracks - end - end - - end - - describe 'when a user should not be emailed' do - before do - @user = mock_model(User, :no_xapian_reindex= => false, - :last_daily_track_email= => true, - :save! => true, - :url_name => 'test-name', - :should_be_emailed? => false) - User.stub!(:find).and_return([@user]) - @user.stub!(:receive_email_alerts).and_return(true) - @user.stub!(:no_xapian_reindex=) - end - - it 'should not ask for any daily track things for the user' do - expected_conditions = [ "tracking_user_id = ? and track_medium = ?", @user.id, 'email_daily' ] - TrackThing.should_not_receive(:find).with(:all, :conditions => expected_conditions).and_return([]) - TrackMailer.alert_tracks - end - - it 'should not ask for any daily track things for the user if they have receive_email_alerts off but could otherwise be emailed' do - @user.stub(:should_be_emailed?).and_return(true) - @user.stub(:receive_email_alerts).and_return(false) - expected_conditions = [ "tracking_user_id = ? and track_medium = ?", @user.id, 'email_daily' ] - TrackThing.should_not_receive(:find).with(:all, :conditions => expected_conditions).and_return([]) - TrackMailer.alert_tracks - end - - it 'should not set the no_xapian_reindex flag on the user' do - @user.should_not_receive(:no_xapian_reindex=).with(true) - TrackMailer.alert_tracks - end - - it 'should not update the time of the user\'s last daily tracking email' do - @user.should_not_receive(:last_daily_track_email=).with(Time.now) - @user.should_not_receive(:save!) - TrackMailer.alert_tracks - end - it 'should return false' do - TrackMailer.alert_tracks.should == false - end - end - - end - - describe 'delivering the email' do - - before :each do - @post_redirect = mock_model(PostRedirect, :save! => true, - :email_token => "token") - PostRedirect.stub!(:new).and_return(@post_redirect) - ActionMailer::Base.deliveries = [] - @user = mock_model(User, - :name_and_email => MailHandler.address_from_name_and_email('Tippy Test', 'tippy@localhost'), - :url_name => 'tippy_test' - ) - TrackMailer.deliver_event_digest(@user, []) # no items in it email for minimal test - end - - it 'should deliver one email, with right headers' do - deliveries = ActionMailer::Base.deliveries - if deliveries.size > 1 # debugging if there is an error - deliveries.each do |d| - $stderr.puts "------------------------------" - $stderr.puts d.body - $stderr.puts "------------------------------" - end - end - deliveries.size.should == 1 - mail = deliveries[0] - - mail['Auto-Submitted'].to_s.should == 'auto-generated' - mail['Precedence'].to_s.should == 'bulk' - - deliveries.clear - end - - context "force ssl is off" do - # Force SSL is off in the tests. Since the code that selectively switches the protocols - # is in the initialiser for Rails it's hard to test. Hmmm... - # We could Configuration.stub!(:force_ssl).and_return(true) but the config/environment.rb - # wouldn't get reloaded - - it "should have http links in the email" do - deliveries = ActionMailer::Base.deliveries - deliveries.size.should == 1 - mail = deliveries[0] - - mail.body.should include("http://") - mail.body.should_not include("https://") - end - end - end - -end - - - diff --git a/spec/models/user_mailer_spec.rb b/spec/models/user_mailer_spec.rb deleted file mode 100644 index 0792aaab2..000000000 --- a/spec/models/user_mailer_spec.rb +++ /dev/null @@ -1,8 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') - -describe UserMailer, " when blah" do - before do - end -end - - diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index e31c3f1b5..96c169604 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -27,11 +27,22 @@ describe User, "showing the name" do @user.name.should == 'Some Name' end - it 'should show if user has been banned' do - @user.ban_text = "Naughty user" - @user.name.should == 'Some Name (Account suspended)' + describe 'if user has been banned' do + + before do + @user.ban_text = "Naughty user" + end + + it 'should show an "Account suspended" suffix' do + @user.name.should == 'Some Name (Account suspended)' + end + + it 'should return a string when the user has been banned, not a SafeBuffer' do + @user.name.class.should == String + end end + end describe User, " when authenticating" do @@ -152,10 +163,10 @@ end describe User, "when reindexing referencing models" do before do - @request_event = safe_mock_model(InfoRequestEvent, :xapian_mark_needs_index => true) - @request = safe_mock_model(InfoRequest, :info_request_events => [@request_event]) - @comment_event = safe_mock_model(InfoRequestEvent, :xapian_mark_needs_index => true) - @comment = safe_mock_model(Comment, :info_request_events => [@comment_event]) + @request_event = mock_model(InfoRequestEvent, :xapian_mark_needs_index => true) + @request = mock_model(InfoRequest, :info_request_events => [@request_event]) + @comment_event = mock_model(InfoRequestEvent, :xapian_mark_needs_index => true) + @comment = mock_model(Comment, :info_request_events => [@comment_event]) @user = User.new(:comments => [@comment], :info_requests => [@request]) end diff --git a/spec/models/xapian_spec.rb b/spec/models/xapian_spec.rb index 8c99d550f..7aab9cdc6 100644 --- a/spec/models/xapian_spec.rb +++ b/spec/models/xapian_spec.rb @@ -1,3 +1,4 @@ +# encoding: utf-8 require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe User, " when indexing users with Xapian" do @@ -8,8 +9,7 @@ describe User, " when indexing users with Xapian" do 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) + xapian_object = ActsAsXapian::Search.new([User], "Silly", :limit => 100) xapian_object.results.size.should == 1 xapian_object.results[0][:model].should == users(:silly_name_user) end @@ -17,8 +17,7 @@ describe User, " when indexing users with Xapian" do it "should search by 'about me' text" do user = users(:bob_smith_user) - # def InfoRequest.full_search(models, query, order, ascending, collapse, per_page, page) - xapian_object = InfoRequest.full_search([User], "stuff", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([User], "stuff", :limit => 100) xapian_object.results.size.should == 1 xapian_object.results[0][:model].should == user @@ -26,10 +25,10 @@ describe User, " when indexing users with Xapian" do user.save! update_xapian_index - xapian_object = InfoRequest.full_search([User], "stuff", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([User], "stuff", :limit => 100) xapian_object.results.size.should == 0 - xapian_object = InfoRequest.full_search([User], "aardvark", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([User], "aardvark", :limit => 100) xapian_object.results.size.should == 1 xapian_object.results[0][:model].should == user end @@ -42,26 +41,26 @@ describe PublicBody, " when indexing public bodies with Xapian" do end it "should search index the main name field" do - xapian_object = InfoRequest.full_search([PublicBody], "humpadinking", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([PublicBody], "humpadinking", :limit => 100) xapian_object.results.size.should == 1 xapian_object.results[0][:model].should == public_bodies(:humpadink_public_body) end it "should search index the notes field" do - xapian_object = InfoRequest.full_search([PublicBody], "albatross", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([PublicBody], "albatross", :limit => 100) xapian_object.results.size.should == 1 xapian_object.results[0][:model].should == public_bodies(:humpadink_public_body) end it "should delete public bodies from the index when they are destroyed" do - xapian_object = InfoRequest.full_search([PublicBody], "albatross", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([PublicBody], "albatross", :limit => 100) xapian_object.results.size.should == 1 xapian_object.results[0][:model].should == public_bodies(:humpadink_public_body) public_bodies(:forlorn_public_body).destroy update_xapian_index - xapian_object = InfoRequest.full_search([PublicBody], "lonely", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([PublicBody], "lonely", :limit => 100) xapian_object.results.should == [] end @@ -75,13 +74,13 @@ describe PublicBody, " when indexing requests by body they are to" do end it "should find requests to the body" do - xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_from:tgq", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([InfoRequestEvent], "requested_from:tgq", :limit => 100) xapian_object.results.size.should == PublicBody.find_by_url_name("tgq").info_requests.map(&:info_request_events).flatten.size end it "should update index correctly when URL name of body changes" do # initial search - xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_from:tgq", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([InfoRequestEvent], "requested_from:tgq", :limit => 100) xapian_object.results.size.should == PublicBody.find_by_url_name("tgq").info_requests.map(&:info_request_events).flatten.size models_found_before = xapian_object.results.map { |x| x[:model] } @@ -93,9 +92,9 @@ describe PublicBody, " when indexing requests by body they are to" do update_xapian_index # check we get results expected - xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_from:tgq", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([InfoRequestEvent], "requested_from:tgq", :limit => 100) xapian_object.results.size.should == 0 - xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_from:gq", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([InfoRequestEvent], "requested_from:gq", :limit => 100) xapian_object.results.size.should == PublicBody.find_by_url_name("gq").info_requests.map(&:info_request_events).flatten.size models_found_after = xapian_object.results.map { |x| x[:model] } @@ -113,11 +112,11 @@ describe PublicBody, " when indexing requests by body they are to" do update_xapian_index # check we get results expected - xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_from:tgq", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([InfoRequestEvent], "requested_from:tgq", :limit => 100) xapian_object.results.size.should == 0 - xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_from:gq", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([InfoRequestEvent], "requested_from:gq", :limit => 100) xapian_object.results.size.should == 0 - xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_from:" + body.url_name, 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([InfoRequestEvent], "requested_from:#{body.url_name}", :limit => 100) xapian_object.results.size.should == public_bodies(:geraldine_public_body).info_requests.map(&:info_request_events).flatten.size models_found_after = xapian_object.results.map { |x| x[:model] } end @@ -130,13 +129,14 @@ describe User, " when indexing requests by user they are from" do end it "should find requests from the user" do - xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_by:bob_smith", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([InfoRequestEvent], "requested_by:bob_smith", + :sort_by_prefix => 'created_at', :sort_by_ascending => true, :limit => 100) xapian_object.results.map{|x|x[:model]}.should =~ InfoRequestEvent.all(:conditions => "info_request_id in (select id from info_requests where user_id = #{users(:bob_smith_user).id})") end it "should find just the sent message events from a particular user" do - # def InfoRequest.full_search(models, query, order, ascending, collapse, per_page, page) - xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_by:bob_smith variety:sent", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([InfoRequestEvent], "requested_by:bob_smith variety:sent", + :sort_by_prefix => 'created_at', :sort_by_ascending => true, :limit => 100) xapian_object.results.map{|x|x[:model]}.should =~ InfoRequestEvent.all(:conditions => "info_request_id in (select id from info_requests where user_id = #{users(:bob_smith_user).id}) and event_type = 'sent'") xapian_object.results[2][:model].should == info_request_events(:useless_outgoing_message_event) xapian_object.results[1][:model].should == info_request_events(:silly_outgoing_message_event) @@ -150,8 +150,9 @@ describe User, " when indexing requests by user they are from" do update_xapian_index - # def InfoRequest.full_search(models, query, order, ascending, collapse, per_page, page) - xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_by:bob_smith", 'created_at', true, 'request_collapse', 100, 1) + xapian_object = ActsAsXapian::Search.new([InfoRequestEvent], "requested_by:bob_smith", + :sort_by_prefix => 'created_at', :sort_by_ascending => true, + :collapse_by_prefix => 'request_collapse', :limit => 100) xapian_object.results.map{|x|x[:model].info_request}.should =~ InfoRequest.all(:conditions => "user_id = #{users(:bob_smith_user).id}") end @@ -172,8 +173,7 @@ describe User, " when indexing requests by user they are from" do update_xapian_index - # def InfoRequest.full_search(models, query, order, ascending, collapse, per_page, page) - xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_by:john_k", 'created_at', true, 'request_collapse', 100, 1) + xapian_object = ActsAsXapian::Search.new([InfoRequestEvent], "requested_by:john_k", :limit => 100) xapian_object.results.size.should == 1 xapian_object.results[0][:model].should == info_request_events(:silly_outgoing_message_event) end @@ -181,7 +181,8 @@ describe User, " when indexing requests by user they are from" do it "should update index correctly when URL name of user changes" do # initial search - xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_by:bob_smith", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([InfoRequestEvent], "requested_by:bob_smith", + :sort_by_prefix => 'created_at', :sort_by_ascending => true, :limit => 100) xapian_object.results.map{|x|x[:model]}.should =~ InfoRequestEvent.all(:conditions => "info_request_id in (select id from info_requests where user_id = #{users(:bob_smith_user).id})") models_found_before = xapian_object.results.map { |x| x[:model] } @@ -193,9 +194,10 @@ describe User, " when indexing requests by user they are from" do update_xapian_index # check we get results expected - xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_by:bob_smith", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([InfoRequestEvent], "requested_by:bob_smith", :limit => 100) xapian_object.results.size.should == 0 - xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_by:robert_smith", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([InfoRequestEvent], "requested_by:robert_smith", + :sort_by_prefix => 'created_at', :sort_by_ascending => true, :limit => 100) models_found_after = xapian_object.results.map { |x| x[:model] } models_found_before.should == models_found_after end @@ -208,13 +210,13 @@ describe User, " when indexing comments by user they are by" do end it "should find requests from the user" do - xapian_object = InfoRequest.full_search([InfoRequestEvent], "commented_by:silly_emnameem", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([InfoRequestEvent], "commented_by:silly_emnameem", :limit => 100) xapian_object.results.size.should == 1 end it "should update index correctly when URL name of user changes" do # initial search - xapian_object = InfoRequest.full_search([InfoRequestEvent], "commented_by:silly_emnameem", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([InfoRequestEvent], "commented_by:silly_emnameem", :limit => 100) xapian_object.results.size.should == 1 models_found_before = xapian_object.results.map { |x| x[:model] } @@ -226,9 +228,9 @@ describe User, " when indexing comments by user they are by" do update_xapian_index # check we get results expected - xapian_object = InfoRequest.full_search([InfoRequestEvent], "commented_by:silly_emnameem", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([InfoRequestEvent], "commented_by:silly_emnameem", :limit => 100) xapian_object.results.size.should == 0 - xapian_object = InfoRequest.full_search([InfoRequestEvent], "commented_by:silly_name", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([InfoRequestEvent], "commented_by:silly_name", :limit => 100) xapian_object.results.size.should == 1 models_found_after = xapian_object.results.map { |x| x[:model] } @@ -243,7 +245,7 @@ describe InfoRequest, " when indexing requests by their title" do end it "should find events for the request" do - xapian_object = InfoRequest.full_search([InfoRequestEvent], "request:how_much_public_money_is_wasted_o", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([InfoRequestEvent], "request:how_much_public_money_is_wasted_o", :limit => 100) xapian_object.results.size.should == 1 xapian_object.results[0][:model] == info_request_events(:silly_outgoing_message_event) end @@ -257,9 +259,9 @@ describe InfoRequest, " when indexing requests by their title" do update_xapian_index # check we get results expected - xapian_object = InfoRequest.full_search([InfoRequestEvent], "request:how_much_public_money_is_wasted_o", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([InfoRequestEvent], "request:how_much_public_money_is_wasted_o", :limit => 100) xapian_object.results.size.should == 0 - xapian_object = InfoRequest.full_search([InfoRequestEvent], "request:really_naughty", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([InfoRequestEvent], "request:really_naughty", :limit => 100) xapian_object.results.size.should == 1 xapian_object.results[0][:model] == info_request_events(:silly_outgoing_message_event) end @@ -277,11 +279,11 @@ describe InfoRequest, " when indexing requests by tag" do ir.save! update_xapian_index - xapian_object = InfoRequest.full_search([InfoRequestEvent], "tag:bunnyrabbit", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([InfoRequestEvent], "tag:bunnyrabbit", :limit => 100) xapian_object.results.size.should == 1 xapian_object.results[0][:model] == info_request_events(:silly_outgoing_message_event) - xapian_object = InfoRequest.full_search([InfoRequestEvent], "tag:orangeaardvark", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([InfoRequestEvent], "tag:orangeaardvark", :limit => 100) xapian_object.results.size.should == 0 end end @@ -298,14 +300,14 @@ describe PublicBody, " when indexing authorities by tag" do body.save! update_xapian_index - xapian_object = InfoRequest.full_search([PublicBody], "tag:mice", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([PublicBody], "tag:mice", :limit => 100) xapian_object.results.size.should == 1 xapian_object.results[0][:model] == public_bodies(:geraldine_public_body) - xapian_object = InfoRequest.full_search([PublicBody], "tag:mice:3", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([PublicBody], "tag:mice:3", :limit => 100) xapian_object.results.size.should == 1 xapian_object.results[0][:model] == public_bodies(:geraldine_public_body) - xapian_object = InfoRequest.full_search([PublicBody], "tag:orangeaardvark", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([PublicBody], "tag:orangeaardvark", :limit => 100) xapian_object.results.size.should == 0 end end @@ -327,11 +329,11 @@ describe PublicBody, " when only indexing selected things on a rebuild" do values = false texts = false rebuild_xapian_index(terms, values, texts, dropfirst) - xapian_object = InfoRequest.full_search([PublicBody], "tag:mice", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([PublicBody], "tag:mice", :limit => 100) xapian_object.results.size.should == 0 - xapian_object = InfoRequest.full_search([PublicBody], "frobzn", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([PublicBody], "frobzn", :limit => 100) xapian_object.results.size.should == 0 - xapian_object = InfoRequest.full_search([PublicBody], "variety:authority", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([PublicBody], "variety:authority", :limit => 100) xapian_object.results.map{|x|x[:model]}.should =~ PublicBody.all # only reindex 'tag' and text dropfirst = true @@ -339,32 +341,62 @@ describe PublicBody, " when only indexing selected things on a rebuild" do values = false texts = true rebuild_xapian_index(terms, values, texts, dropfirst) - xapian_object = InfoRequest.full_search([PublicBody], "tag:mice", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([PublicBody], "tag:mice", :limit => 100) xapian_object.results.size.should == 1 - xapian_object = InfoRequest.full_search([PublicBody], "frobzn", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([PublicBody], "frobzn", :limit => 100) xapian_object.results.size.should == 1 - xapian_object = InfoRequest.full_search([PublicBody], "variety:authority", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([PublicBody], "variety:authority", :limit => 100) xapian_object.results.size.should == 0 # only reindex 'variety' term, but keeping the existing data in-place dropfirst = false terms = "V" texts = false rebuild_xapian_index(terms, values, texts, dropfirst) - xapian_object = InfoRequest.full_search([PublicBody], "tag:mice", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([PublicBody], "tag:mice", :limit => 100) xapian_object.results.size.should == 1 - xapian_object = InfoRequest.full_search([PublicBody], "frobzn", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([PublicBody], "frobzn", :limit => 100) xapian_object.results.size.should == 1 - xapian_object = InfoRequest.full_search([PublicBody], "variety:authority", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([PublicBody], "variety:authority", :limit => 100) xapian_object.results.map{|x|x[:model]}.should =~ PublicBody.all # only reindex 'variety' term, blowing away existing data dropfirst = true rebuild_xapian_index(terms, values, texts, dropfirst) - xapian_object = InfoRequest.full_search([PublicBody], "tag:mice", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([PublicBody], "tag:mice", :limit => 100) xapian_object.results.size.should == 0 - xapian_object = InfoRequest.full_search([PublicBody], "frobzn", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([PublicBody], "frobzn", :limit => 100) xapian_object.results.size.should == 0 - xapian_object = InfoRequest.full_search([PublicBody], "variety:authority", 'created_at', true, nil, 100, 1) + xapian_object = ActsAsXapian::Search.new([PublicBody], "variety:authority", :limit => 100) xapian_object.results.map{|x|x[:model]}.should =~ PublicBody.all end end +# I would expect ActsAsXapian to have some tests under vendor/plugins/acts_as_xapian, but +# it looks like this is not the case. Putting a test here instead. +describe ActsAsXapian::Search, "#words_to_highlight" do + before(:each) do + load_raw_emails_data + get_fixtures_xapian_index + end + + it "should return a list of words used in the search" do + s = ActsAsXapian::Search.new([PublicBody], "albatross words", :limit => 100) + s.words_to_highlight.should == ["albatross", "words"] + end + + it "should remove any operators" do + s = ActsAsXapian::Search.new([PublicBody], "albatross words tag:mice", :limit => 100) + s.words_to_highlight.should == ["albatross", "words"] + end + + # This is the current behaviour but it seems a little simplistic to me + it "should separate punctuation" do + s = ActsAsXapian::Search.new([PublicBody], "The doctor's patient", :limit => 100) + s.words_to_highlight.should == ["The", "doctor", "s", "patient"] + end + + it "should handle non-ascii characters" do + s = ActsAsXapian::Search.new([PublicBody], "adatigénylés words tag:mice", :limit => 100) + s.words_to_highlight.should == ["adatigénylés", "words"] + end + +end |