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?
@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.
The approach I am using is to create a user, then add a user_subject in the connection that will be used for protected routes.
I could also reduce this time if there was a way of signing in once at the beginning for all tests instead of having to sign in on every test. Do you know to sign in once before all tests begin?
Something like setup function be for all tests in the suite.
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?
You should just be able to generate a token for your test user. There is no need to validate their password. How are you generating the user_subject? bcrypt shouldn’t even really come into the picture right? Well…I guess it does to hash their password. But with log_rounds configured to 1 it should be very fast. Feels like something else might be a factor?
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
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
@kamaroly - do you actually need to register a user for most of your test cases, or do you just need a user record to exist? If the latter then I would suggest just inserting one with Ash.Seed.seed!.