Wallaby testing when browser session is required (i.e. login)

How do I test some code if a browser session with some user data inside (perhaps from Guardian) is required? How do I get that session data into my Wallaby session?

1 Like

By performing the steps necessary to create that data. Click login, fill form etc

If you’re not concerned about phoenix’s ability to hold the session you can also test this without browser testing. You should also be able to set values to the conn’s session manually like so:

conn =
      conn
      |> bypass_through(MyAppWeb.Router, [:browser])
      |> put_session(:account_id, 1)

get conn, …
1 Like

Yeah. But doesn’t Wallaby create a new session for each test? If so, I must perform the steps each time I want to test something that requires me to authenticate?

Yeah, but when I’m using Wallaby (feature/acceptance testing), there is no conn, only the wallaby session. How do I then insert data into the browser session?

1 Like

Yes. That’s what setup is for. Or you can write a helper function that does this.

2 Likes

There’s no session data on the browser side. There is in the case of a default phoenix project just a cookie, which identifies the session a user does have on the server side. So you might want to see how wallaby does handle cookies in browser sessions (in the wallaby docs sessions seem to mean browsing sessions).

Edit:
Also logging in in browser testing is probably simplest done with the login page or whatever login machanism you’re using.

1 Like

Thanks a lot everyone! After googling for the terms that were suggested, I found a GitHub issue that explained that the best way of going about this is to write a helper method thats called in setup to login via the normal steps a user would take.

https://github.com/keathley/wallaby/issues/57

1 Like

It’s 2020 and Wallaby can access browser cookies. Is there any way to login a user without walking through the login steps?

I asked on stackoverflow as well:

1 Like

For future reference in case anyone bumps into this:

# test/support/test_helpers.ex
defmodule MyAppWeb.TestHelpers do
  @user_remember_me "_my_app_web_user_remember_me"

  def log_in(%{session: session} = context) do
    user = Map.get(context, :user, user_fixture())
    user_token = MyApp.Accounts.generate_user_session_token(user)

    endpoint_opts = Application.get_env(:dorado, MyAppWeb.Endpoint)
    secret_key_base = Keyword.fetch!(endpoint_opts, :secret_key_base)

    conn =
      %Plug.Conn{secret_key_base: secret_key_base}
      |> Plug.Conn.put_resp_cookie(@user_remember_me, user_token, sign: true)

    session
    |> visit("/")
    |> set_cookie(@user_remember_me, conn.resp_cookies[@user_remember_me][:value])

    {:ok, %{session: session, user: user}}
  end
end

This assumes you created MyApp.Accounts via mix phx.gen.auth. The test setup can then simply look like this:

  setup context do
    log_in(context)
  end
5 Likes

Thanks so much for providing this. Im trying to emulate something similar, but have errors stating set_cookie/3 and visit/2 are not available. I tried adding use Wallaby.Feature but that isnt available for a .ex file (undefined function setup/2 (there is no such import)).

Is there a piece I am missing from this setup? I have a “vanilla” mix phx.gen.auth.

I have this login helper working for my app, using wallaby, ex machina, and auth generator:

defmodule MyAppElixirWeb.TestHelpers.AdminAuthHelper do
  use Wallaby.DSL

  import MyAppElixir.Factories.AdminFactory

  @admin_cookie "_myapp_elixir_web_admin_remember_me"

  def create_admin_and_log_in(session) do
    admin =
      build(:admin, %{})
      |> set_password("1VeryValidPassword$")
      |> insert()

    admin_token = MyAppElixir.Accounts.generate_admin_session_token(admin)

    endpoint_opts =
      Application.get_env(:myapp_elixir, MyAppElixirWeb.Endpoint)

    secret_key_base = Keyword.fetch!(endpoint_opts, :secret_key_base)

    conn =
      %Plug.Conn{secret_key_base: secret_key_base}
      |> Plug.Conn.put_resp_cookie(
        @admin_cookie,
        admin_token,
        sign: true
      )

    visit_a_page_to_set_auth_cookie_for_wallaby(session, conn)

    {:ok, %{session: session, admin: admin}}
  end

  defp visit_a_page_to_set_auth_cookie_for_wallaby(session, conn) do
    session
    |> visit("/")
    |> set_cookie(
      @admin_cookie,
      conn.resp_cookies[@admin_cookie][:value]
    )
  end
end

I think you may have to add import Wallaby.Browser to the module

1 Like