Summer Sale! All accounts are 50% off this week.

wonder95's avatar

Dusk tests fail only on CircleCI with 401 Unauthorized

One of the last things to do in my Laravel 11 upgrade from hell is to figure why all of my Dusk tests fail with a

Failed to load resource: the server responded with a status of 401 (Unauthorized)

error, but only on CircleCI. When I run them locally, they all pass just fine, but when they run on Circle, there are many that fail - some intermittently - at our /auth-user endpoint. This is the controller function

    public function authUser(Request $request)
    {
        if ($userModel = $request->user()) {
            return new UserResource(
                $userModel->load(self::RELATIONSHIPS)
            );
        }
    }

For the upgrade I went from Laravel 10 to 11, and laravel/dusk from 7.11.1 to 8.

I know it's a broad question, but I'm hoping someone might have seen the same thing with Dusk 8.

0 likes
1 reply
LaryAI's avatar
Level 58

It sounds like the issue you're encountering is related to authentication in your Dusk tests, specifically when running on CircleCI. This can often be due to differences in environment configuration between your local machine and the CI environment.

Here are a few steps you can take to troubleshoot and potentially resolve the issue:

  1. Check Environment Variables: Ensure that all necessary environment variables are correctly set in your CircleCI configuration. This includes any variables related to authentication, such as API keys, tokens, or database credentials.

  2. Database State: Make sure that the database state on CircleCI is consistent with your local environment. This includes running migrations and seeding the database with the necessary data before running your tests.

  3. Session Configuration: Verify that the session configuration is correctly set up. Sometimes, session handling can differ between environments, leading to authentication issues.

  4. Dusk Environment File: Ensure that you have a .env.dusk file set up correctly for your Dusk tests. This file should contain the necessary environment variables for running your tests.

  5. CircleCI Configuration: Double-check your CircleCI configuration file (.circleci/config.yml) to ensure that all steps required to set up the environment and run the tests are included.

  6. Debugging: Add some debugging statements to your authUser method to log the request and user information. This can help you understand why the request is failing.

Here is an example of how you might modify your authUser method to include some debugging:

public function authUser(Request $request)
{
    if ($userModel = $request->user()) {
        \Log::info('User authenticated', ['user' => $userModel]);
        return new UserResource(
            $userModel->load(self::RELATIONSHIPS)
        );
    } else {
        \Log::warning('User not authenticated', ['request' => $request->all()]);
        return response()->json(['error' => 'Unauthorized'], 401);
    }
}
  1. CircleCI Cache: Sometimes, CircleCI caches can cause issues. Try clearing the cache and rerunning the tests.

  2. Dusk Configuration: Ensure that your Dusk configuration is correctly set up for CircleCI. You might need to adjust the DuskTestCase to handle the CI environment properly.

Here is an example of how you might configure Dusk for CircleCI:

use Laravel\Dusk\TestCase as BaseTestCase;

abstract class DuskTestCase extends BaseTestCase
{
    use CreatesApplication;

    /**
     * Prepare for Dusk test execution.
     *
     * @beforeClass
     * @return void
     */
    public static function prepare()
    {
        if (env('CI')) {
            static::startChromeDriver();
        }
    }

    /**
     * Create the RemoteWebDriver instance.
     *
     * @return \Facebook\WebDriver\Remote\RemoteWebDriver
     */
    protected function driver()
    {
        $options = (new ChromeOptions)->addArguments([
            '--disable-gpu',
            '--headless',
            '--window-size=1920,1080',
            '--no-sandbox',
        ]);

        return RemoteWebDriver::create(
            'http://localhost:9515', DesiredCapabilities::chrome()->setCapability(
                ChromeOptions::CAPABILITY, $options
            )
        );
    }
}

By following these steps, you should be able to identify and resolve the issue causing your Dusk tests to fail with a 401 Unauthorized error on CircleCI.

Please or to participate in this conversation.