3

I would like to know how to login a user in the session without creating work around routes in the test enviornment or logging the user in through the standard user flow. Is there a way to set the cookie/session so that there is a current_user?

Here is some discussion about it: https://github.com/elixir-wallaby/wallaby/issues/57

Since cookies can be read and set via Wallaby I'm wondering how one would use those to setup the current_user and bypass a manual login flow.

ideally i'd want something like:

test "visit dashboard", %{session: session} do
  {:ok, user} =
      MyApp.Accounts.create_user(%{email: "test@example.com", password: "123456"})

  session = Wallaby.Browser.set_cookie(session, :current_user, user)

  # start test of protected page
  Wallaby.Browser.visit(session, "/dashboard")

  # etc.
  ...

end
Loading...
  • 863
  • 9
  • 21
  • 1
    I researched a bit in the same direction, but ended up manually filling in the forms. However, wallaby has a Test that might interest you: https://github.com/elixir-wallaby/wallaby/blob/45f69c5f4f8b809c6ddbebb373b8f4eb91a79659/integration_test/cases/browser/cookies_test.exs – Peter Jan 12 '20 at 15:27
  • 1
    @Peter that explains a lot. i was wondering why i had to visit the page to get the cookies to even show. I ended up doing a hacky workaround where i add a route in test/dev only. Since wallaby requires visiting a site (in their example it is "/") why not just auth on a Get? So that's what I do in test/dev only. It's.... fine.... – Loading... Jan 13 '20 at 05:57
  • I am using Pow and Pow Assent and I feel the same pain. Slow specs if I need every time pass user through the login form – Sveredyuk Apr 11 '20 at 10:51

0 Answers0