Merge branch 'feature/move-activity' into 'develop'
[akkoma] / test / plugs / admin_secret_authentication_plug_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Plugs.AdminSecretAuthenticationPlugTest do
6 use Pleroma.Web.ConnCase, async: true
7 import Pleroma.Factory
8
9 alias Pleroma.Plugs.AdminSecretAuthenticationPlug
10
11 test "does nothing if a user is assigned", %{conn: conn} do
12 user = insert(:user)
13
14 conn =
15 conn
16 |> assign(:user, user)
17
18 ret_conn =
19 conn
20 |> AdminSecretAuthenticationPlug.call(%{})
21
22 assert conn == ret_conn
23 end
24
25 describe "when secret set it assigns an admin user" do
26 test "with `admin_token` query parameter", %{conn: conn} do
27 Pleroma.Config.put(:admin_token, "password123")
28
29 conn =
30 %{conn | params: %{"admin_token" => "wrong_password"}}
31 |> AdminSecretAuthenticationPlug.call(%{})
32
33 refute conn.assigns[:user]
34
35 conn =
36 %{conn | params: %{"admin_token" => "password123"}}
37 |> AdminSecretAuthenticationPlug.call(%{})
38
39 assert conn.assigns[:user].is_admin
40 end
41
42 test "with `x-admin-token` HTTP header", %{conn: conn} do
43 Pleroma.Config.put(:admin_token, "☕️")
44
45 conn =
46 conn
47 |> put_req_header("x-admin-token", "🥛")
48 |> AdminSecretAuthenticationPlug.call(%{})
49
50 refute conn.assigns[:user]
51
52 conn =
53 conn
54 |> put_req_header("x-admin-token", "☕️")
55 |> AdminSecretAuthenticationPlug.call(%{})
56
57 assert conn.assigns[:user].is_admin
58 end
59 end
60 end