From 69d48e8f160d4fbb703cca3698844fa29454a4cb Mon Sep 17 00:00:00 2001 From: Khaja Minhajuddin Date: Wed, 10 Mar 2021 09:23:09 -0500 Subject: [PATCH] Return email sent in LocalAdapter with an open_email_in_browser_url config (#590) Commit 6dae907714d726de60b1fb874eba1652078ccf2b fixed an issue where the LocalAdapter.deliver function wasn't returning an `{:ok, any}` response. This commit improves upon that by returning the `email` returned from `SentEmail.push(email)` as the second argument in `{:ok, any}` -> `{:ok, email}`. It also adds a test ensuring that we're correctly returning an `{:ok, _}` tuple from the `LocalAdapter.deliver` function. --- lib/bamboo/adapters/local_adapter.ex | 9 ++++++--- test/lib/bamboo/adapters/local_adapter_test.exs | 7 +++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/bamboo/adapters/local_adapter.ex b/lib/bamboo/adapters/local_adapter.ex index e2358f0a..8d9f6291 100644 --- a/lib/bamboo/adapters/local_adapter.ex +++ b/lib/bamboo/adapters/local_adapter.ex @@ -32,9 +32,10 @@ defmodule Bamboo.LocalAdapter do @doc "Adds email to `Bamboo.SentEmail`, can automatically open it in new browser tab" def deliver(email, %{open_email_in_browser_url: open_email_in_browser_url}) do - %{private: %{local_adapter_id: local_adapter_id}} = SentEmail.push(email) - - {:ok, open_url_in_browser("#{open_email_in_browser_url}/#{local_adapter_id}")} + delivered_email = SentEmail.push(email) + %{private: %{local_adapter_id: local_adapter_id}} = delivered_email + open_url_in_browser("#{open_email_in_browser_url}/#{local_adapter_id}") + {:ok, delivered_email} end def deliver(email, _config) do @@ -45,6 +46,8 @@ defmodule Bamboo.LocalAdapter do def supports_attachments?, do: true + defp open_url_in_browser("test://" <> _), do: :opened + defp open_url_in_browser(url) when is_binary(url) do case :os.type() do {:unix, :darwin} -> System.cmd("open", [url]) diff --git a/test/lib/bamboo/adapters/local_adapter_test.exs b/test/lib/bamboo/adapters/local_adapter_test.exs index b89d5db1..6287d9e0 100644 --- a/test/lib/bamboo/adapters/local_adapter_test.exs +++ b/test/lib/bamboo/adapters/local_adapter_test.exs @@ -18,4 +18,11 @@ defmodule Bamboo.LocalAdapterTest do assert [%Bamboo.Email{subject: "This is my email"}] = SentEmail.all() end + + test "using open_email_in_browser_url doesn't raise an error" do + email = new_email(subject: "This is my email") + + assert {:ok, _response} = + email |> LocalAdapter.deliver(%{open_email_in_browser_url: "test://"}) + end end