toby/tests/Feature/KeyTest.php
Adrian Hopek d60dc75f99
#118 - keys (#128)
* #118 - wip

* #118 - keys

* #118 - fix

* #118 - fix menu

* #118 - fix to policies and added translations

* #118 - wip

* #118 - tests

* #118 - fix

* #118 - fix

* #118 - fix

* Update resources/lang/pl.json

Co-authored-by: Ewelina Lasowy <56546832+EwelinaLasowy@users.noreply.github.com>

* #118 - cr fix

Co-authored-by: EwelinaLasowy <ewelina.lasowy@blumilk.pl>
Co-authored-by: Ewelina Lasowy <56546832+EwelinaLasowy@users.noreply.github.com>
2022-04-21 08:16:31 +02:00

119 lines
2.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Inertia\Testing\AssertableInertia as Assert;
use Tests\FeatureTestCase;
use Toby\Eloquent\Models\Key;
use Toby\Eloquent\Models\User;
class KeyTest extends FeatureTestCase
{
use DatabaseMigrations;
public function testUserCanSeeKeyList(): void
{
Key::factory()->count(10)->create();
$user = User::factory()->create();
$this->assertDatabaseCount("keys", 10);
$this->actingAs($user)
->get("/keys")
->assertOk()
->assertInertia(
fn(Assert $page) => $page
->component("Keys")
->has("keys.data", 10),
);
}
public function testAdminCanCreateKey(): void
{
$admin = User::factory()->admin()->create();
$this->assertDatabaseMissing("keys", [
"user_id" => $admin->id,
]);
$this->actingAs($admin)
->post("/keys")
->assertSessionHasNoErrors()
->assertRedirect();
$this->assertDatabaseHas("keys", [
"user_id" => $admin->id,
]);
}
public function testUserCannotCreateKey(): void
{
$user = User::factory()->create();
$this->actingAs($user)
->post("/keys")
->assertForbidden();
}
public function testAdminCanDeleteKey(): void
{
$admin = User::factory()->admin()->create();
$key = Key::factory()->create();
$this->actingAs($admin)
->delete("/keys/{$key->id}")
->assertRedirect();
}
public function testUserCanTakeKeyFromAnotherUser(): void
{
$user = User::factory()->create();
$userWithKey = User::factory()->create();
$key = Key::factory()->for($userWithKey)->create();
$this->assertDatabaseHas("keys", [
"id" => $key->id,
"user_id" => $userWithKey->id,
]);
$this->actingAs($user)
->post("/keys/{$key->id}/take")
->assertRedirect();
$this->assertDatabaseHas("keys", [
"id" => $key->id,
"user_id" => $user->id,
]);
}
public function testUserCanGiveTheirKeyToAnotherUser(): void
{
$userWithKey = User::factory()->create();
$user = User::factory()->create();
$key = Key::factory()->for($userWithKey)->create();
$this->assertDatabaseHas("keys", [
"id" => $key->id,
"user_id" => $userWithKey->id,
]);
$this->actingAs($userWithKey)
->post("/keys/{$key->id}/give", [
"user" => $user->id,
])
->assertSessionhasNoErrors()
->assertRedirect();
$this->assertDatabaseHas("keys", [
"id" => $key->id,
"user_id" => $user->id,
]);
}
}