Skip to content

Commit 8d3ab00

Browse files
[13.x] Test Improvements (#1853)
* check device flow enabled before prompting user * fix calling `after` modifier on `nullableMorphs` * test improvements
1 parent b52c089 commit 8d3ab00

11 files changed

+24
-22
lines changed

UPGRADE.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ If you prefer to use the new structure, you may create a migration to apply the
139139

140140
```php
141141
Schema::table('oauth_clients', function (Blueprint $table) {
142-
$table->nullableMorphs('owner')->after('user_id');
142+
$table->after('user_id', function (Blueprint $table) {
143+
$table->nullableMorphs('owner');
144+
});
143145

144146
$table->after('provider', function (Blueprint $table) {
145147
$table->text('redirect_uris');

src/Console/ClientCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Console\Command;
66
use Laravel\Passport\Client;
77
use Laravel\Passport\ClientRepository;
8+
use Laravel\Passport\Passport;
89
use Symfony\Component\Console\Attribute\AsCommand;
910

1011
#[AsCommand(name: 'passport:client')]
@@ -149,7 +150,8 @@ protected function createAuthCodeClient(ClientRepository $clients): Client
149150
? ! $this->option('public')
150151
: $this->components->confirm('Would you like to make this client confidential?', true);
151152

152-
$enableDeviceFlow = $this->confirm('Would you like to enable the device authorization flow for this client?');
153+
$enableDeviceFlow = Passport::$deviceCodeGrantEnabled &&
154+
$this->confirm('Would you like to enable the device authorization flow for this client?');
153155

154156
return $clients->createAuthorizationCodeGrantClient(
155157
$this->option('name'), explode(',', $redirect), $confidential, null, $enableDeviceFlow

tests/Feature/AccessTokenControllerTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ public function testGettingAccessTokenWithClientCredentialsGrant()
4949
$this->assertArrayHasKey('expires_in', $decodedResponse);
5050
$this->assertArrayHasKey('access_token', $decodedResponse);
5151
$this->assertSame('Bearer', $decodedResponse['token_type']);
52-
$expiresInSeconds = 31536000;
53-
$this->assertEqualsWithDelta($expiresInSeconds, $decodedResponse['expires_in'], 5);
52+
$this->assertEqualsWithDelta(31536000, $decodedResponse['expires_in'], 2);
5453
}
5554

5655
public function testGettingAccessTokenWithClientCredentialsGrantInvalidClientSecret()
@@ -131,8 +130,7 @@ public function testGettingAccessTokenWithPasswordGrant()
131130
$this->assertArrayHasKey('access_token', $decodedResponse);
132131
$this->assertArrayHasKey('refresh_token', $decodedResponse);
133132
$this->assertSame('Bearer', $decodedResponse['token_type']);
134-
$expiresInSeconds = 31536000;
135-
$this->assertEqualsWithDelta($expiresInSeconds, $decodedResponse['expires_in'], 5);
133+
$this->assertEqualsWithDelta(31536000, $decodedResponse['expires_in'], 2);
136134
}
137135

138136
public function testGettingAccessTokenWithPasswordGrantWithInvalidPassword()

tests/Feature/AuthorizationCodeGrantTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function testIssueAccessToken()
7676
$this->assertArrayHasKey('access_token', $json);
7777
$this->assertArrayHasKey('refresh_token', $json);
7878
$this->assertSame('Bearer', $json['token_type']);
79-
$this->assertSame(31536000, $json['expires_in']);
79+
$this->assertEqualsWithDelta(31536000, $json['expires_in'], 2);
8080

8181
Route::get('/foo', fn (Request $request) => $request->user()->currentAccessToken()->toJson())
8282
->middleware('auth:api');

tests/Feature/AuthorizationCodeGrantWithPkceTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function testIssueAccessToken()
8181
$this->assertArrayHasKey('access_token', $json);
8282
$this->assertArrayHasKey('refresh_token', $json);
8383
$this->assertSame('Bearer', $json['token_type']);
84-
$this->assertSame(31536000, $json['expires_in']);
84+
$this->assertEqualsWithDelta(31536000, $json['expires_in'], 2);
8585

8686
$refreshToken = $json['refresh_token'];
8787

@@ -103,7 +103,7 @@ public function testIssueAccessToken()
103103

104104
$this->assertArrayHasKey('access_token', $newToken);
105105
$this->assertArrayHasKey('refresh_token', $newToken);
106-
$this->assertSame(31536000, $newToken['expires_in']);
106+
$this->assertEqualsWithDelta(31536000, $newToken['expires_in'], 2);
107107
$this->assertSame('Bearer', $newToken['token_type']);
108108
}
109109

tests/Feature/ClientCredentialsGrantTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function testIssueAccessToken()
4040
$this->assertArrayHasKey('access_token', $json);
4141
$this->assertArrayNotHasKey('refresh_token', $json);
4242
$this->assertSame('Bearer', $json['token_type']);
43-
$this->assertSame(31536000, $json['expires_in']);
43+
$this->assertEqualsWithDelta(31536000, $json['expires_in'], 2);
4444

4545
Route::get('/foo', fn (Request $request) => response('response'))
4646
->middleware([EnsureClientIsResourceOwner::using(['create', 'delete'])]);
@@ -70,7 +70,7 @@ public function testIssueAccessTokenWithAllScopes()
7070
$this->assertArrayHasKey('access_token', $json);
7171
$this->assertArrayNotHasKey('refresh_token', $json);
7272
$this->assertSame('Bearer', $json['token_type']);
73-
$this->assertSame(31536000, $json['expires_in']);
73+
$this->assertEqualsWithDelta(31536000, $json['expires_in'], 2);
7474

7575
Route::get('/foo', fn (Request $request) => response('response'))
7676
->middleware([EnsureClientIsResourceOwner::using(['create', 'delete'])]);

tests/Feature/DeviceAuthorizationGrantTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public function testIssueAccessToken()
175175
$this->assertArrayHasKey('access_token', $json);
176176
$this->assertArrayHasKey('refresh_token', $json);
177177
$this->assertSame('Bearer', $json['token_type']);
178-
$this->assertSame(31536000, $json['expires_in']);
178+
$this->assertEqualsWithDelta(31536000, $json['expires_in'], 2);
179179

180180
Route::get('/foo', fn (Request $request) => $request->user()->currentAccessToken()->toJson())
181181
->middleware('auth:api');
@@ -254,7 +254,7 @@ public function testPublicClient()
254254
$this->assertArrayHasKey('access_token', $json);
255255
$this->assertArrayHasKey('refresh_token', $json);
256256
$this->assertSame('Bearer', $json['token_type']);
257-
$this->assertSame(31536000, $json['expires_in']);
257+
$this->assertEqualsWithDelta(31536000, $json['expires_in'], 2);
258258
}
259259

260260
public function testUnauthorizedClient()

tests/Feature/ImplicitGrantTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function testIssueAccessToken()
6565
$this->assertArrayHasKey('access_token', $params);
6666
$this->assertArrayNotHasKey('refresh_token', $params);
6767
$this->assertSame('Bearer', $params['token_type']);
68-
$this->assertSame('31536000', $params['expires_in']);
68+
$this->assertEqualsWithDelta(31536000, $params['expires_in'], 2);
6969

7070
Route::get('/foo', fn (Request $request) => $request->user()->currentAccessToken()->toJson())
7171
->middleware('auth:api');
@@ -145,7 +145,7 @@ public function testSkipsAuthorizationWhenHasGrantedScopes()
145145
$this->assertArrayHasKey('access_token', $params);
146146
$this->assertArrayNotHasKey('refresh_token', $params);
147147
$this->assertSame('Bearer', $params['token_type']);
148-
$this->assertSame('31536000', $params['expires_in']);
148+
$this->assertEqualsWithDelta(31536000, $params['expires_in'], 2);
149149
}
150150

151151
public function testValidateAuthorizationRequest()

tests/Feature/PasswordGrantTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function testIssueToken()
4848
$this->assertArrayHasKey('access_token', $json);
4949
$this->assertArrayHasKey('refresh_token', $json);
5050
$this->assertSame('Bearer', $json['token_type']);
51-
$this->assertSame(31536000, $json['expires_in']);
51+
$this->assertEqualsWithDelta(31536000, $json['expires_in'], 2);
5252

5353
Route::get('/foo', fn (Request $request) => $request->user()->currentAccessToken()->toJson())
5454
->middleware('auth:api');
@@ -77,7 +77,7 @@ public function testIssueTokenWithAllScopes()
7777
$this->assertArrayHasKey('access_token', $json);
7878
$this->assertArrayHasKey('refresh_token', $json);
7979
$this->assertSame('Bearer', $json['token_type']);
80-
$this->assertSame(31536000, $json['expires_in']);
80+
$this->assertEqualsWithDelta(31536000, $json['expires_in'], 2);
8181

8282
Route::get('/foo', fn (Request $request) => $request->user()->currentAccessToken()->toJson())
8383
->middleware('auth:api');
@@ -167,7 +167,7 @@ public function testPublicClient()
167167
$this->assertArrayHasKey('access_token', $json);
168168
$this->assertArrayHasKey('refresh_token', $json);
169169
$this->assertSame('Bearer', $json['token_type']);
170-
$this->assertSame(31536000, $json['expires_in']);
170+
$this->assertEqualsWithDelta(31536000, $json['expires_in'], 2);
171171
}
172172

173173
public function testUnauthorizedClient()

tests/Feature/PersonalAccessGrantTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function testIssueToken()
3838
$this->assertArrayHasKey('accessToken', $result->toArray());
3939
$this->assertSame($token->getKey(), $result->accessTokenId);
4040
$this->assertSame('Bearer', $result->tokenType);
41-
$this->assertSame(31536000, $result->expiresIn);
41+
$this->assertEqualsWithDelta(31536000, $result->expiresIn, 2);
4242
$this->assertSame($client->getKey(), $token->client_id);
4343
$this->assertSame($user->getAuthIdentifier(), $token->user_id);
4444
$this->assertSame(['bar'], $token->scopes);

0 commit comments

Comments
 (0)