From e3268d32df495ed23e60c4c0587bd53fbb6d7485 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 12 Sep 2024 16:00:02 -0700 Subject: [PATCH] Add tests for delete consumable endpoint --- .../Consumables/Api/DeleteConsumablesTest.php | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 tests/Feature/Consumables/Api/DeleteConsumablesTest.php diff --git a/tests/Feature/Consumables/Api/DeleteConsumablesTest.php b/tests/Feature/Consumables/Api/DeleteConsumablesTest.php new file mode 100644 index 0000000000..7a800ca500 --- /dev/null +++ b/tests/Feature/Consumables/Api/DeleteConsumablesTest.php @@ -0,0 +1,64 @@ +create(); + + $this->actingAsForApi(User::factory()->create()) + ->deleteJson(route('api.consumables.destroy', $consumable)) + ->assertForbidden(); + } + + public function testCanDeleteConsumables() + { + $consumable = Consumable::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteConsumables()->create()) + ->deleteJson(route('api.consumables.destroy', $consumable)) + ->assertStatusMessageIs('success'); + + $this->assertTrue($consumable->fresh()->trashed()); + } + + public function testAdheresToMultipleFullCompanySupportScoping() + { + [$companyA, $companyB] = Company::factory()->count(2)->create(); + + $consumableA = Consumable::factory()->for($companyA)->create(); + $consumableB = Consumable::factory()->for($companyB)->create(); + $consumableC = Consumable::factory()->for($companyB)->create(); + + $superUser = $companyA->users()->save(User::factory()->superuser()->make()); + $userInCompanyA = $companyA->users()->save(User::factory()->deleteConsumables()->make()); + $userInCompanyB = $companyB->users()->save(User::factory()->deleteConsumables()->make()); + + $this->settings->enableMultipleFullCompanySupport(); + + $this->actingAsForApi($userInCompanyA) + ->deleteJson(route('api.consumables.destroy', $consumableB)) + ->assertStatusMessageIs('error'); + + $this->actingAsForApi($userInCompanyB) + ->deleteJson(route('api.consumables.destroy', $consumableA)) + ->assertStatusMessageIs('error'); + + $this->actingAsForApi($superUser) + ->deleteJson(route('api.consumables.destroy', $consumableC)) + ->assertStatusMessageIs('success'); + + $this->assertNull($consumableA->fresh()->deleted_at, 'Consumable unexpectedly deleted'); + $this->assertNull($consumableB->fresh()->deleted_at, 'Consumable unexpectedly deleted'); + $this->assertNotNull($consumableC->fresh()->deleted_at, 'Consumable was not deleted'); + } +}