From 86109eaa6ff19037be0c1dce5b4033d4c952758a Mon Sep 17 00:00:00 2001 From: Kayla Firestack Date: Sat, 12 Oct 2024 15:57:49 -0400 Subject: [PATCH] refactor:test(ex/conn_case): split `conn` creation into helper function --- test/support/conn_case.ex | 83 ++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 37 deletions(-) diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index b67c90d7c..7e44d919e 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -14,8 +14,6 @@ defmodule SkateWeb.ConnCase do """ use ExUnit.CaseTemplate - import Plug.Test - alias Skate.Settings.User using do quote do @@ -33,57 +31,68 @@ defmodule SkateWeb.ConnCase do setup tags do alias Ecto.Adapters.SQL.Sandbox + :ok = Sandbox.checkout(Skate.Repo) - username = "test_user" - email = "test_user@test.com" unless tags[:async] do Sandbox.mode(Skate.Repo, {:shared, self()}) end - user = User.upsert(username, email) - resource = %{id: user.id} + setup_from_tags(tags) + end - {conn, user} = - cond do - tags[:authenticated] -> - User.upsert(username, email) + def setup_from_tags(%{authenticated: true}) do + user = create_default_user() - conn = - Phoenix.ConnTest.build_conn() - |> init_test_session(%{}) - |> Guardian.Plug.sign_in(SkateWeb.AuthManager, resource, %{}) + conn = + Phoenix.ConnTest.build_conn() + |> Phoenix.ConnTest.init_test_session(%{}) + |> Guardian.Plug.sign_in(SkateWeb.AuthManager, resource_from_user(user), %{}) - {conn, user} + {:ok, %{conn: conn, user: user}} + end - tags[:authenticated_admin] -> - User.upsert(username, email) + def setup_from_tags(%{authenticated_admin: true}) do + user = create_default_user() - conn = - Phoenix.ConnTest.build_conn() - |> init_test_session(%{}) - |> Guardian.Plug.sign_in(SkateWeb.AuthManager, resource, %{ - "groups" => ["skate-admin"] - }) + conn = + Phoenix.ConnTest.build_conn() + |> Phoenix.ConnTest.init_test_session(%{}) + |> Guardian.Plug.sign_in(SkateWeb.AuthManager, resource_from_user(user), %{ + "groups" => ["skate-admin"] + }) - {conn, user} + {:ok, %{conn: conn, user: user}} + end - tags[:authenticated_dispatcher] -> - User.upsert(username, email) + def setup_from_tags(%{authenticated_dispatcher: true}) do + user = create_default_user() - conn = - Phoenix.ConnTest.build_conn() - |> init_test_session(%{}) - |> Guardian.Plug.sign_in(SkateWeb.AuthManager, resource, %{ - "groups" => ["skate-dispatcher"] - }) + conn = + Phoenix.ConnTest.build_conn() + |> Phoenix.ConnTest.init_test_session(%{}) + |> Guardian.Plug.sign_in(SkateWeb.AuthManager, resource_from_user(user), %{ + "groups" => ["skate-dispatcher"] + }) - {conn, user} + {:ok, %{conn: conn, user: user}} + end - true -> - {Phoenix.ConnTest.build_conn(), nil} - end + def setup_from_tags(_) do + {:ok, %{conn: Phoenix.ConnTest.build_conn(), user: nil}} + end - {:ok, %{conn: conn, user: user}} + # Factory to create users + # Currently uses hardcoded information, but is subject to change + defp create_default_user() do + username = "test_user" + email = "test_user@test.com" + + Skate.Settings.User.upsert(username, email) + end + + # Creates a Guardian resource for a `User` + defp resource_from_user(%Skate.Settings.Db.User{id: id}) do + %{id: id} end end