Skip to content

Cannot use actingAs unit tests functionality #161

Closed
@philliphartin

Description

@philliphartin

Description

I am having issues getting the actingAs($user) functionality working in our http unit tests since using the Auth0 implementation.

Our API uses the CheckJWT middleware outlined in Auth0's Laravel API auth guide. This middleware expects a Bearer token to authenticate the user.

We are always receiving a 401 - Unauthenticated and can see that the middleware is still being processed and obviously failing due to no Bearer token being sent.

What are our options in this regard?

  • The user is created from a user factory and does not actually exist on Auth0, we just want to spoof the identity to facilitate the request.

Example implementation

<?php

namespace Tests\Feature;

use App\Models\User;
use Tests\TestCase;

class ExampleTests extends TestCase
{
    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function testExample()
    {
        $user = factory(User::class)->create();
        $response = $this->actingAs($user, 'api')->json('POST', '/api/v1/post', ['title' => 'Welcome to Mars']);
        $response
            ->assertStatus(200)
            ->assertExactJson([
                'created' => true,
            ]);
    }
}

Environment

Packages:

  • "auth0/login": "~5.0"
  • "laravel/framework": "^6.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions