Skip to content

Commit

Permalink
TransportWeb.Session : corrige erreur pattern matching
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineAugusti committed Jan 9, 2024
1 parent 1d3f4d4 commit 4e43ddd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
9 changes: 3 additions & 6 deletions apps/transport/lib/transport_web/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 4e43ddd

Please sign in to comment.