From 3f07f682defd228dc51d01b962353978e6e5eed8 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 16 Sep 2024 16:35:21 -0700 Subject: [PATCH 01/23] Migrate existing tests to TestsPermissionsRequirement --- tests/Feature/Accessories/Api/IndexAccessoryTest.php | 5 +++-- tests/Feature/Accessories/Api/ShowAccessoryTest.php | 5 +++-- tests/Feature/Accessories/Api/StoreAccessoryTest.php | 5 +++-- tests/Feature/Accessories/Api/UpdateAccessoryTest.php | 5 +++-- tests/Feature/Checkouts/Api/AccessoryCheckoutTest.php | 5 +++-- 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/tests/Feature/Accessories/Api/IndexAccessoryTest.php b/tests/Feature/Accessories/Api/IndexAccessoryTest.php index 509e6fd452..aeadb808bd 100644 --- a/tests/Feature/Accessories/Api/IndexAccessoryTest.php +++ b/tests/Feature/Accessories/Api/IndexAccessoryTest.php @@ -3,11 +3,12 @@ namespace Tests\Feature\Accessories\Api; use App\Models\User; +use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class IndexAccessoryTest extends TestCase +class IndexAccessoryTest extends TestCase implements TestsPermissionsRequirement { - public function testPermissionRequiredToViewAccessoriesIndex() + public function testRequiresPermission() { $this->actingAsForApi(User::factory()->create()) ->getJson(route('api.accessories.index')) diff --git a/tests/Feature/Accessories/Api/ShowAccessoryTest.php b/tests/Feature/Accessories/Api/ShowAccessoryTest.php index 2bc3e88bfc..da35eca3e0 100644 --- a/tests/Feature/Accessories/Api/ShowAccessoryTest.php +++ b/tests/Feature/Accessories/Api/ShowAccessoryTest.php @@ -4,11 +4,12 @@ namespace Tests\Feature\Accessories\Api; use App\Models\Accessory; use App\Models\User; +use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class ShowAccessoryTest extends TestCase +class ShowAccessoryTest extends TestCase implements TestsPermissionsRequirement { - public function testPermissionRequiredToShowAccessory() + public function testRequiresPermission() { $accessory = Accessory::factory()->create(); diff --git a/tests/Feature/Accessories/Api/StoreAccessoryTest.php b/tests/Feature/Accessories/Api/StoreAccessoryTest.php index d29ff32f6e..1c2d3d5fd8 100644 --- a/tests/Feature/Accessories/Api/StoreAccessoryTest.php +++ b/tests/Feature/Accessories/Api/StoreAccessoryTest.php @@ -3,11 +3,12 @@ namespace Tests\Feature\Accessories\Api; use App\Models\User; +use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class StoreAccessoryTest extends TestCase +class StoreAccessoryTest extends TestCase implements TestsPermissionsRequirement { - public function testPermissionRequiredToStoreAccessory() + public function testRequiresPermission() { $this->actingAsForApi(User::factory()->create()) ->postJson(route('api.accessories.store')) diff --git a/tests/Feature/Accessories/Api/UpdateAccessoryTest.php b/tests/Feature/Accessories/Api/UpdateAccessoryTest.php index 45923651f2..932623fb36 100644 --- a/tests/Feature/Accessories/Api/UpdateAccessoryTest.php +++ b/tests/Feature/Accessories/Api/UpdateAccessoryTest.php @@ -4,11 +4,12 @@ namespace Tests\Feature\Accessories\Api; use App\Models\Accessory; use App\Models\User; +use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class UpdateAccessoryTest extends TestCase +class UpdateAccessoryTest extends TestCase implements TestsPermissionsRequirement { - public function testPermissionRequiredToUpdateAccessory() + public function testRequiresPermission() { $accessory = Accessory::factory()->create(); diff --git a/tests/Feature/Checkouts/Api/AccessoryCheckoutTest.php b/tests/Feature/Checkouts/Api/AccessoryCheckoutTest.php index 2b37797fb6..819d4430e1 100644 --- a/tests/Feature/Checkouts/Api/AccessoryCheckoutTest.php +++ b/tests/Feature/Checkouts/Api/AccessoryCheckoutTest.php @@ -7,11 +7,12 @@ use App\Models\Actionlog; use App\Models\User; use App\Notifications\CheckoutAccessoryNotification; use Illuminate\Support\Facades\Notification; +use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class AccessoryCheckoutTest extends TestCase +class AccessoryCheckoutTest extends TestCase implements TestsPermissionsRequirement { - public function testCheckingOutAccessoryRequiresCorrectPermission() + public function testRequiresPermission() { $this->actingAsForApi(User::factory()->create()) ->postJson(route('api.accessories.checkout', Accessory::factory()->create())) From eb35608bb59a3fc72c7fc55c5431462aef04b040 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 16 Sep 2024 16:35:33 -0700 Subject: [PATCH 02/23] Remove call handled by form request --- app/Http/Controllers/Api/AccessoriesController.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/Http/Controllers/Api/AccessoriesController.php b/app/Http/Controllers/Api/AccessoriesController.php index b1506e4f40..578c474889 100644 --- a/app/Http/Controllers/Api/AccessoriesController.php +++ b/app/Http/Controllers/Api/AccessoriesController.php @@ -133,7 +133,6 @@ class AccessoriesController extends Controller */ public function store(StoreAccessoryRequest $request) { - $this->authorize('create', Accessory::class); $accessory = new Accessory; $accessory->fill($request->all()); $accessory = $request->handleImages($accessory); From e835637ef0120a96012951a965848ecb8465e69f Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 16 Sep 2024 16:40:10 -0700 Subject: [PATCH 03/23] Add accessory checkin test --- .../Checkins/Api/AccessoryCheckinTest.php | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 tests/Feature/Checkins/Api/AccessoryCheckinTest.php diff --git a/tests/Feature/Checkins/Api/AccessoryCheckinTest.php b/tests/Feature/Checkins/Api/AccessoryCheckinTest.php new file mode 100644 index 0000000000..925d9dd050 --- /dev/null +++ b/tests/Feature/Checkins/Api/AccessoryCheckinTest.php @@ -0,0 +1,20 @@ +checkedOutToUser()->create(); + + $this->actingAsForApi(User::factory()->create()) + ->postJson(route('api.accessories.checkin', $accessory)) + ->assertForbidden(); + } +} From 8eb1c487bcaa1fb6ec71d85985226d49c01e3cea Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 16 Sep 2024 16:52:10 -0700 Subject: [PATCH 04/23] implement FMCS test for accessory index --- .../Accessories/Api/IndexAccessoryTest.php | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/tests/Feature/Accessories/Api/IndexAccessoryTest.php b/tests/Feature/Accessories/Api/IndexAccessoryTest.php index aeadb808bd..4722bdd551 100644 --- a/tests/Feature/Accessories/Api/IndexAccessoryTest.php +++ b/tests/Feature/Accessories/Api/IndexAccessoryTest.php @@ -2,11 +2,14 @@ namespace Tests\Feature\Accessories\Api; +use App\Models\Accessory; +use App\Models\Company; use App\Models\User; +use Tests\Concerns\TestsMultipleFullCompanySupport; use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class IndexAccessoryTest extends TestCase implements TestsPermissionsRequirement +class IndexAccessoryTest extends TestCase implements TestsMultipleFullCompanySupport, TestsPermissionsRequirement { public function testRequiresPermission() { @@ -14,4 +17,40 @@ class IndexAccessoryTest extends TestCase implements TestsPermissionsRequirement ->getJson(route('api.accessories.index')) ->assertForbidden(); } + + public function testAdheresToMultipleFullCompanySupportScoping() + { + [$companyA, $companyB] = Company::factory()->count(2)->create(); + + $accessoryA = Accessory::factory()->for($companyA)->create(['name' => 'Accessory A']); + $accessoryB = Accessory::factory()->for($companyB)->create(['name' => 'Accessory B']); + $accessoryC = Accessory::factory()->for($companyB)->create(['name' => 'Accessory C']); + + $superUser = $companyA->users()->save(User::factory()->superuser()->make()); + $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.index')) + ->assertOk() + ->assertResponseContainsInRows($accessoryA) + ->assertResponseDoesNotContainInRows($accessoryB) + ->assertResponseDoesNotContainInRows($accessoryC); + + $this->actingAsForApi($userInCompanyB) + ->getJson(route('api.accessories.index')) + ->assertOk() + ->assertResponseDoesNotContainInRows($accessoryA) + ->assertResponseContainsInRows($accessoryB) + ->assertResponseContainsInRows($accessoryC); + + $this->actingAsForApi($superUser) + ->getJson(route('api.accessories.index')) + ->assertOk() + ->assertResponseContainsInRows($accessoryA) + ->assertResponseContainsInRows($accessoryB) + ->assertResponseContainsInRows($accessoryC); + } } From 12fd9cd97a30ca1ab715fd923c40d3558706e88e Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 16 Sep 2024 17:00:08 -0700 Subject: [PATCH 05/23] Update interface name --- tests/Feature/Accessories/Api/IndexAccessoryTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Feature/Accessories/Api/IndexAccessoryTest.php b/tests/Feature/Accessories/Api/IndexAccessoryTest.php index 4722bdd551..999c0f97ff 100644 --- a/tests/Feature/Accessories/Api/IndexAccessoryTest.php +++ b/tests/Feature/Accessories/Api/IndexAccessoryTest.php @@ -5,11 +5,11 @@ namespace Tests\Feature\Accessories\Api; use App\Models\Accessory; use App\Models\Company; use App\Models\User; -use Tests\Concerns\TestsMultipleFullCompanySupport; +use Tests\Concerns\TestsFullMultipleCompaniesSupport; use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class IndexAccessoryTest extends TestCase implements TestsMultipleFullCompanySupport, TestsPermissionsRequirement +class IndexAccessoryTest extends TestCase implements TestsFullMultipleCompaniesSupport, TestsPermissionsRequirement { public function testRequiresPermission() { @@ -18,7 +18,7 @@ class IndexAccessoryTest extends TestCase implements TestsMultipleFullCompanySup ->assertForbidden(); } - public function testAdheresToMultipleFullCompanySupportScoping() + public function testAdheresToFullMultipleCompaniesSupportScoping() { [$companyA, $companyB] = Company::factory()->count(2)->create(); From 7b31df7c148566274527317bb5bf006e5764fda8 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 17 Sep 2024 09:11:58 -0700 Subject: [PATCH 06/23] Begin additional test case --- .../Checkins/Api/AccessoryCheckinTest.php | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/tests/Feature/Checkins/Api/AccessoryCheckinTest.php b/tests/Feature/Checkins/Api/AccessoryCheckinTest.php index 925d9dd050..8321fe9953 100644 --- a/tests/Feature/Checkins/Api/AccessoryCheckinTest.php +++ b/tests/Feature/Checkins/Api/AccessoryCheckinTest.php @@ -3,11 +3,13 @@ namespace Tests\Feature\Checkins\Api; use App\Models\Accessory; +use App\Models\Company; use App\Models\User; +use Tests\Concerns\TestsFullMultipleCompaniesSupport; use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class AccessoryCheckinTest extends TestCase implements TestsPermissionsRequirement +class AccessoryCheckinTest extends TestCase implements TestsFullMultipleCompaniesSupport, TestsPermissionsRequirement { public function testRequiresPermission() { @@ -17,4 +19,24 @@ class AccessoryCheckinTest extends TestCase implements TestsPermissionsRequireme ->postJson(route('api.accessories.checkin', $accessory)) ->assertForbidden(); } + + public function testAdheresToFullMultipleCompaniesSupportScoping() + { + $this->markTestIncomplete(); + + $this->withoutExceptionHandling(); + + [$companyA, $companyB] = Company::factory()->count(2)->create(); + + $userInCompanyA = User::factory()->for($companyA)->checkinAccessories()->create(); + $accessoryForCompanyB = Accessory::factory()->for($companyB)->checkedOutToUser()->create(); + + $this->settings->enableMultipleFullCompanySupport(); + + $this->actingAsForApi($userInCompanyA) + ->postJson(route('api.accessories.checkin', $accessoryForCompanyB)) + ->assertStatusMessageIs('error'); + + // @todo: + } } From fa1968624885e51b71f4a77215cec01beaae01e5 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Wed, 18 Sep 2024 11:18:21 -0700 Subject: [PATCH 07/23] Implement test --- .../Checkins/Api/AccessoryCheckinTest.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/tests/Feature/Checkins/Api/AccessoryCheckinTest.php b/tests/Feature/Checkins/Api/AccessoryCheckinTest.php index 8321fe9953..fd0fc075f4 100644 --- a/tests/Feature/Checkins/Api/AccessoryCheckinTest.php +++ b/tests/Feature/Checkins/Api/AccessoryCheckinTest.php @@ -22,21 +22,27 @@ class AccessoryCheckinTest extends TestCase implements TestsFullMultipleCompanie public function testAdheresToFullMultipleCompaniesSupportScoping() { - $this->markTestIncomplete(); - - $this->withoutExceptionHandling(); - [$companyA, $companyB] = Company::factory()->count(2)->create(); + $superUser = $companyA->users()->save(User::factory()->superuser()->make()); $userInCompanyA = User::factory()->for($companyA)->checkinAccessories()->create(); $accessoryForCompanyB = Accessory::factory()->for($companyB)->checkedOutToUser()->create(); + $anotherAccessoryForCompanyB = Accessory::factory()->for($companyB)->checkedOutToUser()->create(); + + $this->assertEquals(1, $accessoryForCompanyB->checkouts->count()); + $this->assertEquals(1, $anotherAccessoryForCompanyB->checkouts->count()); $this->settings->enableMultipleFullCompanySupport(); $this->actingAsForApi($userInCompanyA) ->postJson(route('api.accessories.checkin', $accessoryForCompanyB)) - ->assertStatusMessageIs('error'); + ->assertForbidden(); - // @todo: + $this->actingAsForApi($superUser) + ->postJson(route('api.accessories.checkin', $anotherAccessoryForCompanyB)) + ->assertStatusMessageIs('success'); + + $this->assertEquals(1, $accessoryForCompanyB->fresh()->checkouts->count(), 'Accessory should not be checked in'); + $this->assertEquals(0, $anotherAccessoryForCompanyB->fresh()->checkouts->count(), 'Accessory should be checked in'); } } From 832e50a71ebcda26451f090042cb072bae05c4f3 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Wed, 18 Sep 2024 11:19:41 -0700 Subject: [PATCH 08/23] Implement test --- tests/Feature/Checkins/Api/AccessoryCheckinTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/Feature/Checkins/Api/AccessoryCheckinTest.php b/tests/Feature/Checkins/Api/AccessoryCheckinTest.php index fd0fc075f4..58c5514190 100644 --- a/tests/Feature/Checkins/Api/AccessoryCheckinTest.php +++ b/tests/Feature/Checkins/Api/AccessoryCheckinTest.php @@ -45,4 +45,17 @@ class AccessoryCheckinTest extends TestCase implements TestsFullMultipleCompanie $this->assertEquals(1, $accessoryForCompanyB->fresh()->checkouts->count(), 'Accessory should not be checked in'); $this->assertEquals(0, $anotherAccessoryForCompanyB->fresh()->checkouts->count(), 'Accessory should be checked in'); } + + public function testCanCheckinAccessory() + { + $accessory = Accessory::factory()->checkedOutToUser()->create(); + + $this->assertEquals(1, $accessory->checkouts->count()); + + $this->actingAsForApi(User::factory()->checkinAccessories()->create()) + ->postJson(route('api.accessories.checkin', $accessory)) + ->assertStatusMessageIs('success'); + + $this->assertEquals(0, $accessory->fresh()->checkouts->count(), 'Accessory should be checked in'); + } } From d5f659024c06a8dc3a6573f1b2d7eddf25b698c4 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Wed, 18 Sep 2024 11:27:09 -0700 Subject: [PATCH 09/23] Add test for logging --- .../Checkins/Api/AccessoryCheckinTest.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/Feature/Checkins/Api/AccessoryCheckinTest.php b/tests/Feature/Checkins/Api/AccessoryCheckinTest.php index 58c5514190..d44a05b310 100644 --- a/tests/Feature/Checkins/Api/AccessoryCheckinTest.php +++ b/tests/Feature/Checkins/Api/AccessoryCheckinTest.php @@ -58,4 +58,25 @@ class AccessoryCheckinTest extends TestCase implements TestsFullMultipleCompanie $this->assertEquals(0, $accessory->fresh()->checkouts->count(), 'Accessory should be checked in'); } + + public function testCheckinIsLogged() + { + $user = User::factory()->create(); + $actor = User::factory()->checkinAccessories()->create(); + + $accessory = Accessory::factory()->checkedOutToUser($user)->create(); + + $this->actingAsForApi($actor) + ->postJson(route('api.accessories.checkin', $accessory)) + ->assertStatusMessageIs('success'); + + $this->assertDatabaseHas('action_logs', [ + 'user_id' => $actor->id, + 'action_type' => 'checkin from', + 'target_id' => $user->id, + 'target_type' => User::class, + 'item_id' => $accessory->id, + 'item_type' => Accessory::class, + ]); + } } From 636c776620a012937c7f79cd67dde9cf7c6a92cf Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Wed, 18 Sep 2024 11:27:37 -0700 Subject: [PATCH 10/23] Remove dead code --- app/Http/Controllers/Api/AccessoriesController.php | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/app/Http/Controllers/Api/AccessoriesController.php b/app/Http/Controllers/Api/AccessoriesController.php index 578c474889..acfdcef870 100644 --- a/app/Http/Controllers/Api/AccessoriesController.php +++ b/app/Http/Controllers/Api/AccessoriesController.php @@ -320,7 +320,7 @@ class AccessoriesController extends Controller $accessory = Accessory::find($accessory_checkout->accessory_id); $this->authorize('checkin', $accessory); - $logaction = $accessory->logCheckin(User::find($accessory_checkout->assigned_to), $request->input('note')); + $accessory->logCheckin(User::find($accessory_checkout->assigned_to), $request->input('note')); // Was the accessory updated? if ($accessory_checkout->delete()) { @@ -328,14 +328,6 @@ class AccessoriesController extends Controller $user = User::find($accessory_checkout->assigned_to); } - $data['log_id'] = $logaction->id; - $data['first_name'] = $user->first_name; - $data['last_name'] = $user->last_name; - $data['item_name'] = $accessory->name; - $data['checkin_date'] = $logaction->created_at; - $data['item_tag'] = ''; - $data['note'] = $logaction->note; - return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/accessories/message.checkin.success'))); } From 9b293afaac70370ad7741c306e7b14c47eecf3ce Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Wed, 18 Sep 2024 11:28:29 -0700 Subject: [PATCH 11/23] Remove more dead code --- app/Models/Loggable.php | 34 ---------------------------------- 1 file changed, 34 deletions(-) diff --git a/app/Models/Loggable.php b/app/Models/Loggable.php index ae5d554882..a246503165 100644 --- a/app/Models/Loggable.php +++ b/app/Models/Loggable.php @@ -117,7 +117,6 @@ trait Loggable */ public function logCheckin($target, $note, $action_date = null, $originalValues = []) { - $settings = Setting::getSettings(); $log = new Actionlog; if($target != null){ @@ -171,39 +170,6 @@ trait Loggable $log->logaction('checkin from'); -// $params = [ -// 'target' => $target, -// 'item' => $log->item, -// 'admin' => $log->user, -// 'note' => $note, -// 'target_type' => $log->target_type, -// 'settings' => $settings, -// ]; -// -// -// $checkinClass = null; -// -// if (method_exists($target, 'notify')) { -// try { -// $target->notify(new static::$checkinClass($params)); -// } catch (\Exception $e) { -// Log::debug($e); -// } -// -// } -// -// // Send to the admin, if settings dictate -// $recipient = new \App\Models\Recipients\AdminRecipient(); -// -// if (($settings->admin_cc_email!='') && (static::$checkinClass!='')) { -// try { -// $recipient->notify(new static::$checkinClass($params)); -// } catch (\Exception $e) { -// Log::debug($e); -// } -// -// } - return $log; } From a6bcd3c0c2b03c81b45ece70d453e767d277acc0 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Wed, 18 Sep 2024 11:47:59 -0700 Subject: [PATCH 12/23] Add validation test --- .../Accessories/Api/StoreAccessoryTest.php | 19 +++++++++++++++++ tests/Support/CustomTestMacros.php | 21 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/tests/Feature/Accessories/Api/StoreAccessoryTest.php b/tests/Feature/Accessories/Api/StoreAccessoryTest.php index 1c2d3d5fd8..496b8e9b3a 100644 --- a/tests/Feature/Accessories/Api/StoreAccessoryTest.php +++ b/tests/Feature/Accessories/Api/StoreAccessoryTest.php @@ -14,4 +14,23 @@ class StoreAccessoryTest extends TestCase implements TestsPermissionsRequirement ->postJson(route('api.accessories.store')) ->assertForbidden(); } + + public function testValidation() + { + $this->actingAsForApi(User::factory()->createAccessories()->create()) + ->postJson(route('api.accessories.store'), [ + // + ]) + ->assertStatusMessageIs('error') + ->assertMessagesContains([ + 'category_id', + 'name', + 'qty', + ]); + } + + public function testCanStoreAccessory() + { + $this->markTestIncomplete(); + } } diff --git a/tests/Support/CustomTestMacros.php b/tests/Support/CustomTestMacros.php index 993f449021..9c0caaf959 100644 --- a/tests/Support/CustomTestMacros.php +++ b/tests/Support/CustomTestMacros.php @@ -100,5 +100,26 @@ trait CustomTestMacros return $this; } ); + + TestResponse::macro( + 'assertMessagesContains', + function (array|string $keys) { + Assert::assertArrayHasKey('messages', $this, 'Response did not contain any messages'); + + if (is_string($keys)) { + $keys = [$keys]; + } + + foreach ($keys as $key) { + Assert::assertArrayHasKey( + $key, + $this['messages'], + "Response messages did not contain the key: {$key}" + ); + } + + return $this; + } + ); } } From 607f29030f13f82b40ccacefeec5f329f250b887 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Wed, 18 Sep 2024 11:53:27 -0700 Subject: [PATCH 13/23] Stub out test --- .../Accessories/Api/StoreAccessoryTest.php | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/tests/Feature/Accessories/Api/StoreAccessoryTest.php b/tests/Feature/Accessories/Api/StoreAccessoryTest.php index 496b8e9b3a..4b5d754633 100644 --- a/tests/Feature/Accessories/Api/StoreAccessoryTest.php +++ b/tests/Feature/Accessories/Api/StoreAccessoryTest.php @@ -2,11 +2,14 @@ namespace Tests\Feature\Accessories\Api; +use App\Models\Category; +use App\Models\Company; use App\Models\User; +use Tests\Concerns\TestsFullMultipleCompaniesSupport; use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class StoreAccessoryTest extends TestCase implements TestsPermissionsRequirement +class StoreAccessoryTest extends TestCase implements TestsFullMultipleCompaniesSupport, TestsPermissionsRequirement { public function testRequiresPermission() { @@ -15,6 +18,29 @@ class StoreAccessoryTest extends TestCase implements TestsPermissionsRequirement ->assertForbidden(); } + public function testAdheresToFullMultipleCompaniesSupportScoping() + { + $this->markTestSkipped('This behavior is not implemented'); + + [$companyA, $companyB] = Company::factory()->count(2)->create(); + $userInCompanyA = User::factory()->for($companyA)->createAccessories()->create(); + + $this->settings->enableMultipleFullCompanySupport(); + + // attempt to store an accessory for company B + $this->actingAsForApi($userInCompanyA) + ->postJson(route('api.accessories.store'), [ + 'category_id' => Category::factory()->forAccessories()->create()->id, + 'name' => 'Accessory A', + 'qty' => 1, + 'company_id' => $companyB->id, + ])->assertStatusMessageIs('error'); + + $this->assertDatabaseMissing('accessories', [ + 'name' => 'Accessory A', + ]); + } + public function testValidation() { $this->actingAsForApi(User::factory()->createAccessories()->create()) From 8b50ef077dc52c09bddd4743336aeea2d2d9de98 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Wed, 18 Sep 2024 11:58:33 -0700 Subject: [PATCH 14/23] Implement test --- .../Accessories/Api/StoreAccessoryTest.php | 42 +++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/tests/Feature/Accessories/Api/StoreAccessoryTest.php b/tests/Feature/Accessories/Api/StoreAccessoryTest.php index 4b5d754633..22ba715519 100644 --- a/tests/Feature/Accessories/Api/StoreAccessoryTest.php +++ b/tests/Feature/Accessories/Api/StoreAccessoryTest.php @@ -4,6 +4,9 @@ namespace Tests\Feature\Accessories\Api; use App\Models\Category; use App\Models\Company; +use App\Models\Location; +use App\Models\Manufacturer; +use App\Models\Supplier; use App\Models\User; use Tests\Concerns\TestsFullMultipleCompaniesSupport; use Tests\Concerns\TestsPermissionsRequirement; @@ -31,13 +34,13 @@ class StoreAccessoryTest extends TestCase implements TestsFullMultipleCompaniesS $this->actingAsForApi($userInCompanyA) ->postJson(route('api.accessories.store'), [ 'category_id' => Category::factory()->forAccessories()->create()->id, - 'name' => 'Accessory A', + 'name' => 'My Awesome Accessory', 'qty' => 1, 'company_id' => $companyB->id, ])->assertStatusMessageIs('error'); $this->assertDatabaseMissing('accessories', [ - 'name' => 'Accessory A', + 'name' => 'My Awesome Accessory', ]); } @@ -57,6 +60,39 @@ class StoreAccessoryTest extends TestCase implements TestsFullMultipleCompaniesS public function testCanStoreAccessory() { - $this->markTestIncomplete(); + $category = Category::factory()->forAccessories()->create(); + $company = Company::factory()->create(); + $location = Location::factory()->create(); + $manufacturer = Manufacturer::factory()->create(); + $supplier = Supplier::factory()->create(); + + $this->actingAsForApi(User::factory()->createAccessories()->create()) + ->postJson(route('api.accessories.store'), [ + 'name' => 'My Awesome Accessory', + 'qty' => 2, + 'order_number' => '12345', + 'purchase_cost' => 100.00, + 'purchase_date' => '2024-09-18', + 'model_number' => '98765', + 'category_id' => $category->id, + 'company_id' => $company->id, + 'location_id' => $location->id, + 'manufacturer_id' => $manufacturer->id, + 'supplier_id' => $supplier->id, + ])->assertStatusMessageIs('success'); + + $this->assertDatabaseHas('accessories', [ + 'name' => 'My Awesome Accessory', + 'qty' => 2, + 'order_number' => '12345', + 'purchase_cost' => 100.00, + 'purchase_date' => '2024-09-18', + 'model_number' => '98765', + 'category_id' => $category->id, + 'company_id' => $company->id, + 'location_id' => $location->id, + 'manufacturer_id' => $manufacturer->id, + 'supplier_id' => $supplier->id, + ]); } } From a071fff954b8f90b7a5e5a4df3f8a5a7d45598c1 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Wed, 18 Sep 2024 12:32:14 -0700 Subject: [PATCH 15/23] Implement tests --- .../Accessories/Api/ShowAccessoryTest.php | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/tests/Feature/Accessories/Api/ShowAccessoryTest.php b/tests/Feature/Accessories/Api/ShowAccessoryTest.php index da35eca3e0..117d725007 100644 --- a/tests/Feature/Accessories/Api/ShowAccessoryTest.php +++ b/tests/Feature/Accessories/Api/ShowAccessoryTest.php @@ -3,11 +3,13 @@ namespace Tests\Feature\Accessories\Api; use App\Models\Accessory; +use App\Models\Company; use App\Models\User; +use Tests\Concerns\TestsFullMultipleCompaniesSupport; use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class ShowAccessoryTest extends TestCase implements TestsPermissionsRequirement +class ShowAccessoryTest extends TestCase implements TestsFullMultipleCompaniesSupport, TestsPermissionsRequirement { public function testRequiresPermission() { @@ -17,4 +19,43 @@ class ShowAccessoryTest extends TestCase implements TestsPermissionsRequirement ->getJson(route('api.accessories.show', $accessory)) ->assertForbidden(); } + + public function testAdheresToFullMultipleCompaniesSupportScoping() + { + [$companyA, $companyB] = Company::factory()->count(2)->create(); + + $accessoryForCompanyA = Accessory::factory()->for($companyA)->create(); + + $superuser = User::factory()->superuser()->create(); + $userForCompanyB = User::factory()->for($companyB)->viewAccessories()->create(); + + $this->settings->enableMultipleFullCompanySupport(); + + $this->actingAsForApi($userForCompanyB) + ->getJson(route('api.accessories.show', $accessoryForCompanyA)) + ->assertOk() + ->assertStatusMessageIs('error'); + + $this->actingAsForApi($superuser) + ->getJson(route('api.accessories.show', $accessoryForCompanyA)) + ->assertOk() + ->assertJsonFragment([ + 'id' => $accessoryForCompanyA->id, + ]); + } + + public function testCanGetSingleAccessory() + { + $accessory = Accessory::factory()->checkedOutToUser()->create(['name' => 'My Accessory']); + + $this->actingAsForApi(User::factory()->viewAccessories()->create()) + ->getJson(route('api.accessories.show', $accessory)) + ->assertOk() + ->assertJsonFragment([ + 'id' => $accessory->id, + 'name' => 'My Accessory', + 'checkouts_count' => 1, + ]); + + } } From 86f13a9735358ce0ce51205a439ce8fc7df5197c Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Wed, 18 Sep 2024 16:13:34 -0700 Subject: [PATCH 16/23] Add index test --- .../Feature/Accessories/Api/IndexAccessoryTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/Feature/Accessories/Api/IndexAccessoryTest.php b/tests/Feature/Accessories/Api/IndexAccessoryTest.php index 999c0f97ff..96263f5380 100644 --- a/tests/Feature/Accessories/Api/IndexAccessoryTest.php +++ b/tests/Feature/Accessories/Api/IndexAccessoryTest.php @@ -53,4 +53,18 @@ class IndexAccessoryTest extends TestCase implements TestsFullMultipleCompaniesS ->assertResponseContainsInRows($accessoryB) ->assertResponseContainsInRows($accessoryC); } + + public function testCanGetAccessories() + { + $user = User::factory()->viewAccessories()->create(); + + $accessoryA = Accessory::factory()->create(['name' => 'Accessory A']); + $accessoryB = Accessory::factory()->create(['name' => 'Accessory B']); + + $this->actingAsForApi($user) + ->getJson(route('api.accessories.index')) + ->assertOk() + ->assertResponseContainsInRows($accessoryA) + ->assertResponseContainsInRows($accessoryB); + } } From 9b22d6d493734e2ff55b6b2e80f682b98f92e6e4 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 19 Sep 2024 11:24:01 -0700 Subject: [PATCH 17/23] Add tests for accessory checkouts endpoint --- database/factories/AccessoryFactory.php | 15 +++++ .../Api/IndexAccessoryCheckoutsTest.php | 64 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 tests/Feature/Accessories/Api/IndexAccessoryCheckoutsTest.php 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); + } +} From 21378904969e0ad166355cd8a92b2bcc74978510 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 19 Sep 2024 11:33:20 -0700 Subject: [PATCH 18/23] Remove dead code --- app/Http/Controllers/Api/AccessoriesController.php | 3 --- database/factories/AccessoryFactory.php | 1 - 2 files changed, 4 deletions(-) diff --git a/app/Http/Controllers/Api/AccessoriesController.php b/app/Http/Controllers/Api/AccessoriesController.php index acfdcef870..d88b5c6849 100644 --- a/app/Http/Controllers/Api/AccessoriesController.php +++ b/app/Http/Controllers/Api/AccessoriesController.php @@ -192,9 +192,6 @@ class AccessoriesController extends Controller $this->authorize('view', Accessory::class); $accessory = Accessory::with('lastCheckout')->findOrFail($id); - if (! Company::isCurrentUserHasAccess($accessory)) { - return ['total' => 0, 'rows' => []]; - } $offset = request('offset', 0); $limit = request('limit', 50); diff --git a/database/factories/AccessoryFactory.php b/database/factories/AccessoryFactory.php index 38d159f423..178f8fa9ad 100644 --- a/database/factories/AccessoryFactory.php +++ b/database/factories/AccessoryFactory.php @@ -3,7 +3,6 @@ namespace Database\Factories; use App\Models\Accessory; -use App\Models\AccessoryCheckout; use App\Models\Category; use App\Models\Location; use App\Models\Manufacturer; From a5be18bb14242da7d503fe8b6e1c5d31ad1a07e8 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 19 Sep 2024 11:55:15 -0700 Subject: [PATCH 19/23] Add test for limit and offset --- .../Api/IndexAccessoryCheckoutsTest.php | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/Feature/Accessories/Api/IndexAccessoryCheckoutsTest.php b/tests/Feature/Accessories/Api/IndexAccessoryCheckoutsTest.php index cad1a03ffd..38f1e99e5d 100644 --- a/tests/Feature/Accessories/Api/IndexAccessoryCheckoutsTest.php +++ b/tests/Feature/Accessories/Api/IndexAccessoryCheckoutsTest.php @@ -61,4 +61,24 @@ class IndexAccessoryCheckoutsTest extends TestCase implements TestsFullMultipleC ->assertJsonPath('rows.0.assigned_to.id', $userA->id) ->assertJsonPath('rows.1.assigned_to.id', $userB->id); } + + public function testCanGetAccessoryCheckoutsWithOffsetAndLimitInQueryString() + { + [$userA, $userB, $userC] = User::factory()->count(3)->create(); + + $accessory = Accessory::factory()->checkedOutToUsers([$userA, $userB, $userC])->create(); + + $actor = $this->actingAsForApi(User::factory()->viewAccessories()->create()); + + $actor->getJson(route('api.accessories.checkedout', ['accessory' => $accessory->id, 'limit' => 1])) + ->assertOk() + ->assertJsonPath('total', 3) + ->assertJsonPath('rows.0.assigned_to.id', $userA->id); + + $actor->getJson(route('api.accessories.checkedout', ['accessory' => $accessory->id, 'limit' => 2, 'offset' => 1])) + ->assertOk() + ->assertJsonPath('total', 3) + ->assertJsonPath('rows.0.assigned_to.id', $userB->id) + ->assertJsonPath('rows.1.assigned_to.id', $userC->id); + } } From fff069824b91753a5b0bbd042f7fe0e9d9240aac Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 19 Sep 2024 12:11:03 -0700 Subject: [PATCH 20/23] Add tests for update accessory endpoint --- .../Accessories/Api/UpdateAccessoryTest.php | 88 ++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) diff --git a/tests/Feature/Accessories/Api/UpdateAccessoryTest.php b/tests/Feature/Accessories/Api/UpdateAccessoryTest.php index 932623fb36..a0009a732a 100644 --- a/tests/Feature/Accessories/Api/UpdateAccessoryTest.php +++ b/tests/Feature/Accessories/Api/UpdateAccessoryTest.php @@ -3,11 +3,17 @@ namespace Tests\Feature\Accessories\Api; use App\Models\Accessory; +use App\Models\Category; +use App\Models\Company; +use App\Models\Location; +use App\Models\Manufacturer; +use App\Models\Supplier; use App\Models\User; +use Tests\Concerns\TestsFullMultipleCompaniesSupport; use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class UpdateAccessoryTest extends TestCase implements TestsPermissionsRequirement +class UpdateAccessoryTest extends TestCase implements TestsFullMultipleCompaniesSupport, TestsPermissionsRequirement { public function testRequiresPermission() { @@ -17,4 +23,84 @@ class UpdateAccessoryTest extends TestCase implements TestsPermissionsRequiremen ->patchJson(route('api.accessories.update', $accessory)) ->assertForbidden(); } + + public function testAdheresToFullMultipleCompaniesSupportScoping() + { + [$companyA, $companyB] = Company::factory()->count(2)->create(); + + $accessoryA = Accessory::factory()->for($companyA)->create(['name' => 'A Name to Change']); + $accessoryB = Accessory::factory()->for($companyB)->create(['name' => 'A Name to Change']); + $accessoryC = Accessory::factory()->for($companyB)->create(['name' => 'A Name to Change']); + + $superuser = User::factory()->superuser()->create(); + $userInCompanyA = $companyA->users()->save(User::factory()->editAccessories()->make()); + $userInCompanyB = $companyB->users()->save(User::factory()->editAccessories()->make()); + + $this->settings->enableMultipleFullCompanySupport(); + + $this->actingAsForApi($userInCompanyA) + ->patchJson(route('api.accessories.update', $accessoryB), ['name' => 'New Name']) + ->assertStatusMessageIs('error'); + + $this->actingAsForApi($userInCompanyB) + ->patchJson(route('api.accessories.update', $accessoryA), ['name' => 'New Name']) + ->assertStatusMessageIs('error'); + + $this->actingAsForApi($superuser) + ->patchJson(route('api.accessories.update', $accessoryC), ['name' => 'New Name']) + ->assertOk(); + + $this->assertEquals('A Name to Change', $accessoryA->fresh()->name); + $this->assertEquals('A Name to Change', $accessoryB->fresh()->name); + $this->assertEquals('New Name', $accessoryC->fresh()->name); + } + + public function testCanUpdateAccessoryViaPatch() + { + [$categoryA, $categoryB] = Category::factory()->count(2)->create(); + [$companyA, $companyB] = Company::factory()->count(2)->create(); + [$locationA, $locationB] = Location::factory()->count(2)->create(); + [$manufacturerA, $manufacturerB] = Manufacturer::factory()->count(2)->create(); + [$supplierA, $supplierB] = Supplier::factory()->count(2)->create(); + + $accessory = Accessory::factory()->create([ + 'name' => 'A Name to Change', + 'qty' => 5, + 'order_number' => 'A12345', + 'purchase_cost' => 99.99, + 'model_number' => 'ABC098', + 'category_id' => $categoryA->id, + 'company_id' => $companyA->id, + 'location_id' => $locationA->id, + 'manufacturer_id' => $manufacturerA->id, + 'supplier_id' => $supplierA->id, + ]); + + $this->actingAsForApi(User::factory()->editAccessories()->create()) + ->patchJson(route('api.accessories.update', $accessory), [ + 'name' => 'A New Name', + 'qty' => 10, + 'order_number' => 'B54321', + 'purchase_cost' => 199.99, + 'model_number' => 'XYZ123', + 'category_id' => $categoryB->id, + 'company_id' => $companyB->id, + 'location_id' => $locationB->id, + 'manufacturer_id' => $manufacturerB->id, + 'supplier_id' => $supplierB->id, + ]) + ->assertOk(); + + $accessory = $accessory->fresh(); + $this->assertEquals('A New Name', $accessory->name); + $this->assertEquals(10, $accessory->qty); + $this->assertEquals('B54321', $accessory->order_number); + $this->assertEquals(199.99, $accessory->purchase_cost); + $this->assertEquals('XYZ123', $accessory->model_number); + $this->assertEquals($categoryB->id, $accessory->category_id); + $this->assertEquals($companyB->id, $accessory->company_id); + $this->assertEquals($locationB->id, $accessory->location_id); + $this->assertEquals($manufacturerB->id, $accessory->manufacturer_id); + $this->assertEquals($supplierB->id, $accessory->supplier_id); + } } From d639d6fbc1467ce8f231c73507ec371f4c705fc5 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 19 Sep 2024 12:21:54 -0700 Subject: [PATCH 21/23] Add tests for accessory select list endpoint --- .../Api/AccessoriesForSelectListTest.php | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 tests/Feature/Accessories/Api/AccessoriesForSelectListTest.php diff --git a/tests/Feature/Accessories/Api/AccessoriesForSelectListTest.php b/tests/Feature/Accessories/Api/AccessoriesForSelectListTest.php new file mode 100644 index 0000000000..f2c2185edc --- /dev/null +++ b/tests/Feature/Accessories/Api/AccessoriesForSelectListTest.php @@ -0,0 +1,59 @@ +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.selectlist')) + ->assertOk() + ->assertJsonPath('total_count', 1) + ->assertResponseContainsInResults($accessoryA) + ->assertResponseDoesNotContainInResults($accessoryB); + + $this->actingAsForApi($userInCompanyB) + ->getJson(route('api.accessories.selectlist')) + ->assertOk() + ->assertJsonPath('total_count', 1) + ->assertResponseDoesNotContainInResults($accessoryA) + ->assertResponseContainsInResults($accessoryB); + + $this->actingAsForApi($superuser) + ->getJson(route('api.accessories.selectlist')) + ->assertOk() + ->assertJsonPath('total_count', 2) + ->assertResponseContainsInResults($accessoryA) + ->assertResponseContainsInResults($accessoryB); + } + + public function testCanGetAccessoriesForSelectList() + { + [$accessoryA, $accessoryB] = Accessory::factory()->count(2)->create(); + + $this->actingAsForApi(User::factory()->viewAccessories()->create()) + ->getJson(route('api.accessories.selectlist')) + ->assertOk() + ->assertJsonPath('total_count', 2) + ->assertResponseContainsInResults($accessoryA) + ->assertResponseContainsInResults($accessoryB); + } +} From 5b90d79494215f27f8d1f8b02e1d14c787f44a6a Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 1 Oct 2024 11:50:48 -0700 Subject: [PATCH 22/23] Use created_by --- tests/Feature/Checkins/Api/AccessoryCheckinTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Feature/Checkins/Api/AccessoryCheckinTest.php b/tests/Feature/Checkins/Api/AccessoryCheckinTest.php index d44a05b310..8e7a07fca5 100644 --- a/tests/Feature/Checkins/Api/AccessoryCheckinTest.php +++ b/tests/Feature/Checkins/Api/AccessoryCheckinTest.php @@ -71,7 +71,7 @@ class AccessoryCheckinTest extends TestCase implements TestsFullMultipleCompanie ->assertStatusMessageIs('success'); $this->assertDatabaseHas('action_logs', [ - 'user_id' => $actor->id, + 'created_by' => $actor->id, 'action_type' => 'checkin from', 'target_id' => $user->id, 'target_type' => User::class, From 3dc64cc5e0465e60116e2afd9e04a865d8781199 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 1 Oct 2024 13:35:39 -0700 Subject: [PATCH 23/23] Reference accessory checkout and not the accessory --- .../Feature/Checkins/Api/AccessoryCheckinTest.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/tests/Feature/Checkins/Api/AccessoryCheckinTest.php b/tests/Feature/Checkins/Api/AccessoryCheckinTest.php index 8e7a07fca5..d5f45f22ae 100644 --- a/tests/Feature/Checkins/Api/AccessoryCheckinTest.php +++ b/tests/Feature/Checkins/Api/AccessoryCheckinTest.php @@ -14,9 +14,10 @@ class AccessoryCheckinTest extends TestCase implements TestsFullMultipleCompanie public function testRequiresPermission() { $accessory = Accessory::factory()->checkedOutToUser()->create(); + $accessoryCheckout = $accessory->checkouts->first(); $this->actingAsForApi(User::factory()->create()) - ->postJson(route('api.accessories.checkin', $accessory)) + ->postJson(route('api.accessories.checkin', $accessoryCheckout)) ->assertForbidden(); } @@ -35,11 +36,11 @@ class AccessoryCheckinTest extends TestCase implements TestsFullMultipleCompanie $this->settings->enableMultipleFullCompanySupport(); $this->actingAsForApi($userInCompanyA) - ->postJson(route('api.accessories.checkin', $accessoryForCompanyB)) + ->postJson(route('api.accessories.checkin', $accessoryForCompanyB->checkouts->first())) ->assertForbidden(); $this->actingAsForApi($superUser) - ->postJson(route('api.accessories.checkin', $anotherAccessoryForCompanyB)) + ->postJson(route('api.accessories.checkin', $anotherAccessoryForCompanyB->checkouts->first())) ->assertStatusMessageIs('success'); $this->assertEquals(1, $accessoryForCompanyB->fresh()->checkouts->count(), 'Accessory should not be checked in'); @@ -52,8 +53,10 @@ class AccessoryCheckinTest extends TestCase implements TestsFullMultipleCompanie $this->assertEquals(1, $accessory->checkouts->count()); + $accessoryCheckout = $accessory->checkouts->first(); + $this->actingAsForApi(User::factory()->checkinAccessories()->create()) - ->postJson(route('api.accessories.checkin', $accessory)) + ->postJson(route('api.accessories.checkin', $accessoryCheckout)) ->assertStatusMessageIs('success'); $this->assertEquals(0, $accessory->fresh()->checkouts->count(), 'Accessory should be checked in'); @@ -65,9 +68,10 @@ class AccessoryCheckinTest extends TestCase implements TestsFullMultipleCompanie $actor = User::factory()->checkinAccessories()->create(); $accessory = Accessory::factory()->checkedOutToUser($user)->create(); + $accessoryCheckout = $accessory->checkouts->first(); $this->actingAsForApi($actor) - ->postJson(route('api.accessories.checkin', $accessory)) + ->postJson(route('api.accessories.checkin', $accessoryCheckout)) ->assertStatusMessageIs('success'); $this->assertDatabaseHas('action_logs', [