Skip to content

Commit 7201a0d

Browse files
author
Luca Degasperi
committed
Improving the filters test coverage
1 parent 1ddc46f commit 7201a0d

File tree

5 files changed

+119
-32
lines changed

5 files changed

+119
-32
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php namespace LucaDegasperi\OAuth2Server\Filters;
2+
3+
use AuthorizationServer;
4+
use Response;
5+
use Session;
6+
7+
class CheckAuthorizationParamsFilter {
8+
9+
public function filter($route, $request, $scope = null)
10+
{
11+
try {
12+
13+
$params = AuthorizationServer::getGrantType('authorization_code')->checkAuthoriseParams();
14+
15+
Session::put('client_id', $params['client_id']);
16+
Session::put('client_details', $params['client_details']);
17+
Session::put('redirect_uri', $params['redirect_uri']);
18+
Session::put('response_type', $params['response_type']);
19+
Session::put('scopes', $params['scopes']);
20+
Session::put('state', $params['state']);
21+
22+
23+
} catch (\League\OAuth2\Server\Exception\ClientException $e) {
24+
25+
return Response::json(array(
26+
'status' => 400,
27+
'error' => 'bad_request',
28+
'error_message' => $e->getMessage(),
29+
), 400);
30+
31+
} catch (\Exception $e) {
32+
33+
return Response::json(array(
34+
'status' => 500,
35+
'error' => 'internal_server_error',
36+
'error_message' => 'Internal Server Error',
37+
), 500);
38+
}
39+
}
40+
}

src/filters.php

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,7 @@
11
<?php
22

3-
Route::filter('check-authorization-params', function($route, $request, $scope = null)
4-
{
5-
try {
6-
7-
$params = AuthorizationServer::getGrantType('authorization_code')->checkAuthoriseParams();
8-
9-
Session::put('client_id', $params['client_id']);
10-
Session::put('client_details', $params['client_details']);
11-
Session::put('redirect_uri', $params['redirect_uri']);
12-
Session::put('response_type', $params['response_type']);
13-
Session::put('scopes', $params['scopes']);
14-
Session::put('state', $params['state']);
15-
16-
17-
} catch (League\OAuth2\Server\Exception\ClientException $e) {
18-
19-
return Response::json(array(
20-
'status' => 400,
21-
'error' => 'bad_request',
22-
'error_message' => $e->getMessage(),
23-
), 400);
24-
25-
} catch (Exception $e) {
26-
27-
return Response::json(array(
28-
'status' => 500,
29-
'error' => 'internal_server_error',
30-
'error_message' => 'Internal Server Error',
31-
), 500);
32-
}
33-
34-
});
3+
// filter to check if the auth code grant type params are provided
4+
Route::filter('check-authorization-params', 'LucaDegasperi\OAuth2Server\Filters\CheckAuthorizationParamsFilter');
355

366
// make sure an endpoint is accessible only by authrized members eventually with specific scopes
377
Route::filter('oauth', 'LucaDegasperi\OAuth2Server\Filters\OAuthFilter');

tests/AuthorizationServerFacadeTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ public function test_make_redirect()
1313
$this->assertEquals('example?', $redirect);
1414
}
1515

16+
public function tearDown() {
17+
m::close();
18+
}
19+
1620
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
use \Mockery as m;
4+
5+
class CheckAuthorizationParamsFilterTest extends TestCase {
6+
7+
public function getFilter()
8+
{
9+
return new LucaDegasperi\OAuth2Server\Filters\CheckAuthorizationParamsFilter;
10+
}
11+
12+
public function getStub()
13+
{
14+
return array(
15+
'client_id' => 1,
16+
'client_details' => 'foo',
17+
'redirect_uri' => 'http://www.example.com/',
18+
'response_type' => 'code',
19+
'scopes' => 'scope',
20+
'state' => '123456789',
21+
);
22+
}
23+
24+
public function test_with_valid_params()
25+
{
26+
$stub = $this->getStub();
27+
28+
AuthorizationServer::shouldReceive('getGrantType->checkAuthoriseParams')
29+
->once()
30+
->andReturn($stub);
31+
32+
Session::shouldReceive('put')->times(6);
33+
34+
$response = $this->getFilter()->filter('','', null);
35+
36+
$this->assertNull($response);
37+
}
38+
39+
public function test_with_invalid_valid_params()
40+
{
41+
42+
AuthorizationServer::shouldReceive('getGrantType->checkAuthoriseParams')
43+
->once()
44+
->andThrow(new \League\OAuth2\Server\Exception\ClientException('Invalid Request'));
45+
46+
$response = $this->getFilter()->filter('','', null);
47+
48+
$this->assertTrue($response instanceof Illuminate\Http\JsonResponse);
49+
$this->assertTrue($response->isClientError());
50+
}
51+
52+
public function test_with_server_error()
53+
{
54+
55+
AuthorizationServer::shouldReceive('getGrantType->checkAuthoriseParams')
56+
->once()
57+
->andThrow(new Exception('Internal Server Error'));
58+
59+
$response = $this->getFilter()->filter('','', null);
60+
61+
$this->assertTrue($response instanceof Illuminate\Http\JsonResponse);
62+
$this->assertTrue($response->isServerError());
63+
}
64+
65+
public function tearDown() {
66+
m::close();
67+
}
68+
69+
}

tests/OAuthOwnerFilterTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,8 @@ public function test_with_unexisting_owner_type()
2626
$this->assertTrue($response->isForbidden());
2727
}
2828

29+
public function tearDown() {
30+
m::close();
31+
}
32+
2933
}

0 commit comments

Comments
 (0)