Estoy teniendo problemas a la hora de escribir peticiones POST en los test de integración, en la maquina en local donde lanzo los test estos pasan sin problemas, pero en el contenedor docker de gitlab fallan por el token csrf, aunque ponga el token en la petición sigo sin pasar el test, es un poco estraño.
Tenia entendito que este middleware se dehabilitaba en los test, pero ya no se que pensar.
Error
Illuminate\Session\TokenMismatchException:
.../vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php:71
Código test
public function testClientStatusUpdate()
{
$this->withoutExceptionHandling();
$user = \App\User::find(1);
$response = $this->actingAs($user)->post('/client/2/status', [
'status' => 1,
'_token' => csrf_token()
]);
$response->assertStatus(302);
$response = $this->actingAs($user)->get('/client/2');
$response->assertViewIs('client.show');
$response->assertSuccessful();
$response->assertSee('Estado cambiado correctamente correctamente.');
$response->assertSee('<span class="label label-success">Activo</span>');
}
CI/CD
image: php:latest
services:
- mysql:5.7.24
variables:
MYSQL_DATABASE: project_name
MYSQL_ROOT_PASSWORD: secret
cache:
paths:
- vendor/
- node_modules/
before_script:
- apt-get update -yqq
- apt-get install libcurl4-gnutls-dev libicu-dev libmcrypt-dev libpng-dev libxml2-dev libbz2-dev libpcre3-dev -yqq
- docker-php-ext-install mbstring pdo_mysql curl json intl gd xml zip bz2 opcache
- curl -sS https://getcomposer.org/installer | php
# Run build
- php composer.phar install
- cp .env.example .env
- php artisan --version
- php artisan key:generate
- php artisan config:cache
- php artisan migrate --force
- php artisan db:seed --force
test:
script:
- php vendor/bin/phpunit --coverage-text --colors=never
Otros datos:
Laravel: 5.5.44
PHPUnit: 6.5.13
¿Hay alguna forma de desactivar el middleware VerifyCsrfToken
para los test o de escribir los test de forma que se obtenga un token válido para que pase la prueba?