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.3KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Plugs.AuthenticationPlugTest do
  5. use Pleroma.Web.ConnCase, async: true
  6. alias Pleroma.Plugs.AuthenticationPlug
  7. alias Pleroma.User
  8. import ExUnit.CaptureLog
  9. setup %{conn: conn} do
  10. user = %User{
  11. id: 1,
  12. name: "dude",
  13. password_hash: Comeonin.Pbkdf2.hashpwsalt("guy")
  14. }
  15. conn =
  16. conn
  17. |> assign(:auth_user, user)
  18. %{user: user, conn: conn}
  19. end
  20. test "it does nothing if a user is assigned", %{conn: conn} do
  21. conn =
  22. conn
  23. |> assign(:user, %User{})
  24. ret_conn =
  25. conn
  26. |> AuthenticationPlug.call(%{})
  27. assert ret_conn == conn
  28. end
  29. test "with a correct password in the credentials, it assigns the auth_user", %{conn: conn} do
  30. conn =
  31. conn
  32. |> assign(:auth_credentials, %{password: "guy"})
  33. |> AuthenticationPlug.call(%{})
  34. assert conn.assigns.user == conn.assigns.auth_user
  35. end
  36. test "with a wrong password in the credentials, it does nothing", %{conn: conn} do
  37. conn =
  38. conn
  39. |> assign(:auth_credentials, %{password: "wrong"})
  40. ret_conn =
  41. conn
  42. |> AuthenticationPlug.call(%{})
  43. assert conn == ret_conn
  44. end
  45. describe "checkpw/2" do
  46. test "check pbkdf2 hash" do
  47. hash =
  48. "$pbkdf2-sha512$160000$loXqbp8GYls43F0i6lEfIw$AY.Ep.2pGe57j2hAPY635sI/6w7l9Q9u9Bp02PkPmF3OrClDtJAI8bCiivPr53OKMF7ph6iHhN68Rom5nEfC2A"
  49. assert AuthenticationPlug.checkpw("test-password", hash)
  50. refute AuthenticationPlug.checkpw("test-password1", hash)
  51. end
  52. @tag :skip_on_mac
  53. test "check sha512-crypt hash" do
  54. hash =
  55. "$6$9psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
  56. assert AuthenticationPlug.checkpw("password", hash)
  57. end
  58. test "it returns false when hash invalid" do
  59. hash =
  60. "psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
  61. assert capture_log(fn ->
  62. refute Pleroma.Plugs.AuthenticationPlug.checkpw("password", hash)
  63. end) =~ "[error] Password hash not recognized"
  64. end
  65. end
  66. end