First
[anni] / test / pleroma / web / plugs / set_user_session_id_plug_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Plugs.SetUserSessionIdPlugTest do
6   use Pleroma.Web.ConnCase, async: true
7
8   alias Pleroma.Helpers.AuthHelper
9   alias Pleroma.Web.Plugs.SetUserSessionIdPlug
10
11   setup %{conn: conn} do
12     session_opts = [
13       store: :cookie,
14       key: "_test",
15       signing_salt: "cooldude"
16     ]
17
18     conn =
19       conn
20       |> Plug.Session.call(Plug.Session.init(session_opts))
21       |> fetch_session()
22
23     %{conn: conn}
24   end
25
26   test "doesn't do anything if the user isn't set", %{conn: conn} do
27     ret_conn = SetUserSessionIdPlug.call(conn, %{})
28
29     assert ret_conn == conn
30   end
31
32   test "sets session token basing on :token assign", %{conn: conn} do
33     %{user: user, token: oauth_token} = oauth_access(["read"])
34
35     ret_conn =
36       conn
37       |> assign(:user, user)
38       |> assign(:token, oauth_token)
39       |> SetUserSessionIdPlug.call(%{})
40
41     assert AuthHelper.get_session_token(ret_conn) == oauth_token.token
42   end
43 end