Api sign in inside session controller test

I need a help to make my session controller test pass because it requires user authentication.
This is my session create method:

my session view:

this is the json response to the session_path:

and this is the test i am trying to do:

sincerily i don’t know how to do the test, so if you find some wrong code please consider i am just a phoenix beginner.

My first guess is that there’s something wrong with your setup block, because you have two {:ok, :conn} tuples, and the first one is basically discarded. Maybe what you’re trying to do is:

setup %{conn: conn} do
  # ... posting to session_path ...
  # putting headers to conn below
  conn =
    conn
    |> put_req_header("authorization", "Bearer <token>")
    |> put_req_header("accept", "application/json")
  {:ok, conn: conn}
end

If you run it as it is, what is the error message?

** (RuntimeError) expected response with status 200, got: 403, with body:
     {"error":"Not Authenticated"}

the token is not a valid expression it is just a ‘replace with your token’ mockup.

As an aside, please always use inline code blocks or gists instead of photos. Different people have different DPIs, screen sizes, and so forth. It also means that if someone wants to help show how to do something differently, they have to retype the entire relevant area instead of just copying / pasting.

7 Likes