2
2
3
3
namespace Laravel \Passport \Tests \Unit ;
4
4
5
+ use Illuminate \Contracts \Auth \Authenticatable ;
5
6
use Illuminate \Contracts \Auth \StatefulGuard ;
6
7
use Illuminate \Http \Request ;
7
8
use Laravel \Passport \Bridge \Scope ;
12
13
use Laravel \Passport \Http \Controllers \AuthorizationController ;
13
14
use Laravel \Passport \Http \Responses \AuthorizationViewResponse ;
14
15
use Laravel \Passport \Passport ;
15
- use Laravel \Passport \Token ;
16
- use Laravel \Passport \TokenRepository ;
17
16
use League \OAuth2 \Server \AuthorizationServer ;
18
17
use League \OAuth2 \Server \Exception \OAuthServerException as LeagueException ;
19
18
use League \OAuth2 \Server \RequestTypes \AuthorizationRequest ;
@@ -44,7 +43,7 @@ public function test_authorization_view_is_presented()
44
43
$ controller = new AuthorizationController ($ server , $ guard , $ response );
45
44
46
45
$ guard ->shouldReceive ('guest ' )->andReturn (false );
47
- $ guard ->shouldReceive ('user ' )->andReturn ($ user = m::mock ());
46
+ $ guard ->shouldReceive ('user ' )->andReturn ($ user = m::mock (Authenticatable::class ));
48
47
$ server ->shouldReceive ('validateAuthorizationRequest ' )->andReturn ($ authRequest = m::mock (AuthorizationRequestInterface::class));
49
48
50
49
$ request = m::mock (Request::class);
@@ -60,9 +59,9 @@ public function test_authorization_view_is_presented()
60
59
$ clients = m::mock (ClientRepository::class);
61
60
$ clients ->shouldReceive ('find ' )->with (1 )->andReturn ($ client = m::mock (Client::class));
62
61
$ client ->shouldReceive ('skipsAuthorization ' )->andReturn (false );
62
+ $ client ->shouldReceive ('tokens->where->pluck->flatten ' )->andReturn (collect ());
63
63
64
- $ tokens = m::mock (TokenRepository::class);
65
- $ tokens ->shouldReceive ('findValidToken ' )->with ($ user , $ client )->andReturnNull ();
64
+ $ user ->shouldReceive ('getAuthIdentifier ' )->andReturn (1 );
66
65
67
66
$ response ->shouldReceive ('withParameters ' )->once ()->andReturnUsing (function ($ data ) use ($ client , $ user , $ request ) {
68
67
$ this ->assertEquals ($ client , $ data ['client ' ]);
@@ -74,7 +73,7 @@ public function test_authorization_view_is_presented()
74
73
});
75
74
76
75
$ this ->assertSame ('view ' , $ controller ->authorize (
77
- m::mock (ServerRequestInterface::class), $ request , $ clients, $ tokens
76
+ m::mock (ServerRequestInterface::class), $ request , $ clients
78
77
));
79
78
}
80
79
@@ -93,12 +92,11 @@ public function test_authorization_exceptions_are_handled()
93
92
$ request ->shouldReceive ('session ' )->andReturn ($ session = m::mock ());
94
93
95
94
$ clients = m::mock (ClientRepository::class);
96
- $ tokens = m::mock (TokenRepository::class);
97
95
98
96
$ this ->expectException (OAuthServerException::class);
99
97
100
98
$ controller ->authorize (
101
- m::mock (ServerRequestInterface::class), $ request , $ clients, $ tokens
99
+ m::mock (ServerRequestInterface::class), $ request , $ clients
102
100
);
103
101
}
104
102
@@ -115,7 +113,7 @@ public function test_request_is_approved_if_valid_token_exists()
115
113
$ controller = new AuthorizationController ($ server , $ guard , $ response );
116
114
117
115
$ guard ->shouldReceive ('guest ' )->andReturn (false );
118
- $ guard ->shouldReceive ('user ' )->andReturn ($ user = m::mock ());
116
+ $ guard ->shouldReceive ('user ' )->andReturn ($ user = m::mock (Authenticatable::class ));
119
117
$ psrResponse = new Response ();
120
118
$ psrResponse ->getBody ()->write ('approved ' );
121
119
$ server ->shouldReceive ('validateAuthorizationRequest ' )
@@ -140,15 +138,11 @@ public function test_request_is_approved_if_valid_token_exists()
140
138
$ clients ->shouldReceive ('find ' )->with (1 )->andReturn ($ client = m::mock (Client::class));
141
139
142
140
$ client ->shouldReceive ('skipsAuthorization ' )->andReturn (false );
143
-
144
- $ tokens = m::mock (TokenRepository::class);
145
- $ tokens ->shouldReceive ('findValidToken ' )
146
- ->with ($ user , $ client )
147
- ->andReturn ($ token = m::mock (Token::class));
148
- $ token ->shouldReceive ('getAttribute ' )->with ('scopes ' )->andReturn (['scope-1 ' ]);
141
+ $ client ->shouldReceive ('getKey ' )->andReturn (1 );
142
+ $ client ->shouldReceive ('tokens->where->pluck->flatten ' )->andReturn (collect (['scope-1 ' ]));
149
143
150
144
$ this ->assertSame ('approved ' , $ controller ->authorize (
151
- m::mock (ServerRequestInterface::class), $ request , $ clients, $ tokens
145
+ m::mock (ServerRequestInterface::class), $ request , $ clients
152
146
)->getContent ());
153
147
}
154
148
@@ -165,7 +159,7 @@ public function test_request_is_approved_if_client_can_skip_authorization()
165
159
$ controller = new AuthorizationController ($ server , $ guard , $ response );
166
160
167
161
$ guard ->shouldReceive ('guest ' )->andReturn (false );
168
- $ guard ->shouldReceive ('user ' )->andReturn ($ user = m::mock ());
162
+ $ guard ->shouldReceive ('user ' )->andReturn ($ user = m::mock (Authenticatable::class ));
169
163
$ psrResponse = new Response ();
170
164
$ psrResponse ->getBody ()->write ('approved ' );
171
165
$ server ->shouldReceive ('validateAuthorizationRequest ' )
@@ -191,13 +185,8 @@ public function test_request_is_approved_if_client_can_skip_authorization()
191
185
192
186
$ client ->shouldReceive ('skipsAuthorization ' )->andReturn (true );
193
187
194
- $ tokens = m::mock (TokenRepository::class);
195
- $ tokens ->shouldReceive ('findValidToken ' )
196
- ->with ($ user , $ client )
197
- ->andReturnNull ();
198
-
199
188
$ this ->assertSame ('approved ' , $ controller ->authorize (
200
- m::mock (ServerRequestInterface::class), $ request , $ clients, $ tokens
189
+ m::mock (ServerRequestInterface::class), $ request , $ clients
201
190
)->getContent ());
202
191
}
203
192
@@ -232,9 +221,6 @@ public function test_authorization_view_is_presented_if_request_has_prompt_equal
232
221
$ clients ->shouldReceive ('find ' )->with (1 )->andReturn ($ client = m::mock (Client::class));
233
222
$ client ->shouldReceive ('skipsAuthorization ' )->andReturn (false );
234
223
235
- $ tokens = m::mock (TokenRepository::class);
236
- $ tokens ->shouldNotReceive ('findValidToken ' );
237
-
238
224
$ response ->shouldReceive ('withParameters ' )->once ()->andReturnUsing (function ($ data ) use ($ client , $ user , $ request ) {
239
225
$ this ->assertEquals ($ client , $ data ['client ' ]);
240
226
$ this ->assertEquals ($ user , $ data ['user ' ]);
@@ -245,7 +231,7 @@ public function test_authorization_view_is_presented_if_request_has_prompt_equal
245
231
});
246
232
247
233
$ this ->assertSame ('view ' , $ controller ->authorize (
248
- m::mock (ServerRequestInterface::class), $ request , $ clients, $ tokens
234
+ m::mock (ServerRequestInterface::class), $ request , $ clients
249
235
));
250
236
}
251
237
@@ -264,7 +250,7 @@ public function test_authorization_denied_if_request_has_prompt_equals_to_none()
264
250
$ controller = new AuthorizationController ($ server , $ guard , $ response );
265
251
266
252
$ guard ->shouldReceive ('guest ' )->andReturn (false );
267
- $ guard ->shouldReceive ('user ' )->andReturn ($ user = m::mock ());
253
+ $ guard ->shouldReceive ('user ' )->andReturn ($ user = m::mock (Authenticatable::class ));
268
254
$ server ->shouldReceive ('validateAuthorizationRequest ' )
269
255
->andReturn ($ authRequest = m::mock (AuthorizationRequest::class));
270
256
$ server ->shouldReceive ('completeAuthorizationRequest ' )
@@ -288,14 +274,11 @@ public function test_authorization_denied_if_request_has_prompt_equals_to_none()
288
274
$ clients = m::mock (ClientRepository::class);
289
275
$ clients ->shouldReceive ('find ' )->with (1 )->andReturn ($ client = m::mock (Client::class));
290
276
$ client ->shouldReceive ('skipsAuthorization ' )->andReturn (false );
291
-
292
- $ tokens = m::mock (TokenRepository::class);
293
- $ tokens ->shouldReceive ('findValidToken ' )
294
- ->with ($ user , $ client )
295
- ->andReturnNull ();
277
+ $ client ->shouldReceive ('getKey ' )->andReturn (1 );
278
+ $ client ->shouldReceive ('tokens->where->pluck->flatten ' )->andReturn (collect ());
296
279
297
280
$ controller ->authorize (
298
- m::mock (ServerRequestInterface::class), $ request , $ clients, $ tokens
281
+ m::mock (ServerRequestInterface::class), $ request , $ clients
299
282
);
300
283
}
301
284
@@ -324,11 +307,10 @@ public function test_authorization_denied_if_unauthenticated_and_request_has_pro
324
307
$ authRequest ->shouldReceive ('getGrantTypeId ' )->andReturn ('authorization_code ' );
325
308
326
309
$ clients = m::mock (ClientRepository::class);
327
- $ tokens = m::mock (TokenRepository::class);
328
310
329
311
try {
330
312
$ controller ->authorize (
331
- m::mock (ServerRequestInterface::class), $ request , $ clients, $ tokens
313
+ m::mock (ServerRequestInterface::class), $ request , $ clients
332
314
);
333
315
} catch (\Laravel \Passport \Exceptions \OAuthServerException $ e ) {
334
316
$ this ->assertStringStartsWith (
@@ -362,10 +344,9 @@ public function test_logout_and_prompt_login_if_request_has_prompt_equals_to_log
362
344
$ request ->shouldReceive ('get ' )->with ('prompt ' )->andReturn ('login ' );
363
345
364
346
$ clients = m::mock (ClientRepository::class);
365
- $ tokens = m::mock (TokenRepository::class);
366
347
367
348
$ controller ->authorize (
368
- m::mock (ServerRequestInterface::class), $ request , $ clients, $ tokens
349
+ m::mock (ServerRequestInterface::class), $ request , $ clients
369
350
);
370
351
}
371
352
@@ -390,10 +371,9 @@ public function test_user_should_be_authenticated()
390
371
$ request ->shouldReceive ('get ' )->with ('prompt ' )->andReturn (null );
391
372
392
373
$ clients = m::mock (ClientRepository::class);
393
- $ tokens = m::mock (TokenRepository::class);
394
374
395
375
$ controller ->authorize (
396
- m::mock (ServerRequestInterface::class), $ request , $ clients, $ tokens
376
+ m::mock (ServerRequestInterface::class), $ request , $ clients
397
377
);
398
378
}
399
379
}
0 commit comments