From b06e8d442d81b40e82ba70aaacf277eb5b5e075f Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 16 Sep 2024 13:37:08 -0700 Subject: [PATCH] Add tests for delete status label endpoint --- database/factories/UserFactory.php | 5 +++ .../Api/DeleteStatusLabelTest.php | 45 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 tests/Feature/StatusLabels/Api/DeleteStatusLabelTest.php 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 @@ +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); + } +}