# 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
