-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #772 from code-corps/forgot-password
Add forgot_password controller action
- Loading branch information
Showing
10 changed files
with
159 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule CodeCorps.Emails.ForgotPasswordEmail do | ||
import Bamboo.Email | ||
import Bamboo.PostmarkHelper | ||
|
||
alias CodeCorps.Emails.BaseEmail | ||
|
||
def create(user, token) do | ||
BaseEmail.create | ||
|> to(user.email) | ||
|> template(template_id(), [link: link(token)]) | ||
end | ||
|
||
defp template_id, do: Application.get_env(:code_corps, :postmark_forgot_password_template) | ||
|
||
defp link(token) do | ||
Application.get_env(:code_corps, :site_url) | ||
|> URI.merge("password/reset?token=#{token}") | ||
|> URI.to_string | ||
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
defmodule CodeCorps.Services.ForgotPasswordService do | ||
|
||
alias CodeCorps.{AuthToken, Emails, Mailer, Repo, User} | ||
|
||
@doc""" | ||
forgot_password should take an email and generate an AuthToken model and send an email | ||
""" | ||
def forgot_password(email) do | ||
with %User{} = user <- Repo.get_by(User, email: email), | ||
{ :ok, %AuthToken{} = %{ value: token } } <- AuthToken.changeset(%AuthToken{}, user) |> Repo.insert | ||
do | ||
Emails.ForgotPasswordEmail.create(user, token) |> Mailer.deliver_now() | ||
else | ||
nil -> nil | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
defmodule CodeCorps.PasswordControllerTest do | ||
@moduledoc false | ||
|
||
use CodeCorps.ApiCase, resource_name: :password | ||
use Bamboo.Test | ||
|
||
alias CodeCorps.AuthToken | ||
|
||
test "creates and renders resource when email is valid", %{conn: conn} do | ||
user = insert(:user) | ||
attrs = %{"email" => user.email} | ||
conn = post conn, password_path(conn, :forgot_password), attrs | ||
response = json_response(conn, 200) | ||
|
||
assert response == %{ "email" => user.email } | ||
|
||
%AuthToken{value: token} = Repo.get_by(AuthToken, user_id: user.id) | ||
assert_delivered_email CodeCorps.Emails.ForgotPasswordEmail.create(user, token) | ||
end | ||
|
||
test "does not create resource and renders 200 when email is invalid", %{conn: conn} do | ||
user = insert(:user) | ||
attrs = %{"email" => "random_email@gmail.com"} | ||
conn = post conn, password_path(conn, :forgot_password), attrs | ||
response = json_response(conn, 200) | ||
|
||
assert response == %{ "email" => "random_email@gmail.com" } | ||
|
||
refute_delivered_email CodeCorps.Emails.ForgotPasswordEmail.create(user, nil) | ||
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule CodeCorps.Emails.ForgotPasswordEmailTest do | ||
use CodeCorps.ModelCase | ||
use Bamboo.Test | ||
|
||
alias CodeCorps.{Emails.ForgotPasswordEmail, AuthToken} | ||
|
||
test "forgot password email works" do | ||
user = insert(:user) | ||
{ :ok, %AuthToken{ value: token } } = AuthToken.changeset(%AuthToken{}, user) |> Repo.insert | ||
|
||
email = ForgotPasswordEmail.create(user, token) | ||
assert email.from == "Code Corps<team@codecorps.org>" | ||
assert email.to == user.email | ||
{ :link, encoded_link } = email.private.template_model |> Enum.at(0) | ||
assert "#{Application.get_env(:code_corps, :site_url)}/password/reset?token=#{token}" == encoded_link | ||
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule CodeCorps.PasswordViewTest do | ||
use CodeCorps.ViewCase | ||
|
||
test "renders show" do | ||
email = "wat@codecorps.org" | ||
|
||
rendered_json = render(CodeCorps.PasswordView, "show.json", %{email: email}) | ||
|
||
expected_json = %{ | ||
email: email | ||
} | ||
|
||
assert expected_json == rendered_json | ||
refute Map.has_key?(expected_json, :token) | ||
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
defmodule CodeCorps.PasswordController do | ||
use CodeCorps.Web, :controller | ||
|
||
alias CodeCorps.{Services.ForgotPasswordService} | ||
|
||
@doc""" | ||
forgot_password should take an email and generate an AuthToken model and send an email | ||
""" | ||
def forgot_password(conn, %{"email" => email}) do | ||
ForgotPasswordService.forgot_password(email) | ||
conn |> put_status(:ok) |> render("show.json", email: email) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
defmodule CodeCorps.PasswordView do | ||
use CodeCorps.Web, :view | ||
|
||
def render("show.json", %{email: email}) do | ||
%{ | ||
email: email | ||
} | ||
end | ||
|
||
end |