-
-
Notifications
You must be signed in to change notification settings - Fork 264
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
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: "new@example.com", username: ""}} | ||
|
||
assert redirected_to(conn, 302) == "/login/sent" | ||
assert get_resp_header(conn, @honeypot_detection_header) == [] | ||
assert_enqueued(worker: Asciinema.Emails.Job, args: %{"to" => "new@example.com"}) | ||
assert_email_delivered_with(to: [{nil, "new@example.com"}]) | ||
end | ||
|
||
test "with valid email (uppercase)", %{conn: conn} do | ||
conn = post conn, "/login", %{login: %{email: "NEW@EXAMPLE.COM", username: ""}} | ||
|
||
assert redirected_to(conn, 302) == "/login/sent" | ||
assert get_resp_header(conn, @honeypot_detection_header) == [] | ||
assert_enqueued(worker: Asciinema.Emails.Job, args: %{"to" => "new@example.com"}) | ||
assert_email_delivered_with(to: [{nil, "new@example.com"}]) | ||
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: "bot@example.com", 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: "bot@example.com", 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