3

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.

4

1 回答 1

4

假设您有一个用户资源进行身份验证,那么您可以像这样设置它:

setup %{conn: conn} do
    user = # Your user resource
    {:ok, jwt, _claims} = Guardian.encode_and_sign(user)
    conn =
      conn
      |> put_req_header("accept", "application/json")
      |> put_req_header("authorization", "Bearer #{jwt}")

    {:ok, conn: conn}
  end
于 2018-04-21T01:31:47.567 回答