1

we tried a variety of methods and all of them yielded different kinds of errors, which luckily I don't remember now. We were able to use bypass_through and friends until the recent time when we need a session for real.

That's what I was able to put together basing on Plug tests:

def conn_with_session do
        build_conn
        |> get("/")
        |> recycle
        |> Plug.Session.call(Plug.Session.init(store: Plug.ProcessStore, key: "_app_key"))
        |> fetch_session
      end

where Plug.ProcessStore is copy-pasted from here https://github.com/elixir-lang/plug/blob/master/test/test_helper.exs#L6

Is there a more convenient/straightforward method to do that?

lessless
  • 866
  • 10
  • 27
  • I think this is the shortest variant, there is [already answer](http://stackoverflow.com/a/31983168/3102718). You probably can omit `recycle` if use `conn` instead of `build_conn`. Also it's good solution to `assign(conn, :current_user, user)` to avoid session check (you probably know that but just in case) – Oleksandr Avoiants Sep 27 '16 at 15:34

1 Answers1

-1

I try to have integration tests mimic a real consumer of your API. Real API consumers don't have access to the raw session, so neither should your tests.

In the case where you have an endpoint used to setup a session with an API key, something like this might work:

defmodule MyIntegrationTest do
  setup %{conn: conn} do
    {:ok, conn: sign_in(conn, "TEST_API_KEY")}
  end

  test "Session is authenticated", %{conn: conn} do
    conn = get(conn, some_protected_path(conn))
    assert conn.status == 200 
  end    

  def sign_in(conn, api_key) do
    # You can make changes to conn.session in the controller action for
    # sign_in_path and those changes will be reflected on the conn returned here.

    post(conn, sign_in_path(conn, :create), %{api_key: api_key}})
  end
end
  • I'm afraid that API endpoints are not affected by that problem as cookies mechanism is not involved. And we do have acceptance tests as a part of our test suite. – lessless Jul 01 '16 at 09:50