Pow: Set up tests with generated controller tests

Created on 7 Jul 2020  路  3Comments  路  Source: danschultzer/pow

Hello!

I hope you can help me out, because I cannot figure out how to set up my controller tests after integrating Pow and banging my head against the wall for two evenings already ouch.

When I "mix phx.gen.html ...", tests are generated like this one, where I can put the setup for Pow

describe "create organisation" do
    setup %{conn: conn} do
      user = %User{email: "[email protected]"}
      conn = Pow.Plug.assign_current_user(conn, user, otp_app: :paper)
      {:ok, conn: conn}
    end

    test "redirects to show when data is valid", %{conn: conn} do
      conn = post(conn, Routes.organisation_path(conn, :create), organisation: @create_attrs)

      assert %{id: id} = redirected_params(conn)
      assert redirected_to(conn) == Routes.organisation_path(conn, :show, id)

      conn = get(conn, Routes.organisation_path(conn, :show, id))
      assert html_response(conn, 200) =~ "Show Organisation"
    end

    ...
  end

When I run the tests, I get this error:

test create organisation redirects to show when data is valid (PaperWeb.OrganisationControllerTest)
     test/paper_web/controllers/organisation_controller_test.exs:52
     ** (RuntimeError) expected response with status 200, got: 302, with body:
     "<html><body>You are being <a href=\"/login\">redirected</a>.</body></html>"
     code: assert html_response(conn, 200) =~ "Show Organisation"
     stacktrace:
       (phoenix) lib/phoenix/test/conn_test.ex:369: Phoenix.ConnTest.response/2
       (phoenix) lib/phoenix/test/conn_test.ex:383: Phoenix.ConnTest.html_response/2
       test/paper_web/controllers/organisation_controller_test.exs:63: (test)

I did a lot of IO.inspecting the conn in various location, did "refute conn.halted" and even compared the count of items in the table before and after create, but everything looks like the implementation is working. So I am sure that with the error in the test I am receiving a false negative.

If I remove the ":protected" from the routes, the error is gone. So I am assuming that some redirection happens within Pow.

Can you help me to understand what is going on and how to set up the tests to follow the redirection?

Thanks a lot!

Yours,
Volker

Most helpful comment

It's because the pow config gets overridden with the resulting conn. Instead you should do something like this:

    test "redirects to show when data is valid", %{conn: authed_conn} do
      conn = post(conn, Routes.organisation_path(authed_conn, :create), organisation: @create_attrs)

      assert %{id: id} = redirected_params(conn)
      assert redirected_to(conn) == Routes.organisation_path(conn, :show, id)

      conn = get(conn, Routes.organisation_path(authed_conn, :show, id))
      assert html_response(conn, 200) =~ "Show Organisation"
    end

All 3 comments

It's because the pow config gets overridden with the resulting conn. Instead you should do something like this:

    test "redirects to show when data is valid", %{conn: authed_conn} do
      conn = post(conn, Routes.organisation_path(authed_conn, :create), organisation: @create_attrs)

      assert %{id: id} = redirected_params(conn)
      assert redirected_to(conn) == Routes.organisation_path(conn, :show, id)

      conn = get(conn, Routes.organisation_path(authed_conn, :show, id))
      assert html_response(conn, 200) =~ "Show Organisation"
    end

Thank you, Dan!

Two conns needed to be changed to "authed_conns", too. Then the tests were passing as expected.
馃憤

    test "redirects to show when data is valid", %{conn: authed_conn} do
      conn =
        post(authed_conn, Routes.organisation_path(authed_conn, :create),
          organisation: @create_attrs
        )

      assert %{id: id} = redirected_params(conn)
      assert redirected_to(conn) == Routes.organisation_path(conn, :show, id)

      conn = get(authed_conn, Routes.organisation_path(authed_conn, :show, id))
      assert html_response(conn, 200) =~ "Show Organisation"
    end

Thanks @volkerholloh and @danschultzer 馃榿.

馃拝馃徑

test "redirects to show when data is valid", %{conn: conn} do
-    conn = post(conn, Routes.organisation_path(conn, :create), organisation: @create_attrs)
+    result_conn = post(conn, Routes.organisation_path(conn, :create), organisation: @create_attrs)

-    assert %{id: id} = redirected_params(conn)
+    assert %{id: id} = redirected_params(result_conn)
-    assert redirected_to(conn) == Routes.organisation_path(conn, :show, id)
+    assert redirected_to(result_conn) == Routes.organisation_path(conn, :show, id)

-    conn = get(conn, Routes.organisation_path(conn, :show, id))
+    result_conn = get(conn, Routes.organisation_path(conn, :show, id))
-    assert html_response(conn, 200) =~ "Show Organisation"
+    assert html_response(result_conn, 200) =~ "Show Organisation"
end
Was this page helpful?
0 / 5 - 0 ratings