Merge branch 'develop' into 'remove-twitter-api'
[akkoma] / test / web / auth / pleroma_authenticator_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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
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", %{user: user, name: name, password: password} do
19 params = %{"authorization" => %{"name" => name, "password" => password}}
20 res = PleromaAuthenticator.get_user(%Plug.Conn{params: params})
21
22 assert {:ok, user} == res
23 end
24
25 test "get_user/authorization with invalid password", %{name: name} do
26 params = %{"authorization" => %{"name" => name, "password" => "password"}}
27 res = PleromaAuthenticator.get_user(%Plug.Conn{params: params})
28
29 assert {:error, {:checkpw, false}} == res
30 end
31
32 test "get_user/grant_type_password", %{user: user, name: name, password: password} do
33 params = %{"grant_type" => "password", "username" => name, "password" => password}
34 res = PleromaAuthenticator.get_user(%Plug.Conn{params: params})
35
36 assert {:ok, user} == res
37 end
38
39 test "error credintails" do
40 res = PleromaAuthenticator.get_user(%Plug.Conn{params: %{}})
41 assert {:error, :invalid_credentials} == res
42 end
43 end