move to 2.5.5
[anni] / test / pleroma / web / auth / pleroma_authenticator_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Auth.PleromaAuthenticatorTest do
6   use Pleroma.Web.ConnCase, async: true
7
8   alias Pleroma.Web.Auth.PleromaAuthenticator
9   import Pleroma.Factory
10
11   setup do
12     password = "testpassword"
13     name = "AgentSmith"
14
15     user =
16       insert(:user,
17         nickname: name,
18         password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt(password)
19       )
20
21     {:ok, [user: user, name: name, password: password]}
22   end
23
24   test "get_user/authorization", %{name: name, password: password} do
25     name = name <> "1"
26     user = insert(:user, nickname: name, password_hash: Bcrypt.hash_pwd_salt(password))
27
28     params = %{"authorization" => %{"name" => name, "password" => password}}
29     res = PleromaAuthenticator.get_user(%Plug.Conn{params: params})
30
31     assert {:ok, returned_user} = res
32     assert returned_user.id == user.id
33     assert "$pbkdf2" <> _ = returned_user.password_hash
34   end
35
36   test "get_user/authorization with invalid password", %{name: name} do
37     params = %{"authorization" => %{"name" => name, "password" => "password"}}
38     res = PleromaAuthenticator.get_user(%Plug.Conn{params: params})
39
40     assert {:error, {:checkpw, false}} == res
41   end
42
43   test "get_user/grant_type_password", %{user: user, name: name, password: password} do
44     params = %{"grant_type" => "password", "username" => name, "password" => password}
45     res = PleromaAuthenticator.get_user(%Plug.Conn{params: params})
46
47     assert {:ok, user} == res
48   end
49
50   test "error credintails" do
51     res = PleromaAuthenticator.get_user(%Plug.Conn{params: %{}})
52     assert {:error, :invalid_credentials} == res
53   end
54 end