racsuk's avatar
Level 8

Dusk with Filament using wrong database for Authentication

Hi Laracasts community.

I've been trying out Dusk recently for a Filament project I've been working on, but have hit a stumbling block on the first test.... LoginTest! I've installed Dusk, setup my .env.dusk.local file, with it using a sqlite file which differs from my production one. My configuration is as follows:

.env

DB_CONNECTION=sqlite

.env.dusk.local

DB_CONNECTION=sqlite
DB_DATABASE=/Users/name/code/laravel/project/database/dusk.sqlite

My SUPER simple test, LoginTest:

<?php

use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTruncation;
use Laravel\Dusk\Browser;

uses(DatabaseTruncation::class);

test('example', function () {
    $user = User::factory()->admin()->create();
    ray(auth()->attempt(['email' => $user->email, 'password' => 'password']));
    ray(auth()->attempt(['email' => "[email protected]", 'password' => 'password']));

    $this->browse(function (Browser $browser) use ($user) {
        $browser->visit('/login')
                ->type('input[id="data.email"]', "[email protected]")
                ->type('input[id="data.password"]', 'password')
                ->clickAndWaitForReload('button[type="submit"]')
                ->assertPathIs('/')
                ->assertSee('Dashboard');
    });
});

HOWEVER... The first ray line (Line 11), returns true - this is expected. The second ray line (Line 12) returns false - this is also expected, as [email protected] is a user in my DEVELOPMENT Sqlite database, not my DUSK Sqlite database.

However.... as you can see, I then use the browser to navigate to /login. I use [email protected] and password, which SUCCESSFULLY logs me in! I was not expecting this!

I've tried clearing cache, config and regenerating auto-load, but nothing I do seems to stop Dusk from using my development Sqlite when attempting to log in!

What am I doing wrong? I'm losing my mind here!

I'm using Herd to host my site. When "dusk" kicks in, if I have my browser open and logged into my project, I get momentarily logged out (see the login screen, and "Dusk" as the project name, so I know it's loading the .env.dusk.local file! and then when dusk finishes, I get logged back in with the project name as the title.

0 likes
1 reply
LaryAI's avatar
Level 58

It sounds like the issue you're experiencing is related to the environment configuration not being properly applied when running Dusk tests. Here are a few steps you can take to ensure that Dusk is using the correct database:

  1. Ensure .env.dusk.local is being used: Make sure that when you run your Dusk tests, the .env.dusk.local file is being loaded. You can verify this by adding a unique environment variable in .env.dusk.local and checking its value in your test.

  2. Clear Configuration Cache: Run the following commands to clear any cached configuration that might be causing the issue:

    php artisan config:clear
    php artisan cache:clear
    
  3. Check Dusk Environment: Ensure that your Dusk environment is correctly set up. You can do this by adding a check in your test to see which database is being used:

    test('check database connection', function () {
        $database = config('database.connections.sqlite.database');
        ray($database); // This should output the path to your dusk.sqlite
    });
    
  4. Database Truncation: Make sure that the DatabaseTruncation trait is correctly truncating the database tables before each test. If it's not working as expected, you might want to manually truncate the tables in a beforeEach hook.

  5. Verify User Creation: Ensure that the user is being created in the correct database. You can do this by checking the database file directly or by outputting the user count after creation:

    test('example', function () {
        $user = User::factory()->admin()->create();
        $userCount = User::count();
        ray($userCount); // This should reflect the number of users in your dusk.sqlite
    });
    
  6. Check Browser Session: Since you mentioned that you see the login screen and "Dusk" as the project name, it indicates that the .env.dusk.local is being loaded. However, ensure that the session is not persisting across tests by clearing cookies or using a fresh session for each test.

  7. Use a Different Browser Profile: If you're using a browser profile that might be retaining session data, consider using a fresh profile for Dusk tests. You can configure this in your DuskTestCase.php by setting a unique profile path.

By following these steps, you should be able to ensure that Dusk is using the correct database for authentication. If the issue persists, consider checking for any custom configurations or middleware that might be affecting the authentication process.

Please or to participate in this conversation.