logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

password_controller_test.exs (2355B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.TwitterAPI.PasswordControllerTest do
  5. use Pleroma.Web.ConnCase
  6. alias Pleroma.PasswordResetToken
  7. alias Pleroma.User
  8. alias Pleroma.Web.OAuth.Token
  9. import Pleroma.Factory
  10. describe "GET /api/pleroma/password_reset/token" do
  11. test "it returns error when token invalid", %{conn: conn} do
  12. response =
  13. conn
  14. |> get("/api/pleroma/password_reset/token")
  15. |> html_response(:ok)
  16. assert response =~ "<h2>Invalid Token</h2>"
  17. end
  18. test "it shows password reset form", %{conn: conn} do
  19. user = insert(:user)
  20. {:ok, token} = PasswordResetToken.create_token(user)
  21. response =
  22. conn
  23. |> get("/api/pleroma/password_reset/#{token.token}")
  24. |> html_response(:ok)
  25. assert response =~ "<h2>Password Reset for #{user.nickname}</h2>"
  26. end
  27. end
  28. describe "POST /api/pleroma/password_reset" do
  29. test "it returns HTTP 200", %{conn: conn} do
  30. user = insert(:user)
  31. {:ok, token} = PasswordResetToken.create_token(user)
  32. {:ok, _access_token} = Token.create(insert(:oauth_app), user, %{})
  33. params = %{
  34. "password" => "test",
  35. password_confirmation: "test",
  36. token: token.token
  37. }
  38. response =
  39. conn
  40. |> assign(:user, user)
  41. |> post("/api/pleroma/password_reset", %{data: params})
  42. |> html_response(:ok)
  43. assert response =~ "<h2>Password changed!</h2>"
  44. user = refresh_record(user)
  45. assert Pbkdf2.verify_pass("test", user.password_hash)
  46. assert Enum.empty?(Token.get_user_tokens(user))
  47. end
  48. test "it sets password_reset_pending to false", %{conn: conn} do
  49. user = insert(:user, password_reset_pending: true)
  50. {:ok, token} = PasswordResetToken.create_token(user)
  51. {:ok, _access_token} = Token.create(insert(:oauth_app), user, %{})
  52. params = %{
  53. "password" => "test",
  54. password_confirmation: "test",
  55. token: token.token
  56. }
  57. conn
  58. |> assign(:user, user)
  59. |> post("/api/pleroma/password_reset", %{data: params})
  60. |> html_response(:ok)
  61. assert User.get_by_id(user.id).password_reset_pending == false
  62. end
  63. end
  64. end