polypush135

polypush135

Say I have a controller like so.

  def create_pass_reset(conn, %{"user" => %{"email" => email}}) do
    case Accounts.reset_pass(email) do
      {:ok, user} ->
        conn
        |> put_flash(:info, "Password Reset Sent to #{user.email}")
        |> redirect(to: session_path(conn, :new))
...
    end
  end

Note that I am setting a flash on a conn that is due to be redirected.

And say I have a test like so.

test "POST /password-reset", %{conn: conn, user: user} do
  conn = post conn, user_path(conn, :create_pass_reset, %{"user" => %{"email" => user.email}})
  assert redirected_to(conn, 302) =~ "/login"
  assert html_response(conn, 302) ==  "Password Reset Sent to #{user.email}"
end

Well clearly the issue with this test is that the conn does not contain the state after the redirection occurred and thus does not have the content we are looking for

IE:

 Assertion with == failed
 code:  assert html_response(conn, 302) == "Password Reset Sent to #{user.email()}"
 left:  "<html><body>You are being <a href=\"/login\">redirected</a>.</body></html>"
 right: "Password Reset Sent to email-11@example.com"

So my question is, how do I perform the redirect so that I can see the state after the redirection has occurred?

Showing Posts 1 to 10

chrismccord

chrismccord

Creator of Phoenix

Something like this should work:

conn = post conn, user_path(conn, :create_pass_reset, %{"user" => %{"email" => user.email}})
assert "/login" = redir_path = redirected_to(conn, 302)
conn = get(recycle(conn), redir_path)
assert html_response(conn, 302) ==  "Password Reset Sent to #{user.email}"
13
Post #1
polypush135

polypush135 OP

Gonna have to use that assert / assignment again, assert "/login" = redir_path = redirected_to(conn, 302) :tada:
Thank you yet again @chrismccord

polypush135

polypush135 OP

Also what are your thoughts about this pattern of following the redirect?
This is something I’ve yet to see in the community.

chrismccord

chrismccord

Creator of Phoenix

It’s just fine pattern wise. It’s not exactly the same, but our context generators make use of redirected_params to construct a path and then get the show page of a created resource:

https://github.com/phoenixframework/phoenix/blob/444569fb99a599d67ec16fd492c3681412dd5759/priv/templates/phx.gen.html/controller_test.exs#L33-L35

Jono

Jono

Sorry to bug you again on this. I’m struggling with something when maintaining state.

I get the principle of saving conn.assigns to save authentication to reuse with different calls. I’m probably misunderstanding, but how do I make sure the validation errors don’t get tossed. Or the 302 is follow without recycling (I think this would solve it)?

The below is the code in question, there is a setup [:create_user] for the describe block.

@tag :authenticated
test "renders errors when data is invalid", %{conn: conn, client: client} do
  conn = put(conn, client_path(conn, :update, client), client: @invalid_attrs)
  assert html_response(conn, 200) =~ "Edit Client"
end

This gets a 302 and “you are being redirected”.

thiago

thiago

Getting the same thing.
302
<html><body>You are being <a href="/auth?redirect_to=...">redirected</a>.</body></html>

polypush135

polypush135 OP

I’m having a hard time understanding the question. From what I can understand from the code is that you want to assert that if you are passing in invalid_attrs to the update function than the html response should be a 200 and include the text edit client.

I assume that to be a correct assertion of what you are expecting from that controller function. Sadly without seeing that functions definition I can’t say as to why its returning a 302 with the text your are being redirected.

I’m also not sure why you are referring to the recycle function . In my case I was prescribe that method because it allowed me to make a follow up request on that same conn in my test which was to follow the redirection so that I could see the follow up page and assert on the flash message of that completed redirection.

Though if I assume you are passing in bad attrs into a update action you would not want a 302 redirection on that post.

polypush135

polypush135 OP

Is it possible you are in a redirection loop?
Edit: Don’t laugh it happens.

thiago

thiago

I think you’re right. That redirect is to /auth path, so my test was probably lacking a current_user. My bad :stuck_out_tongue:

thiago

thiago

Actually, I just tried it on another endpoint of mine and had the same issue.

Is it possible that by calling recycle(conn) we lose the previously assigned current_user?

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
mnkhod
So i have been using ash framework for a while and i love it. However currently the issue im having with ash framework is the error handl...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews