How to test change an user password in sandbox then log in with this new password

Is there a way to persist in the same test a database change to use that change in another assert?
We’d like to implement test functions to our authentication. Since we can’t know an user password, we are also testing the recover process (changing password first).

Our current data_case.ex is:

  def setup_sandbox(tags) do
    pid = Ecto.Adapters.SQL.Sandbox.start_owner!(PeoplexServices.Repo, shared: not tags[:async])
    on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end)
  end

And our current test is:

defmodule LiveAdminWeb.AuthTest do
  use LiveAdminWeb.ConnCase, async: true
  import Phoenix.LiveViewTest

    ...

    test "login", %{conn: conn} do
      # PASSWORD
      {:ok, user} = AdminUsers.get_by_email(@user_email, @subdomain)
      ...
      attrs = %{
        password_hash: password_hash
      }
      AdminUsers.update(user, attrs, @subdomain, nil, "service")

      # ACCESS
      path = "#{@base_path}/auth/"
      conn = get(conn, path)
      assert html_response(conn, 200) =~ "Entrar"
      # LOGIN
      body = %{
        "item" => %{"email" => @user_email, "password" => password_text}
      }
      conn = post(conn, path, body)
      ...
    end
end

This fails probably because the first database change is rollbacked.
The test works with a valid password. (no database change).

1 Like

When you say probably, have you tried it or not? The database change is not rolled back. The roll back happens at the end of the test after the test is completely over. Everything inside the test sees the changes happen.

1 Like

I can check the data updated after initial change.

But when we go to our controller, it compares with old data.

conn = post(conn, path, body)

Is there any other missing part to help the controller fun to see the temporary new data?

1 Like

I think you need to call Phoenix.ConnTest — Phoenix v1.6.12 on the conn if you want to reuse it in another request.

1 Like