From f16f62f76ca602d9edfcf1bdc5013697c83b4186 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 29 Jan 2024 14:21:30 -0800 Subject: [PATCH 1/7] Scaffold and implement some tests around accessory checkout --- database/factories/AccessoryFactory.php | 21 +++++- database/factories/CategoryFactory.php | 6 ++ .../Checkouts/AccessoryCheckoutTest.php | 67 +++++++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 tests/Feature/Checkouts/AccessoryCheckoutTest.php diff --git a/database/factories/AccessoryFactory.php b/database/factories/AccessoryFactory.php index 8ce34303b3..a0d7fbbff6 100644 --- a/database/factories/AccessoryFactory.php +++ b/database/factories/AccessoryFactory.php @@ -33,7 +33,7 @@ class AccessoryFactory extends Factory $this->faker->randomElement(['Keyboard', 'Wired']) ), 'user_id' => User::factory()->superuser(), - 'category_id' => Category::factory(), + 'category_id' => Category::factory()->forAccessories(), 'model_number' => $this->faker->numberBetween(1000000, 50000000), 'location_id' => Location::factory(), 'qty' => 1, @@ -114,4 +114,23 @@ class AccessoryFactory extends Factory ]; }); } + + public function withoutItemsRemaining() + { + return $this->state(function () { + return [ + 'qty' => 1, + ]; + })->afterCreating(function ($accessory) { + $user = User::factory()->create(); + + $accessory->users()->attach($accessory->id, [ + 'accessory_id' => $accessory->id, + 'created_at' => now(), + 'user_id' => $user->id, + 'assigned_to' => $user->id, + 'note' => '', + ]); + }); + } } diff --git a/database/factories/CategoryFactory.php b/database/factories/CategoryFactory.php index 94a9626da4..fe6bc255b7 100644 --- a/database/factories/CategoryFactory.php +++ b/database/factories/CategoryFactory.php @@ -172,4 +172,10 @@ class CategoryFactory extends Factory ]); } + public function forAccessories() + { + return $this->state([ + 'category_type' => 'accessory', + ]); + } } diff --git a/tests/Feature/Checkouts/AccessoryCheckoutTest.php b/tests/Feature/Checkouts/AccessoryCheckoutTest.php new file mode 100644 index 0000000000..e81d1f7af3 --- /dev/null +++ b/tests/Feature/Checkouts/AccessoryCheckoutTest.php @@ -0,0 +1,67 @@ +actingAs(User::factory()->create()) + ->post(route('accessories.checkout.store', Accessory::factory()->create())) + ->assertForbidden(); + } + + public function testValidation() + { + $accessory = Accessory::factory()->create(); + + $this->actingAs(User::factory()->checkoutAccessories()->create()) + ->post(route('accessories.checkout.store', $accessory), [ + // missing assigned_to + ]) + ->assertSessionHas('error'); + } + + public function testAccessoryMustBeAvailableWhenCheckingOut() + { + $this->actingAs(User::factory()->checkoutAccessories()->create()) + ->post(route('accessories.checkout.store', Accessory::factory()->withoutItemsRemaining()->create()), [ + 'assigned_to' => User::factory()->create()->id, + ]) + ->assertSessionHas('error'); + } + + public function testAccessoryCanBeCheckedOut() + { + $accessory = Accessory::factory()->create(); + + $user = User::factory()->create(); + + $this->actingAs(User::factory()->checkoutAccessories()->create()) + ->post(route('accessories.checkout.store', $accessory), [ + 'assigned_to' => $user->id, + ]); + + $this->assertTrue($accessory->users->contains($user)); + } + + public function testUserSentEulaUponCheckoutIfAcceptanceRequired() + { + $this->markTestIncomplete(); + } + + public function testActionLogCreatedUponCheckout() + { + $this->markTestIncomplete(); + + // check 'note' is saved in action_logs + // check 'action_source' is saved in action_logs as gui + } +} From 987676df08726d7047d2954ac83fc7ab07ecde25 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 29 Jan 2024 15:56:18 -0800 Subject: [PATCH 2/7] Implement additional tests --- database/factories/AccessoryFactory.php | 7 ++++ .../Checkouts/AccessoryCheckoutTest.php | 38 ++++++++++++++++--- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/database/factories/AccessoryFactory.php b/database/factories/AccessoryFactory.php index a0d7fbbff6..4066526770 100644 --- a/database/factories/AccessoryFactory.php +++ b/database/factories/AccessoryFactory.php @@ -133,4 +133,11 @@ class AccessoryFactory extends Factory ]); }); } + + public function requiringAcceptance() + { + return $this->afterCreating(function ($accessory) { + $accessory->category->update(['require_acceptance' => 1]); + }); + } } diff --git a/tests/Feature/Checkouts/AccessoryCheckoutTest.php b/tests/Feature/Checkouts/AccessoryCheckoutTest.php index e81d1f7af3..df89c5037b 100644 --- a/tests/Feature/Checkouts/AccessoryCheckoutTest.php +++ b/tests/Feature/Checkouts/AccessoryCheckoutTest.php @@ -3,7 +3,10 @@ namespace Tests\Feature\Checkouts; use App\Models\Accessory; +use App\Models\Actionlog; use App\Models\User; +use App\Notifications\CheckoutAccessoryNotification; +use Illuminate\Support\Facades\Notification; use Tests\Support\InteractsWithSettings; use Tests\TestCase; @@ -52,16 +55,41 @@ class AccessoryCheckoutTest extends TestCase $this->assertTrue($accessory->users->contains($user)); } - public function testUserSentEulaUponCheckoutIfAcceptanceRequired() + public function testUserSentNotificationUponCheckout() { - $this->markTestIncomplete(); + Notification::fake(); + + $accessory = Accessory::factory()->requiringAcceptance()->create(); + $user = User::factory()->create(); + + $this->actingAs(User::factory()->checkoutAccessories()->create()) + ->post(route('accessories.checkout.store', $accessory), [ + 'assigned_to' => $user->id, + ]); + + Notification::assertSentTo($user, CheckoutAccessoryNotification::class); } public function testActionLogCreatedUponCheckout() { - $this->markTestIncomplete(); + $accessory = Accessory::factory()->create(); + $actor = User::factory()->checkoutAccessories()->create(); + $user = User::factory()->create(); - // check 'note' is saved in action_logs - // check 'action_source' is saved in action_logs as gui + $this->actingAs($actor) + ->post(route('accessories.checkout.store', $accessory), [ + 'assigned_to' => $user->id, + 'note' => 'oh hi there', + ]); + + $this->assertDatabaseHas('action_logs', [ + 'action_type' => 'checkout', + 'target_id' => $user->id, + 'target_type' => User::class, + 'item_id' => $accessory->id, + 'item_type' => Accessory::class, + 'user_id' => $actor->id, + 'note' => 'oh hi there', + ]); } } From e5d3df7d2411bf17d7d20b16e10bdb11c5b23db7 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 29 Jan 2024 15:59:23 -0800 Subject: [PATCH 3/7] Scaffold accessory checkout tests for api --- .../Api/Accessories/AccessoryCheckoutTest.php | 64 +++++++++++++++++++ .../Checkouts/AccessoryCheckoutTest.php | 1 - 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 tests/Feature/Api/Accessories/AccessoryCheckoutTest.php diff --git a/tests/Feature/Api/Accessories/AccessoryCheckoutTest.php b/tests/Feature/Api/Accessories/AccessoryCheckoutTest.php new file mode 100644 index 0000000000..14ac9da1bf --- /dev/null +++ b/tests/Feature/Api/Accessories/AccessoryCheckoutTest.php @@ -0,0 +1,64 @@ +markTestIncomplete(); + } + + public function testValidation() + { + $this->markTestIncomplete(); + } + + public function testAccessoryMustBeAvailableWhenCheckingOut() + { + $this->markTestIncomplete(); + } + + public function testAccessoryCanBeCheckedOut() + { + $this->markTestIncomplete(); + } + + public function testUserSentNotificationUponCheckout() + { + $this->markTestIncomplete(); + + $this->withoutExceptionHandling(); + + Notification::fake(); + + $accessory = Accessory::factory()->requiringAcceptance()->create(); + $user = User::factory()->create(); + + $this->actingAsForApi(User::factory()->checkoutAccessories()->create()) + ->postJson(route('api.accessories.checkout', $accessory), [ + 'assigned_to' => $user->id, + ]); + + Notification::assertSentTo($user, CheckoutAccessoryNotification::class); + } + + public function testActionLogCreatedUponCheckout() + { + $this->markTestIncomplete(); + } + + public function testUserSentEulaUponCheckoutIfAcceptanceRequired() + { + $this->markTestIncomplete(); + } +} diff --git a/tests/Feature/Checkouts/AccessoryCheckoutTest.php b/tests/Feature/Checkouts/AccessoryCheckoutTest.php index df89c5037b..daa9b5fb61 100644 --- a/tests/Feature/Checkouts/AccessoryCheckoutTest.php +++ b/tests/Feature/Checkouts/AccessoryCheckoutTest.php @@ -3,7 +3,6 @@ namespace Tests\Feature\Checkouts; use App\Models\Accessory; -use App\Models\Actionlog; use App\Models\User; use App\Notifications\CheckoutAccessoryNotification; use Illuminate\Support\Facades\Notification; From f1ab8253f0b59c13211378f5da5e1b8845f93f3d Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 29 Jan 2024 16:48:40 -0800 Subject: [PATCH 4/7] Implement tests included two currently failing tests --- .../Api/Accessories/AccessoryCheckoutTest.php | 53 ++++++++++++++----- .../Checkouts/AccessoryCheckoutTest.php | 5 +- 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/tests/Feature/Api/Accessories/AccessoryCheckoutTest.php b/tests/Feature/Api/Accessories/AccessoryCheckoutTest.php index 14ac9da1bf..41070943c8 100644 --- a/tests/Feature/Api/Accessories/AccessoryCheckoutTest.php +++ b/tests/Feature/Api/Accessories/AccessoryCheckoutTest.php @@ -15,30 +15,44 @@ class AccessoryCheckoutTest extends TestCase public function testCheckingOutAccessoryRequiresCorrectPermission() { - $this->markTestIncomplete(); + $this->actingAsForApi(User::factory()->create()) + ->postJson(route('api.accessories.checkout', Accessory::factory()->create())) + ->assertForbidden(); } public function testValidation() { - $this->markTestIncomplete(); + $this->actingAsForApi(User::factory()->checkoutAccessories()->create()) + ->postJson(route('api.accessories.checkout', Accessory::factory()->create()), [ + // missing assigned_to + ]) + ->assertStatusMessageIs('error'); } public function testAccessoryMustBeAvailableWhenCheckingOut() { - $this->markTestIncomplete(); + $this->actingAsForApi(User::factory()->checkoutAccessories()->create()) + ->postJson(route('api.accessories.checkout', Accessory::factory()->withoutItemsRemaining()->create()), [ + 'assigned_to' => User::factory()->create()->id, + ]) + ->assertStatusMessageIs('error'); } public function testAccessoryCanBeCheckedOut() { - $this->markTestIncomplete(); + $accessory = Accessory::factory()->create(); + $user = User::factory()->create(); + + $this->actingAsForApi(User::factory()->checkoutAccessories()->create()) + ->postJson(route('api.accessories.checkout', $accessory), [ + 'assigned_to' => $user->id, + ]); + + $this->assertTrue($accessory->users->contains($user)); } public function testUserSentNotificationUponCheckout() { - $this->markTestIncomplete(); - - $this->withoutExceptionHandling(); - Notification::fake(); $accessory = Accessory::factory()->requiringAcceptance()->create(); @@ -54,11 +68,24 @@ class AccessoryCheckoutTest extends TestCase public function testActionLogCreatedUponCheckout() { - $this->markTestIncomplete(); - } + $accessory = Accessory::factory()->create(); + $actor = User::factory()->checkoutAccessories()->create(); + $user = User::factory()->create(); - public function testUserSentEulaUponCheckoutIfAcceptanceRequired() - { - $this->markTestIncomplete(); + $this->actingAsForApi($actor) + ->postJson(route('api.accessories.checkout', $accessory), [ + 'assigned_to' => $user->id, + 'note' => 'oh hi there', + ]); + + $this->assertDatabaseHas('action_logs', [ + 'action_type' => 'checkout', + 'target_id' => $user->id, + 'target_type' => User::class, + 'item_id' => $accessory->id, + 'item_type' => Accessory::class, + 'user_id' => $actor->id, + 'note' => 'oh hi there', + ]); } } diff --git a/tests/Feature/Checkouts/AccessoryCheckoutTest.php b/tests/Feature/Checkouts/AccessoryCheckoutTest.php index daa9b5fb61..a61ca1eb76 100644 --- a/tests/Feature/Checkouts/AccessoryCheckoutTest.php +++ b/tests/Feature/Checkouts/AccessoryCheckoutTest.php @@ -22,10 +22,8 @@ class AccessoryCheckoutTest extends TestCase public function testValidation() { - $accessory = Accessory::factory()->create(); - $this->actingAs(User::factory()->checkoutAccessories()->create()) - ->post(route('accessories.checkout.store', $accessory), [ + ->post(route('accessories.checkout.store', Accessory::factory()->create()), [ // missing assigned_to ]) ->assertSessionHas('error'); @@ -43,7 +41,6 @@ class AccessoryCheckoutTest extends TestCase public function testAccessoryCanBeCheckedOut() { $accessory = Accessory::factory()->create(); - $user = User::factory()->create(); $this->actingAs(User::factory()->checkoutAccessories()->create()) From 7d45cfff2cf003b5303611ae332b68c6afb8242d Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 29 Jan 2024 16:49:09 -0800 Subject: [PATCH 5/7] Ensure accessory available when checking out via api --- app/Http/Controllers/Api/AccessoriesController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/Api/AccessoriesController.php b/app/Http/Controllers/Api/AccessoriesController.php index 654f3c2e24..1edd282b05 100644 --- a/app/Http/Controllers/Api/AccessoriesController.php +++ b/app/Http/Controllers/Api/AccessoriesController.php @@ -278,7 +278,7 @@ class AccessoriesController extends Controller public function checkout(Request $request, $accessoryId) { // Check if the accessory exists - if (is_null($accessory = Accessory::find($accessoryId))) { + if (is_null($accessory = Accessory::withCount('users as users_count')->find($accessoryId))) { return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/accessories/message.does_not_exist'))); } From a2cba67f4e3a30762e209423d6f6cb8b714c9663 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 29 Jan 2024 16:59:57 -0800 Subject: [PATCH 6/7] Improve assertion --- .../Api/Accessories/AccessoryCheckoutTest.php | 23 +++++++++++-------- .../Checkouts/AccessoryCheckoutTest.php | 23 +++++++++++-------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/tests/Feature/Api/Accessories/AccessoryCheckoutTest.php b/tests/Feature/Api/Accessories/AccessoryCheckoutTest.php index 41070943c8..f4f49f0dbc 100644 --- a/tests/Feature/Api/Accessories/AccessoryCheckoutTest.php +++ b/tests/Feature/Api/Accessories/AccessoryCheckoutTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature\Api\Accessories; use App\Models\Accessory; +use App\Models\Actionlog; use App\Models\User; use App\Notifications\CheckoutAccessoryNotification; use Illuminate\Support\Facades\Notification; @@ -78,14 +79,18 @@ class AccessoryCheckoutTest extends TestCase 'note' => 'oh hi there', ]); - $this->assertDatabaseHas('action_logs', [ - 'action_type' => 'checkout', - 'target_id' => $user->id, - 'target_type' => User::class, - 'item_id' => $accessory->id, - 'item_type' => Accessory::class, - 'user_id' => $actor->id, - 'note' => 'oh hi there', - ]); + $this->assertEquals( + 1, + Actionlog::where([ + 'action_type' => 'checkout', + 'target_id' => $user->id, + 'target_type' => User::class, + 'item_id' => $accessory->id, + 'item_type' => Accessory::class, + 'user_id' => $actor->id, + 'note' => 'oh hi there', + ])->count(), + 'Log entry either does not exist or there are more than expected' + ); } } diff --git a/tests/Feature/Checkouts/AccessoryCheckoutTest.php b/tests/Feature/Checkouts/AccessoryCheckoutTest.php index a61ca1eb76..74269bfbb9 100644 --- a/tests/Feature/Checkouts/AccessoryCheckoutTest.php +++ b/tests/Feature/Checkouts/AccessoryCheckoutTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature\Checkouts; use App\Models\Accessory; +use App\Models\Actionlog; use App\Models\User; use App\Notifications\CheckoutAccessoryNotification; use Illuminate\Support\Facades\Notification; @@ -78,14 +79,18 @@ class AccessoryCheckoutTest extends TestCase 'note' => 'oh hi there', ]); - $this->assertDatabaseHas('action_logs', [ - 'action_type' => 'checkout', - 'target_id' => $user->id, - 'target_type' => User::class, - 'item_id' => $accessory->id, - 'item_type' => Accessory::class, - 'user_id' => $actor->id, - 'note' => 'oh hi there', - ]); + $this->assertEquals( + 1, + Actionlog::where([ + 'action_type' => 'checkout', + 'target_id' => $user->id, + 'target_type' => User::class, + 'item_id' => $accessory->id, + 'item_type' => Accessory::class, + 'user_id' => $actor->id, + 'note' => 'oh hi there', + ])->count(), + 'Log entry either does not exist or there are more than expected' + ); } } From 42ec2548c95319ad65187c3304dd298bf9f2d191 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 29 Jan 2024 17:03:19 -0800 Subject: [PATCH 7/7] Fire event when accessory checked out via API Brings behavior in line with GUI controller --- app/Http/Controllers/Api/AccessoriesController.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Api/AccessoriesController.php b/app/Http/Controllers/Api/AccessoriesController.php index 1edd282b05..7dcee93204 100644 --- a/app/Http/Controllers/Api/AccessoriesController.php +++ b/app/Http/Controllers/Api/AccessoriesController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\Api; +use App\Events\CheckoutableCheckedOut; use App\Helpers\Helper; use App\Http\Controllers\Controller; use App\Http\Transformers\AccessoriesTransformer; @@ -302,7 +303,7 @@ class AccessoriesController extends Controller 'note' => $request->get('note'), ]); - $accessory->logCheckout($request->input('note'), $user); + event(new CheckoutableCheckedOut($accessory, $user, Auth::user(), $request->input('note'))); return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/accessories/message.checkout.success'))); }