Skip to content

Commit

Permalink
chore: add tests for apikey
Browse files Browse the repository at this point in the history
  • Loading branch information
mdshack committed Jan 16, 2024
1 parent 2be1260 commit 169047e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
3 changes: 2 additions & 1 deletion app/Http/Controllers/ApiKeyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;

class ApiKeyController extends Controller
{
Expand All @@ -23,6 +24,6 @@ public function destroy(Request $request, string $id)
{
$request->user()->tokens()->whereId($id)->delete();

return response(status: 204);
return response(status: Response::HTTP_NO_CONTENT);
}
}
4 changes: 2 additions & 2 deletions app/Http/Controllers/UploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public function __invoke(UploadRequest $request)
if ($request->expectsJson()) {
return response()->json([
'data' => [
'link' => route('shots.show', $id)
]
'link' => route('shots.show', $id),
],
]);

Check warning on line 36 in app/Http/Controllers/UploadController.php

View check run for this annotation

Codecov / codecov/patch

app/Http/Controllers/UploadController.php#L32-L36

Added lines #L32 - L36 were not covered by tests
}

Expand Down
45 changes: 45 additions & 0 deletions tests/Unit/Http/Controllers/ApiKeyControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Tests\Unit\Http\Controllers;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ApiKeyControllerTest extends TestCase
{
use RefreshDatabase;

protected User $user;

public function setUp(): void
{
parent::setUp();

$this->user = User::factory()->create();
}

public function test_it_creates_api_keys()
{
$response = $this->actingAs($this->user)
->post(route('api-keys.store'), ['name' => $name = 'test'])
->assertOk();

$token = $this->user->tokens->first();

$this->assertEquals($name, $token->name, 'it sets the desired name');
$this->assertEquals($token->token, hash('sha256', $response->getOriginalContent()['token']), 'it returns correct token value');
}

public function test_it_deletes_api_keys()
{
$this->user->createToken('test');
$token = $this->user->tokens()->first();

$this->actingAs($this->user)
->delete(route('api-keys.destroy', $token->id))
->assertNoContent();

$this->assertNull($token->fresh(), 'it deletes token');
}
}

0 comments on commit 169047e

Please sign in to comment.