livebook/test/livebook_web/plugs/user_plug_test.exs
José Valim 29c5cb1904 ZTA revamp
* Rename SessionIdentity to PassThrough and make it part of ZTA

* Compute the ID at the Plug level, rather than ZTA level and
  avoid storing it twice

* Stop the user "avatar" from flashing on initial render

* Do not duplicate identity data inside user data, rather keep
  them distinct
2024-04-13 10:29:22 +02:00

55 lines
1.4 KiB
Elixir

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(%{})
|> fetch_cookies()
|> call()
assert get_session(conn, :identity_data)[:id] != nil
end
test "keeps user id in the session if present" do
conn =
conn(:get, "/")
|> init_test_session(%{identity_data: %{id: "valid_user_id"}})
|> fetch_cookies()
|> call()
assert get_session(conn, :identity_data)[:id] != nil
end
test "given no user_data cookie, generates and stores new data" do
conn =
conn(:get, "/")
|> init_test_session(%{})
|> fetch_cookies()
|> call()
assert %{
"name" => nil,
"hex_color" => <<_::binary>>
} = conn.cookies["lb_user_data"] |> Base.decode64!() |> Jason.decode!()
end
test "keeps user_data cookie if present" do
cookie_value =
%{name: "Jake Peralta", hex_color: "#000000"} |> Jason.encode!() |> Base.encode64()
conn =
conn(:get, "/")
|> init_test_session(%{})
|> put_req_cookie("lb_user_data", cookie_value)
|> fetch_cookies()
|> call()
assert conn.cookies["lb_user_data"] == cookie_value
end
end