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.

89 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
  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_mocks([
  41. {:crypt, [], [crypt: fn _password, password_hash -> password_hash end]},
  42. {User, [],
  43. [
  44. reset_password: fn user, %{password: password, password_confirmation: password} ->
  45. {:ok, user}
  46. end
  47. ]}
  48. ]) do
  49. LegacyAuthenticationPlug.call(conn, %{})
  50. end
  51. assert conn.assigns.user == user
  52. end
  53. test "it does nothing if the password is wrong", %{
  54. conn: conn,
  55. user: user
  56. } do
  57. conn =
  58. conn
  59. |> assign(:auth_credentials, %{username: "dude", password: "wrong_password"})
  60. |> assign(:auth_user, user)
  61. ret_conn =
  62. conn
  63. |> LegacyAuthenticationPlug.call(%{})
  64. assert conn == ret_conn
  65. end
  66. test "with no credentials or user it does nothing", %{conn: conn} do
  67. ret_conn =
  68. conn
  69. |> LegacyAuthenticationPlug.call(%{})
  70. assert ret_conn == conn
  71. end
  72. end