aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/controllers/action_mailroom/inbound_emails_controller.rb2
-rw-r--r--app/models/action_mailroom/inbound_email.rb17
-rw-r--r--db/migrate/20180917164000_create_action_mailbox_tables.rb1
-rw-r--r--lib/action_mailroom/test_helper.rb10
-rw-r--r--test/dummy/db/migrate/20180208205311_create_action_mailroom_tables.rb1
-rw-r--r--test/dummy/db/schema.rb1
-rw-r--r--test/unit/inbound_email_test.rb10
7 files changed, 36 insertions, 6 deletions
diff --git a/app/controllers/action_mailroom/inbound_emails_controller.rb b/app/controllers/action_mailroom/inbound_emails_controller.rb
index e5970b38ec..2aa2a89323 100644
--- a/app/controllers/action_mailroom/inbound_emails_controller.rb
+++ b/app/controllers/action_mailroom/inbound_emails_controller.rb
@@ -4,7 +4,7 @@ class ActionMailroom::InboundEmailsController < ActionController::Base
before_action :require_rfc822_message
def create
- ActionMailroom::InboundEmail.create!(raw_email: params[:message])
+ ActionMailroom::InboundEmail.create_from_raw_email!(params[:message])
head :created
end
diff --git a/app/models/action_mailroom/inbound_email.rb b/app/models/action_mailroom/inbound_email.rb
index 8526b47e78..790e0ccc4f 100644
--- a/app/models/action_mailroom/inbound_email.rb
+++ b/app/models/action_mailroom/inbound_email.rb
@@ -9,7 +9,22 @@ class ActionMailroom::InboundEmail < ActiveRecord::Base
has_one_attached :raw_email
enum status: %i[ pending processing delivered failed bounced ]
+ class << self
+ def create_from_raw_email!(raw_email, **options)
+ create! raw_email: raw_email, message_id: extract_message_id(raw_email), **options
+ end
+
+ def mail_from_raw_content(raw_email_content)
+ Mail.new(Mail::Utilities.binary_unsafe_to_crlf(raw_email_content.to_s))
+ end
+
+ private
+ def extract_message_id(raw_email)
+ mail_from_raw_content(raw_email.read).message_id
+ end
+ end
+
def mail
- @mail ||= Mail.new(Mail::Utilities.binary_unsafe_to_crlf(raw_email.download))
+ @mail ||= self.class.mail_from_raw_content(raw_email.download)
end
end
diff --git a/db/migrate/20180917164000_create_action_mailbox_tables.rb b/db/migrate/20180917164000_create_action_mailbox_tables.rb
index f488919138..cea05a6437 100644
--- a/db/migrate/20180917164000_create_action_mailbox_tables.rb
+++ b/db/migrate/20180917164000_create_action_mailbox_tables.rb
@@ -2,6 +2,7 @@ class CreateActionMailroomTables < ActiveRecord::Migration[5.2]
def change
create_table :action_mailroom_inbound_emails do |t|
t.integer :status, default: 0, null: false
+ t.string :message_id
t.datetime :created_at, precision: 6
t.datetime :updated_at, precision: 6
diff --git a/lib/action_mailroom/test_helper.rb b/lib/action_mailroom/test_helper.rb
index 33d32ec899..8ffbe94e6c 100644
--- a/lib/action_mailroom/test_helper.rb
+++ b/lib/action_mailroom/test_helper.rb
@@ -5,16 +5,18 @@ module ActionMailroom
# Create an InboundEmail record using an eml fixture in the format of message/rfc822
# referenced with +fixture_name+ located in +test/fixtures/files/fixture_name+.
def create_inbound_email_from_fixture(fixture_name, status: :processing)
- create_inbound_email file_fixture(fixture_name).open, filename: fixture_name, status: status
+ create_inbound_email file_fixture(fixture_name), filename: fixture_name, status: status
end
def create_inbound_email_from_mail(status: :processing, **mail_options)
- create_inbound_email(StringIO.new(Mail.new(mail_options).to_s), status: status)
+ raw_email = Tempfile.new.tap { |raw_email| raw_email.write Mail.new(mail_options).to_s }
+ create_inbound_email(raw_email, status: status)
end
def create_inbound_email(io, filename: 'mail.eml', status: :processing)
- ActionMailroom::InboundEmail.create! status: status, raw_email:
- ActiveStorage::Blob.create_after_upload!(io: io, filename: filename, content_type: 'message/rfc822')
+ ActionMailroom::InboundEmail.create_from_raw_email! \
+ ActionDispatch::Http::UploadedFile.new(tempfile: io, filename: filename, type: 'message/rfc822'),
+ status: status
end
end
end
diff --git a/test/dummy/db/migrate/20180208205311_create_action_mailroom_tables.rb b/test/dummy/db/migrate/20180208205311_create_action_mailroom_tables.rb
index f488919138..cea05a6437 100644
--- a/test/dummy/db/migrate/20180208205311_create_action_mailroom_tables.rb
+++ b/test/dummy/db/migrate/20180208205311_create_action_mailroom_tables.rb
@@ -2,6 +2,7 @@ class CreateActionMailroomTables < ActiveRecord::Migration[5.2]
def change
create_table :action_mailroom_inbound_emails do |t|
t.integer :status, default: 0, null: false
+ t.string :message_id
t.datetime :created_at, precision: 6
t.datetime :updated_at, precision: 6
diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb
index cf66062891..339a6b2afd 100644
--- a/test/dummy/db/schema.rb
+++ b/test/dummy/db/schema.rb
@@ -14,6 +14,7 @@ ActiveRecord::Schema.define(version: 2018_02_12_164506) do
create_table "action_mailroom_inbound_emails", force: :cascade do |t|
t.integer "status", default: 0, null: false
+ t.string "message_id"
t.datetime "created_at", precision: 6
t.datetime "updated_at", precision: 6
end
diff --git a/test/unit/inbound_email_test.rb b/test/unit/inbound_email_test.rb
new file mode 100644
index 0000000000..3eedd60472
--- /dev/null
+++ b/test/unit/inbound_email_test.rb
@@ -0,0 +1,10 @@
+require_relative '../test_helper'
+
+module ActionMailroom
+ class InboundEmailTest < ActiveSupport::TestCase
+ test "message id is extracted from raw email" do
+ inbound_email = create_inbound_email_from_fixture("welcome.eml")
+ assert_equal "0CB459E0-0336-41DA-BC88-E6E28C697DDB@37signals.com", inbound_email.message_id
+ end
+ end
+end