diff --git a/lib/interac_email.rb b/lib/interac_email.rb index b12b045da6a9444f56d0d58a5c3b0c9f33c85ac9..1a46120cfe3893d54114a99cc6c3bd0f4f4dfafb 100644 --- a/lib/interac_email.rb +++ b/lib/interac_email.rb @@ -119,17 +119,17 @@ class InteracEmail end def authentication_header - @m["Authentication-Results"]&.value + Array(@m["Authentication-Results"]).map(&:value) end HEADER_REGEX = /\sheader.d=payments.interac.ca\s/.freeze def ensure_authentication_header - auth = authentication_header - + auth = authentication_header.find { |a| + a =~ HEADER_REGEX + } raise Error::NoAuth, @m unless auth raise Error::BadAuth, @m unless auth =~ /\sdkim=pass\s/ - raise Error::BadDomain, @m unless auth =~ HEADER_REGEX end def dkim_header diff --git a/test/test_interac_email.rb b/test/test_interac_email.rb new file mode 100644 index 0000000000000000000000000000000000000000..b54a60bac68012bf2aa95bb8deb8c541ac889358 --- /dev/null +++ b/test/test_interac_email.rb @@ -0,0 +1,75 @@ +# frozen_string_literal: true + +require "interac_email" +require "mail" +require "test_helper" + +class InteracEmailTest < Minitest::Test + def test_authentication_header + @m = Mail.new(<<~MAIL) + Authentication-Results: hai + To: someone@example.com + From: interac@example.com + + body + MAIL + @validator = InteracEmail::Validator.new(@m) + assert_equal ["hai"], @validator.authentication_header + end + + def test_authentication_headers + @m = Mail.new(<<~MAIL) + Authentication-Results: hai + Authentication-Results: hai2 + To: someone@example.com + From: interac@example.com + + body + MAIL + @validator = InteracEmail::Validator.new(@m) + assert_equal ["hai", "hai2"], @validator.authentication_header + end + + def test_ensure_authentication_header + @m = Mail.new(<<~MAIL) + Authentication-Results: stuff header.d=payments.interac.ca dkim=pass and + Authentication-Results: and + To: someone@example.com + From: interac@example.com + + body + MAIL + @validator = InteracEmail::Validator.new(@m) + @validator.ensure_authentication_header + end + + def test_ensure_authentication_header_fail + @m = Mail.new(<<~MAIL) + Authentication-Results: stuff header.d=payments.interac.ca dkim=fail and + Authentication-Results: and + To: someone@example.com + From: interac@example.com + + body + MAIL + @validator = InteracEmail::Validator.new(@m) + assert_raises(InteracEmail::Error::BadAuth) do + @validator.ensure_authentication_header + end + end + + def test_ensure_authentication_header_no_interac + @m = Mail.new(<<~MAIL) + Authentication-Results: stuff header.d=fakey.fake dkim=pass and + Authentication-Results: and dkim=pass stuff + To: someone@example.com + From: interac@example.com + + body + MAIL + @validator = InteracEmail::Validator.new(@m) + assert_raises(InteracEmail::Error::NoAuth) do + @validator.ensure_authentication_header + end + end +end