Search code examples
phplaravelbitbucketlaravel-8laravel-breeze

assertAuthenticated passes locally but fails in Bitbucket Pipeline


I am building a Laravel 8 Application using the built-in Authentication used by Laravel/Breeze. I've written several tests using assertAuthenticated() which pass when running the tests locally, but when running in the Bitbucket Pipeline, the following is returned for any tests that call that function/require authentication:

The user is not authenticated
Failed asserting that false is true.

I'm using session-based authentication, and have included the bitbucket.yml file, kernel.php, and auth.php files below.

Bitbucket-pipelines.yml

image: php:8.0.2-fpm

definitions:
  services:
    mysql:
      image: mysql
      environment:
        MYSQL_DATABASE: "test"
        MYSQL_USER: "root"
        MYSQL_PASSWORD: "root"
        MYSQL_ROOT_PASSWORD: "root"

pipelines:
  default:
    - step:
        name: Test
        services:
          - mysql
        script:
          - apt-get update && apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev
          - docker-php-ext-install pdo_mysql
          - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
          - docker-php-ext-configure gd --with-freetype --with-jpeg
          - docker-php-ext-install -j$(nproc) gd
          - apt-get update && apt-get install -y unzip libzip-dev
          - docker-php-ext-install zip
          - docker-php-ext-enable zip
          - composer require monolog/monolog
          - composer require laravel/breeze --dev
          - composer install
          - composer update
          - cp .env.testing .env
          - php artisan migrate:fresh
          - php artisan db:seed
          - php artisan config:clear
          - php artisan config:cache
          - php artisan cache:clear
          - php artisan key:generate
          - php artisan test --testsuite=Feature #--filter EmailVerificationTest
        caches:
          - composer

app\Http\Kernel.php (Included as I had to change the API group to resolve authentication not working )

protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
            'throttle:api',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
    ];

config\auth.php

'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'api' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

    ],

Solution

  • Turns out the issue was with the clearing of the cache/config.

    Removing the following three lines from the .yml file resolved the issue and allowed all tests to pass.

    - php artisan config:clear
    - php artisan config:cache
    - php artisan cache:clear
    

    Credit to https://forum.gitlab.com/t/laravel-pipeline-tests-with-posts-fail-with-419/50358 for providing this solution.