mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 03:32:06 +00:00
Set up SES mail delivery and harden the mailing list (#120)
* setup basic SES email sending * basic honeypot form form submission
This commit is contained in:
@@ -9,6 +9,8 @@ defmodule Chesstrainer.AccountsTest do
|
||||
alias Chesstrainer.Attempts
|
||||
alias Chesstrainer.Attempts.Attempt
|
||||
alias Chesstrainer.Accounts.Scope
|
||||
alias Chesstrainer.MailingList
|
||||
alias Chesstrainer.MailingList.Subscriber
|
||||
|
||||
describe "get_user_by_email/1" do
|
||||
test "does not return the user if the email does not exist" do
|
||||
@@ -340,6 +342,19 @@ defmodule Chesstrainer.AccountsTest do
|
||||
refute Repo.get_by(UserToken, user_id: user.id)
|
||||
end
|
||||
|
||||
test "updates the linked mailing-list address", %{
|
||||
user: user,
|
||||
token: token,
|
||||
email: email
|
||||
} do
|
||||
{:ok, _subscriber} = MailingList.subscribe_user(user)
|
||||
MailingList.confirm_user_subscription(user)
|
||||
|
||||
assert {:ok, %{email: ^email}} = Accounts.update_user_email(user, token)
|
||||
|
||||
assert %Subscriber{email: ^email} = Repo.get_by(Subscriber, user_id: user.id)
|
||||
end
|
||||
|
||||
test "does not update email with invalid token", %{user: user} do
|
||||
assert Accounts.update_user_email(user, "oops") ==
|
||||
{:error, :transaction_aborted}
|
||||
|
||||
@@ -127,4 +127,172 @@ defmodule Chesstrainer.MailingListTest do
|
||||
Repo.get_by(Subscriber, email: "player@example.com")
|
||||
end
|
||||
end
|
||||
|
||||
describe "unsubscribe/1" do
|
||||
test "marks a confirmed address as unsubscribed" do
|
||||
{:ok, :confirmation_required} = MailingList.subscribe("player@example.com")
|
||||
{:ok, _subscriber} = MailingList.confirm("player@example.com")
|
||||
|
||||
assert {:ok, %Subscriber{unsubscribed_at: unsubscribed_at}} =
|
||||
MailingList.unsubscribe("player@example.com")
|
||||
|
||||
assert unsubscribed_at
|
||||
assert MailingList.count_confirmed_subscribers() == 0
|
||||
end
|
||||
|
||||
test "is idempotent" do
|
||||
{:ok, :confirmation_required} = MailingList.subscribe("player@example.com")
|
||||
{:ok, _subscriber} = MailingList.confirm("player@example.com")
|
||||
|
||||
{:ok, first} = MailingList.unsubscribe("player@example.com")
|
||||
{:ok, second} = MailingList.unsubscribe("player@example.com")
|
||||
|
||||
assert first.unsubscribed_at == second.unsubscribed_at
|
||||
end
|
||||
|
||||
test "returns not found for an unknown address" do
|
||||
assert {:error, :not_found} = MailingList.unsubscribe("nobody@example.com")
|
||||
end
|
||||
end
|
||||
|
||||
describe "unsubscribe_user/1" do
|
||||
test "unsubscribes the address linked to the user" do
|
||||
user = user_fixture()
|
||||
{:ok, _subscriber} = MailingList.subscribe_user(user)
|
||||
MailingList.confirm_user_subscription(user)
|
||||
|
||||
assert {:ok, %Subscriber{unsubscribed_at: unsubscribed_at}} =
|
||||
MailingList.unsubscribe_user(user)
|
||||
|
||||
assert unsubscribed_at
|
||||
refute MailingList.user_subscribed?(user)
|
||||
end
|
||||
|
||||
test "returns not found when the user is not on the list" do
|
||||
assert {:error, :not_found} = MailingList.unsubscribe_user(user_fixture())
|
||||
end
|
||||
end
|
||||
|
||||
describe "user_subscribed?/1" do
|
||||
test "is true only for a confirmed, active subscription" do
|
||||
user = user_fixture()
|
||||
{:ok, _subscriber} = MailingList.subscribe_user(user)
|
||||
|
||||
refute MailingList.user_subscribed?(user)
|
||||
|
||||
MailingList.confirm_user_subscription(user)
|
||||
assert MailingList.user_subscribed?(user)
|
||||
|
||||
{:ok, _subscriber} = MailingList.unsubscribe_user(user)
|
||||
refute MailingList.user_subscribed?(user)
|
||||
end
|
||||
end
|
||||
|
||||
describe "re-subscribing after unsubscribe" do
|
||||
test "requires a fresh confirmation and clears the unsubscribe" do
|
||||
{:ok, :confirmation_required} = MailingList.subscribe("player@example.com")
|
||||
{:ok, _subscriber} = MailingList.confirm("player@example.com")
|
||||
{:ok, _subscriber} = MailingList.unsubscribe("player@example.com")
|
||||
|
||||
assert {:ok, :confirmation_required} = MailingList.subscribe("player@example.com")
|
||||
|
||||
assert {:ok, %Subscriber{confirmed_at: confirmed_at, unsubscribed_at: nil}} =
|
||||
MailingList.confirm("player@example.com")
|
||||
|
||||
assert confirmed_at
|
||||
assert MailingList.count_confirmed_subscribers() == 1
|
||||
end
|
||||
end
|
||||
|
||||
describe "update_subscriber_email/2" do
|
||||
test "moves the linked subscription to the new address" do
|
||||
user = user_fixture()
|
||||
{:ok, _subscriber} = MailingList.subscribe_user(user)
|
||||
MailingList.confirm_user_subscription(user)
|
||||
|
||||
new_email = unique_user_email()
|
||||
|
||||
assert {:ok, %Subscriber{email: ^new_email}} =
|
||||
MailingList.update_subscriber_email(user, new_email)
|
||||
|
||||
assert Repo.get_by(Subscriber, user_id: user.id).email == new_email
|
||||
end
|
||||
|
||||
test "removes an anonymous row that already holds the new address" do
|
||||
user = user_fixture()
|
||||
{:ok, _subscriber} = MailingList.subscribe_user(user)
|
||||
new_email = unique_user_email()
|
||||
{:ok, :confirmation_required} = MailingList.subscribe(new_email)
|
||||
|
||||
assert {:ok, _subscriber} = MailingList.update_subscriber_email(user, new_email)
|
||||
|
||||
assert Repo.aggregate(Subscriber, :count) == 1
|
||||
assert %Subscriber{user_id: user_id} = Repo.get_by(Subscriber, email: new_email)
|
||||
assert user_id == user.id
|
||||
end
|
||||
|
||||
test "keeps an unsubscribe from an anonymous row at the new address" do
|
||||
user = user_fixture()
|
||||
{:ok, _subscriber} = MailingList.subscribe_user(user)
|
||||
MailingList.confirm_user_subscription(user)
|
||||
new_email = unique_user_email()
|
||||
{:ok, :confirmation_required} = MailingList.subscribe(new_email)
|
||||
{:ok, _subscriber} = MailingList.confirm(new_email)
|
||||
{:ok, _subscriber} = MailingList.unsubscribe(new_email)
|
||||
|
||||
assert {:ok, %Subscriber{unsubscribed_at: unsubscribed_at}} =
|
||||
MailingList.update_subscriber_email(user, new_email)
|
||||
|
||||
assert unsubscribed_at
|
||||
refute MailingList.user_subscribed?(user)
|
||||
end
|
||||
|
||||
test "does nothing when the user is not on the list" do
|
||||
user = user_fixture()
|
||||
|
||||
assert {:ok, :no_subscription} =
|
||||
MailingList.update_subscriber_email(user, unique_user_email())
|
||||
end
|
||||
end
|
||||
|
||||
describe "confirmation cooldown" do
|
||||
test "allows an address that has never received a confirmation" do
|
||||
assert MailingList.confirmation_allowed?("player@example.com")
|
||||
end
|
||||
|
||||
test "blocks a recently sent confirmation" do
|
||||
{:ok, :confirmation_required} = MailingList.subscribe("player@example.com")
|
||||
{:ok, _subscriber} = MailingList.mark_confirmation_sent("player@example.com")
|
||||
|
||||
refute MailingList.confirmation_allowed?("player@example.com")
|
||||
end
|
||||
|
||||
test "allows another send after the cooldown" do
|
||||
{:ok, :confirmation_required} = MailingList.subscribe("player@example.com")
|
||||
|
||||
Repo.get_by(Subscriber, email: "player@example.com")
|
||||
|> Ecto.Changeset.change(
|
||||
confirmation_sent_at: DateTime.add(DateTime.utc_now(:second), -11, :minute)
|
||||
)
|
||||
|> Repo.update!()
|
||||
|
||||
assert MailingList.confirmation_allowed?("player@example.com")
|
||||
end
|
||||
|
||||
test "allows a confirmed address" do
|
||||
{:ok, :confirmation_required} = MailingList.subscribe("player@example.com")
|
||||
{:ok, _subscriber} = MailingList.confirm("player@example.com")
|
||||
|
||||
assert MailingList.confirmation_allowed?("player@example.com")
|
||||
end
|
||||
|
||||
test "still applies after an unsubscribe" do
|
||||
{:ok, :confirmation_required} = MailingList.subscribe("player@example.com")
|
||||
{:ok, _subscriber} = MailingList.confirm("player@example.com")
|
||||
{:ok, _subscriber} = MailingList.unsubscribe("player@example.com")
|
||||
{:ok, _subscriber} = MailingList.mark_confirmation_sent("player@example.com")
|
||||
|
||||
refute MailingList.confirmation_allowed?("player@example.com")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,14 +1,19 @@
|
||||
defmodule ChesstrainerWeb.MailingListControllerTest do
|
||||
use ChesstrainerWeb.ConnCase, async: true
|
||||
|
||||
alias Chesstrainer.MailingList
|
||||
alias Chesstrainer.MailingList.Subscriber
|
||||
alias Chesstrainer.Repo
|
||||
alias ChesstrainerWeb.MailingListFormToken
|
||||
|
||||
@salt "mailing_list_confirm"
|
||||
|
||||
describe "POST /mailing-list" do
|
||||
test "sends a confirmation email for a new address", %{conn: conn} do
|
||||
conn = post(conn, ~p"/mailing-list", subscriber: %{email: "player@example.com"})
|
||||
conn =
|
||||
post(conn, ~p"/mailing-list",
|
||||
subscriber: %{email: "player@example.com", form_token: form_token()}
|
||||
)
|
||||
|
||||
assert redirected_to(conn) == "/#mailing-list"
|
||||
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ "Check your inbox"
|
||||
@@ -17,32 +22,140 @@ defmodule ChesstrainerWeb.MailingListControllerTest do
|
||||
assert_received {:email, email}
|
||||
assert email.subject == "Confirm your mailing list subscription"
|
||||
assert email.text_body =~ "/mailing-list/confirm/"
|
||||
assert email.reply_to == {"", "support@innersquare.app"}
|
||||
end
|
||||
|
||||
test "resends confirmation for an address that has not confirmed yet", %{conn: conn} do
|
||||
post(conn, ~p"/mailing-list", subscriber: %{email: "player@example.com"})
|
||||
assert_received {:email, _email}
|
||||
test "re-confirms an address that previously unsubscribed", %{conn: conn} do
|
||||
{:ok, :confirmation_required} = MailingList.subscribe("player@example.com")
|
||||
{:ok, _subscriber} = MailingList.confirm("player@example.com")
|
||||
{:ok, _subscriber} = MailingList.unsubscribe("player@example.com")
|
||||
|
||||
conn = post(build_conn(), ~p"/mailing-list", subscriber: %{email: "player@example.com"})
|
||||
conn =
|
||||
post(conn, ~p"/mailing-list",
|
||||
subscriber: %{email: "player@example.com", form_token: form_token()}
|
||||
)
|
||||
|
||||
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ "Check your inbox"
|
||||
assert_received {:email, _email}
|
||||
assert Repo.aggregate(Subscriber, :count) == 1
|
||||
end
|
||||
|
||||
test "does not email an address that has already confirmed", %{conn: conn} do
|
||||
post(conn, ~p"/mailing-list", subscriber: %{email: "player@example.com"})
|
||||
assert_received {:email, email}
|
||||
get(build_conn(), "/mailing-list/confirm/#{extract_token(email)}")
|
||||
|
||||
conn = post(build_conn(), ~p"/mailing-list", subscriber: %{email: "player@example.com"})
|
||||
subscriber = Repo.get_by(Subscriber, email: "player@example.com")
|
||||
assert subscriber.confirmed_at
|
||||
assert subscriber.unsubscribed_at == nil
|
||||
assert MailingList.count_confirmed_subscribers() == 1
|
||||
end
|
||||
|
||||
test "does not resend confirmation while the cooldown is active", %{conn: conn} do
|
||||
post(conn, ~p"/mailing-list",
|
||||
subscriber: %{email: "player@example.com", form_token: form_token()}
|
||||
)
|
||||
|
||||
assert_received {:email, _email}
|
||||
|
||||
conn =
|
||||
post(build_conn(), ~p"/mailing-list",
|
||||
subscriber: %{email: "player@example.com", form_token: form_token()}
|
||||
)
|
||||
|
||||
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ "Check your inbox"
|
||||
refute_received {:email, _email}
|
||||
assert Repo.aggregate(Subscriber, :count) == 1
|
||||
end
|
||||
|
||||
test "resends confirmation after the cooldown", %{conn: conn} do
|
||||
post(conn, ~p"/mailing-list",
|
||||
subscriber: %{email: "player@example.com", form_token: form_token()}
|
||||
)
|
||||
|
||||
assert_received {:email, _email}
|
||||
|
||||
subscriber = Repo.get_by(Subscriber, email: "player@example.com")
|
||||
|
||||
subscriber
|
||||
|> Ecto.Changeset.change(
|
||||
confirmation_sent_at: DateTime.add(DateTime.utc_now(:second), -11, :minute)
|
||||
)
|
||||
|> Repo.update!()
|
||||
|
||||
post(conn, ~p"/mailing-list",
|
||||
subscriber: %{email: "player@example.com", form_token: form_token()}
|
||||
)
|
||||
|
||||
assert_received {:email, _email}
|
||||
end
|
||||
|
||||
test "silently accepts a honeypot submission", %{conn: conn} do
|
||||
conn =
|
||||
post(conn, ~p"/mailing-list",
|
||||
subscriber: %{
|
||||
email: "player@example.com",
|
||||
company: "spam.example",
|
||||
form_token: form_token()
|
||||
}
|
||||
)
|
||||
|
||||
assert redirected_to(conn) == "/#mailing-list"
|
||||
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ "Check your inbox"
|
||||
refute_received {:email, _email}
|
||||
assert Repo.aggregate(Subscriber, :count) == 0
|
||||
end
|
||||
|
||||
test "rejects a form token submitted too quickly", %{conn: conn} do
|
||||
conn =
|
||||
post(conn, ~p"/mailing-list",
|
||||
subscriber: %{email: "player@example.com", form_token: MailingListFormToken.sign()}
|
||||
)
|
||||
|
||||
assert Phoenix.Flash.get(conn.assigns.flash, :error) =~ "refresh the page"
|
||||
refute_received {:email, _email}
|
||||
assert Repo.aggregate(Subscriber, :count) == 0
|
||||
end
|
||||
|
||||
test "rejects an expired form token", %{conn: conn} do
|
||||
token = MailingListFormToken.sign(System.system_time(:second) - 3 * 60 * 60)
|
||||
|
||||
conn =
|
||||
post(conn, ~p"/mailing-list",
|
||||
subscriber: %{email: "player@example.com", form_token: token}
|
||||
)
|
||||
|
||||
assert Phoenix.Flash.get(conn.assigns.flash, :error) =~ "refresh the page"
|
||||
refute_received {:email, _email}
|
||||
end
|
||||
|
||||
test "rejects an invalid form token", %{conn: conn} do
|
||||
conn =
|
||||
post(conn, ~p"/mailing-list",
|
||||
subscriber: %{email: "player@example.com", form_token: "oops"}
|
||||
)
|
||||
|
||||
assert Phoenix.Flash.get(conn.assigns.flash, :error) =~ "refresh the page"
|
||||
refute_received {:email, _email}
|
||||
end
|
||||
|
||||
test "does not email an address that has already confirmed", %{conn: conn} do
|
||||
post(conn, ~p"/mailing-list",
|
||||
subscriber: %{email: "player@example.com", form_token: form_token()}
|
||||
)
|
||||
|
||||
assert_received {:email, email}
|
||||
get(build_conn(), "/mailing-list/confirm/#{extract_token(email)}")
|
||||
|
||||
conn =
|
||||
post(build_conn(), ~p"/mailing-list",
|
||||
subscriber: %{email: "player@example.com", form_token: form_token()}
|
||||
)
|
||||
|
||||
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ "already on the mailing list"
|
||||
refute_received {:email, _email}
|
||||
end
|
||||
|
||||
test "reports an invalid address", %{conn: conn} do
|
||||
conn = post(conn, ~p"/mailing-list", subscriber: %{email: "not an email"})
|
||||
conn =
|
||||
post(conn, ~p"/mailing-list",
|
||||
subscriber: %{email: "not an email", form_token: form_token()}
|
||||
)
|
||||
|
||||
assert redirected_to(conn) == "/#mailing-list"
|
||||
assert Phoenix.Flash.get(conn.assigns.flash, :error) =~ "valid email address"
|
||||
@@ -60,7 +173,10 @@ defmodule ChesstrainerWeb.MailingListControllerTest do
|
||||
|
||||
describe "GET /mailing-list/confirm/:token" do
|
||||
test "confirms the subscription from the emailed link", %{conn: conn} do
|
||||
post(conn, ~p"/mailing-list", subscriber: %{email: "player@example.com"})
|
||||
post(conn, ~p"/mailing-list",
|
||||
subscriber: %{email: "player@example.com", form_token: form_token()}
|
||||
)
|
||||
|
||||
assert_received {:email, email}
|
||||
|
||||
conn = get(build_conn(), "/mailing-list/confirm/#{extract_token(email)}")
|
||||
@@ -107,4 +223,8 @@ defmodule ChesstrainerWeb.MailingListControllerTest do
|
||||
[_, token] = Regex.run(~r{/mailing-list/confirm/(\S+)}, email.text_body)
|
||||
token
|
||||
end
|
||||
|
||||
defp form_token do
|
||||
MailingListFormToken.sign(System.system_time(:second) - 5)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -194,6 +194,9 @@ defmodule ChesstrainerWeb.PageControllerTest do
|
||||
|
||||
assert html =~ "Join the list"
|
||||
assert html =~ ~s(name="_csrf_token")
|
||||
assert html =~ ~s(name="subscriber[form_token]")
|
||||
assert html =~ ~s(id="mailing-list-company")
|
||||
assert html =~ ~s(class="mailing-list-honeypot")
|
||||
end
|
||||
|
||||
test "guest navbar links to log in and register", %{conn: conn} do
|
||||
|
||||
@@ -4,6 +4,9 @@ defmodule ChesstrainerWeb.UserLive.ProfileTest do
|
||||
alias Chesstrainer.Accounts
|
||||
alias Chesstrainer.Accounts.Scope
|
||||
alias Chesstrainer.Attempts
|
||||
alias Chesstrainer.MailingList
|
||||
alias Chesstrainer.MailingList.Subscriber
|
||||
alias Chesstrainer.Repo
|
||||
import Phoenix.LiveViewTest
|
||||
import Chesstrainer.AccountsFixtures
|
||||
import Chesstrainer.EndgamesFixtures
|
||||
@@ -303,4 +306,38 @@ defmodule ChesstrainerWeb.UserLive.ProfileTest do
|
||||
assert message == "You must log in to access this page."
|
||||
end
|
||||
end
|
||||
|
||||
describe "mailing list settings" do
|
||||
setup %{conn: conn} do
|
||||
user = user_fixture()
|
||||
%{conn: log_in_user(conn, user), user: user}
|
||||
end
|
||||
|
||||
test "shows the not-subscribed state", %{conn: conn} do
|
||||
{:ok, view, html} = live(conn, ~p"/users/profile/settings")
|
||||
|
||||
assert has_element?(view, "#mailing-list-settings")
|
||||
assert html =~ "not subscribed"
|
||||
refute has_element?(view, "#unsubscribe-mailing-list")
|
||||
end
|
||||
|
||||
test "unsubscribes a subscribed user", %{conn: conn, user: user} do
|
||||
{:ok, _subscriber} = MailingList.subscribe_user(user)
|
||||
MailingList.confirm_user_subscription(user)
|
||||
|
||||
{:ok, view, _html} = live(conn, ~p"/users/profile/settings")
|
||||
|
||||
assert has_element?(view, "#unsubscribe-mailing-list")
|
||||
|
||||
html = view |> element("#unsubscribe-mailing-list") |> render_click()
|
||||
|
||||
assert html =~ "removed from the mailing list"
|
||||
refute has_element?(view, "#unsubscribe-mailing-list")
|
||||
|
||||
assert %Subscriber{unsubscribed_at: unsubscribed_at} =
|
||||
Repo.get_by(Subscriber, user_id: user.id)
|
||||
|
||||
assert unsubscribed_at
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user