Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor:test(ex/conn_case): split conn creation into helper function #2864

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 46 additions & 37 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 = "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
Loading