-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor:test(ex/conn_case): split
conn
creation into helper function
- Loading branch information
Showing
1 changed file
with
47 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = "[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 |