Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.1KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Plugs.LegacyAuthenticationPlugTest do
  5. use Pleroma.Web.ConnCase, async: true
  6. alias Pleroma.Plugs.LegacyAuthenticationPlug
  7. alias Pleroma.User
  8. import Mock
  9. setup do
  10. # password is "password"
  11. user = %User{
  12. id: 1,
  13. name: "dude",
  14. password_hash:
  15. "$6$9psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
  16. }
  17. %{user: user}
  18. end
  19. test "it does nothing if a user is assigned", %{conn: conn, user: user} do
  20. conn =
  21. conn
  22. |> assign(:auth_credentials, %{username: "dude", password: "password"})
  23. |> assign(:auth_user, user)
  24. |> assign(:user, %User{})
  25. ret_conn =
  26. conn
  27. |> LegacyAuthenticationPlug.call(%{})
  28. assert ret_conn == conn
  29. end
  30. test "it authenticates the auth_user if present and password is correct and resets the password",
  31. %{
  32. conn: conn,
  33. user: user
  34. } do
  35. conn =
  36. conn
  37. |> assign(:auth_credentials, %{username: "dude", password: "password"})
  38. |> assign(:auth_user, user)
  39. conn =
  40. with_mock User,
  41. reset_password: fn user, %{password: password, password_confirmation: password} ->
  42. send(self(), :reset_password)
  43. {:ok, user}
  44. end do
  45. conn
  46. |> LegacyAuthenticationPlug.call(%{})
  47. end
  48. assert_received :reset_password
  49. assert conn.assigns.user == user
  50. end
  51. test "it does nothing if the password is wrong", %{
  52. conn: conn,
  53. user: user
  54. } do
  55. conn =
  56. conn
  57. |> assign(:auth_credentials, %{username: "dude", password: "wrong_password"})
  58. |> assign(:auth_user, user)
  59. ret_conn =
  60. conn
  61. |> LegacyAuthenticationPlug.call(%{})
  62. assert conn == ret_conn
  63. end
  64. test "with no credentials or user it does nothing", %{conn: conn} do
  65. ret_conn =
  66. conn
  67. |> LegacyAuthenticationPlug.call(%{})
  68. assert ret_conn == conn
  69. end
  70. end