Add tests for delete depreciation endpoint

This commit is contained in:
Marcus Moore 2024-09-16 10:35:44 -07:00
parent 2f76c1bc5b
commit 79a4bb7316
No known key found for this signature in database
2 changed files with 47 additions and 0 deletions

View file

@ -336,6 +336,11 @@ class UserFactory extends Factory
return $this->appendPermission(['customfields.delete' => '1']); return $this->appendPermission(['customfields.delete' => '1']);
} }
public function deleteDepreciations()
{
return $this->appendPermission(['depreciations.delete' => '1']);
}
private function appendPermission(array $permission) private function appendPermission(array $permission)
{ {
return $this->state(function ($currentState) use ($permission) { return $this->state(function ($currentState) use ($permission) {

View file

@ -0,0 +1,42 @@
<?php
namespace Tests\Feature\Depreciations\Api;
use App\Models\Depreciation;
use App\Models\User;
use Tests\Concerns\TestsPermissionsRequirement;
use Tests\TestCase;
class DeleteDepreciationTest extends TestCase implements TestsPermissionsRequirement
{
public function testRequiresPermission()
{
$depreciation = Depreciation::factory()->create();
$this->actingAsForApi(User::factory()->create())
->deleteJson(route('api.depreciations.destroy', $depreciation))
->assertForbidden();
}
public function testCanDeleteDepreciation()
{
$depreciation = Depreciation::factory()->create();
$this->actingAsForApi(User::factory()->deleteDepreciations()->create())
->deleteJson(route('api.depreciations.destroy', $depreciation))
->assertStatusMessageIs('success');
$this->assertDatabaseMissing('depreciations', ['id' => $depreciation->id]);
}
public function testCannotDeleteDepreciationThatHasAssociatedModels()
{
$depreciation = Depreciation::factory()->hasModels()->create();
$this->actingAsForApi(User::factory()->deleteDepreciations()->create())
->deleteJson(route('api.depreciations.destroy', $depreciation))
->assertStatusMessageIs('error');
$this->assertNotNull($depreciation->fresh(), 'Depreciation unexpectedly deleted');
}
}