From 4e7d98922ec621a5c9bc6638a40c09890f0f17a4 Mon Sep 17 00:00:00 2001
From: Ivan Tashkinov <ivantashkinov@gmail.com>
Date: Wed, 12 Dec 2018 16:28:00 +0300
Subject: [PATCH] [#114] Added tests for "POST /api/account/password_reset".

---
 config/test.exs                               |  2 +
 .../twitter_api_controller_test.exs           | 43 ++++++++++++++++++-
 2 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/config/test.exs b/config/test.exs
index ca10a616c..5c6acfead 100644
--- a/config/test.exs
+++ b/config/test.exs
@@ -11,6 +11,8 @@ config :logger, level: :warn
 
 config :pleroma, Pleroma.Uploaders.Local, uploads: "test/uploads"
 
+config :pleroma, Pleroma.Mailer, adapter: Swoosh.Adapters.Test
+
 # Configure your database
 config :pleroma, Pleroma.Repo,
   adapter: Ecto.Adapters.Postgres,
diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs
index a30d415a7..c16c0cdc0 100644
--- a/test/web/twitter_api/twitter_api_controller_test.exs
+++ b/test/web/twitter_api/twitter_api_controller_test.exs
@@ -9,6 +9,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
   alias Pleroma.Web.CommonAPI
   alias Pleroma.Web.TwitterAPI.TwitterAPI
   alias Comeonin.Pbkdf2
+  alias Ecto.Changeset
 
   import Pleroma.Factory
 
@@ -270,7 +271,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
       since_id = List.last(activities).id
 
       current_user =
-        Ecto.Changeset.change(current_user, following: [User.ap_followers(user)])
+        Changeset.change(current_user, following: [User.ap_followers(user)])
         |> Repo.update!()
 
       conn =
@@ -832,6 +833,46 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
     end
   end
 
+  describe "POST /api/account/password_reset, with valid parameters" do
+    setup %{conn: conn} do
+      user = insert(:user)
+      conn = post(conn, "/api/account/password_reset?email=#{user.email}")
+      %{conn: conn, user: user}
+    end
+
+    test "it returns 204", %{conn: conn} do
+      assert json_response(conn, :no_content)
+    end
+
+    test "it creates a PasswordResetToken record for user", %{user: user} do
+      token_record = Repo.get_by(Pleroma.PasswordResetToken, user_id: user.id)
+      assert token_record
+    end
+
+    test "it sends an email to user", %{user: user} do
+      token_record = Repo.get_by(Pleroma.PasswordResetToken, user_id: user.id)
+
+      Swoosh.TestAssertions.assert_email_sent(
+        Pleroma.UserEmail.password_reset_email(user, token_record.token)
+      )
+    end
+  end
+
+  describe "POST /api/account/password_reset, with invalid parameters" do
+    setup [:valid_user]
+
+    test "it returns 500 when user is not found", %{conn: conn, user: user} do
+      conn = post(conn, "/api/account/password_reset?email=nonexisting_#{user.email}")
+      assert json_response(conn, :internal_server_error)
+    end
+
+    test "it returns 500 when user is not local", %{conn: conn, user: user} do
+      {:ok, user} = Repo.update(Changeset.change(user, local: false))
+      conn = post(conn, "/api/account/password_reset?email=#{user.email}")
+      assert json_response(conn, :internal_server_error)
+    end
+  end
+
   describe "GET /api/externalprofile/show" do
     test "it returns the user", %{conn: conn} do
       user = insert(:user)