caslu
I added Pow to my application using this guide because i have a JSON api structure, but after integrate with pow, my controller tests broke because neither of them needed to have a session before and now they do. The problem is that i’m not being able to make the tests work, my approach so far was change the default setup method generated by phoenix to this:
# before
setup %{conn: conn} do
{:ok, conn: put_req_header(conn, "accept", "application/json")}
end
# now
setup %{conn: conn} do
user = user_fixture()
authed_conn =
conn
|> put_req_header("accept", "application/json")
|> pow.plug.assign_current_user(user, [])
{:ok, conn: authed_conn}
end
Now the index test pass successfully, but the others give me this error message:
** (RuntimeError) expected connection to have a response but no response was set/sent.
Please verify that you assign to "conn" after a request:
conn = get(conn, "/")
assert html_response(conn) =~ "Hello"
code: assert json_response(conn, 422)["errors"] != %{}
stacktrace:
(phoenix 1.7.12) lib/phoenix/test/conn_test.ex:358: Phoenix.ConnTest.response/2
(phoenix 1.7.12) lib/phoenix/test/conn_test.ex:419: Phoenix.ConnTest.json_response/2
test/ponto_cao_web/controllers/pet_controller_test.exs:106: (test)
I didn’t find in pow docs and repo some recommended way to tests authenticated controllers on API only projects, can you guys help me to understand why this is happening ?
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
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
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Other Trending Topics
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
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
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
Latest Phoenix Threads
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #ai
- #graphql
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All Posts (oldest first)
- Show All Posts (newest first)
Hermanverschooten
Haven’t used pow myself, but I guess you should do something like they do in their tests,
conn |> PowPlug.assign_current_user(@user, [])https://github.com/pow-auth/pow/blob/801408e7ce74ecfd85aae1ecc0380187ddb25c3c/test/extensions/reset_password/phoenix/controllers/reset_password_controller_test.exs#L32
caslu
I’m already doing this on setup method, but on my snippet for some reason the name of modules are lowercased, but you can check here:
Hermanverschooten
Could you show your complete test?
caslu
Sure, here…
The
index/2test and the error case increate/2test passes successfully, but the success case ofcreate/2gives this error now:So, i guess the error is located at
get/3call in this line:but i still do not understand neither why and what is happening
Hermanverschooten
Is the project somewhere accessible?
caslu
Yeah bro, you can check the repo here
caslu
So i solved the problem by starting a new connection life cycle before dispatch the
getaction increate/2test, now the test looks like this:Now i would like to know why starts a fresh connection with
build_conn/0solved the problem, and why this works:and this don’t:
LostKobrakai
Recycling a conn tries to do essentially the same as what happens when a browser would navigate to a new route. Only things like cookies, some headers, hostname, … will be retained.
assignsare not retained between multiple requests being made, but need to be computed and set freshly for each request, so the same applies with recycles.Here you have a conn, which was already used to do a request, then you assign some data and then you request a new page, trigger a recycle and therefore get rid of tje assign again.
If you do multiple request you likely want to authenticate, so that a session is started and not just for a single request by setting just assigns.
caslu
Yeah, i did understand the connection flow but what i’m not getting here is why the connection do not re-assign the user before do the
getaction even when i pipe through the plugassign_current_userbut it does work if i do a manual recycle to connection withrecycle/2or build a fresh conn then assign the user to it. Also i’d like to know if there’s a better way to encapsulate the authentication logic in my tests for avoid this re-assign thing everywhere.Hermanverschooten
Learned something new here. I think I’ve never reused a
connin a test before, at least not in this fashion.