diff --git a/apps/transport/lib/transport_web/session.ex b/apps/transport/lib/transport_web/session.ex index e3e8fca929..9408470f66 100644 --- a/apps/transport/lib/transport_web/session.ex +++ b/apps/transport/lib/transport_web/session.ex @@ -20,12 +20,9 @@ defmodule TransportWeb.Session do set_session_attribute_attribute(conn, @is_producer_key_name, is_producer?(params)) end - def set_is_producer(%Plug.Conn{} = conn, [%DB.Dataset{}] = _datasets_for_user) do - set_session_attribute_attribute(conn, @is_producer_key_name, true) - end - - def set_is_producer(%Plug.Conn{} = conn, [] = _datasets_for_user) do - set_session_attribute_attribute(conn, @is_producer_key_name, false) + def set_is_producer(%Plug.Conn{} = conn, datasets_for_user) when is_list(datasets_for_user) do + is_producer = not Enum.empty?(datasets_for_user) + set_session_attribute_attribute(conn, @is_producer_key_name, is_producer) end @doc """ diff --git a/apps/transport/test/transport_web/session_text.exs b/apps/transport/test/transport_web/session_test.exs similarity index 65% rename from apps/transport/test/transport_web/session_text.exs rename to apps/transport/test/transport_web/session_test.exs index ebaeb26085..cf24adcda4 100644 --- a/apps/transport/test/transport_web/session_text.exs +++ b/apps/transport/test/transport_web/session_test.exs @@ -27,7 +27,7 @@ defmodule TransportWeb.SessionTest do test "is_admin?" do refute is_admin?(Plug.Test.init_test_session(%Plug.Conn{}, %{})) assert is_admin?(Plug.Test.init_test_session(%Plug.Conn{}, %{current_user: %{"is_admin" => true}})) - assert is_admin?(%{"is_admin" => true}) + assert is_admin?(%Phoenix.LiveView.Socket{assigns: %{current_user: %{"is_admin" => true}}}) end test "is_producer?" do @@ -36,6 +36,24 @@ defmodule TransportWeb.SessionTest do end end + describe "set_is_producer" do + test "no datasets" do + assert %{"is_producer" => false} == + %Plug.Conn{} + |> Plug.Test.init_test_session(%{current_user: %{}}) + |> set_is_producer([]) + |> Plug.Conn.get_session(:current_user) + end + + test "2 datasets" do + assert %{"is_producer" => true} == + %Plug.Conn{} + |> Plug.Test.init_test_session(%{current_user: %{}}) + |> set_is_producer([build(:dataset), build(:dataset)]) + |> Plug.Conn.get_session(:current_user) + end + end + def pan_org do %{"slug" => "equipe-transport-data-gouv-fr", "name" => "PAN", "id" => @pan_org_id} end