-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'account-verification' into staging
- Loading branch information
Showing
9 changed files
with
128 additions
and
19 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
2 changes: 1 addition & 1 deletion
2
lib/recognizer/verification_token_task.ex → ...ccounts/verification_code_cleanup_task.ex
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
25 changes: 23 additions & 2 deletions
25
lib/recognizer_web/controllers/accounts/prompt/verification_controller.ex
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,9 +1,30 @@ | ||
defmodule RecognizerWeb.Accounts.Prompt.VerificationController do | ||
use RecognizerWeb, :controller | ||
|
||
alias Recognizer.Accounts | ||
alias RecognizerWeb.Authentication | ||
|
||
@one_minute 60_000 | ||
|
||
plug :ensure_user | ||
|
||
def new(conn, _params) do | ||
render(conn, "new.html", user: conn.assigns.user) | ||
plug Hammer.Plug, | ||
[ | ||
rate_limit: {"user:verification", @one_minute, 3}, | ||
by: {:conn, &get_user_id_from_unverified_request/1} | ||
] | ||
when action in [:resend] | ||
|
||
def new(%{assigns: %{user: %{verified_at: nil}}} = conn, _params) do | ||
render(conn, "new.html", resend?: false) | ||
end | ||
|
||
def new(%{assigns: %{user: user}} = conn, _params) do | ||
Authentication.log_in_user(conn, user) | ||
end | ||
|
||
def resend(%{assigns: %{user: user}} = conn, _params) do | ||
Accounts.resend_verification_code(user, &Routes.verification_code_url(conn, :new, &1)) | ||
render(conn, "new.html", resend?: true) | ||
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
40 changes: 40 additions & 0 deletions
40
test/recognizer_web/controllers/accounts/prompt/verification_controller_test.exs
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
defmodule RecognizerWeb.Accounts.Prompt.VerificationCodeControllerTest do | ||
use RecognizerWeb.ConnCase | ||
|
||
import Recognizer.AccountFactory | ||
|
||
setup %{conn: conn} do | ||
user = insert(:user, verified_at: nil) | ||
verified_user = insert(:user) | ||
|
||
%{ | ||
unverified_conn: | ||
Phoenix.ConnTest.init_test_session(conn, %{ | ||
prompt_user_id: user.id | ||
}), | ||
verified_conn: | ||
Phoenix.ConnTest.init_test_session(conn, %{ | ||
prompt_user_id: verified_user.id | ||
}), | ||
empty_conn: conn | ||
} | ||
end | ||
|
||
describe "GET /prompt/verification" do | ||
test "renders the verification required page for an unverified user", %{unverified_conn: conn} do | ||
conn = get(conn, Routes.prompt_verification_path(conn, :new)) | ||
response = html_response(conn, 200) | ||
assert response =~ "Account Verification Pending</h2>" | ||
end | ||
|
||
test "redirects verified user", %{verified_conn: conn} do | ||
conn = get(conn, Routes.prompt_verification_path(conn, :new)) | ||
assert redirected_to(conn) =~ "/settings" | ||
end | ||
|
||
test "redirects anonymous user", %{empty_conn: conn} do | ||
conn = get(conn, Routes.prompt_verification_path(conn, :new)) | ||
assert redirected_to(conn) =~ "/login" | ||
end | ||
end | ||
end |
31 changes: 31 additions & 0 deletions
31
test/recognizer_web/controllers/accounts/verification_code_controller_test.exs
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
defmodule RecognizerWeb.Accounts.UserVerificationCodeControllerTest do | ||
use RecognizerWeb.ConnCase | ||
|
||
import Recognizer.AccountFactory | ||
|
||
setup do | ||
%{user: insert(:user, verified_at: nil)} | ||
end | ||
|
||
describe "GET /verify" do | ||
test "shows message for an expired code", %{conn: conn} do | ||
conn = get(conn, Routes.verification_code_path(conn, :new, "expired")) | ||
response = html_response(conn, 200) | ||
assert response =~ "Expired Verification Code</h2>" | ||
end | ||
|
||
test "verifies a valid code", %{conn: conn, user: user} do | ||
verification = insert(:verification_code, user: user) | ||
conn = get(conn, Routes.verification_code_path(conn, :new, verification.code)) | ||
assert redirected_to(conn) =~ "/settings" | ||
end | ||
|
||
test "verifies a valid code multiple times", %{conn: conn, user: user} do | ||
verification = insert(:verification_code, user: user) | ||
conn = get(conn, Routes.verification_code_path(conn, :new, verification.code)) | ||
conn = get(conn, Routes.verification_code_path(conn, :new, verification.code)) | ||
conn = get(conn, Routes.verification_code_path(conn, :new, verification.code)) | ||
assert redirected_to(conn) =~ "/settings" | ||
end | ||
end | ||
end |