caslu

caslu

Testing authenticated routes in Live View

Hello guys, i was coding my new project and something curious happened to me. Gust giving a bit of context, i’ve been using Phoenix and Elixir for a while but not LiveView, which i’m getting more a little more comfortable now. But here’s the “problem”, i’m gonna provide a simplified example:

In my routes i have a “/” homepage path, which have a button to navigate to a protected route “/protected”, consider that in this project i’m managing authentication through phx.gen.auth (MyAppWeb.UserAuth module), when i tried to test this flow it continues to redirect me for /protected even when i’m not logged in, i’ll leave some snippets below:

router.ex

    live_session :require_authenticated_user,
      on_mount: [{EurekaWeb.UserAuth, :ensure_authenticated}] do
      live "/users/settings", UserSettingsLive, :edit
      live "/users/settings/confirm_email/:token", UserSettingsLive, :confirm_email
      live "/protected_page", AuthenticatedPageLive # The protected page
    end

And the failing test snippet:

    test "opens guest user sign in modal when user is not signed in", %{conn: conn} do
      {:ok, view, html} = live(conn, ~p"/")

      html = view |> element("a", "Create a new room") |> render_click()
      assert_redirect(view, ~p"/users/guest/log_in")
    end

But when i execute the test here’s the output

 ** (ArgumentError) expected EurekaWeb.PageLive.Home
 to redirect to "/users/guest/log_in", but got a redirect to "/protected_page"

Can someone explain me why in test scenario live view for some reason do not redirect to login modal such as when i’m interacting through web?

Please leave me a feedback if for some reason the questions isn’t clear enough, thanks guys! :handshake:

First Post!

krwenholz

krwenholz

Hard to say without knowing the rest of your codebase, but here are some gotchas that have caught me:

  1. Incorrect test isolation, such that the user on that test connection is signed in (can happen in a number of ways if you’re trying to be too clever)
  2. You’ve overridden some behavior in ensure_authentication that isn’t protecting the page correctly.
  3. Defining the route higher up without authentication.

Have you verified it works outside the test?

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New

Other popular topics Top

New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement