Skip to content

Commit 88a349a

Browse files
committed
Update tests of 'check client credentials (of any scope)' middlewares
1 parent 1d90eca commit 88a349a

File tree

2 files changed

+38
-75
lines changed

2 files changed

+38
-75
lines changed

tests/CheckClientCredentialsForAnyScopeTest.php

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
use Illuminate\Http\Request;
66
use Laravel\Passport\Client;
77
use Laravel\Passport\Http\Middleware\CheckClientCredentialsForAnyScope;
8-
use Laravel\Passport\Token;
9-
use Laravel\Passport\TokenRepository;
8+
use Laravel\Passport\ClientRepository;
109
use League\OAuth2\Server\Exception\OAuthServerException;
1110
use League\OAuth2\Server\ResourceServer;
1211
use Mockery as m;
@@ -24,21 +23,17 @@ public function test_request_is_passed_along_if_token_is_valid()
2423
$resourceServer = m::mock(ResourceServer::class);
2524
$resourceServer->shouldReceive('validateAuthenticatedRequest')->andReturn($psr = m::mock());
2625
$psr->shouldReceive('getAttribute')->with('oauth_user_id')->andReturn(1);
27-
$psr->shouldReceive('getAttribute')->with('oauth_client_id')->andReturn(1);
26+
$psr->shouldReceive('getAttribute')->with('oauth_client_id')->andReturn(2);
2827
$psr->shouldReceive('getAttribute')->with('oauth_access_token_id')->andReturn('token');
2928
$psr->shouldReceive('getAttribute')->with('oauth_scopes')->andReturn(['*']);
3029

3130
$client = m::mock(Client::class);
3231
$client->shouldReceive('firstParty')->andReturnFalse();
3332

34-
$token = m::mock(Token::class);
35-
$token->shouldReceive('getAttribute')->with('client')->andReturn($client);
36-
$token->shouldReceive('getAttribute')->with('scopes')->andReturn(['*']);
33+
$clientRepository = m::mock(ClientRepository::class);
34+
$clientRepository->shouldReceive('find')->with(2)->andReturn($client);
3735

38-
$tokenRepository = m::mock(TokenRepository::class);
39-
$tokenRepository->shouldReceive('find')->with('token')->andReturn($token);
40-
41-
$middleware = new CheckClientCredentialsForAnyScope($resourceServer, $tokenRepository);
36+
$middleware = new CheckClientCredentialsForAnyScope($resourceServer, $clientRepository);
4237

4338
$request = Request::create('/');
4439
$request->headers->set('Authorization', 'Bearer token');
@@ -55,23 +50,17 @@ public function test_request_is_passed_along_if_token_has_any_required_scope()
5550
$resourceServer = m::mock(ResourceServer::class);
5651
$resourceServer->shouldReceive('validateAuthenticatedRequest')->andReturn($psr = m::mock());
5752
$psr->shouldReceive('getAttribute')->with('oauth_user_id')->andReturn(1);
58-
$psr->shouldReceive('getAttribute')->with('oauth_client_id')->andReturn(1);
53+
$psr->shouldReceive('getAttribute')->with('oauth_client_id')->andReturn(2);
5954
$psr->shouldReceive('getAttribute')->with('oauth_access_token_id')->andReturn('token');
6055
$psr->shouldReceive('getAttribute')->with('oauth_scopes')->andReturn(['foo', 'bar', 'baz']);
6156

6257
$client = m::mock(Client::class);
6358
$client->shouldReceive('firstParty')->andReturnFalse();
6459

65-
$token = m::mock(Token::class);
66-
$token->shouldReceive('getAttribute')->with('client')->andReturn($client);
67-
$token->shouldReceive('getAttribute')->with('scopes')->andReturn(['foo', 'bar', 'baz']);
68-
$token->shouldReceive('can')->with('notfoo')->andReturnFalse();
69-
$token->shouldReceive('can')->with('bar')->andReturnTrue();
70-
71-
$tokenRepository = m::mock(TokenRepository::class);
72-
$tokenRepository->shouldReceive('find')->with('token')->andReturn($token);
60+
$clientRepository = m::mock(ClientRepository::class);
61+
$clientRepository->shouldReceive('find')->with(2)->andReturn($client);
7362

74-
$middleware = new CheckClientCredentialsForAnyScope($resourceServer, $tokenRepository);
63+
$middleware = new CheckClientCredentialsForAnyScope($resourceServer, $clientRepository);
7564

7665
$request = Request::create('/');
7766
$request->headers->set('Authorization', 'Bearer token');
@@ -88,13 +77,13 @@ public function test_request_is_passed_along_if_token_has_any_required_scope()
8877
*/
8978
public function test_exception_is_thrown_when_oauth_throws_exception()
9079
{
91-
$tokenRepository = m::mock(TokenRepository::class);
80+
$clientRepository = m::mock(ClientRepository::class);
9281
$resourceServer = m::mock(ResourceServer::class);
9382
$resourceServer->shouldReceive('validateAuthenticatedRequest')->andThrow(
9483
new OAuthServerException('message', 500, 'error type')
9584
);
9685

97-
$middleware = new CheckClientCredentialsForAnyScope($resourceServer, $tokenRepository);
86+
$middleware = new CheckClientCredentialsForAnyScope($resourceServer, $clientRepository);
9887

9988
$request = Request::create('/');
10089
$request->headers->set('Authorization', 'Bearer token');
@@ -112,23 +101,17 @@ public function test_exception_is_thrown_if_token_does_not_have_required_scope()
112101
$resourceServer = m::mock(ResourceServer::class);
113102
$resourceServer->shouldReceive('validateAuthenticatedRequest')->andReturn($psr = m::mock());
114103
$psr->shouldReceive('getAttribute')->with('oauth_user_id')->andReturn(1);
115-
$psr->shouldReceive('getAttribute')->with('oauth_client_id')->andReturn(1);
104+
$psr->shouldReceive('getAttribute')->with('oauth_client_id')->andReturn(2);
116105
$psr->shouldReceive('getAttribute')->with('oauth_access_token_id')->andReturn('token');
117106
$psr->shouldReceive('getAttribute')->with('oauth_scopes')->andReturn(['foo', 'bar']);
118107

119108
$client = m::mock(Client::class);
120109
$client->shouldReceive('firstParty')->andReturnFalse();
121110

122-
$token = m::mock(Token::class);
123-
$token->shouldReceive('getAttribute')->with('client')->andReturn($client);
124-
$token->shouldReceive('getAttribute')->with('scopes')->andReturn(['foo', 'bar']);
125-
$token->shouldReceive('can')->with('baz')->andReturnFalse();
126-
$token->shouldReceive('can')->with('notbar')->andReturnFalse();
111+
$clientRepository = m::mock(ClientRepository::class);
112+
$clientRepository->shouldReceive('find')->with(2)->andReturn($client);
127113

128-
$tokenRepository = m::mock(TokenRepository::class);
129-
$tokenRepository->shouldReceive('find')->with('token')->andReturn($token);
130-
131-
$middleware = new CheckClientCredentialsForAnyScope($resourceServer, $tokenRepository);
114+
$middleware = new CheckClientCredentialsForAnyScope($resourceServer, $clientRepository);
132115

133116
$request = Request::create('/');
134117
$request->headers->set('Authorization', 'Bearer token');
@@ -146,20 +129,17 @@ public function test_exception_is_thrown_if_token_belongs_to_first_party_client(
146129
$resourceServer = m::mock(ResourceServer::class);
147130
$resourceServer->shouldReceive('validateAuthenticatedRequest')->andReturn($psr = m::mock());
148131
$psr->shouldReceive('getAttribute')->with('oauth_user_id')->andReturn(1);
149-
$psr->shouldReceive('getAttribute')->with('oauth_client_id')->andReturn(1);
132+
$psr->shouldReceive('getAttribute')->with('oauth_client_id')->andReturn(2);
150133
$psr->shouldReceive('getAttribute')->with('oauth_access_token_id')->andReturn('token');
151134
$psr->shouldReceive('getAttribute')->with('oauth_scopes')->andReturn(['*']);
152135

153136
$client = m::mock(Client::class);
154137
$client->shouldReceive('firstParty')->andReturnTrue();
155138

156-
$token = m::mock(Token::class);
157-
$token->shouldReceive('getAttribute')->with('client')->andReturn($client);
158-
159-
$tokenRepository = m::mock(TokenRepository::class);
160-
$tokenRepository->shouldReceive('find')->with('token')->andReturn($token);
139+
$clientRepository = m::mock(ClientRepository::class);
140+
$clientRepository->shouldReceive('find')->with(2)->andReturn($client);
161141

162-
$middleware = new CheckClientCredentialsForAnyScope($resourceServer, $tokenRepository);
142+
$middleware = new CheckClientCredentialsForAnyScope($resourceServer, $clientRepository);
163143

164144
$request = Request::create('/');
165145
$request->headers->set('Authorization', 'Bearer token');

tests/CheckClientCredentialsTest.php

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Laravel\Passport\Http\Middleware\CheckClientCredentials;
88
use Laravel\Passport\Token;
99
use Laravel\Passport\TokenRepository;
10+
use Laravel\Passport\ClientRepository;
1011
use League\OAuth2\Server\Exception\OAuthServerException;
1112
use League\OAuth2\Server\ResourceServer;
1213
use Mockery as m;
@@ -24,21 +25,17 @@ public function test_request_is_passed_along_if_token_is_valid()
2425
$resourceServer = m::mock(ResourceServer::class);
2526
$resourceServer->shouldReceive('validateAuthenticatedRequest')->andReturn($psr = m::mock());
2627
$psr->shouldReceive('getAttribute')->with('oauth_user_id')->andReturn(1);
27-
$psr->shouldReceive('getAttribute')->with('oauth_client_id')->andReturn(1);
28+
$psr->shouldReceive('getAttribute')->with('oauth_client_id')->andReturn(2);
2829
$psr->shouldReceive('getAttribute')->with('oauth_access_token_id')->andReturn('token');
2930
$psr->shouldReceive('getAttribute')->with('oauth_scopes')->andReturn(['*']);
3031

3132
$client = m::mock(Client::class);
3233
$client->shouldReceive('firstParty')->andReturnFalse();
3334

34-
$token = m::mock(Token::class);
35-
$token->shouldReceive('getAttribute')->with('client')->andReturn($client);
36-
$token->shouldReceive('getAttribute')->with('scopes')->andReturn(['*']);
35+
$clientRepository = m::mock(ClientRepository::class);
36+
$clientRepository->shouldReceive('find')->with(2)->andReturn($client);
3737

38-
$tokenRepository = m::mock(TokenRepository::class);
39-
$tokenRepository->shouldReceive('find')->with('token')->andReturn($token);
40-
41-
$middleware = new CheckClientCredentials($resourceServer, $tokenRepository);
38+
$middleware = new CheckClientCredentials($resourceServer, $clientRepository);
4239

4340
$request = Request::create('/');
4441
$request->headers->set('Authorization', 'Bearer token');
@@ -55,22 +52,17 @@ public function test_request_is_passed_along_if_token_and_scope_are_valid()
5552
$resourceServer = m::mock(ResourceServer::class);
5653
$resourceServer->shouldReceive('validateAuthenticatedRequest')->andReturn($psr = m::mock());
5754
$psr->shouldReceive('getAttribute')->with('oauth_user_id')->andReturn(1);
58-
$psr->shouldReceive('getAttribute')->with('oauth_client_id')->andReturn(1);
55+
$psr->shouldReceive('getAttribute')->with('oauth_client_id')->andReturn(2);
5956
$psr->shouldReceive('getAttribute')->with('oauth_access_token_id')->andReturn('token');
6057
$psr->shouldReceive('getAttribute')->with('oauth_scopes')->andReturn(['see-profile']);
6158

6259
$client = m::mock(Client::class);
6360
$client->shouldReceive('firstParty')->andReturnFalse();
6461

65-
$token = m::mock(Token::class);
66-
$token->shouldReceive('getAttribute')->with('client')->andReturn($client);
67-
$token->shouldReceive('getAttribute')->with('scopes')->andReturn(['see-profile']);
68-
$token->shouldReceive('cant')->with('see-profile')->andReturnFalse();
69-
70-
$tokenRepository = m::mock(TokenRepository::class);
71-
$tokenRepository->shouldReceive('find')->with('token')->andReturn($token);
62+
$clientRepository = m::mock(ClientRepository::class);
63+
$clientRepository->shouldReceive('find')->with(2)->andReturn($client);
7264

73-
$middleware = new CheckClientCredentials($resourceServer, $tokenRepository);
65+
$middleware = new CheckClientCredentials($resourceServer, $clientRepository);
7466

7567
$request = Request::create('/');
7668
$request->headers->set('Authorization', 'Bearer token');
@@ -87,13 +79,13 @@ public function test_request_is_passed_along_if_token_and_scope_are_valid()
8779
*/
8880
public function test_exception_is_thrown_when_oauth_throws_exception()
8981
{
90-
$tokenRepository = m::mock(TokenRepository::class);
82+
$clientRepository = m::mock(ClientRepository::class);
9183
$resourceServer = m::mock(ResourceServer::class);
9284
$resourceServer->shouldReceive('validateAuthenticatedRequest')->andThrow(
9385
new OAuthServerException('message', 500, 'error type')
9486
);
9587

96-
$middleware = new CheckClientCredentials($resourceServer, $tokenRepository);
88+
$middleware = new CheckClientCredentials($resourceServer, $clientRepository);
9789

9890
$request = Request::create('/');
9991
$request->headers->set('Authorization', 'Bearer token');
@@ -111,23 +103,17 @@ public function test_exception_is_thrown_if_token_does_not_have_required_scopes(
111103
$resourceServer = m::mock(ResourceServer::class);
112104
$resourceServer->shouldReceive('validateAuthenticatedRequest')->andReturn($psr = m::mock());
113105
$psr->shouldReceive('getAttribute')->with('oauth_user_id')->andReturn(1);
114-
$psr->shouldReceive('getAttribute')->with('oauth_client_id')->andReturn(1);
106+
$psr->shouldReceive('getAttribute')->with('oauth_client_id')->andReturn(2);
115107
$psr->shouldReceive('getAttribute')->with('oauth_access_token_id')->andReturn('token');
116108
$psr->shouldReceive('getAttribute')->with('oauth_scopes')->andReturn(['foo', 'notbar']);
117109

118110
$client = m::mock(Client::class);
119111
$client->shouldReceive('firstParty')->andReturnFalse();
120112

121-
$token = m::mock(Token::class);
122-
$token->shouldReceive('getAttribute')->with('client')->andReturn($client);
123-
$token->shouldReceive('getAttribute')->with('scopes')->andReturn(['foo', 'notbar']);
124-
$token->shouldReceive('cant')->with('foo')->andReturnFalse();
125-
$token->shouldReceive('cant')->with('bar')->andReturnTrue();
113+
$clientRepository = m::mock(ClientRepository::class);
114+
$clientRepository->shouldReceive('find')->with(2)->andReturn($client);
126115

127-
$tokenRepository = m::mock(TokenRepository::class);
128-
$tokenRepository->shouldReceive('find')->with('token')->andReturn($token);
129-
130-
$middleware = new CheckClientCredentials($resourceServer, $tokenRepository);
116+
$middleware = new CheckClientCredentials($resourceServer, $clientRepository);
131117

132118
$request = Request::create('/');
133119
$request->headers->set('Authorization', 'Bearer token');
@@ -145,20 +131,17 @@ public function test_exception_is_thrown_if_token_belongs_to_first_party_client(
145131
$resourceServer = m::mock(ResourceServer::class);
146132
$resourceServer->shouldReceive('validateAuthenticatedRequest')->andReturn($psr = m::mock());
147133
$psr->shouldReceive('getAttribute')->with('oauth_user_id')->andReturn(1);
148-
$psr->shouldReceive('getAttribute')->with('oauth_client_id')->andReturn(1);
134+
$psr->shouldReceive('getAttribute')->with('oauth_client_id')->andReturn(2);
149135
$psr->shouldReceive('getAttribute')->with('oauth_access_token_id')->andReturn('token');
150136
$psr->shouldReceive('getAttribute')->with('oauth_scopes')->andReturn(['*']);
151137

152138
$client = m::mock(Client::class);
153139
$client->shouldReceive('firstParty')->andReturnTrue();
154140

155-
$token = m::mock(Token::class);
156-
$token->shouldReceive('getAttribute')->with('client')->andReturn($client);
157-
158-
$tokenRepository = m::mock(TokenRepository::class);
159-
$tokenRepository->shouldReceive('find')->with('token')->andReturn($token);
141+
$clientRepository = m::mock(ClientRepository::class);
142+
$clientRepository->shouldReceive('find')->with(2)->andReturn($client);
160143

161-
$middleware = new CheckClientCredentials($resourceServer, $tokenRepository);
144+
$middleware = new CheckClientCredentials($resourceServer, $clientRepository);
162145

163146
$request = Request::create('/');
164147
$request->headers->set('Authorization', 'Bearer token');

0 commit comments

Comments
 (0)