3

How can we send POST request from dusk browser ? I am sending the POST request, in dusk test case, from admin user to login as a different user but debugging shows that it gives 403 error, which is obvious as i am not sending the request through the browser instance. To elaborate what i am doing following is what i am trying to test

public function testLoginAs()
{
    $original = factory(User::class)->create([
        'roles' => ['admin'],
    ]);
    $other = factory(User::class)->create();

    $this->browse(function (Browser $browser) use ($original, $other) {
        $browser
            ->loginAs($original)
            ->assertAuthenticated()
            ->assertAuthenticatedAs($original)
            ->visit('/');

        // Only admin can perform this action
        $response = $this->call('POST', route('login.as', $other), ['_token' => csrf_token()]);
        //dd($response);

        $browser
            ->visit('/')
            ->assertAuthenticated()
            ->assertAuthenticatedAs($other);
    });
}

Following is the error message for dd($response) in test.

Sorry, you are not authorized to perform this action.
You are not currently logged in.

Is there any way i can send a POST call from dusk browser ? Something like

$browser->call('POST', route('login.as', $other));
// OR
$browser->post(route('login.as', $other));

Am i missing something ?

To avoid CSRF token issue i had added ['_token' => csrf_token()] and also tried to use use WithoutMiddleware; but with no result.

hashmi
  • 651
  • 2
  • 9
  • 24

0 Answers0