0

I have this code

    public function it_can_change_password()
    {
        DB::beginTransaction();

        $payloadForUser = [
            "name" => "Name",
            "first_name" => $this->faker->firstName,
            "last_name"  => $this->faker->lastName,
            "email"      => $this->faker->unique()->safeEmail,
            "role_id"    => $this->faker->numberBetween(1, 5),
            "branch_id"  => 1,
            "password" => bcrypt("password")
        ];

        $user = User::create($payloadForUser);

        $credentials = [
            'email' => $user->email,
            'password' => 'password',
            'token_name' => 'admin',
            'api_client' => true,
        ];

        $loginResponse = $this->postJson('api/login', $credentials);
        $loginResponse->assertStatus(200);
        $data = $loginResponse->getData();
        $this->token = $data->token;

        $password = $this->faker->password;

        $payload = [
            'current_password' => "password", # Todo-139
            'new_password' => $password,
            'new_password_confirmation' => $password,
        ];

        $response = $this->postJson('api/user/change/password', $payload, ['Authorization' => 'Bearer ' . $this->token]);
        $response->assertOk();

        $response = $this->postJson('api/logout', [], ['Authorization' => 'Bearer ' . $this->token]);
        $response->assertServerError();

        # Login using old password
        $credentials = [
            'email' => $user->email,
            'password' => 'password',
            'token_name' => 'admin',
            'api_client' => true,
        ];

        $loginResponse = $this->postJson('api/login', $credentials);
        $response->assertServerError();

        # Login using new password
        $credentials = [
            'email' => $user->email,
            'password' => $password,
            'token_name' => 'admin',
            'api_client' => true,
        ];

        $loginResponse = $this->postJson('api/login', $credentials);
        $loginResponse->assertStatus(200);

        DB::rollBack();
    }

But I encountered error below when running php artisan test --filter=it_can_change_password

BadMethodCallException: Method Illuminate\Auth\RequestGuard::attempt does not exist. in /vendor/laravel/framework/src/Illuminate/Macroable/Traits/Macroable.php:113

on the last

        $loginResponse = $this->postJson('api/login', $credentials);
        $loginResponse->assertStatus(200);

Has anyone know how to mitigate this issue?

Fil
  • 8,225
  • 14
  • 59
  • 85

1 Answers1

0

This was solved by finding this answer here https://github.com/tymondesigns/jwt-auth/issues/1498#issuecomment-521829241

The function ( Auth::attempt ) this is only available for routes with web guard so if you are using another middleware to access this function you need to use the web guard. A simple solution is to insert guard('web') , Auth::guard('web')->attempt like so

$credentials = ['email' => $request->username, 'password' => $request->password];

if (Auth::guard('web')->attempt($credentials, false, false)) {
    return ['result' => 'ok'];
}

return ['result' => 'not ok'];

This should give you access to the Auth class without using the web guard in your request.

Fil
  • 8,225
  • 14
  • 59
  • 85