kamaroly

kamaroly

Testing with Ash authentication is extremely slow even after configuring `config :bcrypt_elixir, :log_rounds, 1`

I have been experiencing extremely slow tests for each test behind a protected route with Ash authentication. 54 tests are taking 47 seconds. I tried various tips available online including config :bcrypt_elixir, :log_rounds, 1 but it didn’t help. Meanwhile others are ble to run 376 tests in 7 seconds. How do you speed up your tests?

376 tests in 7 seconds: Anthony Accomazzo on X: "@elixirlang `make signoff` in action: https://t.co/EqDWRGaTsp" / X

My experience:

Marked As Solved

kamaroly

kamaroly

Here is what I did to reduce the testing time.

I created a fake hash provider to skip the bycrypt hashing in while testing. This improved test speed to over 50%.

defmodule MyApp.Accounts.User.HashProviders.TestHashProvider do
  @behaviour AshAuthentication.HashProvider

  @impl true
  def hash(input) when is_binary(input) do
    # Fake hashing logic: prefix the input with "fake_hash_"
    {:ok, "fake_hash_" <> input}
  end

  @impl true
  def valid?(input, "fake_hash_" <> input), do: true
  def valid?(_input, _hash), do: true

  @impl true
  def simulate do
    # For testing purposes, return false to simulate a hash check
    false
  end
end

Then, I made it configurable in config/test.exs like the following

# Do NOT set this value for production
config :ash_authentication, hash_provider: MyApp.Accounts.User.HashProviders.TestHashProvider
  1. I told User resource to pick the hash provider from configuration if available like the following
# /lib/my_app/accounts/user.ex
defmodule MyApp.Accounts.User do
  #...
  authentication do
    strategies do
      password :password do
        identity_field :email
        # Tell AshAuthentication to use a faker hash provider to speed up tests
        hash_provider Application.compile_env(
                        :ash_authentication,
                        :hash_provider,
                        AshAuthentication.BcryptProvider
                      )
      end
    end
 #...
end

Also Liked

zachdaniel

zachdaniel

Creator of Ash

:thinking: @jimsynz may have some thoughts on speeding that process up, but I believe that most people are not actually doing password authentication in the vast majority of cases.

They will simply generate a token for the user and set it into the conn, for example, as opposed to first going through authentication flow.

zachdaniel

zachdaniel

Creator of Ash

There is a setup_all that you can run at the start of each test to ensure your test user is created. It doesn’t happen in the sandbox, so won’t be rolled back on each test. So each test could check first if the user exists and create it otherwise, or do an upsert into the user’s table.

And your config :bcrypt_elixir, log_rounds: 1 is in config/test.exs?

zachdaniel

zachdaniel

Creator of Ash

you can do that for each test using setup_all, or for all tests in your test_helper file IIRC.

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48475 226
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29703 241
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31307 143
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

We're in Beta

About us Mission Statement