Merge branch 'cachex-test' into 'develop'
[akkoma] / test / pleroma / web / auth / pleroma_authenticator_test.exs
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
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 user = insert(:user, nickname: name, password_hash: Pbkdf2.hash_pwd_salt(password))
15 {:ok, [user: user, name: name, password: password]}
16 end
17
18 test "get_user/authorization", %{name: name, password: password} do
19 name = name <> "1"
20 user = insert(:user, nickname: name, password_hash: Bcrypt.hash_pwd_salt(password))
21
22 params = %{"authorization" => %{"name" => name, "password" => password}}
23 res = PleromaAuthenticator.get_user(%Plug.Conn{params: params})
24
25 assert {:ok, returned_user} = res
26 assert returned_user.id == user.id
27 assert "$pbkdf2" <> _ = returned_user.password_hash
28 end
29
30 test "get_user/authorization with invalid password", %{name: name} do
31 params = %{"authorization" => %{"name" => name, "password" => "password"}}
32 res = PleromaAuthenticator.get_user(%Plug.Conn{params: params})
33
34 assert {:error, {:checkpw, false}} == res
35 end
36
37 test "get_user/grant_type_password", %{user: user, name: name, password: password} do
38 params = %{"grant_type" => "password", "username" => name, "password" => password}
39 res = PleromaAuthenticator.get_user(%Plug.Conn{params: params})
40
41 assert {:ok, user} == res
42 end
43
44 test "error credintails" do
45 res = PleromaAuthenticator.get_user(%Plug.Conn{params: %{}})
46 assert {:error, :invalid_credentials} == res
47 end
48 end