在本地通过的义务通行证,但在Bitbucket管道中失败

发布于 2025-01-31 04:54:03 字数 2998 浏览 3 评论 0原文

我正在使用Laravel/Breeze使用的内置身份验证构建Laravel 8应用程序。我已经使用AsserTauthenticated()编写了几个测试,这些测试在本地运行时通过,但是在Bitbucket Pipeline运行时,以下任何调用功能/需要身份验证的测试返回以下内容:

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

我使用基于会话的身份验证,并在下面包含bitbucket.yml文件,kernel.php和auth.php文件。

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(包括我必须更改API组以解决身份验证不起作用)

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

    ],

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

    ],

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潜移默化 2025-02-07 04:54:03

事实证明,问题是在缓存/配置的清除中。

从.yml文件中删除以下三行解决了问题,并允许所有测试通过。

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

归功于 https:https:// forum。 gitlab.com/t/laravel-pipeline-tests-with-posts-fabs-fail-with-419/50358 提供此解决方案。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文