1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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"
14 user = insert(:user, nickname: name, password_hash: Pbkdf2.hash_pwd_salt(password))
15 {:ok, [user: user, name: name, password: password]}
18 test "get_user/authorization", %{name: name, password: password} do
20 user = insert(:user, nickname: name, password_hash: Bcrypt.hash_pwd_salt(password))
22 params = %{"authorization" => %{"name" => name, "password" => password}}
23 res = PleromaAuthenticator.get_user(%Plug.Conn{params: params})
25 assert {:ok, returned_user} = res
26 assert returned_user.id == user.id
27 assert "$pbkdf2" <> _ = returned_user.password_hash
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})
34 assert {:error, {:checkpw, false}} == res
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})
41 assert {:ok, user} == res
44 test "error credintails" do
45 res = PleromaAuthenticator.get_user(%Plug.Conn{params: %{}})
46 assert {:error, :invalid_credentials} == res