aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models
diff options
context:
space:
mode:
Diffstat (limited to 'spec/models')
-rw-r--r--spec/models/customstates.rb20
-rw-r--r--spec/models/incoming_message_spec.rb87
-rw-r--r--spec/models/info_request_spec.rb8
-rw-r--r--spec/models/outgoing_mailer_spec.rb1
-rw-r--r--spec/models/outgoing_message_spec.rb7
-rw-r--r--spec/models/public_body_spec.rb156
-rw-r--r--spec/models/track_mailer_spec.rb40
-rw-r--r--spec/models/track_thing_spec.rb10
-rw-r--r--spec/models/user_spec.rb23
-rw-r--r--spec/models/xapian_spec.rb53
10 files changed, 321 insertions, 84 deletions
diff --git a/spec/models/customstates.rb b/spec/models/customstates.rb
index 406d4ead9..3488e6730 100644
--- a/spec/models/customstates.rb
+++ b/spec/models/customstates.rb
@@ -5,16 +5,6 @@ module InfoRequestCustomStates
end
# Mixin methods for InfoRequest
- def theme_display_status(status)
- if status == 'deadline_extended'
- _("Deadline extended.")
- elsif status == 'wrong_response'
- _("Wrong Response.")
- else
- raise _("unknown status ") + status
- end
- end
-
def theme_calculate_status
return 'waiting_classification' if self.awaiting_description
waiting_response = self.described_state == "waiting_response" || self.described_state == "deadline_extended"
@@ -41,6 +31,16 @@ module InfoRequestCustomStates
end
module ClassMethods
+ def theme_display_status(status)
+ if status == 'deadline_extended'
+ _("Deadline extended.")
+ elsif status == 'wrong_response'
+ _("Wrong Response.")
+ else
+ raise _("unknown status ") + status
+ end
+ end
+
def theme_extra_states
return ['deadline_extended',
'wrong_response']
diff --git a/spec/models/incoming_message_spec.rb b/spec/models/incoming_message_spec.rb
index 183a258af..a8411bc34 100644
--- a/spec/models/incoming_message_spec.rb
+++ b/spec/models/incoming_message_spec.rb
@@ -1,7 +1,7 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe IncomingMessage, " when dealing with incoming mail" do
- fixtures :incoming_messages, :raw_emails, :info_requests
+ fixtures :users, :raw_emails, :public_bodies, :public_body_translations, :info_requests, :incoming_messages
before(:each) do
@im = incoming_messages(:useless_incoming_message)
@@ -17,6 +17,27 @@ describe IncomingMessage, " when dealing with incoming mail" do
TMail::Address.parse(em)
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|
+ message = File.read(file)
+ parsed = IncomingMessage.remove_quoted_sections(message)
+ expected = File.read("#{file}.expected")
+ parsed.should include(expected)
+ end
+ end
+
+ it "should fold multiline sections" do
+ {
+ "foo\n--------\nconfidential" => "foo\nFOLDED_QUOTED_SECTION\n", # basic test
+ "foo\n--------\nbar - confidential" => "foo\nFOLDED_QUOTED_SECTION\n", # allow scorechar inside folded section
+ "foo\n--------\nbar\n--------\nconfidential" => "foo\n--------\nbar\nFOLDED_QUOTED_SECTION\n", # don't assume that anything after a score is a folded section
+ "foo\n--------\nbar\n--------\nconfidential\n--------\nrest" => "foo\n--------\nbar\nFOLDED_QUOTED_SECTION\nrest", # don't assume that a folded section continues to the end of the message
+ "foo\n--------\nbar\n- - - - - - - -\nconfidential\n--------\nrest" => "foo\n--------\nbar\nFOLDED_QUOTED_SECTION\nrest", # allow spaces in the score
+ }.each do |input,output|
+ IncomingMessage.remove_quoted_sections(input).should == output
+ end
+ end
+
end
describe IncomingMessage, "when parsing HTML mail" do
@@ -79,11 +100,21 @@ describe IncomingMessage, " folding quoted parts of emails" do
end
describe IncomingMessage, " checking validity to reply to" do
- def test_email(email, result)
+ def test_email(result, email, return_path, autosubmitted)
@address = mock(TMail::Address)
@address.stub!(:spec).and_return(email)
+
+ @return_path = mock(TMail::ReturnPathHeader)
+ @return_path.stub!(:addr).and_return(return_path)
+
+ @autosubmitted = mock(TMail::KeywordsHeader)
+ @autosubmitted.stub!(:keys).and_return(autosubmitted)
+
@mail = mock(TMail::Mail)
@mail.stub!(:from_addrs).and_return( [ @address ] )
+ @mail.stub!(:[]).with("return-path").and_return(@return_path)
+ @mail.stub!(:[]).with("auto-submitted").and_return(@autosubmitted)
+
@incoming_message = IncomingMessage.new()
@incoming_message.stub!(:mail).and_return(@mail)
@@ -91,33 +122,69 @@ describe IncomingMessage, " checking validity to reply to" do
end
it "says a valid email is fine" do
- test_email("team@mysociety.org", true)
+ test_email(true, "team@mysociety.org", nil, [])
end
it "says postmaster email is bad" do
- test_email("postmaster@mysociety.org", false)
+ test_email(false, "postmaster@mysociety.org", nil, [])
end
it "says Mailer-Daemon email is bad" do
- test_email("Mailer-Daemon@mysociety.org", false)
+ test_email(false, "Mailer-Daemon@mysociety.org", nil, [])
end
it "says case mangled MaIler-DaemOn email is bad" do
- test_email("MaIler-DaemOn@mysociety.org", false)
+ test_email(false, "MaIler-DaemOn@mysociety.org", nil, [])
end
it "says Auto_Reply email is bad" do
- test_email("Auto_Reply@mysociety.org", false)
+ test_email(false, "Auto_Reply@mysociety.org", nil, [])
end
it "says DoNotReply email is bad" do
- test_email("DoNotReply@tube.tfl.gov.uk", false)
+ test_email(false, "DoNotReply@tube.tfl.gov.uk", nil, [])
+ end
+
+ it "says a filled-out return-path is fine" do
+ test_email(true, "team@mysociety.org", "Return-path: <foo@baz.com>", [])
+ end
+
+ it "says an empty return-path is bad" do
+ test_email(false, "team@mysociety.org", "<>", [])
+ end
+
+ it "says an auto-submitted keyword is bad" do
+ test_email(false, "team@mysociety.org", nil, ["auto-replied"])
+ end
+
+end
+
+describe IncomingMessage, " checking validity to reply to with real emails" do
+ fixtures :users, :raw_emails, :public_bodies, :public_body_translations, :info_requests, :incoming_messages
+
+ after(:all) do
+ ActionMailer::Base.deliveries.clear
+ end
+ it "should allow a reply to plain emails" do
+ ir = info_requests(:fancy_dog_request)
+ receive_incoming_mail('incoming-request-plain.email', ir.incoming_email)
+ ir.incoming_messages[1].valid_to_reply_to?.should == true
+ end
+ it "should not allow a reply to emails with empty return-paths" do
+ ir = info_requests(:fancy_dog_request)
+ receive_incoming_mail('empty-return-path.email', ir.incoming_email)
+ ir.incoming_messages[1].valid_to_reply_to?.should == false
+ end
+ it "should not allow a reply to emails with autoresponse headers" do
+ ir = info_requests(:fancy_dog_request)
+ receive_incoming_mail('autoresponse-header.email', ir.incoming_email)
+ ir.incoming_messages[1].valid_to_reply_to?.should == false
end
end
describe IncomingMessage, " when censoring data" do
- fixtures :incoming_messages, :raw_emails, :public_bodies, :public_body_translations, :info_requests, :users
+ fixtures :users, :raw_emails, :public_bodies, :public_body_translations, :info_requests, :incoming_messages
before(:each) do
@test_data = "There was a mouse called Stilton, he wished that he was blue."
@@ -227,7 +294,7 @@ describe IncomingMessage, " when censoring data" do
end
describe IncomingMessage, " when censoring whole users" do
- fixtures :incoming_messages, :raw_emails, :public_bodies, :public_body_translations, :info_requests, :users
+ fixtures :users, :raw_emails, :public_bodies, :public_body_translations, :info_requests, :incoming_messages
before(:each) do
@test_data = "There was a mouse called Stilton, he wished that he was blue."
diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb
index b82052a0f..d50d6dace 100644
--- a/spec/models/info_request_spec.rb
+++ b/spec/models/info_request_spec.rb
@@ -3,7 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe InfoRequest do
describe "guessing a request from an email" do
- fixtures :info_requests, :public_bodies, :incoming_messages, :raw_emails
+ fixtures :public_bodies, :info_requests, :raw_emails, :incoming_messages
before(:each) do
@im = incoming_messages(:useless_incoming_message)
@@ -74,7 +74,7 @@ describe InfoRequest do
describe " when emailing" do
- fixtures :info_requests, :info_request_events, :outgoing_messages, :public_bodies, :public_body_translations, :users, :comments
+ fixtures :public_bodies, :public_body_translations, :users, :info_requests, :outgoing_messages, :info_request_events, :comments
before do
@info_request = info_requests(:fancy_dog_request)
@@ -154,7 +154,7 @@ describe InfoRequest do
end
describe "when calculating the status" do
- fixtures :info_requests, :info_request_events, :holidays, :public_bodies, :public_body_translations, :outgoing_messages
+ fixtures :holidays, :public_bodies, :public_body_translations, :info_requests, :outgoing_messages, :info_request_events
before do
@ir = info_requests(:naughty_chicken_request)
@@ -231,7 +231,7 @@ describe InfoRequest do
describe "when calculating the status for a school" do
- fixtures :info_requests, :info_request_events, :holidays, :public_bodies, :public_body_translations
+ fixtures :holidays, :public_bodies, :public_body_translations, :info_requests, :info_request_events
before do
@ir = info_requests(:naughty_chicken_request)
diff --git a/spec/models/outgoing_mailer_spec.rb b/spec/models/outgoing_mailer_spec.rb
index d7587cb41..c96a3fb74 100644
--- a/spec/models/outgoing_mailer_spec.rb
+++ b/spec/models/outgoing_mailer_spec.rb
@@ -112,7 +112,6 @@ describe OutgoingMailer, "when working out follow up subjects" do
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]
- puts im.raw_email.data
om = outgoing_messages(:useless_outgoing_message)
om.incoming_message_followup = im
diff --git a/spec/models/outgoing_message_spec.rb b/spec/models/outgoing_message_spec.rb
index a9ef57b4f..1956c4d73 100644
--- a/spec/models/outgoing_message_spec.rb
+++ b/spec/models/outgoing_message_spec.rb
@@ -1,7 +1,10 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe OutgoingMessage, " when making an outgoing message" do
+ fixtures :outgoing_messages, :info_requests, :incoming_messages, :public_bodies, :public_body_translations
+
before do
+ @om = outgoing_messages(:useless_outgoing_message)
@outgoing_message = OutgoingMessage.new({
:status => 'ready',
:message_type => 'initial_request',
@@ -27,6 +30,10 @@ describe OutgoingMessage, " when making an outgoing message" do
it "should include email addresses in outgoing messages" do
@outgoing_message.body.should include("foo@bar.com")
end
+
+ it "should work out a salutation" do
+ @om.get_salutation.should == "Dear Geraldine Quango,"
+ end
end
diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb
index ec84cbe65..33ab8ffdb 100644
--- a/spec/models/public_body_spec.rb
+++ b/spec/models/public_body_spec.rb
@@ -210,19 +210,52 @@ describe PublicBody, "when searching" do
body.id.should == 3
body.class.to_s.should == 'PublicBody'
end
+
+ it "should cope with same url_name across multiple locales" do
+ PublicBody.with_locale(:es) do
+ # use the unique spanish name to retrieve and edit
+ body = PublicBody.find_by_url_name_with_historic('etgq')
+ body.short_name = 'tgq' # Same as english version
+ body.save!
+
+ # now try to retrieve it
+ body = PublicBody.find_by_url_name_with_historic('tgq')
+ body.id.should == public_bodies(:geraldine_public_body).id
+ body.name.should == "El A Geraldine Quango"
+ end
+ end
end
describe PublicBody, " when loading CSV files" do
+ before(:each) do
+ # InternalBody is created the first time it's accessed, which happens sometimes during imports,
+ # depending on the tag used. By accessing it here before every test, it doesn't disturb our checks later on
+ PublicBody.internal_admin_body
+ end
+
+ it "should import even if no email is provided" do
+ errors, notes = PublicBody.import_csv("1,aBody", '', 'replace', true, 'someadmin') # true means dry run
+ errors.should == []
+ notes.size.should == 2
+ notes.should == [
+ "line 1: creating new authority 'aBody' (locale: en):\n\t{\"name\":\"aBody\"}",
+ "Notes: Some bodies are in database, but not in CSV file:\n Department for Humpadinking\n Geraldine Quango\nYou may want to delete them manually.\n"
+ ]
+ end
+
it "should do a dry run successfully" do
original_count = PublicBody.count
csv_contents = load_file_fixture("fake-authority-type.csv")
- errors, notes = PublicBody.import_csv(csv_contents, 'fake', true, 'someadmin') # true means dry run
+ errors, notes = PublicBody.import_csv(csv_contents, '', 'replace', true, 'someadmin') # true means dry run
errors.should == []
- notes.size.should == 3
- notes.should == ["line 1: new authority 'North West Fake Authority' with email north_west_foi@localhost",
- "line 2: new authority 'Scottish Fake Authority' with email scottish_foi@localhost",
- "line 3: new authority 'Fake Authority of Northern Ireland' with email ni_foi@localhost"]
+ notes.size.should == 4
+ notes.should == [
+ "line 1: creating new authority 'North West Fake Authority' (locale: en):\n\t\{\"name\":\"North West Fake Authority\",\"request_email\":\"north_west_foi@localhost\"\}",
+ "line 2: creating new authority 'Scottish Fake Authority' (locale: en):\n\t\{\"name\":\"Scottish Fake Authority\",\"request_email\":\"scottish_foi@localhost\"\}",
+ "line 3: creating new authority 'Fake Authority of Northern Ireland' (locale: en):\n\t\{\"name\":\"Fake Authority of Northern Ireland\",\"request_email\":\"ni_foi@localhost\"\}",
+ "Notes: Some bodies are in database, but not in CSV file:\n Department for Humpadinking\n Geraldine Quango\nYou may want to delete them manually.\n"
+ ]
PublicBody.count.should == original_count
end
@@ -231,44 +264,105 @@ describe PublicBody, " when loading CSV files" do
original_count = PublicBody.count
csv_contents = load_file_fixture("fake-authority-type.csv")
- errors, notes = PublicBody.import_csv(csv_contents, 'fake', false, 'someadmin') # false means real run
+ errors, notes = PublicBody.import_csv(csv_contents, '', 'replace', false, 'someadmin') # false means real run
errors.should == []
- notes.size.should == 3
- notes.should == ["line 1: new authority 'North West Fake Authority' with email north_west_foi@localhost",
- "line 2: new authority 'Scottish Fake Authority' with email scottish_foi@localhost",
- "line 3: new authority 'Fake Authority of Northern Ireland' with email ni_foi@localhost"]
+ notes.size.should == 4
+ notes.should == [
+ "line 1: creating new authority 'North West Fake Authority' (locale: en):\n\t\{\"name\":\"North West Fake Authority\",\"request_email\":\"north_west_foi@localhost\"\}",
+ "line 2: creating new authority 'Scottish Fake Authority' (locale: en):\n\t\{\"name\":\"Scottish Fake Authority\",\"request_email\":\"scottish_foi@localhost\"\}",
+ "line 3: creating new authority 'Fake Authority of Northern Ireland' (locale: en):\n\t\{\"name\":\"Fake Authority of Northern Ireland\",\"request_email\":\"ni_foi@localhost\"\}",
+ "Notes: Some bodies are in database, but not in CSV file:\n Department for Humpadinking\n Geraldine Quango\nYou may want to delete them manually.\n"
+ ]
PublicBody.count.should == original_count + 3
end
+ it "should do imports without a tag successfully" do
+ original_count = PublicBody.count
+
+ csv_contents = load_file_fixture("fake-authority-type.csv")
+ errors, notes = PublicBody.import_csv(csv_contents, '', 'replace', false, 'someadmin') # false means real run
+ errors.should == []
+ notes.size.should == 4
+ notes.should == [
+ "line 1: creating new authority 'North West Fake Authority' (locale: en):\n\t\{\"name\":\"North West Fake Authority\",\"request_email\":\"north_west_foi@localhost\"\}",
+ "line 2: creating new authority 'Scottish Fake Authority' (locale: en):\n\t\{\"name\":\"Scottish Fake Authority\",\"request_email\":\"scottish_foi@localhost\"\}",
+ "line 3: creating new authority 'Fake Authority of Northern Ireland' (locale: en):\n\t\{\"name\":\"Fake Authority of Northern Ireland\",\"request_email\":\"ni_foi@localhost\"\}",
+ "Notes: Some bodies are in database, but not in CSV file:\n Department for Humpadinking\n Geraldine Quango\nYou may want to delete them manually.\n"
+ ]
+ PublicBody.count.should == original_count + 3
+ end
+
it "should handle a field list and fields out of order" do
original_count = PublicBody.count
csv_contents = load_file_fixture("fake-authority-type-with-field-names.csv")
- errors, notes = PublicBody.import_csv(csv_contents, 'fake', true, 'someadmin') # true means dry run
+ errors, notes = PublicBody.import_csv(csv_contents, '', 'replace', true, 'someadmin') # true means dry run
errors.should == []
- notes.size.should == 3
- notes.should == ["line 2: new authority 'North West Fake Authority' with email north_west_foi@localhost",
- "line 3: new authority 'Scottish Fake Authority' with email scottish_foi@localhost",
- "line 4: new authority 'Fake Authority of Northern Ireland' with email ni_foi@localhost"]
+ notes.size.should == 4
+ notes.should == [
+ "line 2: creating new authority 'North West Fake Authority' (locale: en):\n\t\{\"name\":\"North West Fake Authority\",\"request_email\":\"north_west_foi@localhost\",\"home_page\":\"http://northwest.org\"\}",
+ "line 3: creating new authority 'Scottish Fake Authority' (locale: en):\n\t\{\"tag_string\":\"scottish\",\"name\":\"Scottish Fake Authority\",\"request_email\":\"scottish_foi@localhost\",\"home_page\":\"http://scottish.org\"\}",
+ "line 4: creating new authority 'Fake Authority of Northern Ireland' (locale: en):\n\t\{\"tag_string\":\"fake aTag\",\"name\":\"Fake Authority of Northern Ireland\",\"request_email\":\"ni_foi@localhost\"\}",
+ "Notes: Some bodies are in database, but not in CSV file:\n Department for Humpadinking\n Geraldine Quango\nYou may want to delete them manually.\n"
+ ]
PublicBody.count.should == original_count
end
+
+ it "should import tags successfully when the import tag is not set" do
+ csv_contents = load_file_fixture("fake-authority-type-with-field-names.csv")
+ errors, notes = PublicBody.import_csv(csv_contents, '', 'replace', false, 'someadmin') # false means real run
+
+ PublicBody.find_by_name('North West Fake Authority').tag_array_for_search.should == []
+ PublicBody.find_by_name('Scottish Fake Authority').tag_array_for_search.should == ['scottish']
+ PublicBody.find_by_name('Fake Authority of Northern Ireland').tag_array_for_search.should == ['fake', 'aTag']
+
+ # Import again to check the 'add' tag functionality works
+ new_tags_file = load_file_fixture('fake-authority-add-tags.rb')
+ errors, notes = PublicBody.import_csv(new_tags_file, '', 'add', false, 'someadmin') # false means real run
+
+ # Check tags were added successfully
+ PublicBody.find_by_name('North West Fake Authority').tag_array_for_search.should == ['aTag']
+ PublicBody.find_by_name('Scottish Fake Authority').tag_array_for_search.should == ['scottish', 'aTag']
+ PublicBody.find_by_name('Fake Authority of Northern Ireland').tag_array_for_search.should == ['fake', 'aTag']
+ end
+
+ it "should import tags successfully when the import tag is set" do
+ csv_contents = load_file_fixture("fake-authority-type-with-field-names.csv")
+ errors, notes = PublicBody.import_csv(csv_contents, 'fake', 'add', false, 'someadmin') # false means real run
+
+ # Check new bodies were imported successfully
+ PublicBody.find_by_name('North West Fake Authority').tag_array_for_search.should == ['fake']
+ PublicBody.find_by_name('Scottish Fake Authority').tag_array_for_search.should == ['scottish', 'fake']
+ PublicBody.find_by_name('Fake Authority of Northern Ireland').tag_array_for_search.should == ['fake', 'aTag']
+
+ # Import again to check the 'replace' tag functionality works
+ new_tags_file = load_file_fixture('fake-authority-add-tags.rb')
+ errors, notes = PublicBody.import_csv(new_tags_file, 'fake', 'replace', false, 'someadmin') # false means real run
+
+ # Check tags were added successfully
+ PublicBody.find_by_name('North West Fake Authority').tag_array_for_search.should == ['aTag']
+ PublicBody.find_by_name('Scottish Fake Authority').tag_array_for_search.should == ['aTag']
+ PublicBody.find_by_name('Fake Authority of Northern Ireland').tag_array_for_search.should == ['fake', 'aTag']
+ end
it "should create bodies with names in multiple locales" do
original_count = PublicBody.count
csv_contents = load_file_fixture("fake-authority-type-with-field-names.csv")
- errors, notes = PublicBody.import_csv(csv_contents, 'fake', false, 'someadmin', ['es'])
+ errors, notes = PublicBody.import_csv(csv_contents, '', 'replace', false, 'someadmin', [:en, :es])
errors.should == []
- notes.size.should == 6
+ notes.size.should == 7
notes.should == [
- "line 2: new authority 'North West Fake Authority' with email north_west_foi@localhost",
- "line 2: (aka 'Autoridad del Nordeste' in locale es)",
- "line 3: new authority 'Scottish Fake Authority' with email scottish_foi@localhost",
- "line 3: (aka 'Autoridad Escocesa' in locale es)",
- "line 4: new authority 'Fake Authority of Northern Ireland' with email ni_foi@localhost",
- "line 4: (aka 'Autoridad Irlandesa' in locale es)"]
+ "line 2: creating new authority 'North West Fake Authority' (locale: en):\n\t{\"name\":\"North West Fake Authority\",\"request_email\":\"north_west_foi@localhost\",\"home_page\":\"http://northwest.org\"}",
+ "line 2: creating new authority 'North West Fake Authority' (locale: es):\n\t{\"name\":\"Autoridad del Nordeste\"}",
+ "line 3: creating new authority 'Scottish Fake Authority' (locale: en):\n\t{\"tag_string\":\"scottish\",\"name\":\"Scottish Fake Authority\",\"request_email\":\"scottish_foi@localhost\",\"home_page\":\"http://scottish.org\"}",
+ "line 3: creating new authority 'Scottish Fake Authority' (locale: es):\n\t{\"name\":\"Autoridad Escocesa\"}",
+ "line 4: creating new authority 'Fake Authority of Northern Ireland' (locale: en):\n\t{\"tag_string\":\"fake aTag\",\"name\":\"Fake Authority of Northern Ireland\",\"request_email\":\"ni_foi@localhost\"}",
+ "line 4: creating new authority 'Fake Authority of Northern Ireland' (locale: es):\n\t{\"name\":\"Autoridad Irlandesa\"}",
+ "Notes: Some bodies are in database, but not in CSV file:\n Department for Humpadinking\n Geraldine Quango\nYou may want to delete them manually.\n"
+ ]
PublicBody.count.should == original_count + 3
@@ -283,12 +377,18 @@ describe PublicBody, " when loading CSV files" do
original_count = PublicBody.count
csv_contents = load_file_fixture("fake-authority-type-with-field-names.csv")
- errors, notes = PublicBody.import_csv(csv_contents, 'fake', true, 'someadmin', ['xx']) # true means dry run
+ # Depending on the runtime environment (Ruby version? OS?) the list of available locales
+ # is made of strings or symbols, so we use 'en' here as a string to test both scenarios.
+ # See https://github.com/sebbacon/alaveteli/issues/193
+ errors, notes = PublicBody.import_csv(csv_contents, '', 'replace', true, 'someadmin', ['en', :xx]) # true means dry run
errors.should == []
- notes.size.should == 3
- notes.should == ["line 2: new authority 'North West Fake Authority' with email north_west_foi@localhost",
- "line 3: new authority 'Scottish Fake Authority' with email scottish_foi@localhost",
- "line 4: new authority 'Fake Authority of Northern Ireland' with email ni_foi@localhost"]
+ notes.size.should == 4
+ notes.should == [
+ "line 2: creating new authority 'North West Fake Authority' (locale: en):\n\t{\"name\":\"North West Fake Authority\",\"request_email\":\"north_west_foi@localhost\",\"home_page\":\"http://northwest.org\"}",
+ "line 3: creating new authority 'Scottish Fake Authority' (locale: en):\n\t{\"tag_string\":\"scottish\",\"name\":\"Scottish Fake Authority\",\"request_email\":\"scottish_foi@localhost\",\"home_page\":\"http://scottish.org\"}",
+ "line 4: creating new authority 'Fake Authority of Northern Ireland' (locale: en):\n\t{\"tag_string\":\"fake aTag\",\"name\":\"Fake Authority of Northern Ireland\",\"request_email\":\"ni_foi@localhost\"}",
+ "Notes: Some bodies are in database, but not in CSV file:\n Department for Humpadinking\n Geraldine Quango\nYou may want to delete them manually.\n"
+ ]
PublicBody.count.should == original_count
end
diff --git a/spec/models/track_mailer_spec.rb b/spec/models/track_mailer_spec.rb
index 828904d02..67a64ee10 100644
--- a/spec/models/track_mailer_spec.rb
+++ b/spec/models/track_mailer_spec.rb
@@ -21,7 +21,9 @@ describe TrackMailer do
@user = mock_model(User, :no_xapian_reindex= => false,
:last_daily_track_email= => true,
:save! => true,
- :url_name => 'test-name')
+ :url_name => 'test-name',
+ :get_locale => 'en',
+ :should_be_emailed? => true)
User.stub!(:find).and_return([@user])
@user.stub!(:no_xapian_reindex=)
end
@@ -43,6 +45,9 @@ describe TrackMailer do
@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
@@ -109,6 +114,39 @@ describe TrackMailer do
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!(: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 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
diff --git a/spec/models/track_thing_spec.rb b/spec/models/track_thing_spec.rb
index 6b9cd6d4a..1a0324a78 100644
--- a/spec/models/track_thing_spec.rb
+++ b/spec/models/track_thing_spec.rb
@@ -28,5 +28,15 @@ describe TrackThing, "when tracking changes" do
found_track.should == @track_thing
end
+ it "will make some sane descriptions of search-based tracks" do
+ tests = [['bob variety:user', "users matching text 'bob'"],
+ ['bob (variety:sent OR variety:followup_sent OR variety:response OR variety:comment) (latest_status:successful OR latest_status:partially_successful OR latest_status:rejected OR latest_status:not_held)', "requests which are successful or unsuccessful or comments matching text 'bob'"],
+ ['(latest_status:waiting_response OR latest_status:waiting_clarification OR waiting_classification:true)', 'requests which are awaiting a response']]
+ for query, description in tests
+ track_thing = TrackThing.create_track_for_search_query(query)
+ track_thing.track_query_description.should == description
+ end
+ end
+
end
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index ee6916ffc..751a61060 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -282,3 +282,26 @@ describe User, "when setting a profile photo" do
# end
end
+describe User, "when unconfirmed" do
+ fixtures :users
+
+ before do
+ @user = users(:unconfirmed_user)
+ end
+
+ it "should not be emailed" do
+ @user.should_be_emailed?.should be_false
+ end
+end
+
+describe User, "when emails have bounced" do
+ fixtures :users
+
+ it "should record bounces" do
+ User.record_bounce_for_email("bob@localhost", "The reason we think the email bounced (e.g. a bounce message)")
+
+ user = User.find_user_by_email("bob@localhost")
+ user.email_bounced_at.should_not be_nil
+ user.email_bounce_message.should == "The reason we think the email bounced (e.g. a bounce message)"
+ end
+end
diff --git a/spec/models/xapian_spec.rb b/spec/models/xapian_spec.rb
index c08b3e5a4..8e8616df5 100644
--- a/spec/models/xapian_spec.rb
+++ b/spec/models/xapian_spec.rb
@@ -2,12 +2,9 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe User, " when indexing users with Xapian" do
fixtures :users
-
- before(:all) do
- rebuild_xapian_index
- end
-
+
it "should search by name" do
+ rebuild_xapian_index
# 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.results.size.should == 1
@@ -37,27 +34,29 @@ end
describe PublicBody, " when indexing public bodies with Xapian" do
fixtures :public_bodies, :public_body_translations, :incoming_messages, :outgoing_messages, :raw_emails, :comments, :info_requests
-
- before(:all) do
- rebuild_xapian_index
- end
before(:each) do
load_raw_emails_data(raw_emails)
end
it "should search index the main name field" do
+ rebuild_xapian_index
+
xapian_object = InfoRequest.full_search([PublicBody], "humpadinking", 'created_at', true, nil, 100, 1)
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
+ rebuild_xapian_index
+
xapian_object = InfoRequest.full_search([PublicBody], "albatross", 'created_at', true, nil, 100, 1)
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
+ rebuild_xapian_index
+
xapian_object = InfoRequest.full_search([PublicBody], "albatross", 'created_at', true, nil, 100, 1)
xapian_object.results.size.should == 1
xapian_object.results[0][:model].should == public_bodies(:humpadink_public_body)
@@ -74,20 +73,19 @@ end
describe PublicBody, " when indexing requests by body they are to" do
fixtures :public_bodies, :public_body_translations, :info_request_events, :info_requests, :raw_emails, :comments
- before(:all) do
- rebuild_xapian_index
- end
before(:each) do
load_raw_emails_data(raw_emails)
end
it "should find requests to the body" do
+ rebuild_xapian_index
xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_from:tgq", 'created_at', true, nil, 100, 1)
xapian_object.results.size.should == 4
end
it "should update index correctly when URL name of body changes" do
# initial search
+ rebuild_xapian_index
xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_from:tgq", 'created_at', true, nil, 100, 1)
xapian_object.results.size.should == 4
models_found_before = xapian_object.results.map { |x| x[:model] }
@@ -112,6 +110,8 @@ describe PublicBody, " when indexing requests by body they are to" do
# if you index via the Xapian TermGenerator, it ignores terms of this length,
# this checks we're using Document:::add_term() instead
it "should work with URL names that are longer than 64 characters" do
+ rebuild_xapian_index
+
# change the URL name of the body
body = public_bodies(:geraldine_public_body)
body.short_name = 'The Uncensored, Complete Name of the Quasi-Autonomous Public Body Also Known As Geraldine'
@@ -132,20 +132,18 @@ end
describe User, " when indexing requests by user they are from" do
fixtures :users, :info_request_events, :info_requests, :incoming_messages, :outgoing_messages, :raw_emails, :comments
-
- before(:all) do
- rebuild_xapian_index
- end
before(:each) do
load_raw_emails_data(raw_emails)
end
it "should find requests from the user" do
+ rebuild_xapian_index
xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_by:bob_smith", 'created_at', true, nil, 100, 1)
xapian_object.results.size.should == 4
end
it "should find just the sent message events from a particular user" do
+ rebuild_xapian_index
# 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.results.size.should == 2
@@ -154,6 +152,7 @@ describe User, " when indexing requests by user they are from" do
end
it "should not find it when one of the request's users is changed" do
+ rebuild_xapian_index
silly_user = users(:silly_name_user)
naughty_chicken_request = info_requests(:naughty_chicken_request)
naughty_chicken_request.user = silly_user
@@ -168,6 +167,8 @@ describe User, " when indexing requests by user they are from" do
end
it "should not get confused searching for requests when one user has a name which has same stem as another" do
+ rebuild_xapian_index
+
bob_smith_user = users(:bob_smith_user)
bob_smith_user.name = "John King"
bob_smith_user.url_name.should == 'john_king'
@@ -218,21 +219,19 @@ end
describe User, " when indexing comments by user they are by" do
fixtures :users, :info_request_events, :info_requests, :comments, :incoming_messages, :outgoing_messages, :raw_emails, :comments
-
- before(:all) do
- rebuild_xapian_index
- end
before(:each) do
load_raw_emails_data(raw_emails)
end
it "should find requests from the user" do
+ rebuild_xapian_index
xapian_object = InfoRequest.full_search([InfoRequestEvent], "commented_by:silly_emnameem", 'created_at', true, nil, 100, 1)
xapian_object.results.size.should == 1
end
it "should update index correctly when URL name of user changes" do
# initial search
+ rebuild_xapian_index
xapian_object = InfoRequest.full_search([InfoRequestEvent], "commented_by:silly_emnameem", 'created_at', true, nil, 100, 1)
xapian_object.results.size.should == 1
models_found_before = xapian_object.results.map { |x| x[:model] }
@@ -257,15 +256,12 @@ end
describe InfoRequest, " when indexing requests by their title" do
fixtures :info_request_events, :info_requests, :incoming_messages, :raw_emails, :comments
-
- before(:all) do
- rebuild_xapian_index
- end
before(:each) do
load_raw_emails_data(raw_emails)
end
it "should find events for the request" do
+ rebuild_xapian_index
xapian_object = InfoRequest.full_search([InfoRequestEvent], "request:how_much_public_money_is_wasted_o", 'created_at', true, nil, 100, 1)
xapian_object.results.size.should == 1
xapian_object.results[0][:model] == info_request_events(:silly_outgoing_message_event)
@@ -273,6 +269,7 @@ describe InfoRequest, " when indexing requests by their title" do
it "should update index correctly when URL title of request changes" do
# change the URL name of the body
+ rebuild_xapian_index
ir = info_requests(:naughty_chicken_request)
ir.title = 'Really naughty'
ir.save!
@@ -290,14 +287,12 @@ end
describe InfoRequest, " when indexing requests by tag" do
fixtures :info_request_events, :info_requests, :incoming_messages, :raw_emails, :comments
- before(:all) do
- rebuild_xapian_index
- end
before(:each) do
load_raw_emails_data(raw_emails)
end
it "should find request by tag, even when changes" do
+ rebuild_xapian_index
ir = info_requests(:naughty_chicken_request)
ir.tag_string = 'bunnyrabbit'
ir.save!
@@ -314,14 +309,12 @@ end
describe PublicBody, " when indexing authorities by tag" do
fixtures :public_bodies, :public_body_translations, :incoming_messages, :outgoing_messages, :raw_emails, :comments
- before(:all) do
- rebuild_xapian_index
- end
before(:each) do
load_raw_emails_data(raw_emails)
end
it "should find request by tag, even when changes" do
+ rebuild_xapian_index
body = public_bodies(:geraldine_public_body)
body.tag_string = 'mice:3'
body.save!