Some checks failed
Build and Push / Build and Push Docker Image (push) Failing after 8m16s
60 lines
1.8 KiB
Elixir
60 lines
1.8 KiB
Elixir
defmodule MicrowavepropWeb.ConnCase do
|
|
@moduledoc """
|
|
Test case template for HTTP connection tests.
|
|
"""
|
|
|
|
use ExUnit.CaseTemplate
|
|
|
|
alias Microwaveprop.Accounts
|
|
alias Microwaveprop.Accounts.Scope
|
|
alias Microwaveprop.Accounts.User
|
|
alias Microwaveprop.DataCase
|
|
alias Phoenix.Ecto.SQL.Sandbox
|
|
|
|
using do
|
|
quote do
|
|
use MicrowavepropWeb, :verified_routes
|
|
|
|
import MicrowavepropWeb.ConnCase
|
|
import Phoenix.ConnTest
|
|
import Plug.Conn
|
|
|
|
@endpoint MicrowavepropWeb.Endpoint
|
|
end
|
|
end
|
|
|
|
setup tags do
|
|
owner = DataCase.setup_sandbox(tags)
|
|
DataCase.reset_test_state()
|
|
|
|
metadata = Sandbox.metadata_for(Microwaveprop.Repo, owner)
|
|
conn = Plug.Conn.put_req_header(Phoenix.ConnTest.build_conn(), "user-agent", Sandbox.encode_metadata(metadata))
|
|
|
|
{:ok, conn: conn}
|
|
end
|
|
|
|
@spec register_and_log_in_user(map()) :: %{conn: Plug.Conn.t(), user: User.t(), scope: Scope.t()}
|
|
def register_and_log_in_user(%{conn: conn} = context) do
|
|
user = Microwaveprop.AccountsFixtures.user_fixture()
|
|
scope = Scope.for_user(user)
|
|
|
|
opts = context |> Map.take([:token_authenticated_at]) |> Enum.to_list()
|
|
%{conn: log_in_user(conn, user, opts), user: user, scope: scope}
|
|
end
|
|
|
|
@spec log_in_user(Plug.Conn.t(), User.t(), Keyword.t()) :: Plug.Conn.t()
|
|
def log_in_user(conn, user, opts \\ []) do
|
|
token = Accounts.generate_user_session_token(user)
|
|
maybe_set_token_authenticated_at(token, opts[:token_authenticated_at])
|
|
|
|
conn
|
|
|> Phoenix.ConnTest.init_test_session(%{})
|
|
|> Plug.Conn.put_session(:user_token, token)
|
|
end
|
|
|
|
defp maybe_set_token_authenticated_at(_token, nil), do: nil
|
|
|
|
defp maybe_set_token_authenticated_at(token, authenticated_at) do
|
|
Microwaveprop.AccountsFixtures.override_token_authenticated_at(token, authenticated_at)
|
|
end
|
|
end
|