I added Pow to my application using this guide because i have a JSON api structure, but after integrate with pow, my controller tests broke because neither of them needed to have a session before and now they do. The problem is that i’m not being able to make the tests work, my approach so far was change the default setup
method generated by phoenix to this:
# before
setup %{conn: conn} do
{:ok, conn: put_req_header(conn, "accept", "application/json")}
end
# now
setup %{conn: conn} do
user = user_fixture()
authed_conn =
conn
|> put_req_header("accept", "application/json")
|> pow.plug.assign_current_user(user, [])
{:ok, conn: authed_conn}
end
Now the index test pass successfully, but the others give me this error message:
** (RuntimeError) expected connection to have a response but no response was set/sent.
Please verify that you assign to "conn" after a request:
conn = get(conn, "/")
assert html_response(conn) =~ "Hello"
code: assert json_response(conn, 422)["errors"] != %{}
stacktrace:
(phoenix 1.7.12) lib/phoenix/test/conn_test.ex:358: Phoenix.ConnTest.response/2
(phoenix 1.7.12) lib/phoenix/test/conn_test.ex:419: Phoenix.ConnTest.json_response/2
test/ponto_cao_web/controllers/pet_controller_test.exs:106: (test)
I didn’t find in pow
docs and repo some recommended way to tests authenticated controllers on API only projects, can you guys help me to understand why this is happening ?