-
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.
[FORGOT PWD]: initial commit with controller and email test
- Loading branch information
Showing
7 changed files
with
102 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
defmodule CodeCorps.Emails.ForgotPasswordEmail do | ||
import Bamboo.Email | ||
import Bamboo.PostmarkHelper | ||
|
||
alias CodeCorps.Emails.BaseEmail | ||
|
||
def create(user) do | ||
BaseEmail.create | ||
|> to(user.email) | ||
|> template(template_id(), {}) | ||
end | ||
|
||
defp template_id, do: Application.get_env(:code_corps, :postmark_receipt_template) | ||
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,25 @@ | ||
defmodule CodeCorps.PasswordControllerTest do | ||
@moduledoc false | ||
|
||
use CodeCorps.ApiCase, resource_name: :password | ||
use Bamboo.Test | ||
|
||
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 } | ||
assert_delivered_email CodeCorps.Emails.ForgotPasswordEmail.create(user) | ||
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" => nil } | ||
refute_delivered_email CodeCorps.Emails.ForgotPasswordEmail.create(user) | ||
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.Emails.ForgotPasswordEmailTest do | ||
use CodeCorps.ModelCase | ||
use Bamboo.Test | ||
|
||
alias CodeCorps.Emails.ForgotPasswordEmail | ||
|
||
test "forgot password email works" do | ||
user = insert(:user) | ||
|
||
email = ForgotPasswordEmail.create(user) | ||
assert email.from == "Code Corps<team@codecorps.org>" | ||
assert email.to == user.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
defmodule CodeCorps.PasswordController do | ||
use CodeCorps.Web, :controller | ||
|
||
alias CodeCorps.{Emails, Mailer, User, AuthToken} | ||
alias Ecto.Changeset | ||
|
||
@doc""" | ||
forgot_password should take an email and generate an AuthToken model and send an email | ||
""" | ||
def forgot_password(conn, %{"email" => email}) do | ||
with %User{} = user <- Repo.get_by(User, email: email), | ||
%Changeset{valid?: true} <- AuthToken.changeset(%AuthToken{}, user) do | ||
|
||
try_create_forgot_password(user) | ||
|> send_forgot_password_email | ||
|
||
conn | ||
|> put_status(200) | ||
|> render("show.json", email: user.email) | ||
|
||
else | ||
nil -> | ||
conn | ||
|> put_status(200) | ||
|> render("show.json", email: nil) | ||
end | ||
end | ||
|
||
defp try_create_forgot_password(user) do | ||
Emails.ForgotPasswordEmail.create(user) | ||
end | ||
|
||
defp send_forgot_password_email(email) do | ||
email |> Mailer.deliver_now | ||
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 |