Skip to content

Commit

Permalink
Raise if adapter does not support attachments yet
Browse files Browse the repository at this point in the history
Not all adapters will support attachments right away. This makes it so
that if you try to add an attachment, but are using an adapter that
doesn't do anything with them, it raises letting you know that's the
problem. Otherwise it would be successful, but the attachments would do
nothing
  • Loading branch information
paulcsmith committed May 5, 2017
1 parent d478331 commit ce2249c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
3 changes: 3 additions & 0 deletions lib/bamboo/adapters/mandrill_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ defmodule Bamboo.MandrillAdapter do
end
end

@doc false
def supports_attachments?, do: true

defp get_key(config) do
case Map.get(config, :api_key) do
nil -> raise_api_key_error(config)
Expand Down
20 changes: 15 additions & 5 deletions lib/bamboo/mailer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ defmodule Bamboo.Mailer do

@doc false
def deliver_now(adapter, email, config) do
email = email |> validate_and_normalize
email = email |> validate_and_normalize(adapter)

if email.to == [] && email.cc == [] && email.bcc == [] do
debug_unsent(email)
Expand All @@ -127,7 +127,7 @@ defmodule Bamboo.Mailer do

@doc false
def deliver_later(adapter, email, config) do
email = email |> validate_and_normalize
email = email |> validate_and_normalize(adapter)

if email.to == [] && email.cc == [] && email.bcc == [] do
debug_unsent(email)
Expand All @@ -154,14 +154,24 @@ defmodule Bamboo.Mailer do
"""
end

defp validate_and_normalize(email) do
email |> validate |> normalize_addresses
defp validate_and_normalize(email, adapter) do
email |> validate(adapter) |> normalize_addresses
end

defp validate(email) do
defp validate(email, adapter) do
email
|> validate_from_address
|> validate_recipients
|> validate_attachment_support(adapter)
end

defp validate_attachment_support(%{attachments: []} = email, _adapter), do: email
defp validate_attachment_support(email, adapter) do
if function_exported?(adapter, :supports_attachments?, 0) && adapter.supports_attachments? do
email
else
raise "the #{adapter} does not support attachments yet."
end
end

defp validate_from_address(%{from: nil}) do
Expand Down
1 change: 1 addition & 0 deletions test/lib/bamboo/adapters/mandrill_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ defmodule Bamboo.MandrillAdapterTest do

email |> MandrillAdapter.deliver(@config)

assert MandrillAdapter.supports_attachments?
assert_receive {:fake_mandrill, %{params: params}}
assert params["key"] == @config[:api_key]
message = params["message"]
Expand Down
45 changes: 45 additions & 0 deletions test/lib/bamboo/mailer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ defmodule Bamboo.MailerTest do
end

def handle_config(config), do: config

def supports_attachments?, do: true
end

defmodule CustomConfigAdapter do
Expand All @@ -20,6 +22,14 @@ defmodule Bamboo.MailerTest do
end
end

defmodule AdapterWithoutAttachmentSupport do
def deliver(_email, _config) do
:noop
end

def handle_config(config), do: config
end

@custom_config adapter: CustomConfigAdapter, foo: :bar

Application.put_env(:bamboo, __MODULE__.CustomConfigMailer, @custom_config)
Expand All @@ -28,6 +38,14 @@ defmodule Bamboo.MailerTest do
use Bamboo.Mailer, otp_app: :bamboo
end

@mailer_config adapter: AdapterWithoutAttachmentSupport, foo: :bar

Application.put_env(:bamboo, __MODULE__.AdapterWithoutAttachmentSupportMailer, @mailer_config)

defmodule AdapterWithoutAttachmentSupportMailer do
use Bamboo.Mailer, otp_app: :bamboo
end

@mailer_config adapter: FooAdapter, foo: :bar

Application.put_env(:bamboo, __MODULE__.FooMailer, @mailer_config)
Expand All @@ -41,6 +59,33 @@ defmodule Bamboo.MailerTest do
:ok
end

test "raise if adapter does not support attachments and attachments are sent" do
path = Path.join(__DIR__, "../../support/attachment.docx")
email = new_email(to: "foo@bar.com") |> Email.put_attachment(path)

assert_raise RuntimeError, ~r/does not support attachments/, fn ->
AdapterWithoutAttachmentSupportMailer.deliver_now(email)
end

assert_raise RuntimeError, ~r/does not support attachments/, fn ->
AdapterWithoutAttachmentSupportMailer.deliver_later(email)
end
end

test "does not raise if adapter supports attachments" do
path = Path.join(__DIR__, "../../support/attachment.docx")
email = new_email(to: "foo@bar.com") |> Email.put_attachment(path)

FooMailer.deliver_now(email)

assert_received {:deliver, _email, _config}
end

test "does not raise if no attachments are on the email" do
email = new_email(to: "foo@bar.com")
AdapterWithoutAttachmentSupportMailer.deliver_now(email)
end

test "uses adapter's handle_config/1 to customize or validate the config" do
email = new_email(to: "foo@bar.com")

Expand Down

0 comments on commit ce2249c

Please sign in to comment.