Skip to content

Instantly share code, notes, and snippets.

@HashNotAdam
Last active January 15, 2024 08:18
Show Gist options
  • Save HashNotAdam/f5d08e370e70e9e39105fa765563c5b3 to your computer and use it in GitHub Desktop.
Save HashNotAdam/f5d08e370e70e9e39105fa765563c5b3 to your computer and use it in GitHub Desktop.
Testing Action Mailbox receiving an email with no body context but an attachment
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails"
# If you want to test against edge Rails replace the previous line with this:
# gem "rails", github: "rails/rails", branch: "main"
gem "sqlite3"
end
require "active_record/railtie"
require "active_storage/engine"
require "action_mailbox/engine"
require "tmpdir"
class TestApp < Rails::Application
config.load_defaults Rails::VERSION::STRING.to_f
config.root = __dir__
config.hosts << "example.org"
config.eager_load = false
config.session_store :cookie_store, key: "cookie_store_key"
config.secret_key_base = "secret_key_base"
config.logger = Logger.new($stdout)
Rails.logger = config.logger
config.active_storage.service = :local
config.active_storage.service_configurations = {
local: {
root: Dir.tmpdir,
service: "Disk"
}
}
config.action_mailbox.ingress = :relay
end
ENV["DATABASE_URL"] = "sqlite3::memory:"
Rails.application.initialize!
require ActiveStorage::Engine.root.join("db/migrate/20170806125915_create_active_storage_tables.rb").to_s
require ActionMailbox::Engine.root.join("db/migrate/20180917164000_create_action_mailbox_tables.rb").to_s
ActiveRecord::Schema.define do
CreateActiveStorageTables.new.change
CreateActionMailboxTables.new.change
end
class ApplicationMailbox < ActionMailbox::Base
routing (/^report@/i) => :dmarc_report
end
class DmarcReportMailbox < ActionMailbox::Base
def process
$attachments = mail.attachments
end
end
require "minitest/autorun"
class DmarcReportMailboxTest < ActionMailbox::TestCase
setup do
source = <<~SOURCE
Return-Path: <noreply-dmarc-support@google.com>
Received: from mail-qt1-f202.google.com (mail-qt1-f202.google.com [209.85.160.202])
by inbound-smtp.ap-southeast-2.amazonaws.com with SMTP id 26357nglg0b5tii5omf2tachvofqs9jrq5eslt01
for report@dmarc.eduadv.com.au;
Sun, 14 Jan 2024 10:48:55 +0000 (UTC)
X-SES-Spam-Verdict: PASS
X-SES-Virus-Verdict: PASS
Received-SPF: pass (spfCheck: domain of google.com designates 209.85.160.202 as permitted sender) client-ip=209.85.160.202; envelope-from=noreply-dmarc-support@google.com; helo=mail-qt1-f202.google.com;
Authentication-Results: amazonses.com;
spf=pass (spfCheck: domain of google.com designates 209.85.160.202 as permitted sender) client-ip=209.85.160.202; envelope-from=noreply-dmarc-support@google.com; helo=mail-qt1-f202.google.com;
dkim=pass header.i=@google.com;
dmarc=pass header.from=google.com;
X-SES-RECEIPT: AEFBQUFBQUFBQUFHcGR1UnNvYlhVbXFod2pNZ3ZiYmt4V3JnTStYa1BXSVlkR2FJTkV1RFl5OXU1M1grOGdSZTkraUtCeUZEV0pBL1R1S3ViS3NSaXlJVTFjNUhsaGhoTTEzWW1wSVZ5RTdlRzgyWlVnM3ZRRmY2VkJod3M4aXpsY2NQa2MwZitaUmJYaG42Y2oyMjdpZXNEZkgxQ2FueTJKNTZLdHMrbUduM29tWVZZcFRBV2hNbXV4cy9BMUozcUxKQjE0c0ZIZzd1c3ZmdWxqcWd6MGdhLy8vQUROdk5SQ0VaV0x5WGsxdDdlUXErZ0p1VXI5ZDFDdjA5NTN3S21CSnJJbEJOT29CaFY0RzZ0YzlaZXp2VWhObXR6Qlg2UFlWY0t3c3g1bGtsWjNITUpxTGFJVmcxZjYxQzNMZGVEQ1NZY3Mya0RTNFE9
X-SES-DKIM-SIGNATURE: a=rsa-sha256; q=dns/txt; b=X3DHB3cy6l12gvJWTVIxGcB4KPn+YBD8YDqKTNEY8OQtfrWoONjUPR0kVOZ8YIzTQk+f3n/r3fNC3YfP4LXOhvcXX2JOFaEVWoXztP+/lG1cHAv+gHDUk5O2pFg/rf571iofwtgs3l0NJtCgMvg9rN0RYk4kHMtYfiYH9sJnmxY=; c=relaxed/simple; s=ulrbq2zjesb42hdt6rpnifgor3epinsy; d=amazonses.com; t=1705229335; v=1; bh=xKwd+W/bWH/znbCrRbFtTQpBB5/DtaOjr/JzGbp1wjU=; h=From:To:Cc:Bcc:Subject:Date:Message-ID:MIME-Version:Content-Type:X-SES-RECEIPT;
Received: by mail-qt1-f202.google.com with SMTP id d75a77b69052e-429c7bfb94bso49565381cf.2
for <report@dmarc.eduadv.com.au>; Sun, 14 Jan 2024 02:48:54 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=google.com; s=20230601; t=1705229332; x=1705834132; darn=dmarc.eduadv.com.au;
h=content-transfer-encoding:content-disposition:to:from:subject
:message-id:date:mime-version:from:to:cc:subject:date:message-id
:reply-to;
bh=xKwd+W/bWH/znbCrRbFtTQpBB5/DtaOjr/JzGbp1wjU=;
b=KnS2xIXURrLLENNMHWxoJH5f1DUGFCW+QVhoEKsMFLpmZbK8vz81OKD38cOOOlDZvD
UzUf7s8BOuKtyWCjUSR4TxQUOa5sYm33KhtBlTXyPbhw0zjunEF0WYNr4AMWPJ0pu05K
mVAuJFKeiFc+aMM6u+/53NSVWMBYhzPCenWVghL33xC215KJ3FyAYfTVmIrLKlZsyIfd
WeYltusZyRgKXX2mKD6JNZlgvJMo0+1YBaSa64hrv4lfXrYEcT7O5idEPrNtO+U2tATq
oVdT418RqmHdoIwoltGs6kEE8JxVEZ1n5BPTybYxgsfrwuoaew0BlN9/OTogeOU8np8o
oKqg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1705229332; x=1705834132;
h=content-transfer-encoding:content-disposition:to:from:subject
:message-id:date:mime-version:x-gm-message-state:from:to:cc:subject
:date:message-id:reply-to;
bh=xKwd+W/bWH/znbCrRbFtTQpBB5/DtaOjr/JzGbp1wjU=;
b=cDhlz17ObSMMbyM9RVnVodb9nd7MDVGXaBZGgJeAfwhvDA9xQD8gSyvCCHst+NPMgu
BT5uDScsk4P4VNzWEIc7PodloGVPjdDQKmzONa/tlclva6GA10AbFXbo0+eMdTlPWMvd
ubcHIgFWFF5GR+hssMsuNTZ6v4RqFXxXxG8nG8wzBSmnUMCcXeoCJ4afAvBcPPvw6ZD5
nY9eH1swD2O2KDcXrO9XxIx9D32H7+gd3KCiYKUsM4FwdpfbAwJqCVlmHG1t3hd20ltK
3EAuClb57Si1Og8JakS861FMag8AcLbYuVHatMCKp1e0BzwL+sh8PvlubjJ73gQ/3kbi
+SLQ==
X-Gm-Message-State: AOJu0YxJsT8cNGInJvjvtsFswI9pmOj1SaghhMM4tYcMMt4UgXdgViKZ
Vh92swywlgFuX3TE/TqXUg==
X-Google-Smtp-Source: AGHT+IGCyTSQkQzppUTVhqiskyWQ0c3kGzgTPhgJzy9WD1li5vPgDF0WENyBXT+mq6OUA+ubwqXQCssCCuK4lQ==
MIME-Version: 1.0
X-Received: by 2002:ac8:5756:0:b0:429:96c4:3298 with SMTP id
22-20020ac85756000000b0042996c43298mr28591qtx.9.1705229332731; Sun, 14 Jan
2024 02:48:52 -0800 (PST)
Date: Sat, 13 Jan 2024 15:59:59 -0800
Message-ID: <17096490616065293100@google.com>
Subject: Report domain: eduadv.com.au Submitter: google.com Report-ID: 17096490616065293100
From: noreply-dmarc-support@google.com
To: report@dmarc.eduadv.com.au
Content-Type: application/zip;
name="google.com!eduadv.com.au!1705104000!1705190399.zip"
Content-Disposition: attachment;
filename="google.com!eduadv.com.au!1705104000!1705190399.zip"
Content-Transfer-Encoding: base64
UEsDBAoAAAAIAAlSLlglSNngBQIAAEoFAAAyAAAAZ29vZ2xlLmNvbSFlZHVhZHYuY29tLmF1ITE3
MDUxMDQwMDAhMTcwNTE5MDM5OS54bWy1VMFy2yAQvecrPL5bSI6tRB1CeuoXtGcNhpXMRAIGkBP/
fcEgiTqdyaHTiw1vd9/uvl2EXz/GYXMBY4WSL9uqKLcbkExxIfuX7a+fP3bP280recAdAD9R9kYe
NhtsQCvj2hEc5dTRgHlUmb6VdATSK9UPUDA1YrSA0QdGKgYilWcYrjs+UsN2dtKB7nseFv1SzIcz
tGVKOspcK2SnyNk5bb8hlEKLNRRRRKV9B4P2h7o+Ppee63N8JE5tCE6qp7KpD01ZV3VZH/fNY1X6
wNUe/X2v0Boq+9SNh07QCxnCj1V5KENQRGY7SB6tTfnYNL4WOZOhP9mWbLmoWKtBsGurp9Mg7BmW
QpSXRxLgE+WX0HVBJ08Y0ehC+ZsYicEoHhJodXfDwn+EtB+GBIx0utsZsDOimSM3OcIhQnJ2kjrW
/rc6vbxMmblko94XUayaDINWaFIdmuJpX/jffciwGmZXpibp02MUDzOc8sGFDpOXkc+GoI2wWlnh
/D6nInMk8wuydH7LvMOiUJKgS4ZFpqzHu5x+bnNnWHCQTnTCv6Yl7AyUg2k7o8b7eeWmxPWJAdPJ
nVsDdhrcSnpXcBq8PSstuuvt6cRXlG9E2vhARDS1Nizc7ZI1DgMwpwyho/bNz7dFgTztf6/BVl+W
kI1nrSBkBlP8WyHZ6P1ruRtBcI6bjdH6WfwNUEsBAgoACgAAAAgACVIuWCVI2eAFAgAASgUAADIA
AAAAAAAAAAAAAAAAAAAAAGdvb2dsZS5jb20hZWR1YWR2LmNvbS5hdSExNzA1MTA0MDAwITE3MDUx
OTAzOTkueG1sUEsFBgAAAAABAAEAYAAAAFUCAAAAAA==
SOURCE
@inbound_email = receive_inbound_email_from_source(source)
end
test "the email has an attachment" do
assert_equal $attachments.size, 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment