|
2 | 2 |
|
3 | 3 | namespace Givebutter\Tests\Feature; |
4 | 4 |
|
| 5 | +use Givebutter\LaravelKeyable\Exceptions\ForbidenRequestParamException; |
5 | 6 | use Givebutter\Tests\TestCase; |
6 | 7 | use Givebutter\Tests\Support\Account; |
7 | 8 | use Illuminate\Support\Facades\Route; |
@@ -81,4 +82,72 @@ public function request_without_api_key_responds_unauthorized() |
81 | 82 |
|
82 | 83 | $this->get("/api/posts")->assertUnauthorized(); |
83 | 84 | } |
| 85 | + |
| 86 | + /** |
| 87 | + * @test |
| 88 | + * @dataProvider forbiddenRequestParams |
| 89 | + */ |
| 90 | + public function throw_exception_if_unauthorized_get_request_has_forbidden_request_query_params(string $queryParam): void |
| 91 | + { |
| 92 | + Route::get('/api/posts', function () { |
| 93 | + return response('All good', 200); |
| 94 | + })->middleware(['api', 'auth.apikey']); |
| 95 | + |
| 96 | + $this->get("/api/posts?{$queryParam}=value") |
| 97 | + ->assertBadRequest() |
| 98 | + ->assertContent("Request param '{$queryParam}' is not allowed."); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @test |
| 103 | + * @dataProvider forbiddenRequestParams |
| 104 | + */ |
| 105 | + public function throw_exception_if_unauthorized_post_request_has_forbidden_request_body_params(string $bodyParam): void |
| 106 | + { |
| 107 | + Route::post('/api/posts', function () { |
| 108 | + return response('All good', 200); |
| 109 | + })->middleware(['api', 'auth.apikey']); |
| 110 | + |
| 111 | + $this->post('/api/posts', [$bodyParam => 'value']) |
| 112 | + ->assertBadRequest() |
| 113 | + ->assertContent("Request param '{$bodyParam}' is not allowed."); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @test |
| 118 | + * @dataProvider forbiddenRequestParams |
| 119 | + */ |
| 120 | + public function throw_exception_if_unauthorized_json_get_request_has_forbidden_request_query_params(string $queryParam): void |
| 121 | + { |
| 122 | + Route::get('/api/posts', function () { |
| 123 | + return response('All good', 200); |
| 124 | + })->middleware(['api', 'auth.apikey']); |
| 125 | + |
| 126 | + $this->getJson("/api/posts?{$queryParam}=value") |
| 127 | + ->assertBadRequest() |
| 128 | + ->assertJson(['message' => "Request param '{$queryParam}' is not allowed."]); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @test |
| 133 | + * @dataProvider forbiddenRequestParams |
| 134 | + */ |
| 135 | + public function throw_exception_if_unauthorized_json_post_request_has_forbidden_request_body_params(string $bodyParam): void |
| 136 | + { |
| 137 | + Route::post('/api/posts', function () { |
| 138 | + return response('All good', 200); |
| 139 | + })->middleware(['api', 'auth.apikey']); |
| 140 | + |
| 141 | + $this->postJson('/api/posts', [$bodyParam => 'value']) |
| 142 | + ->assertBadRequest() |
| 143 | + ->assertJson(['message' => "Request param '{$bodyParam}' is not allowed."]); |
| 144 | + } |
| 145 | + |
| 146 | + public function forbiddenRequestParams(): array |
| 147 | + { |
| 148 | + return [ |
| 149 | + ['keyable'], |
| 150 | + ['apiKey'], |
| 151 | + ]; |
| 152 | + } |
84 | 153 | } |
0 commit comments