In laravel 5.8 app I make Feature/test with user creation and opening profile page of the created user and I got error when triggering button click:
<?php
namespace Tests\Feature;
use Tests\TestCase;
use DB;
use Illuminate\Foundation\Testing\WithFaker;
// use Illuminate\Foundation\Testing\WithoutMiddleware; // Prevent all middleware from being executed for this test class.
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\User;
class ProfileTestingTest extends TestCase
{
use DatabaseTransactions;
public function testProfilePage()
{
$response->assertStatus(HTTP_RESPONSE_OK);
$response->assertSee(htmlspecialchars("Profile : Details"));
// Error next lines triggering button click :
$response = $this->actingAs($newUser, 'web')->json('POST', 'profile/generate-password',[ '_token' => $csrf_token ]);
$response->assertStatus(200);
$response->assertJson(['error_code' => 0]);
$response->assertJson(['message' => 'New password was generated !' ]);
All above works ok, but this form has button for password generation and pushing it I have error :
Expected status code 200 but received 419.
419 unknown status - usually that error when request has no csrf.
In routes/web.php :
Route::group(array('prefix' => 'profile', 'middleware' => ['auth', 'isVerified', 'CheckUserStatus']), function(){
...
Route::post('generate-password', array(
'as' => 'profile-generate-password',
'uses' => 'ProfileController@generate_password'
));
}); // Route::group(array('prefix' => 'profile'), function(){
And as far as I know tests can not have csrf and there is WithoutMiddleware, which prevents all middleware from being executed for this test class.
But if WithoutMiddleware is used when in controll action triggered by profile/generate-password I have ref to current logged user
Auth::user()
I got error on invalid user.
If there is a way to deal it ?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire