kamaroly

kamaroly

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:

Showing Posts 1 to 10

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.

kamaroly

kamaroly OP

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.

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?

kamaroly

kamaroly OP

zachdaniel

zachdaniel

Creator of Ash

kk. And you’re able to confirm that the majority of your test duration is taken up doing sign in?

kamaroly

kamaroly OP

Yes. like 90% of my tests require login.

zachdaniel

zachdaniel

Creator of Ash

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?

kamaroly

kamaroly OP

I’d like also to mention that I am using Ash multitenancy, thus, here’s what happens every time I need to tests a protected age:

  1. Create user
  2. Add user to organisation
  3. Add user to owner’s group
  4. Add all permissions to owner’s group
  5. Login the user and return authorized conn.
  6. Test pages behind protected routes.

I think if there’re could be a way of doing step 1 to 5 once for all. It would significantly speed up the tests.

Here’s the test login function: https://github.com/kamaroly/hr/blob/005b42a185106442b8f8820bfd85c04f4d38192b/test/support/auth_case.ex#L37

Here’s a form test:

https://github.com/kamaroly/hr/blob/dev/test/hr_web/live/organisations/job_titles/form_live_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.

kamaroly

kamaroly OP

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

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews