-
Notifications
You must be signed in to change notification settings - Fork 783
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[10.x] Client Credentials Feature tests (#1341)
* feat: add factory state method for client credentials grant * test: add pass/fail tests for client credentials grant * style: trailing comma
- Loading branch information
1 parent
df21a43
commit e576928
Showing
2 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,95 @@ protected function getUserClass() | |
return User::class; | ||
} | ||
|
||
public function testGettingAccessTokenWithClientCredentialsGrant() | ||
{ | ||
$this->withoutExceptionHandling(); | ||
|
||
$user = new User(); | ||
$user->email = '[email protected]'; | ||
$user->password = $this->app->make(Hasher::class)->make('foobar123'); | ||
$user->save(); | ||
|
||
/** @var Client $client */ | ||
$client = ClientFactory::new()->asClientCredentials()->create(['user_id' => $user->id]); | ||
|
||
$response = $this->post( | ||
'/oauth/token', | ||
[ | ||
'grant_type' => 'client_credentials', | ||
'client_id' => $client->id, | ||
'client_secret' => $client->secret, | ||
] | ||
); | ||
|
||
$response->assertOk(); | ||
|
||
$response->assertHeader('pragma', 'no-cache'); | ||
$response->assertHeader('cache-control', 'no-store, private'); | ||
$response->assertHeader('content-type', 'application/json; charset=UTF-8'); | ||
|
||
$decodedResponse = $response->decodeResponseJson()->json(); | ||
|
||
$this->assertArrayHasKey('token_type', $decodedResponse); | ||
$this->assertArrayHasKey('expires_in', $decodedResponse); | ||
$this->assertArrayHasKey('access_token', $decodedResponse); | ||
$this->assertSame('Bearer', $decodedResponse['token_type']); | ||
$expiresInSeconds = 31536000; | ||
$this->assertEqualsWithDelta($expiresInSeconds, $decodedResponse['expires_in'], 5); | ||
|
||
$jwtAccessToken = (new Parser())->parse($decodedResponse['access_token']); | ||
$this->assertTrue($this->app->make(ClientRepository::class)->findActive($jwtAccessToken->getClaim('aud'))->is($client)); | ||
|
||
$token = $this->app->make(TokenRepository::class)->find($jwtAccessToken->getClaim('jti')); | ||
$this->assertInstanceOf(Token::class, $token); | ||
$this->assertTrue($token->client->is($client)); | ||
$this->assertFalse($token->revoked); | ||
$this->assertNull($token->name); | ||
$this->assertNull($token->user_id); | ||
$this->assertLessThanOrEqual(5, CarbonImmutable::now()->addSeconds($expiresInSeconds)->diffInSeconds($token->expires_at)); | ||
} | ||
|
||
public function testGettingAccessTokenWithClientCredentialsGrantInvalidClientSecret() | ||
{ | ||
$user = new User(); | ||
$user->email = '[email protected]'; | ||
$user->password = $this->app->make(Hasher::class)->make('foobar123'); | ||
$user->save(); | ||
|
||
/** @var Client $client */ | ||
$client = ClientFactory::new()->asClientCredentials()->create(['user_id' => $user->id]); | ||
|
||
$response = $this->post( | ||
'/oauth/token', | ||
[ | ||
'grant_type' => 'client_credentials', | ||
'client_id' => $client->id, | ||
'client_secret' => $client->secret.'foo', | ||
] | ||
); | ||
|
||
$response->assertStatus(401); | ||
|
||
$response->assertHeader('cache-control', 'no-cache, private'); | ||
$response->assertHeader('content-type', 'application/json'); | ||
|
||
$decodedResponse = $response->decodeResponseJson()->json(); | ||
|
||
$this->assertArrayNotHasKey('token_type', $decodedResponse); | ||
$this->assertArrayNotHasKey('expires_in', $decodedResponse); | ||
$this->assertArrayNotHasKey('access_token', $decodedResponse); | ||
|
||
$this->assertArrayHasKey('error', $decodedResponse); | ||
$this->assertSame('invalid_client', $decodedResponse['error']); | ||
$this->assertArrayHasKey('error_description', $decodedResponse); | ||
$this->assertSame('Client authentication failed', $decodedResponse['error_description']); | ||
$this->assertArrayNotHasKey('hint', $decodedResponse); | ||
$this->assertArrayHasKey('message', $decodedResponse); | ||
$this->assertSame('Client authentication failed', $decodedResponse['message']); | ||
|
||
$this->assertSame(0, Token::count()); | ||
} | ||
|
||
public function testGettingAccessTokenWithPasswordGrant() | ||
{ | ||
$this->withoutExceptionHandling(); | ||
|