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

Create a custom test adapter. #151

Merged
merged 7 commits into from
Nov 3, 2020
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ The *hostname* option sets the FQDN to the header of your emails, its optional,

4. Follow Bamboo [Getting Started Guide](https://github.com/thoughtbot/bamboo#getting-started)

5. **Optional** Set `BambooSMTP.TestAdapter` as your test adapter:

```elixir
# In your config/config.exs file
if Mix.env() == :test do
config :my_app, MyApp.Mailer, adapter: MyApp.SMTPTestAdapter
end
```
## Usage

You can find more information about advanced features in the [Wiki](https://github.com/fewlinesco/bamboo_smtp/wiki).
Expand Down
56 changes: 56 additions & 0 deletions lib/bamboo/adapters/test_adapter.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
defmodule BambooSMTP.TestAdapter do
arnaudmorisset marked this conversation as resolved.
Show resolved Hide resolved
@moduledoc """
Based on `Bamboo.TestAdapter`, this module can be used for testing email delivery.

The `deliver/2` function will provide a response that follow the format of a SMTP server raw response.

No emails are sent, instead it sends back `{%Bamboo.Email{...}, {:ok,"<raw_smtp_response>"}}`
for success and raise an exception on error.

## Example config

# Typically done in config/test.exs
config :my_app, MyApp.Mailer,
adapter: BambooSMTP.TestAdapter

# Define a Mailer. Typically in lib/my_app/mailer.ex
defmodule MyApp.Mailer do
use Bamboo.Mailer, otp_app: :my_app
end
"""

@behaviour Bamboo.Adapter

@doc false
def deliver(_email, _config) do
send(test_process(), {:ok, "Ok #{Enum.random(100_000_000..999_999_999)}"})
end

defp test_process do
Application.get_env(:bamboo, :shared_test_process) || self()
end

def handle_config(config) do
case config[:deliver_later_strategy] do
nil ->
Map.put(config, :deliver_later_strategy, Bamboo.ImmediateDeliveryStrategy)

Bamboo.ImmediateDeliveryStrategy ->
config

_ ->
raise ArgumentError, """
BambooSMTP.TestAdapter requires that the deliver_later_strategy is
Bamboo.ImmediateDeliveryStrategy

Instead it got: #{inspect(config[:deliver_later_strategy])}

Please remove the deliver_later_strategy from your config options, or
set it to Bamboo.ImmediateDeliveryStrategy.
"""
end
end

@doc false
def supports_attachments?, do: true
end