From c7ae9d9dfa76b0efadf47695492761a8120439bf Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 12 Sep 2024 12:58:47 -0700 Subject: [PATCH] Add tests for delete accessory endpoint --- .../TestsMultipleFullCompanySupport.php | 8 +++ .../Accessories/Api/DeleteAccessoryTest.php | 57 ++++++++++++++++++- 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 tests/Concerns/TestsMultipleFullCompanySupport.php diff --git a/tests/Concerns/TestsMultipleFullCompanySupport.php b/tests/Concerns/TestsMultipleFullCompanySupport.php new file mode 100644 index 0000000000..7c62b90782 --- /dev/null +++ b/tests/Concerns/TestsMultipleFullCompanySupport.php @@ -0,0 +1,8 @@ +deleteJson(route('api.accessories.destroy', $accessory)) ->assertForbidden(); } + + public function testCanDeleteAccessory() + { + $accessory = Accessory::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteAccessories()->create()) + ->deleteJson(route('api.accessories.destroy', $accessory)) + ->assertStatusMessageIs('success'); + + $this->assertTrue($accessory->fresh()->trashed()); + } + + public function testCannotDeleteAccessoryThatHasCheckouts() + { + $accessory = Accessory::factory()->checkedOutToUser()->create(); + + $this->actingAsForApi(User::factory()->deleteAccessories()->create()) + ->deleteJson(route('api.accessories.destroy', $accessory)) + ->assertStatusMessageIs('error'); + + $this->assertFalse($accessory->fresh()->trashed()); + } + + public function testAdheresToMultipleFullCompanySupportScoping() + { + [$companyA, $companyB] = Company::factory()->count(2)->create(); + + $accessoryA = Accessory::factory()->for($companyA)->create(); + $accessoryB = Accessory::factory()->for($companyB)->create(); + $accessoryC = Accessory::factory()->for($companyB)->create(); + + $superUser = $companyA->users()->save(User::factory()->superuser()->make()); + $userInCompanyA = $companyA->users()->save(User::factory()->deleteAccessories()->make()); + $userInCompanyB = $companyB->users()->save(User::factory()->deleteAccessories()->make()); + + $this->settings->enableMultipleFullCompanySupport(); + + $this->actingAsForApi($userInCompanyA) + ->deleteJson(route('api.accessories.destroy', $accessoryB)) + ->assertStatusMessageIs('error'); + + $this->actingAsForApi($userInCompanyB) + ->deleteJson(route('api.accessories.destroy', $accessoryA)) + ->assertStatusMessageIs('error'); + + $this->actingAsForApi($superUser) + ->deleteJson(route('api.accessories.destroy', $accessoryC)) + ->assertStatusMessageIs('success'); + + $this->assertNull($accessoryA->fresh()->deleted_at, 'Accessory unexpectedly deleted'); + $this->assertNull($accessoryB->fresh()->deleted_at, 'Accessory unexpectedly deleted'); + $this->assertNotNull($accessoryC->fresh()->deleted_at, 'Accessory was not deleted'); + } }