daily-tracker

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

conn_case.ex (2283B)


      1 defmodule DailyTrackerWeb.ConnCase do
      2   @moduledoc """
      3   This module defines the test case to be used by
      4   tests that require setting up a connection.
      5 
      6   Such tests rely on `Phoenix.ConnTest` and also
      7   import other functionality to make it easier
      8   to build common data structures and query the data layer.
      9 
     10   Finally, if the test case interacts with the database,
     11   we enable the SQL sandbox, so changes done to the database
     12   are reverted at the end of every test. If you are using
     13   PostgreSQL, you can even run database tests asynchronously
     14   by setting `use DailyTrackerWeb.ConnCase, async: true`, although
     15   this option is not recommended for other databases.
     16   """
     17 
     18   use ExUnit.CaseTemplate
     19 
     20   using do
     21     quote do
     22       # The default endpoint for testing
     23       @endpoint DailyTrackerWeb.Endpoint
     24 
     25       use DailyTrackerWeb, :verified_routes
     26 
     27       # Import conveniences for testing with connections
     28       import Plug.Conn
     29       import Phoenix.ConnTest
     30       import DailyTrackerWeb.ConnCase
     31     end
     32   end
     33 
     34   setup tags do
     35     DailyTracker.DataCase.setup_sandbox(tags)
     36     {:ok, conn: Phoenix.ConnTest.build_conn()}
     37   end
     38 
     39   @doc """
     40   Setup helper that registers and logs in users.
     41 
     42       setup :register_and_log_in_user
     43 
     44   It stores an updated connection and a registered user in the
     45   test context.
     46   """
     47   def register_and_log_in_user(%{conn: conn} = context) do
     48     user = DailyTracker.AccountsFixtures.user_fixture()
     49     scope = DailyTracker.Accounts.Scope.for_user(user)
     50 
     51     opts =
     52       context
     53       |> Map.take([:token_authenticated_at])
     54       |> Enum.into([])
     55 
     56     %{conn: log_in_user(conn, user, opts), user: user, scope: scope}
     57   end
     58 
     59   @doc """
     60   Logs the given `user` into the `conn`.
     61 
     62   It returns an updated `conn`.
     63   """
     64   def log_in_user(conn, user, opts \\ []) do
     65     token = DailyTracker.Accounts.generate_user_session_token(user)
     66 
     67     maybe_set_token_authenticated_at(token, opts[:token_authenticated_at])
     68 
     69     conn
     70     |> Phoenix.ConnTest.init_test_session(%{})
     71     |> Plug.Conn.put_session(:user_token, token)
     72   end
     73 
     74   defp maybe_set_token_authenticated_at(_token, nil), do: nil
     75 
     76   defp maybe_set_token_authenticated_at(token, authenticated_at) do
     77     DailyTracker.AccountsFixtures.override_token_authenticated_at(token, authenticated_at)
     78   end
     79 end