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
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
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
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Marked As Solved- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
A
connknows and stores if it was already used to make a request (iircconn.state). So your two approaches are.and
You could do
conn |> recycle() |> Pow.Plug.assign_current_user(user, [])to work around the automatic recycling happing at an inconvencient time.You could also consider not writing to
conn.assigns, but to put the authentication details in the session. That way authentication will persist across recycles just like it does persist across many requests outside of testing.Also Liked
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.
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
Hermanverschooten
Could you show your complete test?
Last Post!
habutre
I was facing the same issue and what worked for me instead of recycling the conn was to take advantage of immutability and not reuse the authenticated connection from setup
I named the conn from context as
conn_auth, use it in my calls e.g.conn = get(conn_auth,…)So my authenticated connection was kept untouched and I could assert my responses normally and as much as I need