test_interac_email.rb

 1# frozen_string_literal: true
 2
 3require "interac_email"
 4require "mail"
 5require "test_helper"
 6
 7class InteracEmailTest < Minitest::Test
 8	def test_authentication_header
 9		@m = Mail.new(<<~MAIL)
10			Authentication-Results: hai
11			To: someone@example.com
12			From: interac@example.com
13
14			body
15		MAIL
16		@validator = InteracEmail::Validator.new(@m)
17		assert_equal ["hai"], @validator.authentication_header
18	end
19
20	def test_authentication_headers
21		@m = Mail.new(<<~MAIL)
22			Authentication-Results: hai
23			Authentication-Results: hai2
24			To: someone@example.com
25			From: interac@example.com
26
27			body
28		MAIL
29		@validator = InteracEmail::Validator.new(@m)
30		assert_equal ["hai", "hai2"], @validator.authentication_header
31	end
32
33	def test_ensure_authentication_header
34		@m = Mail.new(<<~MAIL)
35			Authentication-Results: stuff header.d=payments.interac.ca dkim=pass and
36			Authentication-Results: and
37			To: someone@example.com
38			From: interac@example.com
39
40			body
41		MAIL
42		@validator = InteracEmail::Validator.new(@m)
43		@validator.ensure_authentication_header
44	end
45
46	def test_ensure_authentication_header_fail
47		@m = Mail.new(<<~MAIL)
48			Authentication-Results: stuff header.d=payments.interac.ca dkim=fail and
49			Authentication-Results: and
50			To: someone@example.com
51			From: interac@example.com
52
53			body
54		MAIL
55		@validator = InteracEmail::Validator.new(@m)
56		assert_raises(InteracEmail::Error::BadAuth) do
57			@validator.ensure_authentication_header
58		end
59	end
60
61	def test_ensure_authentication_header_no_interac
62		@m = Mail.new(<<~MAIL)
63			Authentication-Results: stuff header.d=fakey.fake dkim=pass and
64			Authentication-Results: and dkim=pass stuff
65			To: someone@example.com
66			From: interac@example.com
67
68			body
69		MAIL
70		@validator = InteracEmail::Validator.new(@m)
71		assert_raises(InteracEmail::Error::NoAuth) do
72			@validator.ensure_authentication_header
73		end
74	end
75end