-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
157 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
defmodule Bamboo.MailtrapHelper do | ||
alias Mailtrap.Email | ||
|
||
def build_from_bamboo_email(bamboo_email) do | ||
%Email{} | ||
|> Email.put_from(bamboo_email.from) | ||
|> Email.put_to(bamboo_email.to) | ||
|> Email.put_cc(bamboo_email.cc) | ||
|> Email.put_bcc(bamboo_email.bcc) | ||
|> Email.put_bcc(bamboo_email.bcc) | ||
|> Email.put_subject(bamboo_email.subject) | ||
|> Email.put_text(bamboo_email.text_body) | ||
|> Email.put_html(bamboo_email.html_body) | ||
|> Email.put_headers(bamboo_email.headers) | ||
|> Email.put_attachments(prepare_attachments(bamboo_email.attachments)) | ||
end | ||
|
||
def get_key(config, key) do | ||
case Map.get(config, key) do | ||
nil -> raise_key_error(config, key) | ||
key -> key | ||
end | ||
end | ||
|
||
defp prepare_attachments(attachments) do | ||
Enum.map( | ||
attachments, | ||
fn attachment -> | ||
mailtrap_attachment = | ||
Mailtrap.Email.Attachment.build(attachment.data, attachments.filename) | ||
|
||
Mailtrap.Email.Attachment.put_content_id(mailtrap_attachment, attachment.content_id) | ||
end | ||
) | ||
end | ||
|
||
defp raise_key_error(config, key) do | ||
raise ArgumentError, """ | ||
There was no #{key} set for the adapter. | ||
* Here are the config options that were passed in: | ||
#{inspect(config)} | ||
""" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,8 @@ | ||
defmodule Bamboo.MailtrapSandboxAdapterTest do | ||
use ExUnit.Case | ||
import Tesla.Mock | ||
alias Bamboo.{Email, Mailer, MailtrapSandboxAdapter} | ||
|
||
@doc false | ||
def new_email(to \\ "[email protected]", subject \\ "Welcome to the app.") do | ||
Email.new_email( | ||
to: to, | ||
from: "[email protected]", | ||
cc: "[email protected]", | ||
bcc: "[email protected]", | ||
subject: subject, | ||
html_body: "<strong>Thanks for joining!</strong>", | ||
text_body: "Thanks for joining!" | ||
) | ||
|> Mailer.normalize_addresses() | ||
end | ||
import Mailtrap.BambooTestHelper | ||
alias Bamboo.MailtrapSandboxAdapter | ||
|
||
def api_configs, do: %{api_token: "api_token", inbox_id: 11111} | ||
|
||
|
@@ -30,7 +17,7 @@ defmodule Bamboo.MailtrapSandboxAdapterTest do | |
html: "<strong>Thanks for joining!</strong>", | ||
subject: "Welcome to the app.", | ||
text: "Thanks for joining!", | ||
to: [%{email: "[email protected]"}], | ||
to: [%{email: "[email protected]"}] | ||
}) | ||
|
||
mock(fn | ||
|
@@ -41,7 +28,8 @@ defmodule Bamboo.MailtrapSandboxAdapterTest do | |
{"content-type", "application/json"}, | ||
{"authorization", "Bearer api_token"} | ||
], | ||
url: "https://sandbox.api.mailtrap.io/api/send/11111"} -> | ||
url: "https://sandbox.api.mailtrap.io/api/send/11111" | ||
} -> | ||
json(%{success: true, message_ids: [1]}, status: 200) | ||
end) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
defmodule Bamboo.MailtrapSendingAdapterTest do | ||
use ExUnit.Case | ||
import Tesla.Mock | ||
import Mailtrap.BambooTestHelper | ||
alias Bamboo.MailtrapSendingAdapter | ||
|
||
def api_configs, do: %{api_token: "api_token"} | ||
|
||
test "happy path" do | ||
expected_request_body = | ||
Jason.encode!(%{ | ||
attachments: [], | ||
bcc: [%{email: "[email protected]"}], | ||
cc: [%{email: "[email protected]"}], | ||
from: %{email: "[email protected]"}, | ||
headers: %{}, | ||
html: "<strong>Thanks for joining!</strong>", | ||
subject: "Welcome to the app.", | ||
text: "Thanks for joining!", | ||
to: [%{email: "[email protected]"}] | ||
}) | ||
|
||
mock(fn | ||
%{ | ||
method: :post, | ||
body: ^expected_request_body, | ||
headers: [ | ||
{"content-type", "application/json"}, | ||
{"authorization", "Bearer api_token"} | ||
], | ||
url: "https://send.api.mailtrap.io/api/send" | ||
} -> | ||
json(%{success: true, message_ids: [1]}, status: 200) | ||
end) | ||
|
||
assert {:ok, _} = MailtrapSendingAdapter.deliver(new_email(), api_configs()) | ||
end | ||
|
||
test "returns error" do | ||
mock(fn | ||
%{method: :post, url: "https://send.api.mailtrap.io/api/send"} -> | ||
json(%{error: "Incorrect API token"}, status: 401) | ||
end) | ||
|
||
assert {:error, _} = MailtrapSendingAdapter.deliver(new_email(), api_configs()) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,8 @@ defmodule Mailtrap.SendingTest do | |
end) | ||
|
||
message = %Mailtrap.Email{} | ||
assert {:error, %Tesla.Env{status: 401, body: body}} = Mailtrap.Sending.send(message) | ||
client = Mailtrap.Sending.client("api_token") | ||
assert {:error, %Tesla.Env{status: 401, body: body}} = Mailtrap.Sending.send(client, message) | ||
assert %{"error" => "Incorrect API token"} == body | ||
end | ||
|
||
|
@@ -31,15 +32,17 @@ defmodule Mailtrap.SendingTest do | |
json(%{success: true, message_ids: ["1", "2"]}) | ||
end) | ||
|
||
response = | ||
message = | ||
%Email{} | ||
|> Email.put_subject("Hello") | ||
|> Email.put_from({"John Doe", "[email protected]"}) | ||
|> Email.put_to({"Jane Doe", "[email protected]"}) | ||
|> Email.put_cc({"Alice", "[email protected]"}) | ||
|> Email.put_text("Hello") | ||
|> Email.put_html("<strong>Hello</strong>") | ||
|> Mailtrap.Sending.send() | ||
|
||
client = Mailtrap.Sending.client("api_token") | ||
response = Mailtrap.Sending.send(client, message) | ||
|
||
assert {:ok, %{"message_ids" => ["1", "2"], "success" => true}} = response | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule Mailtrap.BambooTestHelper do | ||
alias Bamboo.{Email, Mailer} | ||
|
||
@doc false | ||
def new_email(to \\ "[email protected]", subject \\ "Welcome to the app.") do | ||
Email.new_email( | ||
to: to, | ||
from: "[email protected]", | ||
cc: "[email protected]", | ||
bcc: "[email protected]", | ||
subject: subject, | ||
html_body: "<strong>Thanks for joining!</strong>", | ||
text_body: "Thanks for joining!" | ||
) | ||
|> Mailer.normalize_addresses() | ||
end | ||
end |