[7.x] 'actingAsClient' method for tests #1083
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Laravel Passport already provides an
actingAs
method that mocks the user authentication. This pull request adds the methodactingAsClient
to mock the client authentication (optionally using certain scopes).Why ?
If you currently want to test an API route that uses client authentication, you have to make an actual request to the
/oauth/token
route to get a client access token. Then you have to add that token to theAuthorization
header of the next request that tests the API route.Fixes issues #514 and #680.
How?
This method was already proposed in an older pull request (#847), but that implementation mocked the middleware.
My implementation mocks the
ResourceServer
class. This class is used by both theCheckClientCredentials
andCheckClientCredentialsForAnyScope
middleware classes.The
ResourceServer
class parses the requestAuthorization
header and sets certain attributes on the request. (in theBearerTokenValidator
)Those attributes are later used in the middleware classes to validate the scopes.
By just mocking the
ResourceServer
class, we don't need to mock the entire middleware classes or the scopes logic.If the pull request is approved, I will make a pull request to update the documentation