Skip to content

Commit

Permalink
refactor:test(ex/conn_case): split conn creation into helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
firestack committed Oct 21, 2024
1 parent 0d224a6 commit b487760
Showing 1 changed file with 47 additions and 38 deletions.
85 changes: 47 additions & 38 deletions test/support/conn_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ defmodule SkateWeb.ConnCase do
"""

use ExUnit.CaseTemplate
import Plug.Test
alias Skate.Settings.User

using do
quote do
Expand All @@ -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 = "[email protected]"

:ok = Sandbox.checkout(Skate.Repo)
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()
|> Phoenix.ConnTest.init_test_session(%{})
|> Guardian.Plug.sign_in(SkateWeb.AuthManager, resource_from_user(user), %{})

{:ok, %{conn: conn, user: user}}
end

conn =
Phoenix.ConnTest.build_conn()
|> init_test_session(%{})
|> Guardian.Plug.sign_in(SkateWeb.AuthManager, resource, %{})
def setup_from_tags(%{authenticated_admin: true}) do
user = create_default_user()

{conn, user}
conn =
Phoenix.ConnTest.build_conn()
|> Phoenix.ConnTest.init_test_session(%{})
|> Guardian.Plug.sign_in(SkateWeb.AuthManager, resource_from_user(user), %{
"groups" => ["skate-admin"]
})

tags[:authenticated_admin] ->
User.upsert(username, email)
{:ok, %{conn: conn, user: user}}
end

conn =
Phoenix.ConnTest.build_conn()
|> init_test_session(%{})
|> Guardian.Plug.sign_in(SkateWeb.AuthManager, resource, %{
"groups" => ["skate-admin"]
})
def setup_from_tags(%{authenticated_dispatcher: true}) do
user = create_default_user()

{conn, user}
conn =
Phoenix.ConnTest.build_conn()
|> Phoenix.ConnTest.init_test_session(%{})
|> Guardian.Plug.sign_in(SkateWeb.AuthManager, resource_from_user(user), %{
"groups" => ["skate-dispatcher"]
})

tags[:authenticated_dispatcher] ->
User.upsert(username, email)
{:ok, %{conn: conn, user: user}}
end

conn =
Phoenix.ConnTest.build_conn()
|> init_test_session(%{})
|> Guardian.Plug.sign_in(SkateWeb.AuthManager, resource, %{
"groups" => ["skate-dispatcher"]
})
def setup_from_tags(_) do
# user = create_default_user()
{:ok, %{conn: Phoenix.ConnTest.build_conn(), user: nil}}
end

{conn, user}
# Factory to create users
# Currently uses hardcoded information, but is subject to change
defp create_default_user() do
username = "test_user"
email = "[email protected]"

true ->
{Phoenix.ConnTest.build_conn(), nil}
end
Skate.Settings.User.upsert(username, email)
end

{:ok, %{conn: conn, user: user}}
# Creates a Guardian resource for a `User`
defp resource_from_user(%Skate.Settings.Db.User{id: id}) do
%{id: id}
end
end

0 comments on commit b487760

Please sign in to comment.