logo

pleroma

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

pleroma_authenticator_test.exs (1706B)


  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.Auth.PleromaAuthenticatorTest do
  5. use Pleroma.Web.ConnCase
  6. alias Pleroma.Web.Auth.PleromaAuthenticator
  7. import Pleroma.Factory
  8. setup do
  9. password = "testpassword"
  10. name = "AgentSmith"
  11. user = insert(:user, nickname: name, password_hash: Pbkdf2.hash_pwd_salt(password))
  12. {:ok, [user: user, name: name, password: password]}
  13. end
  14. test "get_user/authorization", %{name: name, password: password} do
  15. name = name <> "1"
  16. user = insert(:user, nickname: name, password_hash: Bcrypt.hash_pwd_salt(password))
  17. params = %{"authorization" => %{"name" => name, "password" => password}}
  18. res = PleromaAuthenticator.get_user(%Plug.Conn{params: params})
  19. assert {:ok, returned_user} = res
  20. assert returned_user.id == user.id
  21. assert "$pbkdf2" <> _ = returned_user.password_hash
  22. end
  23. test "get_user/authorization with invalid password", %{name: name} do
  24. params = %{"authorization" => %{"name" => name, "password" => "password"}}
  25. res = PleromaAuthenticator.get_user(%Plug.Conn{params: params})
  26. assert {:error, {:checkpw, false}} == res
  27. end
  28. test "get_user/grant_type_password", %{user: user, name: name, password: password} do
  29. params = %{"grant_type" => "password", "username" => name, "password" => password}
  30. res = PleromaAuthenticator.get_user(%Plug.Conn{params: params})
  31. assert {:ok, user} == res
  32. end
  33. test "error credintails" do
  34. res = PleromaAuthenticator.get_user(%Plug.Conn{params: %{}})
  35. assert {:error, :invalid_credentials} == res
  36. end
  37. end