kamaroly

kamaroly

Ash Authentication - How To Test Liveview with Authentication

I am getting error (ArgumentError) session not fetched, call fetch_session/2 while testing liveview that require authentication. I am using Ash Authentication.

How do I test liveviews with Ash Authentication?

Here is the helper function I am using to login while testing

def login(conn, email\\ "test@example.com", hashed_password \\  "$2b$12$mZjy/lPACxT/WcwL8V9og.MtGzgOZc9VEuDdbnRZ2mmHbRV/9xvdK") do
    {:ok, user} = Accounts.create_user(email, hashed_password)
  
    conn
    |> assign(:current_user, user)
    |> put_session(:user, user)
  end

Here is a test I am trying to run.

    @tag run: true
    test "User can list stock categories", %{conn: conn} do
     {:ok, view, html} = 
              conn
              |> login()
              |> live(~p"/stock/categories")

      # Click on each one of them to be edited
      for category <- Category.all!() do
        assert html =~ category.name
        assert html =~ category.description
      end
    end

Marked As Solved

shamanime

shamanime

You can use Phoenix.ConnTest — Phoenix v1.7.12 in your pipeline to get rid of the fetch_session/2 error.

Your tests will run your router pipeline, and somewhere there will be some code that will read the session and try to load the user from it. So all you need to do is add to session whatever will please that logic.

If I’m not mistaken, Ash will store a key user with value like user?id=ce7969f9-afa5-474c-bc52-ac23a103cef6.

This way you might be able to just do:

  conn
  |> Phoenix.ConnTest.init_test_session(%{})
  |> Plug.Conn.put_session(:user, "user?id=#{user.id}")

Also Liked

zachdaniel

zachdaniel

Creator of Ash

I don’t believe a helper exists, but you can write your own. This thread has an example How to test live views with ash authentication plugs - #5 by zachdaniel

zachdaniel

zachdaniel

Creator of Ash

That looks nice :slight_smile:

kamaroly

kamaroly

@zachdaniel, for some reasons, I could not edit that question hours after I notice it was not well framed. So, I had to ask again and delete the former one.

Where Next?

Popular in Questions Top

mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
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
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I fore...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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 52238 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
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47849 226
New

We're in Beta

About us Mission Statement