Skip to content

Instantly share code, notes, and snippets.

@honzasterba
Created October 21, 2022 09:07
Show Gist options
  • Save honzasterba/5bb34759f0a83243bb020b383222e54a to your computer and use it in GitHub Desktop.
Save honzasterba/5bb34759f0a83243bb020b383222e54a to your computer and use it in GitHub Desktop.
mailgun ingest test
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
ruby "3.1.2"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# Activate the gem you are reporting the issue against.
gem "rails", "~> 7.0.0"
gem "sqlite3"
end
require "rack/test"
require "action_controller/railtie"
require "active_record/railtie"
require "active_storage/engine"
require "action_mailbox/engine"
require "tmpdir"
class TestApp < Rails::Application
config.root = __dir__
config.hosts << "example.org"
config.session_store :cookie_store, key: "cookie_store_key"
secrets.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"
}
}
routes.draw do
scope "/rails/action_mailbox", module: "action_mailbox/ingresses" do
post "/mailgun/inbound_emails/mime" => "mailgun/inbound_emails#create", as: :rails_mailgun_inbound_emails
end
end
config.action_mailbox.ingress = :mailgun
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
require "minitest/autorun"
class MailgunIngressTest < Minitest::Test
include Rack::Test::Methods
def setup
@inbound_email = File.binread "email.eml"
@timestamp = Time.now.to_i
@key = "RANDOM_SIGNING_KEY"
ENV["MAILGUN_INGRESS_SIGNING_KEY"] = @key
@signature = OpenSSL::HMAC.hexdigest OpenSSL::Digest::SHA256.new, @key, "#{@timestamp}TOKEN"
end
def test_controller_ingress
post "/rails/action_mailbox/mailgun/inbound_emails/mime", params: {
signature: @signature,
token: "TOKEN",
timestamp: @timestamp,
"body-mime" => @inbound_email
}
assert last_response.ok?
end
private
def app
Rails.application
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment