2021-05-04 02:03:19 +08:00
|
|
|
defmodule LivebookWeb.UserPlugTest do
|
|
|
|
use ExUnit.Case, async: true
|
|
|
|
use Plug.Test
|
|
|
|
|
|
|
|
defp call(conn) do
|
|
|
|
LivebookWeb.UserPlug.call(conn, LivebookWeb.UserPlug.init([]))
|
|
|
|
end
|
|
|
|
|
|
|
|
test "given no user id in the session, generates a new user id" do
|
|
|
|
conn =
|
|
|
|
conn(:get, "/")
|
|
|
|
|> init_test_session(%{})
|
2021-06-16 01:33:16 +08:00
|
|
|
|> fetch_cookies()
|
2021-05-04 02:03:19 +08:00
|
|
|
|> call()
|
|
|
|
|
|
|
|
assert get_session(conn, :current_user_id) != nil
|
|
|
|
end
|
|
|
|
|
|
|
|
test "keeps user id in the session if present" do
|
|
|
|
conn =
|
|
|
|
conn(:get, "/")
|
|
|
|
|> init_test_session(%{current_user_id: "valid_user_id"})
|
2021-06-16 01:33:16 +08:00
|
|
|
|> fetch_cookies()
|
2021-05-04 02:03:19 +08:00
|
|
|
|> call()
|
|
|
|
|
|
|
|
assert get_session(conn, :current_user_id) != nil
|
|
|
|
end
|
|
|
|
|
|
|
|
test "given no user_data cookie, generates and stores new data" do
|
|
|
|
conn =
|
|
|
|
conn(:get, "/")
|
|
|
|
|> init_test_session(%{})
|
|
|
|
|> fetch_cookies()
|
|
|
|
|> call()
|
|
|
|
|
2023-03-28 23:11:44 +08:00
|
|
|
assert conn.cookies["lb:user_data"] != nil
|
2021-05-04 02:03:19 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
test "keeps user_data cookie if present" do
|
2021-06-16 01:33:16 +08:00
|
|
|
cookie_value =
|
|
|
|
%{name: "Jake Peralta", hex_color: "#000000"} |> Jason.encode!() |> Base.encode64()
|
2021-05-04 02:03:19 +08:00
|
|
|
|
|
|
|
conn =
|
|
|
|
conn(:get, "/")
|
|
|
|
|> init_test_session(%{})
|
2023-03-28 23:11:44 +08:00
|
|
|
|> put_req_cookie("lb:user_data", cookie_value)
|
2021-05-04 02:03:19 +08:00
|
|
|
|> fetch_cookies()
|
|
|
|
|> call()
|
|
|
|
|
2023-03-28 23:11:44 +08:00
|
|
|
assert conn.cookies["lb:user_data"] == cookie_value
|
2021-05-04 02:03:19 +08:00
|
|
|
end
|
|
|
|
end
|