Skip to content

A free open source IT asset/license management system

Add tests for delete status label endpoint

Marcus Mooreb06e8d442d81
diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php
index 288949199c..dd92f38f67 100644
--- a/database/factories/UserFactory.php
+++ b/database/factories/UserFactory.php
@@ -356,6 +356,11 @@ class UserFactory extends Factory
return $this->appendPermission(['kits.delete' => '1']);
}
+ public function deleteStatusLabels()
+ {
+ return $this->appendPermission(['statuslabels.delete' => '1']);
+ }
+
private function appendPermission(array $permission)
{
return $this->state(function ($currentState) use ($permission) {
diff --git a/tests/Feature/StatusLabels/Api/DeleteStatusLabelTest.php b/tests/Feature/StatusLabels/Api/DeleteStatusLabelTest.php
new file mode 100644
index 0000000000..b7e92dcf22
--- /dev/null
+++ b/tests/Feature/StatusLabels/Api/DeleteStatusLabelTest.php
@@ -0,0 +1,45 @@
+<?php
+
+namespace Tests\Feature\StatusLabels\Api;
+
+use App\Models\Statuslabel;
+use App\Models\User;
+use Tests\Concerns\TestsPermissionsRequirement;
+use Tests\TestCase;
+
+class DeleteStatusLabelTest extends TestCase implements TestsPermissionsRequirement
+{
+ public function testRequiresPermission()
+ {
+ $statusLabel = Statuslabel::factory()->create();
+
+ $this->actingAsForApi(User::factory()->create())
+ ->deleteJson(route('api.statuslabels.destroy', $statusLabel))
+ ->assertForbidden();
+ }
+
+ public function testCannotDeleteStatusLabelWhileStillAssociatedToAssets()
+ {
+ $statusLabel = Statuslabel::factory()->hasAssets()->create();
+
+ $this->assertGreaterThan(0, $statusLabel->assets->count(), 'Precondition failed: StatusLabel has no assets');
+
+ $this->actingAsForApi(User::factory()->deleteStatusLabels()->create())
+ ->deleteJson(route('api.statuslabels.destroy', $statusLabel))
+ ->assertStatusMessageIs('error');
+
+ $this->assertNotSoftDeleted($statusLabel);
+ }
+
+ public function testCanDeleteStatusLabel()
+ {
+ $statusLabel = Statuslabel::factory()->create();
+
+ $this->actingAsForApi(User::factory()->deleteStatusLabels()->create())
+ ->deleteJson(route('api.statuslabels.destroy', $statusLabel))
+ ->assertOk()
+ ->assertStatusMessageIs('success');
+
+ $this->assertSoftDeleted($statusLabel);
+ }
+}