diff options
Diffstat (limited to 'spec/lib')
-rw-r--r-- | spec/lib/health_checks/checks/days_ago_check_spec.rb | 66 | ||||
-rw-r--r-- | spec/lib/health_checks/health_checkable_spec.rb | 128 | ||||
-rw-r--r-- | spec/lib/health_checks/health_checks_spec.rb | 77 | ||||
-rw-r--r-- | spec/lib/mail_handler/backends/mail_backend_spec.rb | 145 | ||||
-rw-r--r-- | spec/lib/mail_handler/mail_handler_spec.rb | 6 | ||||
-rw-r--r-- | spec/lib/public_body_categories_spec.rb | 42 |
6 files changed, 422 insertions, 42 deletions
diff --git a/spec/lib/health_checks/checks/days_ago_check_spec.rb b/spec/lib/health_checks/checks/days_ago_check_spec.rb new file mode 100644 index 000000000..33b4642cd --- /dev/null +++ b/spec/lib/health_checks/checks/days_ago_check_spec.rb @@ -0,0 +1,66 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper') + +describe HealthChecks::Checks::DaysAgoCheck do + include HealthChecks::Checks + + it { should be_kind_of(HealthChecks::HealthCheckable) } + + it 'defaults to comparing to one day ago' do + check = HealthChecks::Checks::DaysAgoCheck.new + expect(check.days).to eq(1) + end + + it 'accepts a custom number of days' do + check = HealthChecks::Checks::DaysAgoCheck.new(:days => 4) + expect(check.days).to eq(4) + end + + describe :check do + + it 'is successful if the subject is in the last day' do + check = HealthChecks::Checks::DaysAgoCheck.new { Time.now } + expect(check.check).to be_true + end + + it 'fails if the subject is over a day ago' do + check = HealthChecks::Checks::DaysAgoCheck.new { 2.days.ago } + expect(check.check).to be_false + end + + end + + describe :failure_message do + + it 'includes the check subject in the default message' do + subject = 2.days.ago + check = HealthChecks::Checks::DaysAgoCheck.new { subject } + expect(check.failure_message).to include(subject.to_s) + end + + it 'includes the check subject in a custom message' do + params = { :failure_message => 'This check failed' } + subject = 2.days.ago + check = HealthChecks::Checks::DaysAgoCheck.new(params) { subject } + expect(check.failure_message).to include(subject.to_s) + end + + end + + describe :success_message do + + it 'includes the check subject in the default message' do + subject = Time.now + check = HealthChecks::Checks::DaysAgoCheck.new { subject } + expect(check.failure_message).to include(subject.to_s) + end + + it 'includes the check subject in a custom message' do + params = { :success_message => 'This check succeeded' } + subject = Time.now + check = HealthChecks::Checks::DaysAgoCheck.new(params) { subject } + expect(check.success_message).to include(subject.to_s) + end + + end + +end diff --git a/spec/lib/health_checks/health_checkable_spec.rb b/spec/lib/health_checks/health_checkable_spec.rb new file mode 100644 index 000000000..abfeb5c21 --- /dev/null +++ b/spec/lib/health_checks/health_checkable_spec.rb @@ -0,0 +1,128 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +describe HealthChecks::HealthCheckable do + + before(:each) do + class MockCheck + include HealthChecks::HealthCheckable + end + @subject = MockCheck.new + end + + describe :initialize do + + it 'allows a custom failure message to be set' do + @subject = MockCheck.new(:failure_message => 'F') + expect(@subject.failure_message).to eq('F') + end + + it 'allows a custom success message to be set' do + @subject = MockCheck.new(:success_message => 'S') + expect(@subject.success_message).to eq('S') + end + + end + + describe :name do + + it 'returns the name of the check' do + expect(@subject.name).to eq('MockCheck') + end + + end + + describe :check do + + it 'is intended to be overridden by the includer' do + expect{ @subject.check }.to raise_error(NotImplementedError) + end + + end + + describe :ok? do + + it 'returns true if the check was successful' do + @subject.stub(:check => true) + expect(@subject.ok?).to be_true + end + + it 'returns false if the check failed' do + @subject.stub(:check => false) + expect(@subject.ok?).to be_false + end + + end + + describe :failure_message do + + it 'returns a default message if one has not been set' do + expect(@subject.failure_message).to eq('Failed') + end + + end + + describe :failure_message= do + + it 'allows a custom failure message to be set' do + @subject.failure_message = 'F' + expect(@subject.failure_message).to eq('F') + end + + end + + describe :success_message do + + it 'returns a default message if one has not been set' do + expect(@subject.success_message).to eq('Success') + end + + end + + describe :success_message= do + + it 'allows a custom success message to be set' do + @subject.success_message = 'S' + expect(@subject.success_message).to eq('S') + end + + end + + describe :message do + + context 'if the check succeeds' do + + before(:each) do + @subject.stub(:check => true) + end + + it 'returns the default success message' do + expect(@subject.message).to eq('Success') + end + + it 'returns a custom success message if one has been set' do + @subject.success_message = 'Custom Success' + expect(@subject.message).to eq('Custom Success') + end + + end + + context 'if the check fails' do + + before(:each) do + @subject.stub(:check => false) + end + + it 'returns the default failure message' do + expect(@subject.message).to eq('Failed') + end + + it 'returns a custom failure message if one has been set' do + @subject.failure_message = 'Custom Failed' + expect(@subject.message).to eq('Custom Failed') + end + + end + + end + +end diff --git a/spec/lib/health_checks/health_checks_spec.rb b/spec/lib/health_checks/health_checks_spec.rb new file mode 100644 index 000000000..c7037b813 --- /dev/null +++ b/spec/lib/health_checks/health_checks_spec.rb @@ -0,0 +1,77 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +describe HealthChecks do + include HealthChecks + + describe :add do + + it 'adds a check to the collection and returns the check' do + check = double('MockCheck', :check => true) + expect(add(check)).to eq(check) + end + + it 'does not add checks that do not define the check method' do + check = double('BadCheck') + expect(add(check)).to eq(false) + end + + end + + describe :all do + + it 'returns all the checks' do + check1 = double('MockCheck', :check => true) + check2 = double('AnotherCheck', :check => false) + add(check1) + add(check2) + expect(all).to include(check1, check2) + end + + end + + describe :each do + + it 'iterates over each check' do + expect(subject).to respond_to(:each) + end + + end + + describe :ok? do + + it 'returns true if all checks are ok' do + checks = [ + double('MockCheck', :ok? => true), + double('FakeCheck', :ok? => true), + double('TestCheck', :ok? => true) + ] + HealthChecks.stub(:all => checks) + + expect(HealthChecks.ok?).to be_true + end + + it 'returns false if all checks fail' do + checks = [ + double('MockCheck', :ok? => false), + double('FakeCheck', :ok? => false), + double('TestCheck', :ok? => false) + ] + HealthChecks.stub(:all => checks) + + expect(HealthChecks.ok?).to be_false + end + + it 'returns false if a single check fails' do + checks = [ + double('MockCheck', :ok? => true), + double('FakeCheck', :ok? => false), + double('TestCheck', :ok? => true) + ] + HealthChecks.stub(:all => checks) + + expect(HealthChecks.ok?).to be_false + end + + end + +end diff --git a/spec/lib/mail_handler/backends/mail_backend_spec.rb b/spec/lib/mail_handler/backends/mail_backend_spec.rb new file mode 100644 index 000000000..588033faf --- /dev/null +++ b/spec/lib/mail_handler/backends/mail_backend_spec.rb @@ -0,0 +1,145 @@ +# coding: utf-8 +require File.expand_path(File.dirname(__FILE__) + '../../../../spec_helper') + +describe MailHandler::Backends::MailBackend do + include MailHandler + include MailHandler::Backends::MailBackend + + describe :backend do + + it 'should return the name of the backend' do + backend.should == 'Mail' + end + + end + + describe :mail_from_raw_email do + + it 'returns a new mail instance of the email' do + raw_mail = load_file_fixture('raw_emails/1.email') + expected = Mail.read_from_string(raw_mail) + mail_from_raw_email(raw_mail).should == expected + end + + end + + describe :get_part_file_name do + + it 'returns the part file name' do + mail = get_fixture_mail('document-pdf.email') + part = mail.attachments.first + get_part_file_name(part).should == 'tiny-example.pdf' + end + + it 'returns nil if there is no file name' do + mail = get_fixture_mail('document-pdf.email') + part = mail.parts.first + get_part_file_name(part).should be_nil + end + + end + + describe :get_part_body do + + it 'returns the body of a part' do + expected = <<-DOC +Here's a PDF attachement which has a document/pdf content-type, +when it really should be application/pdf.\n +DOC + mail = get_fixture_mail('document-pdf.email') + part = mail.parts.first + get_part_body(part).should == expected + end + + end + + describe :first_from do + + it 'finds the first from field' do + mail = get_fixture_mail('raw_emails/1.email') + expected = Mail::Address.new('FOI Person <foiperson@localhost>').to_s + first_from(mail).to_s.should == expected + end + + end + + describe :get_from_address do + + it 'finds the first address' do + mail = get_fixture_mail('raw_emails/1.email') + get_from_address(mail).should == 'foiperson@localhost' + end + + end + + describe :get_from_name do + + it 'finds the first from name' do + mail = get_fixture_mail('raw_emails/1.email') + get_from_name(mail).should == 'FOI Person' + end + + end + + describe :get_all_addresses do + + it 'returns all addresses present in an email' do + mail = get_fixture_mail('raw_emails/1.email') + mail.cc = 'bob@example.com' + mail['envelope-to'] = 'bob@example.net' + expected = %w(bob@localhost bob@example.com bob@example.net) + get_all_addresses(mail).should == expected + end + + end + + describe :empty_return_path? do + + it 'is false if the return path is nil' do + mail = Mail.new + empty_return_path?(mail).should be_false + end + + it 'is false if the return path has some data' do + mail = Mail.new + mail['return-path'] = 'xyz' + empty_return_path?(mail).should be_false + end + + it 'is true if the return path is blank' do + mail = Mail.new + mail['return-path'] = '' + empty_return_path?(mail).should be_true + end + + end + + describe :get_auto_submitted do + + it 'returns the auto-submitted attribute' do + mail = Mail.new + mail['auto-submitted'] = 'xyz' + get_auto_submitted(mail).should == 'xyz' + end + + it 'returns nil if there is no auto-submitted attribute' do + mail = Mail.new + get_auto_submitted(mail).should be_nil + end + + end + + describe :expand_and_normalize_parts do + + context 'when given a multipart message' do + + it 'should return a Mail::PartsList' do + mail = get_fixture_mail('incoming-request-oft-attachments.email') + expand_and_normalize_parts(mail, mail).class.should == Mail::PartsList + end + + end + + end + +end diff --git a/spec/lib/mail_handler/mail_handler_spec.rb b/spec/lib/mail_handler/mail_handler_spec.rb index ffc40ced9..e7ad93300 100644 --- a/spec/lib/mail_handler/mail_handler_spec.rb +++ b/spec/lib/mail_handler/mail_handler_spec.rb @@ -346,6 +346,12 @@ describe 'when getting attachment attributes' do attributes[0][:body].is_utf8?.should == true end + it 'should get multiple attachments from a multipart mail with text and HTML alternatives, which should be UTF-8' do + mail = get_fixture_mail('apple-mail-with-attachments.email') + attributes = MailHandler.get_attachment_attributes(mail) + attributes.length.should == 7 + end + it 'should expand a mail attached as text' do # Note that this spec will only pass using Tmail in the timezone set as datetime headers # are rendered out in the local time - using the Mail gem this is not necessary diff --git a/spec/lib/public_body_categories_spec.rb b/spec/lib/public_body_categories_spec.rb deleted file mode 100644 index e53d9a028..000000000 --- a/spec/lib/public_body_categories_spec.rb +++ /dev/null @@ -1,42 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') - -describe PublicBodyCategories do - - before do - load_test_categories - end - - describe 'when asked for categories with headings' do - - it 'should return a list of headings as plain strings, each followed by n tag specifications as - lists in the form: - ["tag_to_use_as_category", "Sub category title", "Instance description"]' do - expected_categories = ["Local and regional", ["local_council", - "Local councils", - "a local council"], - "Miscellaneous", ["other", - "Miscellaneous", - "miscellaneous"]] - PublicBodyCategories::get().with_headings().should == expected_categories - end - - end - - describe 'when asked for headings' do - - it 'should return a list of headings' do - PublicBodyCategories::get().headings().should == ['Local and regional', 'Miscellaneous'] - end - - end - - describe 'when asked for tags by headings' do - - it 'should return a hash of tags keyed by heading' do - PublicBodyCategories::get().by_heading().should == {'Local and regional' => ['local_council'], - 'Miscellaneous' => ['other']} - end - - end - -end
\ No newline at end of file |