Skip to content

Commit

Permalink
Ensure EmailPreviewPlug always uses text/html
Browse files Browse the repository at this point in the history
  • Loading branch information
shhavel authored and paulcsmith committed Feb 22, 2017
1 parent 9ba5526 commit e6f5389
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
16 changes: 10 additions & 6 deletions lib/bamboo/plug/email_preview_plug.ex
Expand Up @@ -66,9 +66,7 @@ defmodule Bamboo.EmailPreviewPlug do
|> Plug.Conn.put_resp_content_type("text/html")
|> send_resp(:ok, email.html_body || "")
else
conn
|> Plug.Conn.put_resp_content_type("text/html")
|> render_not_found
conn |> render_not_found
end
end

Expand All @@ -81,12 +79,12 @@ defmodule Bamboo.EmailPreviewPlug do
end

defp render_no_emails(conn) do
send_resp(conn, :ok, no_emails())
send_html(conn, :ok, no_emails())
end

defp render_not_found(conn) do
assigns = %{base_path: base_path(conn)}
send_resp(conn, :not_found, not_found(assigns))
send_html(conn, :not_found, not_found(assigns))
end

defp render_index(conn, email) do
Expand All @@ -96,7 +94,13 @@ defmodule Bamboo.EmailPreviewPlug do
emails: all_emails(),
selected_email: email,
}
send_resp(conn, :ok, index(assigns))
send_html(conn, :ok, index(assigns))
end

defp send_html(conn, status, body) do
conn
|> Plug.Conn.put_resp_content_type("text/html")
|> send_resp(status, body)
end

defp base_path(%{script_name: []}), do: ""
Expand Down
4 changes: 2 additions & 2 deletions mix.lock
@@ -1,5 +1,5 @@
%{"certifi": {:hex, :certifi, "0.4.0", "a7966efb868b179023618d29a407548f70c52466bf1849b9e8ebd0e34b7ea11f", [:rebar3], []},
"cowboy": {:hex, :cowboy, "1.0.4", "a324a8df9f2316c833a470d918aaf73ae894278b8aa6226ce7a9bf699388f878", [:rebar, :make], [{:cowlib, "~> 1.0.0", [hex: :cowlib, optional: false]}, {:ranch, "~> 1.0", [hex: :ranch, optional: false]}]},
"cowboy": {:hex, :cowboy, "1.0.4", "a324a8df9f2316c833a470d918aaf73ae894278b8aa6226ce7a9bf699388f878", [:make, :rebar], [{:cowlib, "~> 1.0.0", [hex: :cowlib, optional: false]}, {:ranch, "~> 1.0", [hex: :ranch, optional: false]}]},
"cowlib": {:hex, :cowlib, "1.0.2", "9d769a1d062c9c3ac753096f868ca121e2730b9a377de23dec0f7e08b1df84ee", [:make], []},
"earmark": {:hex, :earmark, "1.0.1", "2c2cd903bfdc3de3f189bd9a8d4569a075b88a8981ded9a0d95672f6e2b63141", [:mix], []},
"ex_doc": {:hex, :ex_doc, "0.13.1", "658dbfc8cc5b0fac192f0f3254efe66ee6294200804a291549e0aeb052053bba", [:mix], [{:earmark, "~> 1.0", [hex: :earmark, optional: false]}]},
Expand All @@ -20,4 +20,4 @@
"plug": {:hex, :plug, "1.2.0", "496bef96634a49d7803ab2671482f0c5ce9ce0b7b9bc25bc0ae8e09859dd2004", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, optional: true]}, {:mime, "~> 1.0", [hex: :mime, optional: false]}]},
"poison": {:hex, :poison, "2.2.0", "4763b69a8a77bd77d26f477d196428b741261a761257ff1cf92753a0d4d24a63", [:mix], []},
"ranch": {:hex, :ranch, "1.2.1", "a6fb992c10f2187b46ffd17ce398ddf8a54f691b81768f9ef5f461ea7e28c762", [:make], []},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.0", "edee20847c42e379bf91261db474ffbe373f8acb56e9079acb6038d4e0bf414f", [:rebar, :make], []}}
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.0", "edee20847c42e379bf91261db474ffbe373f8acb56e9079acb6038d4e0bf414f", [:make, :rebar], []}}
11 changes: 11 additions & 0 deletions test/lib/bamboo/plug/email_preview_plug_test.exs
Expand Up @@ -26,6 +26,7 @@ defmodule Bamboo.EmailPreviewTest do
conn = AppRouter.call(conn, nil)

assert conn.status == 200
assert {"content-type", "text/html; charset=utf-8"} in conn.resp_headers
assert selected_sidebar_email_text(conn) =~ newest_email().subject
assert showing_in_preview_pane?(conn, newest_email())
refute showing_in_preview_pane?(conn, oldest_email())
Expand All @@ -46,6 +47,7 @@ defmodule Bamboo.EmailPreviewTest do
conn = AppRouter.call(conn, nil)

assert conn.status == 200
assert {"content-type", "text/html; charset=utf-8"} in conn.resp_headers
assert conn.resp_body =~ Bamboo.Email.get_address(email.from)
for email_address <- Bamboo.Email.all_recipients(email) do
assert conn.resp_body =~ Bamboo.Email.get_address(email_address)
Expand All @@ -59,6 +61,7 @@ defmodule Bamboo.EmailPreviewTest do
conn = AppRouter.call(conn, nil)

assert conn.status == 200
assert {"content-type", "text/html; charset=utf-8"} in conn.resp_headers
assert conn.resp_body =~ Bamboo.Email.get_address(email.from)
assert conn.resp_body =~ "Reply-To"
assert conn.resp_body =~ "reply-to@example.com"
Expand All @@ -71,6 +74,7 @@ defmodule Bamboo.EmailPreviewTest do
conn = AppRouter.call(conn, nil)

assert conn.status == 200
assert {"content-type", "text/html; charset=utf-8"} in conn.resp_headers
assert conn.resp_body =~ Bamboo.Email.get_address(email.from)
assert conn.resp_body =~ "Reply-To"
assert conn.resp_body =~ "reply-to1@example.com"
Expand All @@ -86,6 +90,7 @@ defmodule Bamboo.EmailPreviewTest do
conn = AppRouter.call(conn, nil)

assert conn.status == 200
assert {"content-type", "text/html; charset=utf-8"} in conn.resp_headers
assert conn.resp_body =~ "SomeHeader"
assert conn.resp_body =~ "%{\"Some\" => \"Header\"}"
end
Expand All @@ -101,6 +106,7 @@ defmodule Bamboo.EmailPreviewTest do
conn = AppRouter.call(conn, nil)

assert conn.status == 200
assert {"content-type", "text/html; charset=utf-8"} in conn.resp_headers
assert Floki.raw_html(sidebar(conn)) =~ ~s(href="/#{SentEmail.get_id(email)}")
assert Floki.text(sidebar(conn)) =~ email.subject
end
Expand All @@ -111,6 +117,7 @@ defmodule Bamboo.EmailPreviewTest do
conn = AppRouter.call(conn, nil)

assert conn.status == 200
assert {"content-type", "text/html; charset=utf-8"} in conn.resp_headers
assert conn.resp_body =~ "No emails sent"
end

Expand All @@ -123,6 +130,7 @@ defmodule Bamboo.EmailPreviewTest do
conn = AppRouter.call(conn, nil)

assert conn.status == 200
assert {"content-type", "text/html; charset=utf-8"} in conn.resp_headers
assert showing_in_preview_pane?(conn, SentEmail.get(selected_email_id))
refute showing_in_preview_pane?(conn, SentEmail.get(unselected_email_id))
end
Expand All @@ -135,6 +143,7 @@ defmodule Bamboo.EmailPreviewTest do
conn = AppRouter.call(conn, nil)

assert conn.status == 200
assert {"content-type", "text/html; charset=utf-8"} in conn.resp_headers
assert conn.resp_body =~ SentEmail.get(selected_email_id).html_body
end

Expand All @@ -146,6 +155,7 @@ defmodule Bamboo.EmailPreviewTest do
conn = AppRouter.call(conn, nil)

assert conn.status == 200
assert {"content-type", "text/html; charset=utf-8"} in conn.resp_headers
assert conn.resp_body == ""
end

Expand All @@ -155,6 +165,7 @@ defmodule Bamboo.EmailPreviewTest do
conn = AppRouter.call(conn, nil)

assert conn.status == 404
assert {"content-type", "text/html; charset=utf-8"} in conn.resp_headers
assert conn.resp_body =~ "Email not found"
end

Expand Down

0 comments on commit e6f5389

Please sign in to comment.