1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add basic, unauthenticated inbound emails controller

This commit is contained in:
David Heinemeier Hansson 2018-09-17 22:32:05 -07:00
parent dd55bf66d2
commit 65a6d525c6
3 changed files with 40 additions and 0 deletions

View file

@ -0,0 +1,14 @@
class ActionMailroom::InboundEmailsController < ActionController::Base
skip_forgery_protection
before_action :require_rfc822_message
def create
ActionMailroom::InboundEmail.create!(raw_email: params[:message])
head :created
end
private
def require_rfc822_message
head :unsupported_media_type unless params.require(:message).content_type == 'message/rfc822'
end
end

7
config/routes.rb Normal file
View file

@ -0,0 +1,7 @@
# frozen_string_literal: true
Rails.application.routes.draw do
scope :action_mailroom do
post "/inbound_emails" => "action_mailroom/inbound_emails#create", as: :rails_inbound_emails
end
end

View file

@ -0,0 +1,19 @@
require_relative '../test_helper'
class ActionMailroom::InboundEmailsControllerTest < ActionDispatch::IntegrationTest
test "receiving a valid RFC 822 message" do
assert_difference -> { ActionMailroom::InboundEmail.count }, +1 do
post_inbound_email "welcome.eml"
end
assert_response :created
inbound_email = ActionMailroom::InboundEmail.last
assert_equal file_fixture('../files/welcome.eml').read, inbound_email.raw_email.download
end
private
def post_inbound_email(fixture_name)
post rails_inbound_emails_url, params: { message: fixture_file_upload("files/#{fixture_name}", 'message/rfc822') }
end
end