add no-JS contact form and noscript banner
All checks were successful
deploy / deploy (push) Successful in 1m21s
All checks were successful
deploy / deploy (push) Successful in 1m21s
Wire up the contact form with action/method/name attrs so it works without JavaScript. Add ContactNotifier, ContactController, and a noscript info banner in the shop root layout. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
87
test/berrypod/contact_notifier_test.exs
Normal file
87
test/berrypod/contact_notifier_test.exs
Normal file
@@ -0,0 +1,87 @@
|
||||
defmodule Berrypod.ContactNotifierTest do
|
||||
use Berrypod.DataCase, async: true
|
||||
|
||||
import Swoosh.TestAssertions
|
||||
|
||||
alias Berrypod.ContactNotifier
|
||||
alias Berrypod.Settings
|
||||
|
||||
describe "deliver_contact_message/1" do
|
||||
test "sends email with valid params" do
|
||||
assert {:ok, _} =
|
||||
ContactNotifier.deliver_contact_message(%{
|
||||
"name" => "Jo Bloggs",
|
||||
"email" => "jo@example.com",
|
||||
"subject" => "Question about prints",
|
||||
"message" => "Do you ship to Mars?"
|
||||
})
|
||||
|
||||
assert_email_sent(fn email ->
|
||||
assert email.subject =~ "Question about prints"
|
||||
assert email.text_body =~ "Jo Bloggs"
|
||||
assert email.text_body =~ "jo@example.com"
|
||||
assert email.text_body =~ "Do you ship to Mars?"
|
||||
assert {"", "jo@example.com"} = email.reply_to
|
||||
end)
|
||||
end
|
||||
|
||||
test "sends to contact_email when set" do
|
||||
Settings.put_setting("contact_email", "shop@example.com")
|
||||
|
||||
assert {:ok, _} =
|
||||
ContactNotifier.deliver_contact_message(%{
|
||||
"name" => "Test",
|
||||
"email" => "test@example.com",
|
||||
"message" => "Hello"
|
||||
})
|
||||
|
||||
assert_email_sent(fn email ->
|
||||
assert [{"", "shop@example.com"}] = email.to
|
||||
end)
|
||||
end
|
||||
|
||||
test "uses default subject when not provided" do
|
||||
assert {:ok, _} =
|
||||
ContactNotifier.deliver_contact_message(%{
|
||||
"name" => "Test",
|
||||
"email" => "test@example.com",
|
||||
"message" => "Hello"
|
||||
})
|
||||
|
||||
assert_email_sent(fn email ->
|
||||
assert email.subject =~ "Contact form message"
|
||||
end)
|
||||
end
|
||||
|
||||
test "returns error for missing name" do
|
||||
assert {:error, :invalid_params} =
|
||||
ContactNotifier.deliver_contact_message(%{
|
||||
"name" => "",
|
||||
"email" => "test@example.com",
|
||||
"message" => "Hello"
|
||||
})
|
||||
end
|
||||
|
||||
test "returns error for missing email" do
|
||||
assert {:error, :invalid_params} =
|
||||
ContactNotifier.deliver_contact_message(%{
|
||||
"name" => "Test",
|
||||
"email" => "",
|
||||
"message" => "Hello"
|
||||
})
|
||||
end
|
||||
|
||||
test "returns error for missing message" do
|
||||
assert {:error, :invalid_params} =
|
||||
ContactNotifier.deliver_contact_message(%{
|
||||
"name" => "Test",
|
||||
"email" => "test@example.com",
|
||||
"message" => ""
|
||||
})
|
||||
end
|
||||
|
||||
test "returns error for missing keys" do
|
||||
assert {:error, :invalid_params} = ContactNotifier.deliver_contact_message(%{})
|
||||
end
|
||||
end
|
||||
end
|
||||
48
test/berrypod_web/controllers/contact_controller_test.exs
Normal file
48
test/berrypod_web/controllers/contact_controller_test.exs
Normal file
@@ -0,0 +1,48 @@
|
||||
defmodule BerrypodWeb.ContactControllerTest do
|
||||
use BerrypodWeb.ConnCase, async: false
|
||||
|
||||
import Berrypod.AccountsFixtures
|
||||
import Swoosh.TestAssertions
|
||||
|
||||
setup do
|
||||
user_fixture()
|
||||
{:ok, _} = Berrypod.Settings.set_site_live(true)
|
||||
# Clear confirmation email from user_fixture
|
||||
Swoosh.TestAssertions.assert_email_sent()
|
||||
:ok
|
||||
end
|
||||
|
||||
describe "POST /contact/send" do
|
||||
test "sends email and redirects with success flash", %{conn: conn} do
|
||||
conn =
|
||||
post(conn, ~p"/contact/send", %{
|
||||
"name" => "Jo Bloggs",
|
||||
"email" => "jo@example.com",
|
||||
"subject" => "Question",
|
||||
"message" => "Do you ship internationally?"
|
||||
})
|
||||
|
||||
assert redirected_to(conn) == "/contact"
|
||||
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ "Message sent"
|
||||
|
||||
assert_email_sent(fn email ->
|
||||
assert email.subject =~ "Question"
|
||||
assert email.text_body =~ "Do you ship internationally?"
|
||||
end)
|
||||
end
|
||||
|
||||
test "redirects with error flash when required fields missing", %{conn: conn} do
|
||||
conn = post(conn, ~p"/contact/send", %{"name" => "", "email" => "", "message" => ""})
|
||||
|
||||
assert redirected_to(conn) == "/contact"
|
||||
assert Phoenix.Flash.get(conn.assigns.flash, :error) =~ "required fields"
|
||||
end
|
||||
|
||||
test "redirects with error flash when params empty", %{conn: conn} do
|
||||
conn = post(conn, ~p"/contact/send", %{})
|
||||
|
||||
assert redirected_to(conn) == "/contact"
|
||||
assert Phoenix.Flash.get(conn.assigns.flash, :error) =~ "required fields"
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user