1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Auth.PleromaAuthenticatorTest do
6 use Pleroma.Web.ConnCase, async: true
8 alias Pleroma.Web.Auth.PleromaAuthenticator
12 password = "testpassword"
18 password_hash: Pleroma.Password.hash_pwd_salt(password)
21 {:ok, [user: user, name: name, password: password]}
24 test "get_user/authorization", %{name: name, password: password} do
26 user = insert(:user, nickname: name, password_hash: Bcrypt.hash_pwd_salt(password))
28 params = %{"authorization" => %{"name" => name, "password" => password}}
29 res = PleromaAuthenticator.get_user(%Plug.Conn{params: params})
31 assert {:ok, returned_user} = res
32 assert returned_user.id == user.id
33 assert "$argon2" <> _ = returned_user.password_hash
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})
40 assert {:error, {:checkpw, false}} == res
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})
47 assert {:ok, user} == res
50 test "error credintails" do
51 res = PleromaAuthenticator.get_user(%Plug.Conn{params: %{}})
52 assert {:error, :invalid_credentials} == res