Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Mailgun.deliver to return ok/error tuples #573

Merged
merged 1 commit into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/bamboo/adapters/mailgun_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ defmodule Bamboo.MailgunAdapter do
) do
{:ok, status, _headers, response} when status > 299 ->
body = decode_body(body)
raise_api_error(@service_name, response, body)
{:error, build_api_error(@service_name, response, body)}

{:ok, status, headers, response} ->
%{status_code: status, headers: headers, body: response}
{:ok, %{status_code: status, headers: headers, body: response}}

{:error, reason} ->
raise_api_error(inspect(reason))
{:error, build_api_error(inspect(reason))}
end
end

Expand Down
26 changes: 14 additions & 12 deletions test/lib/bamboo/adapters/mailgun_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ defmodule Bamboo.MailgunAdapterTest do
assert request_path == "/test.tt/messages"
end

test "deliver/2 returns an {:ok, response} tuple" do
{:ok, response} = new_email() |> MailgunAdapter.deliver(@config)

assert %{status_code: 200, headers: _, body: _} = response
end

test "deliver/2 sends from, subject, text body, html body, headers, custom vars and recipient variables" do
email =
new_email(
Expand Down Expand Up @@ -293,28 +299,24 @@ defmodule Bamboo.MailgunAdapterTest do
assert params["t:text"] == "yes"
end

test "raises if the response is not a success" do
test "returns an error if the response is not a success" do
email = new_email(from: "INVALID_EMAIL")

assert_raise Bamboo.ApiError,
~r/.*%{.*\"from\" => \"INVALID_EMAIL\".*}/,
fn ->
email |> MailgunAdapter.deliver(@config)
end
{:error, %Bamboo.ApiError{} = error} = email |> MailgunAdapter.deliver(@config)

assert error.message =~ ~r/.*%{.*\"from\" => \"INVALID_EMAIL\".*}/
end

test "raises if the response is not a success with attachment" do
test "returns an error if the response is not a success with attachment" do
attachment_source_path = Path.join(__DIR__, "../../../support/attachment.txt")

email =
new_email(from: "INVALID_EMAIL")
|> Email.put_attachment(attachment_source_path)

assert_raise Bamboo.ApiError,
~r/.*{.*\"from\", \"INVALID_EMAIL\".*}/,
fn ->
email |> MailgunAdapter.deliver(@config)
end
{:error, %Bamboo.ApiError{} = error} = email |> MailgunAdapter.deliver(@config)

assert error.message =~ ~r/.*{.*\"from\", \"INVALID_EMAIL\".*}/
end

defp new_email(attrs \\ []) do
Expand Down