recycle does essentially what happens if you start a new request in the browser. Any new request in the browser will start of without any assigns. Usually you’re kept logged in by putting something in the session for the user.
That’s just not true. If you’re not using session based authentication you’d need to log in new for each request. The only data which survives between requests is the stuff you put into the session. Anything else is discarded at the end of a request. That’s true for the request you do with a browser in dev mode and exactly what recycle mimics for tests.
Ok, so how can I make the tests better? Writing conn = conn |> cycle() |> Map.put(:assigns, conn.assigns) everywhere in my controller tests is so ugly.
setup %{conn: conn} do
{:ok, conn: Plug.Test.init_test_session(conn, user_id: 666)}
end
I wouldn’t change assigns manually, instead I’d pipe the requests through the actual plug pipelines with the session set via Plug.Test.init_test_session/2 to get the expected conn with all relevant assigns set.
Either what @idi527 said: Use the session and your real plugs / pipelines.
Or simply not reuse the conn for places where you’re e.g. testing a single plug.
result = MyPlug.call(conn, opts)
assert …
result = MyPlug.call(conn, opts_2)
assert …
After reading the source code, I’d like to share what I learned so far.
When we run get(conn, Routes.site_path(conn, :show, site)), a new conn was created, and cookies and some request headers (~w(accept authorization)) from old conn are copied to new conn - that’s what Phoenix recycling does for us, it emulates the behavior of browser. But conn.assigns is not copied, results in a nil value of current_user.
The reason post(conn, Routes.session_path(conn, :create), session: %{email: user.email, password: "123456"} ) works is because Auth module selects another path:
def call(conn, _opts) do
user_id = get_session(conn, :user_id)
cond do
conn.assigns[:current_user] -> # <- the way programming phoenix book suggests
conn
user = user_id && Accounts.get_user(user_id) -> # <- post Routes.session_path just selects this way.
assign(conn, :current_user, user)
true ->
assign(conn, :current_user, nil)
end
end