From f16f62f76ca602d9edfcf1bdc5013697c83b4186 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 29 Jan 2024 14:21:30 -0800 Subject: [PATCH 01/10] 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 8ce34303b..a0d7fbbff 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 94a9626da..fe6bc255b 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 000000000..e81d1f7af --- /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 02/10] 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 a0d7fbbff..406652677 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 e81d1f7af..df89c5037 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 03/10] 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 000000000..14ac9da1b --- /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 df89c5037..daa9b5fb6 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 04/10] 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 14ac9da1b..41070943c 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 daa9b5fb6..a61ca1eb7 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 05/10] 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 654f3c2e2..1edd282b0 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 06/10] 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 41070943c..f4f49f0db 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 a61ca1eb7..74269bfbb 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 07/10] 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 1edd282b0..7dcee9320 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'))); } From d965fe759a35a64c2c25d01031722b3ed7071767 Mon Sep 17 00:00:00 2001 From: snipe Date: Mon, 5 Feb 2024 12:28:24 +0000 Subject: [PATCH 08/10] Fixed #14211 - duplicate array key `ldap_emp_num` Signed-off-by: snipe --- app/Models/Setting.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/Models/Setting.php b/app/Models/Setting.php index caf142cbd..c7e67d24d 100755 --- a/app/Models/Setting.php +++ b/app/Models/Setting.php @@ -352,7 +352,6 @@ class Setting extends Model 'ldap_client_tls_cert', 'ldap_default_group', 'ldap_dept', - 'ldap_emp_num', 'ldap_phone_field', 'ldap_jobtitle', 'ldap_manager', From e6fdeb0e8a0d1ff2fbae412b5add7989fba1a3c4 Mon Sep 17 00:00:00 2001 From: snipe Date: Mon, 5 Feb 2024 15:30:18 +0000 Subject: [PATCH 09/10] Removed initial check for assets, licenses, etc Signed-off-by: snipe --- resources/views/companies/view.blade.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/resources/views/companies/view.blade.php b/resources/views/companies/view.blade.php index ffa5815b6..0b94446b2 100644 --- a/resources/views/companies/view.blade.php +++ b/resources/views/companies/view.blade.php @@ -21,7 +21,7 @@ @@ -33,7 +33,7 @@ @@ -43,7 +43,7 @@ @@ -53,7 +53,7 @@ From 5882d71f9b1406358e54bddc6c5277ba5512689b Mon Sep 17 00:00:00 2001 From: snipe Date: Mon, 5 Feb 2024 15:55:53 +0000 Subject: [PATCH 10/10] Fix company asset counts for dashboard widget Signed-off-by: snipe --- app/Http/Controllers/Api/CompaniesController.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Api/CompaniesController.php b/app/Http/Controllers/Api/CompaniesController.php index 580bc5d9b..9c667973a 100644 --- a/app/Http/Controllers/Api/CompaniesController.php +++ b/app/Http/Controllers/Api/CompaniesController.php @@ -40,7 +40,9 @@ class CompaniesController extends Controller 'components_count', ]; - $companies = Company::withCount('assets as assets_count', 'licenses as licenses_count', 'accessories as accessories_count', 'consumables as consumables_count', 'components as components_count', 'users as users_count'); + $companies = Company::withCount(['assets as assets_count' => function ($query) { + $query->AssetsForShow(); + }])->withCount('licenses as licenses_count', 'accessories as accessories_count', 'consumables as consumables_count', 'components as components_count', 'users as users_count'); if ($request->filled('search')) { $companies->TextSearch($request->input('search'));