Skip to content

Commit f090882

Browse files
add tests
1 parent 02691dc commit f090882

File tree

2 files changed

+111
-10
lines changed

2 files changed

+111
-10
lines changed

tests/Grant/AuthCodeGrantTest.php

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,50 @@ public function testValidateAuthorizationRequestInvalidCodeChallengeMethod(): vo
469469
$grant->validateAuthorizationRequest($request);
470470
}
471471

472+
public function testValidateAuthorizationRequestInvalidScopes(): void
473+
{
474+
$client = new ClientEntity();
475+
$client->setRedirectUri(self::REDIRECT_URI);
476+
$client->setConfidential();
477+
478+
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
479+
$clientRepositoryMock->method('getClientEntity')->willReturn($client);
480+
481+
$scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
482+
$scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn(null);
483+
484+
$grant = new AuthCodeGrant(
485+
$this->getMockBuilder(AuthCodeRepositoryInterface::class)->getMock(),
486+
$this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock(),
487+
new DateInterval('PT10M')
488+
);
489+
490+
$grant->setClientRepository($clientRepositoryMock);
491+
$grant->setScopeRepository($scopeRepositoryMock);
492+
$grant->setDefaultScope(self::DEFAULT_SCOPE);
493+
494+
$request = (new ServerRequest())->withQueryParams([
495+
'response_type' => 'code',
496+
'client_id' => 'foo',
497+
'redirect_uri' => self::REDIRECT_URI,
498+
'scope' => 'foo',
499+
'state' => 'foo',
500+
]);
501+
502+
try {
503+
$grant->validateAuthorizationRequest($request);
504+
} catch (OAuthServerException $e) {
505+
$this->assertSame(5, $e->getCode());
506+
$this->assertSame('invalid_scope', $e->getErrorType());
507+
$this->assertSame('https://foo/bar?state=foo', $e->getRedirectUri());
508+
509+
return;
510+
}
511+
512+
$this->expectException(OAuthServerException::class);
513+
$this->expectExceptionCode(5);
514+
}
515+
472516
public function testCompleteAuthorizationRequest(): void
473517
{
474518
$client = new ClientEntity();
@@ -529,6 +573,7 @@ public function testCompleteAuthorizationRequestDenied(): void
529573
$authRequest->setClient($client);
530574
$authRequest->setGrantTypeId('authorization_code');
531575
$authRequest->setUser(new UserEntity());
576+
$authRequest->setState('foo');
532577

533578
$authCodeRepository = $this->getMockBuilder(AuthCodeRepositoryInterface::class)->getMock();
534579
$authCodeRepository->method('getNewAuthCode')->willReturn(new AuthCodeEntity());
@@ -540,10 +585,18 @@ public function testCompleteAuthorizationRequestDenied(): void
540585
);
541586
$grant->setEncryptionKey($this->cryptStub->getKey());
542587

588+
try {
589+
$grant->completeAuthorizationRequest($authRequest);
590+
} catch (OAuthServerException $e) {
591+
$this->assertSame(9, $e->getCode());
592+
$this->assertSame('access_denied', $e->getErrorType());
593+
$this->assertSame('http://foo/bar?state=foo', $e->getRedirectUri());
594+
595+
return;
596+
}
597+
543598
$this->expectException(OAuthServerException::class);
544599
$this->expectExceptionCode(9);
545-
546-
$grant->completeAuthorizationRequest($authRequest);
547600
}
548601

549602
public function testRespondToAccessTokenRequest(): void

tests/Grant/ImplicitGrantTest.php

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function testValidateAuthorizationRequest(): void
9595
$grant->setDefaultScope(self::DEFAULT_SCOPE);
9696

9797
$request = (new ServerRequest())->withQueryParams([
98-
'response_type' => 'code',
98+
'response_type' => 'token',
9999
'client_id' => 'foo',
100100
'redirect_uri' => self::REDIRECT_URI,
101101
]);
@@ -120,7 +120,7 @@ public function testValidateAuthorizationRequestRedirectUriArray(): void
120120
$grant->setDefaultScope(self::DEFAULT_SCOPE);
121121

122122
$request = (new ServerRequest())->withQueryParams([
123-
'response_type' => 'code',
123+
'response_type' => 'token',
124124
'client_id' => 'foo',
125125
'redirect_uri' => self::REDIRECT_URI,
126126
]);
@@ -135,7 +135,7 @@ public function testValidateAuthorizationRequestMissingClientId(): void
135135
$grant = new ImplicitGrant(new DateInterval('PT10M'));
136136
$grant->setClientRepository($clientRepositoryMock);
137137

138-
$request = (new ServerRequest())->withQueryParams(['response_type' => 'code']);
138+
$request = (new ServerRequest())->withQueryParams(['response_type' => 'token']);
139139

140140
$this->expectException(OAuthServerException::class);
141141
$this->expectExceptionCode(3);
@@ -152,7 +152,7 @@ public function testValidateAuthorizationRequestInvalidClientId(): void
152152
$grant->setClientRepository($clientRepositoryMock);
153153

154154
$request = (new ServerRequest())->withQueryParams([
155-
'response_type' => 'code',
155+
'response_type' => 'token',
156156
'client_id' => 'foo',
157157
]);
158158

@@ -173,7 +173,7 @@ public function testValidateAuthorizationRequestBadRedirectUriString(): void
173173
$grant->setClientRepository($clientRepositoryMock);
174174

175175
$request = (new ServerRequest())->withQueryParams([
176-
'response_type' => 'code',
176+
'response_type' => 'token',
177177
'client_id' => 'foo',
178178
'redirect_uri' => 'http://bar',
179179
]);
@@ -195,7 +195,7 @@ public function testValidateAuthorizationRequestBadRedirectUriArray(): void
195195
$grant->setClientRepository($clientRepositoryMock);
196196

197197
$request = (new ServerRequest())->withQueryParams([
198-
'response_type' => 'code',
198+
'response_type' => 'token',
199199
'client_id' => 'foo',
200200
'redirect_uri' => 'http://bar',
201201
]);
@@ -206,6 +206,45 @@ public function testValidateAuthorizationRequestBadRedirectUriArray(): void
206206
$grant->validateAuthorizationRequest($request);
207207
}
208208

209+
public function testValidateAuthorizationRequestInvalidScopes(): void
210+
{
211+
$client = new ClientEntity();
212+
$client->setRedirectUri(self::REDIRECT_URI);
213+
214+
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock();
215+
$clientRepositoryMock->method('getClientEntity')->willReturn($client);
216+
217+
$scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
218+
$scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn(null);
219+
220+
$grant = new ImplicitGrant(new DateInterval('PT10M'));
221+
222+
$grant->setClientRepository($clientRepositoryMock);
223+
$grant->setScopeRepository($scopeRepositoryMock);
224+
$grant->setDefaultScope(self::DEFAULT_SCOPE);
225+
226+
$request = (new ServerRequest())->withQueryParams([
227+
'response_type' => 'token',
228+
'client_id' => 'foo',
229+
'redirect_uri' => self::REDIRECT_URI,
230+
'scope' => 'foo',
231+
'state' => 'foo',
232+
]);
233+
234+
try {
235+
$grant->validateAuthorizationRequest($request);
236+
} catch (OAuthServerException $e) {
237+
$this->assertSame(5, $e->getCode());
238+
$this->assertSame('invalid_scope', $e->getErrorType());
239+
$this->assertSame('https://foo/bar#state=foo', $e->getRedirectUri());
240+
241+
return;
242+
}
243+
244+
$this->expectException(OAuthServerException::class);
245+
$this->expectExceptionCode(5);
246+
}
247+
209248
public function testCompleteAuthorizationRequest(): void
210249
{
211250
$client = new ClientEntity();
@@ -248,6 +287,7 @@ public function testCompleteAuthorizationRequestDenied(): void
248287
$authRequest->setClient($client);
249288
$authRequest->setGrantTypeId('authorization_code');
250289
$authRequest->setUser(new UserEntity());
290+
$authRequest->setState('foo');
251291

252292
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
253293
$accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity());
@@ -261,10 +301,18 @@ public function testCompleteAuthorizationRequestDenied(): void
261301
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
262302
$grant->setScopeRepository($scopeRepositoryMock);
263303

304+
try {
305+
$grant->completeAuthorizationRequest($authRequest);
306+
} catch (OAuthServerException $e) {
307+
$this->assertSame(9, $e->getCode());
308+
$this->assertSame('access_denied', $e->getErrorType());
309+
$this->assertSame('https://foo/bar#state=foo', $e->getRedirectUri());
310+
311+
return;
312+
}
313+
264314
$this->expectException(OAuthServerException::class);
265315
$this->expectExceptionCode(9);
266-
267-
$grant->completeAuthorizationRequest($authRequest);
268316
}
269317

270318
public function testAccessTokenRepositoryUniqueConstraintCheck(): void

0 commit comments

Comments
 (0)