polypush135

polypush135

Heres the error.

  1) test checks if account is active authenticated?/2 halts if account is no longer active (Beffect.Plugs.AuthTest)
     test/beffect/plug/auth_test.exs:7
     ** (ArgumentError) flash not fetched, call fetch_flash/2
     code: |> Beffect.Plugs.Auth.authenticated?(%{})
     stacktrace:
       (phoenix) lib/phoenix/controller.ex:1228: Phoenix.Controller.get_flash/1
       (phoenix) lib/phoenix/controller.ex:1210: Phoenix.Controller.put_flash/3
       (beffect) lib/beffect/plugs/auth.ex:30: Beffect.Plugs.Auth.authenticated?/2
       test/beffect/plug/auth_test.exs:17: (test)

FYI lib/beffect/plugs/auth.ex:30 is the puts_flash

heres the test

test "authenticated?/2 halts if account is no longer active" do

  user = insert(:user, active: false)

  conn = build_conn()
  |> init_test_session(user_id: user.id)
  |> assign(:current_user, user)
  |> bypass_through(BeffectWeb.Router, :browser) # ? not sure if its really calling that pipeline
  |> Beffect.Plugs.Auth.authenticated?(%{})

  assert get_flash(conn, :error) =~ "Your account is no longer active"
  assert redirected_to(conn) =~ "/login"
end

Heres Beffect.Plugs.Auth.authenticated?

def authenticated?(conn, _opts) do
   cond do
     conn.assigns.current_user && conn.assigns.current_user.active ->
       conn
      conn.assigns.current_user ->
        assign(conn, :current_user, nil)
        conn
          |> put_flash(:error, "Your account is no longer active") # Setting the flash here
          |> redirect(to: Helpers.session_path(conn, :new,origin_path: conn.request_path))
          |> halt()
        ...
  end

Heres the pipeline for :brower

  pipeline :browser do
    ....
    plug :fetch_flash # calling it here....
    ...
  end

Also note that I IO.puts in the pipeline to see if its called and it looks as if it is being called.

Showing Posts 1 to 6

mgwidmann

mgwidmann

Looks to me like it is working. You’re skipping the :browser pipeline, and as expected the flash is not fetched. Maybe you need to separate out your basic browser needs from your authentication needs.

chrismccord

chrismccord

Creator of Phoenix

bypass_through will only invoke the pipeline when you send a request against the router, so your tests should work with the following:

(note, you don’t need init_test_session since bypass_through will fetch the session in your browser pipeline:

  conn =
    build_conn()
    |> bypass_through(BeffectWeb.Router, :browser)
    |> get("/") # invoke the pipelines, bypassing router dispatch
    |> put_session(:user_id, user.id)
    |> assign(:current_user, user)
    |> Beffect.Plugs.Auth.authenticated?(%{})
polypush135

polypush135 OP

I totally missed what
“but you need to invoke the Endpoint plugs and :browser pipeline of your Router for session and flash related dependencies” meant.

Maybe a update to the doc that calls out that what the use of get(“/”) is doing in that case. ( Just a thought. )

chrismccord

chrismccord

Creator of Phoenix

Great opportunity for a PR :slight_smile: Maybe before the ## Examples make a note about a request being sent with get and friends is required to invoke the pipeline(s)?

polypush135

polypush135 OP

Just about to ask if thats something I could do but then found it for me self. Will do :slight_smile:

polypush135

polypush135 OP

Opensource FTW
https://github.com/phoenixframework/phoenix/pull/2633

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews