1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Plugs.AuthenticationPlugTest do
6 use Pleroma.Web.ConnCase, async: true
8 alias Pleroma.Plugs.AuthenticationPlug
11 setup %{conn: conn} do
15 password_hash: Comeonin.Pbkdf2.hashpwsalt("guy")
20 |> assign(:auth_user, user)
22 %{user: user, conn: conn}
25 test "it does nothing if a user is assigned", %{conn: conn} do
28 |> assign(:user, %User{})
32 |> AuthenticationPlug.call(%{})
34 assert ret_conn == conn
37 test "with a correct password in the credentials, it assigns the auth_user", %{conn: conn} do
40 |> assign(:auth_credentials, %{password: "guy"})
41 |> AuthenticationPlug.call(%{})
43 assert conn.assigns.user == conn.assigns.auth_user
46 test "with a wrong password in the credentials, it does nothing", %{conn: conn} do
49 |> assign(:auth_credentials, %{password: "wrong"})
53 |> AuthenticationPlug.call(%{})
55 assert conn == ret_conn
58 describe "checkpw/2" do
59 test "check pbkdf2 hash" do
61 "$pbkdf2-sha512$160000$loXqbp8GYls43F0i6lEfIw$AY.Ep.2pGe57j2hAPY635sI/6w7l9Q9u9Bp02PkPmF3OrClDtJAI8bCiivPr53OKMF7ph6iHhN68Rom5nEfC2A"
63 assert AuthenticationPlug.checkpw("test-password", hash)
64 refute AuthenticationPlug.checkpw("test-password1", hash)
67 test "check sha512-crypt hash" do
69 "$6$9psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
71 assert AuthenticationPlug.checkpw("password", hash)
72 refute AuthenticationPlug.checkpw("password1", hash)
75 test "it returns false when hash invalid" do
77 "psBWV8gxkGOZWBz$PmfCycChoxeJ3GgGzwvhlgacb9mUoZ.KUXNCssekER4SJ7bOK53uXrHNb2e4i8yPFgSKyzaW9CcmrDXWIEMtD1"
79 refute Pleroma.Plugs.AuthenticationPlug.checkpw("password", hash)