Skip to content

Commit

Permalink
api user list endpoint convert to get
Browse files Browse the repository at this point in the history
  • Loading branch information
swoorr committed Jul 28, 2023
1 parent 31232ea commit 431a341
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

Route::middleware('auth')->prefix('/app')->group(function () {
Route::middleware('throttle:60,1')->group(function () {
Route::post('/users', [\App\Http\Controllers\ApiController::class, 'users']);
Route::get('/users', [\App\Http\Controllers\ApiController::class, 'users']);
});
});

17 changes: 10 additions & 7 deletions tests/Feature/ApiAuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ApiAuthenticationTest extends TestCase
use RefreshDatabase;
private mixed $apiKey = null;
private mixed $secretKey = null;
private array $header = [];

public function setUp(): void
{
Expand All @@ -19,11 +20,13 @@ public function setUp(): void
$user = User::factory()->create();
$this->apiKey = $user->api_key;
$this->secretKey = $user->secret_key;

$this->header = ['api_key' => $this->apiKey, 'secret_key' => $this->secretKey];
}

public function test_auth(): void
{
$response = $this->postJson('/api/authenticate', ['api_key' => $this->apiKey, 'secret_key' => $this->secretKey]);
$response = $this->postJson('/api/authenticate', $this->header);

$response
->assertSuccessful();
Expand All @@ -33,7 +36,7 @@ public function test_auth(): void

public function test_users(): void
{
$response = $this->postJson('/api/app/users', data: [], headers: ['api_key' => $this->apiKey, 'secret_key' => $this->secretKey]);
$response = $this->get('/api/app/users', $this->header);

$response
->assertSuccessful();
Expand All @@ -43,7 +46,7 @@ public function test_users(): void
public function test_users_rate_limit(): void
{
for ($i = 0; $i < 70; $i++) {
$response = $this->postJson('/api/app/users', data: [], headers: ['api_key' => $this->apiKey, 'secret_key' => $this->secretKey]);
$response = $this->get('/api/app/users', $this->header);
}

$response->assertStatus(429);
Expand All @@ -53,7 +56,7 @@ public function test_api_response_data(): void
{
User::factory()->count(9)->create();

$response = $this->postJson('/api/app/users', data: [], headers: ['api_key' => $this->apiKey, 'secret_key' => $this->secretKey]);
$response = $this->get('/api/app/users', $this->header);

$response
->assertSuccessful()
Expand Down Expand Up @@ -81,14 +84,14 @@ public function test_error_handling(): void
'expect' => ['message' => 'SECRETKEY required!']
],
];
$response = $this->postJson('/api/app/users', data: [], headers: $dataProvider['apiKeyNull']['data']);

$response = $this->get('/api/app/users', $dataProvider['apiKeyNull']['data']);

$response
->assertStatus(401)
->assertJson($dataProvider['apiKeyNull']['expect']);

$response = $this->postJson('/api/app/users', data: [], headers: $dataProvider['secretKeyNull']['data']);
$response = $this->get('/api/app/users', $dataProvider['secretKeyNull']['data']);

$response
->assertStatus(401)
Expand Down

0 comments on commit 431a341

Please sign in to comment.