mirror of
https://github.com/twinpath/app.git
synced 2026-01-26 05:15:28 +07:00
Initial commit
This commit is contained in:
99
tests/Feature/ApiKeyTest.php
Normal file
99
tests/Feature/ApiKeyTest.php
Normal file
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\ApiKey;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Foundation\Testing\WithFaker;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ApiKeyTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_user_can_view_api_keys_page()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->get(route('api-keys.index'));
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee('API Keys');
|
||||
}
|
||||
|
||||
public function test_user_can_generate_api_key()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->post(route('api-keys.store'), [
|
||||
'name' => 'Test Key',
|
||||
]);
|
||||
|
||||
$response->assertRedirect();
|
||||
$this->assertDatabaseHas('api_keys', [
|
||||
'user_id' => $user->id,
|
||||
'name' => 'Test Key',
|
||||
]);
|
||||
|
||||
$apiKey = ApiKey::where('user_id', $user->id)->first();
|
||||
$this->assertNotNull($apiKey->key);
|
||||
// $this->assertEquals(64, strlen($apiKey->key)); // Length might vary slightly if we use random(60) + 'dvp_' = 64.
|
||||
$this->assertTrue(str_starts_with($apiKey->key, 'dvp_'));
|
||||
}
|
||||
|
||||
public function test_user_can_toggle_api_key_status()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$apiKey = ApiKey::factory()->create([
|
||||
'user_id' => $user->id,
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->patch(route('api-keys.toggle', $apiKey));
|
||||
|
||||
$response->assertRedirect();
|
||||
$this->assertDatabaseHas('api_keys', [
|
||||
'id' => $apiKey->id,
|
||||
'is_active' => false,
|
||||
]);
|
||||
|
||||
// Toggle back
|
||||
$response = $this->actingAs($user)->patch(route('api-keys.toggle', $apiKey));
|
||||
$this->assertDatabaseHas('api_keys', [
|
||||
'id' => $apiKey->id,
|
||||
'is_active' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_user_can_delete_api_key()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$apiKey = ApiKey::factory()->create([
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->delete(route('api-keys.destroy', $apiKey));
|
||||
|
||||
$response->assertRedirect();
|
||||
$this->assertDatabaseMissing('api_keys', [
|
||||
'id' => $apiKey->id,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_user_cannot_delete_others_api_key()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$otherUser = User::factory()->create();
|
||||
$apiKey = ApiKey::factory()->create([
|
||||
'user_id' => $otherUser->id,
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->delete(route('api-keys.destroy', $apiKey));
|
||||
|
||||
$response->assertStatus(403);
|
||||
$this->assertDatabaseHas('api_keys', [
|
||||
'id' => $apiKey->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
7
tests/Feature/ExampleTest.php
Normal file
7
tests/Feature/ExampleTest.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
test('the application returns a successful response', function () {
|
||||
$response = $this->get('/');
|
||||
|
||||
$response->assertStatus(200);
|
||||
});
|
||||
47
tests/Pest.php
Normal file
47
tests/Pest.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Test Case
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| The closure you provide to your test functions is always bound to a specific PHPUnit test
|
||||
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
|
||||
| need to change it using the "pest()" function to bind a different classes or traits.
|
||||
|
|
||||
*/
|
||||
|
||||
pest()->extend(Tests\TestCase::class)
|
||||
// ->use(Illuminate\Foundation\Testing\RefreshDatabase::class)
|
||||
->in('Feature');
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Expectations
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| When you're writing tests, you often need to check that values meet certain conditions. The
|
||||
| "expect()" function gives you access to a set of "expectations" methods that you can use
|
||||
| to assert different things. Of course, you may extend the Expectation API at any time.
|
||||
|
|
||||
*/
|
||||
|
||||
expect()->extend('toBeOne', function () {
|
||||
return $this->toBe(1);
|
||||
});
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Functions
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
|
||||
| project that you don't want to repeat in every file. Here you can also expose helpers as
|
||||
| global functions to help you to reduce the number of lines of code in your test files.
|
||||
|
|
||||
*/
|
||||
|
||||
function something()
|
||||
{
|
||||
// ..
|
||||
}
|
||||
10
tests/TestCase.php
Normal file
10
tests/TestCase.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
|
||||
|
||||
abstract class TestCase extends BaseTestCase
|
||||
{
|
||||
//
|
||||
}
|
||||
5
tests/Unit/ExampleTest.php
Normal file
5
tests/Unit/ExampleTest.php
Normal file
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
test('that true is true', function () {
|
||||
expect(true)->toBeTrue();
|
||||
});
|
||||
Reference in New Issue
Block a user