summaryrefslogtreecommitdiff
path: root/test/pleroma/web/plugs/set_user_session_id_plug_test.exs
blob: a89b5628fb0e3b0791e05215b50461b3857d6e49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.Plugs.SetUserSessionIdPlugTest do
  use Pleroma.Web.ConnCase, async: true

  alias Pleroma.User
  alias Pleroma.Web.Plugs.SetUserSessionIdPlug

  setup %{conn: conn} do
    session_opts = [
      store: :cookie,
      key: "_test",
      signing_salt: "cooldude"
    ]

    conn =
      conn
      |> Plug.Session.call(Plug.Session.init(session_opts))
      |> fetch_session

    %{conn: conn}
  end

  test "doesn't do anything if the user isn't set", %{conn: conn} do
    ret_conn =
      conn
      |> SetUserSessionIdPlug.call(%{})

    assert ret_conn == conn
  end

  test "sets the user_id in the session to the user id of the user assign", %{conn: conn} do
    Code.ensure_compiled(Pleroma.User)

    conn =
      conn
      |> assign(:user, %User{id: 1})
      |> SetUserSessionIdPlug.call(%{})

    id = get_session(conn, :user_id)
    assert id == 1
  end
end