Skip to content

Commit 7f40f55

Browse files
committed
Add tests for delete supplier endpoint
1 parent b06e8d4 commit 7f40f55

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

database/factories/UserFactory.php

+5
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,11 @@ public function deleteStatusLabels()
361361
return $this->appendPermission(['statuslabels.delete' => '1']);
362362
}
363363

364+
public function deleteSuppliers()
365+
{
366+
return $this->appendPermission(['suppliers.delete' => '1']);
367+
}
368+
364369
private function appendPermission(array $permission)
365370
{
366371
return $this->state(function ($currentState) use ($permission) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Tests\Feature\Suppliers\Api;
4+
5+
use App\Models\Asset;
6+
use App\Models\AssetMaintenance;
7+
use App\Models\Supplier;
8+
use App\Models\User;
9+
use Tests\Concerns\TestsPermissionsRequirement;
10+
use Tests\TestCase;
11+
12+
class DeleteSupplierTest extends TestCase implements TestsPermissionsRequirement
13+
{
14+
public function testRequiresPermission()
15+
{
16+
$supplier = Supplier::factory()->create();
17+
18+
$this->actingAsForApi(User::factory()->create())
19+
->deleteJson(route('api.suppliers.destroy', $supplier))
20+
->assertForbidden();
21+
}
22+
23+
public function testCannotDeleteSupplierWithDataStillAssociated()
24+
{
25+
$supplierWithAsset = Supplier::factory()->hasAssets()->create();
26+
$supplierWithAssetMaintenance = Supplier::factory()->has(AssetMaintenance::factory(), 'asset_maintenances')->create();
27+
$supplierWithLicense = Supplier::factory()->hasLicenses()->create();
28+
29+
$actor = $this->actingAsForApi(User::factory()->deleteSuppliers()->create());
30+
31+
$actor->deleteJson(route('api.suppliers.destroy', $supplierWithAsset))->assertStatusMessageIs('error');
32+
$actor->deleteJson(route('api.suppliers.destroy', $supplierWithAssetMaintenance))->assertStatusMessageIs('error');
33+
$actor->deleteJson(route('api.suppliers.destroy', $supplierWithLicense))->assertStatusMessageIs('error');
34+
35+
$this->assertNotSoftDeleted($supplierWithAsset);
36+
$this->assertNotSoftDeleted($supplierWithAssetMaintenance);
37+
$this->assertNotSoftDeleted($supplierWithLicense);
38+
}
39+
40+
public function testCanDeleteSupplier()
41+
{
42+
$supplier = Supplier::factory()->create();
43+
44+
$this->actingAsForApi(User::factory()->deleteSuppliers()->create())
45+
->deleteJson(route('api.suppliers.destroy', $supplier))
46+
->assertOk()
47+
->assertStatusMessageIs('success');
48+
49+
$this->assertSoftDeleted($supplier);
50+
}
51+
}

0 commit comments

Comments
 (0)