Skip to content

Instantly share code, notes, and snippets.

@nmerouze
Created May 12, 2017 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmerouze/4897e509f4c4b577dc2fb8addd5b3ef9 to your computer and use it in GitHub Desktop.
Save nmerouze/4897e509f4c4b577dc2fb8addd5b3ef9 to your computer and use it in GitHub Desktop.
defmodule Dd.Web.ConnCase do
use ExUnit.CaseTemplate
using do
quote do
use Phoenix.ConnTest
import Dd.Web.Router.Helpers
import Dd.Web.TestHelpers # Helpers added directly to ConnCase
@endpoint Dd.Web.Endpoint
end
end
setup tags do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Dd.Repo)
unless tags[:async] do
Ecto.Adapters.SQL.Sandbox.mode(Dd.Repo, {:shared, self()})
end
{:ok, conn: Phoenix.ConnTest.build_conn()}
end
end
defmodule Dd.Web.TestHelpers do
use Phoenix.ConnTest
def json_api_conn do
build_conn()
|> put_req_header("accept", "application/vnd.api+json")
|> put_req_header("content-type", "application/vnd.api+json")
end
def json_api_conn(user) do
conn = Guardian.Plug.api_sign_in(build_conn(), user)
token = Guardian.Plug.current_token(conn)
json_api_conn()
|> put_req_header("authorization", "Bearer " <> token)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment