John Davidson

php - assertAuthenticated passes locally but fails in Bitbucket Pipeline

0 comments
Message:


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',
],

],


My Answer:
It seems like the issue might be related to the session not being properly maintained or authenticated in the Bitbucket Pipeline environment. Here are a few things you can try to troubleshoot and fix the issue:

1. Check the session configuration in your Laravel application. Make sure that the session driver is set to 'file' or 'database' in your .env file or config/session.php file. This will ensure that the session data is persisted and accessible across different environments.

2. Verify that the session cookie domain and path are correctly set in your .env file or config/session.php file. If the cookie domain or path is not set correctly, it may cause issues with session authentication.

3. Check if the session data is being properly stored and retrieved in the Bitbucket Pipeline environment. You can add some debug statements in your test cases to print out the session data and see if it matches what you expect.

4. Make sure that the user is properly authenticated before calling assertAuthenticated() in your test cases. You can use the actingAs() method to authenticate a user before running the test.

5. If the issue persists, you can try using a different session driver (e.g., database) in the Bitbucket Pipeline environment to see if that resolves the authentication issue.

By following these steps and ensuring that the session configuration is set up correctly, you should be able to resolve the authentication issue in the Bitbucket Pipeline environment.

Rate this post

3 of 5 based on 8830 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us