I am writing phpunit tests in Laravel.
I log in successfully into the application using the folloiwng code:
$response = $this->json('POST', 'login',
[
'user_name' => $username,
'password' => $password ,
]);
$response->assertStatus(200); // This is ok and passes the test -- is green
I get the following results when I dd $response:
+original: array:5 [
"status" => 200
"data" => array:4 [
"token_type" => "Bearer"
"expires_in" => 7200
"access_token" => "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOi...snipped"
"refresh_token" => "def50200eb65ae708701e58c732140ea2efca0083...snipped"
When I invoke a protected method called doSomething as follows
$token = "Bearer and I hardcode the access token above"
$response = $this->json('POST', 'doSomething', [
'token' => $token
]);
dd($response);
I get the following message:
"{"status":401,"message":"Unauthenticated.Token invalid or expired. Please login or refresh token to proceed"}"
Question:
- How can I pass the valid token I received from the successful login to the doSomething method and all the other routes I want to test?