diff --git a/database/factories/AccessoryFactory.php b/database/factories/AccessoryFactory.php index 356b367ec4..38d159f423 100644 --- a/database/factories/AccessoryFactory.php +++ b/database/factories/AccessoryFactory.php @@ -156,4 +156,19 @@ class AccessoryFactory extends Factory ]); }); } + + public function checkedOutToUsers(array $users) + { + return $this->afterCreating(function (Accessory $accessory) use ($users) { + foreach ($users as $user) { + $accessory->checkouts()->create([ + 'accessory_id' => $accessory->id, + 'created_at' => Carbon::now(), + 'user_id' => 1, + 'assigned_to' => $user->id, + 'assigned_type' => User::class, + ]); + } + }); + } } diff --git a/tests/Feature/Accessories/Api/IndexAccessoryCheckoutsTest.php b/tests/Feature/Accessories/Api/IndexAccessoryCheckoutsTest.php new file mode 100644 index 0000000000..cad1a03ffd --- /dev/null +++ b/tests/Feature/Accessories/Api/IndexAccessoryCheckoutsTest.php @@ -0,0 +1,64 @@ +create(); + + $this->actingAsForApi(User::factory()->create()) + ->getJson(route('api.accessories.checkedout', $accessory)) + ->assertForbidden(); + } + + public function testAdheresToFullMultipleCompaniesSupportScoping() + { + [$companyA, $companyB] = Company::factory()->count(2)->create(); + + $accessoryA = Accessory::factory()->for($companyA)->create(); + $accessoryB = Accessory::factory()->for($companyB)->create(); + + $superuser = User::factory()->superuser()->create(); + $userInCompanyA = $companyA->users()->save(User::factory()->viewAccessories()->make()); + $userInCompanyB = $companyB->users()->save(User::factory()->viewAccessories()->make()); + + $this->settings->enableMultipleFullCompanySupport(); + + $this->actingAsForApi($userInCompanyA) + ->getJson(route('api.accessories.checkedout', $accessoryB)) + ->assertStatusMessageIs('error'); + + $this->actingAsForApi($userInCompanyB) + ->getJson(route('api.accessories.checkedout', $accessoryA)) + ->assertStatusMessageIs('error'); + + $this->actingAsForApi($superuser) + ->getJson(route('api.accessories.checkedout', $accessoryA)) + ->assertOk(); + } + + public function testCanGetAccessoryCheckouts() + { + [$userA, $userB] = User::factory()->count(2)->create(); + + $accessory = Accessory::factory()->checkedOutToUsers([$userA, $userB])->create(); + + $this->assertEquals(2, $accessory->checkouts()->count()); + + $this->actingAsForApi(User::factory()->viewAccessories()->create()) + ->getJson(route('api.accessories.checkedout', $accessory)) + ->assertOk() + ->assertJsonPath('total', 2) + ->assertJsonPath('rows.0.assigned_to.id', $userA->id) + ->assertJsonPath('rows.1.assigned_to.id', $userB->id); + } +}