-
-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move email sending to foreground, surface delivery error in the UI
- Loading branch information
Showing
8 changed files
with
64 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,33 @@ | ||
defmodule Asciinema.Emails do | ||
alias Asciinema.Emails.{Email, Mailer} | ||
|
||
defmodule Job do | ||
use Oban.Worker, queue: :emails | ||
|
||
@impl Oban.Worker | ||
def perform(job) do | ||
case job.args["type"] do | ||
"signup" -> | ||
job.args["to"] | ||
|> Email.signup_email(job.args["token"]) | ||
|> deliver() | ||
|
||
"login" -> | ||
job.args["to"] | ||
|> Email.login_email(job.args["token"]) | ||
|> deliver() | ||
|
||
"account_deletion" -> | ||
job.args["to"] | ||
|> Email.account_deletion_email(job.args["token"]) | ||
|> deliver() | ||
end | ||
|
||
:ok | ||
end | ||
|
||
defp deliver(email) do | ||
with {:permanent_failure, _, _} <- Mailer.deliver_now!(email) do | ||
{:cancel, :permanent_failure} | ||
end | ||
end | ||
def send_email(:signup, to, token) do | ||
to | ||
|> Email.signup_email(token) | ||
|> deliver() | ||
end | ||
|
||
def send_email(type, to, token) do | ||
Oban.insert!(Job.new(%{type: type, to: to, token: token})) | ||
def send_email(:login, to, token) do | ||
to | ||
|> Email.login_email(token) | ||
|> deliver() | ||
end | ||
|
||
:ok | ||
def send_email(:account_deletion, to, token) do | ||
to | ||
|> Email.account_deletion_email(token) | ||
|> deliver() | ||
end | ||
|
||
def send_email(:test, to) do | ||
to | ||
|> Email.test_email() | ||
|> Mailer.deliver_now!() | ||
|> deliver() | ||
end | ||
|
||
defp deliver(email) do | ||
with {:ok, _email} <- Mailer.deliver_now(email) do | ||
:ok | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
defmodule AsciinemaTest do | ||
import Asciinema.Factory | ||
import Bamboo.Test | ||
use Asciinema.DataCase | ||
use Oban.Testing, repo: Asciinema.Repo | ||
alias Asciinema.Recordings | ||
|
@@ -28,48 +29,42 @@ defmodule AsciinemaTest do | |
|
||
assert Asciinema.send_login_email("[email protected]") == :ok | ||
|
||
assert_enqueued( | ||
worker: Asciinema.Emails.Job, | ||
args: %{"type" => "login", "to" => "[email protected]"} | ||
) | ||
assert_email_delivered_with(to: [{nil, "[email protected]"}], subject: "Login to localhost") | ||
end | ||
|
||
test "existing user, by username" do | ||
insert(:user, username: "foobar", email: "[email protected]") | ||
|
||
assert Asciinema.send_login_email("foobar") == :ok | ||
|
||
assert_enqueued( | ||
worker: Asciinema.Emails.Job, | ||
args: %{"type" => "login", "to" => "[email protected]"} | ||
assert_email_delivered_with( | ||
to: [{nil, "[email protected]"}], | ||
subject: "Login to localhost" | ||
) | ||
end | ||
|
||
test "non-existing user, by email" do | ||
assert Asciinema.send_login_email("[email protected]") == :ok | ||
|
||
assert_enqueued( | ||
worker: Asciinema.Emails.Job, | ||
args: %{"type" => "signup", "to" => "[email protected]"} | ||
) | ||
assert_email_delivered_with(to: [{nil, "[email protected]"}], subject: "Welcome to localhost") | ||
end | ||
|
||
test "non-existing user, by email, when sign up is disabled" do | ||
assert Asciinema.send_login_email("[email protected]", false) == {:error, :user_not_found} | ||
|
||
refute_enqueued(worker: Asciinema.Emails.Job) | ||
assert_no_emails_delivered() | ||
end | ||
|
||
test "non-existing user, by email, when email is invalid" do | ||
assert Asciinema.send_login_email("new@") == {:error, :email_invalid} | ||
|
||
refute_enqueued(worker: Asciinema.Emails.Job) | ||
assert_no_emails_delivered() | ||
end | ||
|
||
test "non-existing user, by username" do | ||
assert Asciinema.send_login_email("idontexist") == {:error, :user_not_found} | ||
|
||
refute_enqueued(worker: Asciinema.Emails.Job) | ||
assert_no_emails_delivered() | ||
end | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,47 @@ | ||
defmodule Asciinema.LoginControllerTest do | ||
use AsciinemaWeb.ConnCase | ||
use Oban.Testing, repo: Asciinema.Repo | ||
import Bamboo.Test | ||
|
||
@honeypot_detection_header "x-melliculum" | ||
|
||
test "with valid email", %{conn: conn} do | ||
conn = post conn, "/login", %{login: %{email: "[email protected]", username: ""}} | ||
|
||
assert redirected_to(conn, 302) == "/login/sent" | ||
assert get_resp_header(conn, @honeypot_detection_header) == [] | ||
assert_enqueued(worker: Asciinema.Emails.Job, args: %{"to" => "[email protected]"}) | ||
assert_email_delivered_with(to: [{nil, "[email protected]"}]) | ||
end | ||
|
||
test "with valid email (uppercase)", %{conn: conn} do | ||
conn = post conn, "/login", %{login: %{email: "[email protected]", username: ""}} | ||
|
||
assert redirected_to(conn, 302) == "/login/sent" | ||
assert get_resp_header(conn, @honeypot_detection_header) == [] | ||
assert_enqueued(worker: Asciinema.Emails.Job, args: %{"to" => "[email protected]"}) | ||
assert_email_delivered_with(to: [{nil, "[email protected]"}]) | ||
end | ||
|
||
test "with invalid email", %{conn: conn} do | ||
conn = post conn, "/login", %{login: %{email: "new@", username: ""}} | ||
|
||
assert html_response(conn, 200) =~ "correct email" | ||
assert get_resp_header(conn, @honeypot_detection_header) == [] | ||
refute_enqueued(worker: Asciinema.Emails.Job) | ||
assert_no_emails_delivered() | ||
end | ||
|
||
test "as bot with username", %{conn: conn} do | ||
conn = post conn, "/login", %{login: %{email: "[email protected]", username: "bot"}} | ||
|
||
assert redirected_to(conn, 302) == "/login/sent" | ||
assert List.first(get_resp_header(conn, @honeypot_detection_header)) == "machina" | ||
refute_enqueued(worker: Asciinema.Emails.Job) | ||
assert_no_emails_delivered() | ||
end | ||
|
||
test "as bot with terms", %{conn: conn} do | ||
conn = post conn, "/login", %{login: %{email: "[email protected]", username: "", terms: "1"}} | ||
|
||
assert redirected_to(conn, 302) == "/login/sent" | ||
assert List.first(get_resp_header(conn, @honeypot_detection_header)) == "machina" | ||
refute_enqueued(worker: Asciinema.Emails.Job) | ||
assert_no_emails_delivered() | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ defmodule Asciinema.UserControllerTest do | |
use AsciinemaWeb.ConnCase | ||
use Oban.Testing, repo: Asciinema.Repo | ||
import Asciinema.Factory | ||
import Bamboo.Test | ||
alias Asciinema.Accounts | ||
|
||
describe "sign-up" do | ||
|
@@ -133,14 +134,14 @@ defmodule Asciinema.UserControllerTest do | |
|
||
describe "account deletion" do | ||
test "phase 1", %{conn: conn} do | ||
user = insert(:user) | ||
user = insert(:user, email: "[email protected]") | ||
conn = log_in(conn, user) | ||
|
||
conn = delete(conn, ~p"/user") | ||
|
||
assert response(conn, 302) | ||
assert flash(conn, :info) =~ ~r/initiated/i | ||
assert_enqueued(worker: Asciinema.Emails.Job, args: %{"type" => "account_deletion"}) | ||
assert_email_delivered_with(to: [{nil, "[email protected]"}], subject: "Account deletion") | ||
end | ||
|
||
test "phase 2", %{conn: conn} do | ||
|