I have authenticated routes within my application and now I'm getting to the point of controller testing these endpoints. But I first need to authorize my request before I can test the API endpoint. Here is my test:
setup %{conn: conn} do
{:ok, conn: put_req_header(conn, "accept", "application/json")}
end
test "creates and renders resource when data is valid", %{conn: conn} do
conn = post(conn, league_path(conn, :create), league: @valid_attrs)
body = json_response(conn, 201)
assert body["name"]
assert Repo.get_by(League, name: "Obama's League")
end
When I try to run that test I get this error:
expected response with status 201, got: 401, with body:
{"errors":["Unauthenticated"]}
ROUTER:
pipeline :authenticated do
plug(Guardian.Plug.EnsureAuthenticated)
end
scope "/api/v1", Statcasters do
pipe_through([:api, :authenticated])
resources("/users", UserController, except: [:create])
resources("/leagues", LeagueController, only: [:create])
end
So I'm trying to hit the create
path in my LeagueController. But I'm hitting the auth error because I don't have a jwt in the bearer header.
How do I generate a token and how to setup it up before the test? Thanks for the help.