pedromvieira

pedromvieira

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).

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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.

pedromvieira

pedromvieira

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?

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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

Where Next?

Popular in Discussions Top

Rustixir
Hi everyone, im working on find best language/framework/system for high concurrency, high performance and stable performance after wor...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
ricklove
I was just introduced to Elixir and Phoenix. I was told about the 2 million websocket test that was done 2 years ago. From my research, t...
New
lucaong
Hello Elixir and Nerves community, I have been working for a while on an open-source embedded key-value database for Elixir, that I call...
230 14049 124
New
lorenzo
Hey everone! I created a prototype for my app using Nodejs for the api. But the framework I chose wasnt great (in general theresnt any g...
New
Crowdhailer
I’ve been hearing much about the new formatter and it’s something I have been keen to try. I find examples buy far the most illuminating...
248 19365 150
New
CharlesO
Erlang :list.nth simple, but 1 - based nth(1, [H|_]) -> H; nth(N, [_|T]) when N > 1 -> nth(N - 1, T). Elixir Enum.at … coo...
New
PragTob
Hey everyone, this has been brewing in my head some time and it came up again while reading Adopting Elixir. GenServers, supervisors et...
New
Qqwy
I would like to spark a discussion about the static access operator: .. For whom does not know: it is used in Elixir to access fields of...
New
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New

We're in Beta

About us Mission Statement