team-alembic/realworld

Ash Authentication compatibility with Wallaby E2E tests

modellurgist opened this issue · 3 comments

Wallaby requires an on_mount hook for LiveViews, but I notice that those hooks don't fire (in test MIX_ENV) when added through the ash_authentication_live_session on_mount support, whereas they do in dev env.

Recent examples are not easy to find, so I thought this repo could be a good showcase for a problem I haven't solved yet, after quite a bit of debugging. I'm not yet sure if this is an Ash, Wallaby, or LiveView issue.

Either way, having a working example here would be a great service.

Our code in the router looks something like this:

  ash_authentication_live_session :authentication_required,
    on_mount: [
      {MyApp.LiveUserAuth, :live_user_required},
      # Needed for Wallaby support, per their docs:
      {MyAppWeb.TestSupport.AllowEctoSandbox, :default}
    ] do
    scope "/u", MyAppWeb.UserLive do

Tested with:

  • ash 2.18.2
  • ash_authentication 3.12.2
  • ash_authentication_phoenix 1.9.2
  • ash_phoenix 1.2.26
  • phoenix 1.7.11
  • phoenix_live_view 0.20.7

Upgrading these next month.

Hello @modellurgist. Thanks for the report! this is pretty strange, but I don't think that there is anything that ash_authentication_live_session does that would cause on mount hooks not to fire on one env or another. I feel fairly confident that any bug in this regard would not be related to ash_authentication. Could be wrong.

Until we can isolate this to being specifically an ash_authentication or ash_authentication_phoenix issue, I think it's best to close this issue. Great to have it for documentation in case someone else runs into

Keep in mind that one on mount hook can halt the flow by returning {:halt, ...}. That prevents other hooks from running. So I'd confirm that your LiveUserAuth hook isn't halting.

For example:

  def on_mount(:live_user_required, _params, _session, socket) do
    if socket.assigns[:current_user] do
      {:cont, socket}
    else
      {:halt, Phoenix.LiveView.redirect(socket, to: ~p"/sign-in")} # <- halt
    end
  end

Thanks! I'll focus on that. I added debugging in the dep, and see an extra hook after its transformation:

opts #=> [
  session: {AshAuthentication.Phoenix.LiveSession, :generate_session, [nil, []]},
  on_mount: [
    AshAuthentication.Phoenix.LiveSession,
    {MyApp.LiveUserAuth, :live_user_required},
    {MyAppWeb.TestSupport.AllowEctoSandbox, :default}
  ]
]

I'll take a closer look at the LiveSession hook, after I isolate where the halt occurs.