Skip to content

A free open source IT asset/license management system

Updated tests for new API endpoints

snipecc5f32352c81
diff --git a/tests/Feature/Users/Api/UserAccessoriesListTest.php b/tests/Feature/Users/Api/UserAccessoriesListTest.php
new file mode 100644
index 0000000000..f41c1a627a
--- /dev/null
+++ b/tests/Feature/Users/Api/UserAccessoriesListTest.php
@@ -0,0 +1,145 @@
+<?php
+
+namespace Tests\Feature\Users\Api;
+
+use App\Models\Accessory;
+use App\Models\User;
+use Illuminate\Support\Facades\DB;
+use Tests\TestCase;
+
+class UserAccessoriesListTest extends TestCase
+{
+ public function test_endpoint_requires_view_permission(): void
+ {
+ $user = User::factory()->create();
+
+ $this->actingAsForApi(User::factory()->create())
+ ->getJson(route('api.users.accessorieslist', ['user' => $user->id]))
+ ->assertForbidden();
+ }
+
+ public function test_endpoint_returns_one_row_per_pivot_entry(): void
+ {
+ $user = User::factory()->create();
+ $keyboards = Accessory::factory()->create(['name' => 'Keyboards']);
+ $mice = Accessory::factory()->create(['name' => 'Mice']);
+
+ DB::table('accessories_checkout')->insert([
+ ['accessory_id' => $keyboards->id, 'assigned_to' => $user->id, 'assigned_type' => User::class, 'created_at' => now(), 'note' => 'first'],
+ ['accessory_id' => $keyboards->id, 'assigned_to' => $user->id, 'assigned_type' => User::class, 'created_at' => now(), 'note' => 'second'],
+ ['accessory_id' => $mice->id, 'assigned_to' => $user->id, 'assigned_type' => User::class, 'created_at' => now(), 'note' => 'third'],
+ ]);
+
+ $response = $this->actingAsForApi(User::factory()->viewUsers()->viewAccessories()->create())
+ ->getJson(route('api.users.accessorieslist', ['user' => $user->id]))
+ ->assertOk()
+ ->json();
+
+ $this->assertSame(3, $response['total']);
+ $this->assertCount(3, $response['rows']);
+ }
+
+ public function test_endpoint_paginates_and_reports_metadata(): void
+ {
+ $user = User::factory()->create();
+ $accessory = Accessory::factory()->create();
+ $rows = array_fill(0, 5, [
+ 'accessory_id' => $accessory->id,
+ 'assigned_to' => $user->id,
+ 'assigned_type' => User::class,
+ 'created_at' => now(),
+ ]);
+ DB::table('accessories_checkout')->insert($rows);
+
+ $response = $this->actingAsForApi(User::factory()->viewUsers()->viewAccessories()->create())
+ ->getJson(route('api.users.accessorieslist', ['user' => $user->id, 'limit' => 2, 'page' => 2]))
+ ->assertOk()
+ ->json();
+
+ $this->assertSame(5, $response['total']);
+ $this->assertCount(2, $response['rows']);
+ $this->assertSame(2, $response['per_page']);
+ $this->assertSame(2, $response['current_page']);
+ $this->assertSame(3, $response['total_pages']);
+ }
+
+ public function test_search_filters_by_accessory_name_and_pivot_note(): void
+ {
+ $user = User::factory()->create();
+ $keyboards = Accessory::factory()->create(['name' => 'USB Keyboards']);
+ $mice = Accessory::factory()->create(['name' => 'Wireless Mice']);
+
+ DB::table('accessories_checkout')->insert([
+ ['accessory_id' => $keyboards->id, 'assigned_to' => $user->id, 'assigned_type' => User::class, 'created_at' => now(), 'note' => 'onboarding'],
+ ['accessory_id' => $mice->id, 'assigned_to' => $user->id, 'assigned_type' => User::class, 'created_at' => now(), 'note' => 'replacement'],
+ ]);
+
+ $caller = User::factory()->viewUsers()->viewAccessories()->create();
+
+ $byName = $this->actingAsForApi($caller)
+ ->getJson(route('api.users.accessorieslist', ['user' => $user->id, 'search' => 'Wireless']))
+ ->assertOk()
+ ->json();
+ $this->assertSame(1, $byName['total']);
+
+ $byNote = $this->actingAsForApi($caller)
+ ->getJson(route('api.users.accessorieslist', ['user' => $user->id, 'search' => 'onboarding']))
+ ->assertOk()
+ ->json();
+ $this->assertSame(1, $byNote['total']);
+ }
+
+ public function test_endpoint_resolves_last_unit_cost_in_one_grouped_query(): void
+ {
+ $user = User::factory()->create();
+ $accessory = Accessory::factory()->create(['default_purchase_cost' => '0.10']);
+ $orderId = DB::table('orders')->insertGetId([
+ 'created_at' => now(),
+ 'updated_at' => now(),
+ ]);
+ DB::table('order_items')->insert([
+ 'order_id' => $orderId,
+ 'item_type' => Accessory::class,
+ 'item_id' => $accessory->id,
+ 'price' => '0.25',
+ 'qty' => 1,
+ 'created_at' => now(),
+ 'updated_at' => now(),
+ ]);
+
+ $rows = array_fill(0, 20, [
+ 'accessory_id' => $accessory->id,
+ 'assigned_to' => $user->id,
+ 'assigned_type' => User::class,
+ 'created_at' => now(),
+ ]);
+ DB::table('accessories_checkout')->insert($rows);
+
+ DB::enableQueryLog();
+ try {
+ $response = $this->actingAsForApi(User::factory()->viewUsers()->viewAccessories()->create())
+ ->getJson(route('api.users.accessorieslist', ['user' => $user->id]))
+ ->assertOk()
+ ->json();
+ } finally {
+ $queries = DB::getQueryLog();
+ DB::disableQueryLog();
+ }
+
+ $orderItemQueries = array_filter(
+ $queries,
+ fn ($q) => str_contains(strtolower($q['query']), 'order_items'),
+ );
+ $this->assertLessThanOrEqual(
+ 2,
+ count($orderItemQueries),
+ 'lastUnitCostsFor must hit order_items at most twice regardless of pivot count',
+ );
+
+ $this->assertCount(20, $response['rows']);
+ foreach ($response['rows'] as $row) {
+ $this->assertNotNull($row['purchase_cost']);
+ $this->assertSame((int) $accessory->id, $row['accessory']['id']);
+ }
+ }
+}
diff --git a/tests/Feature/Users/Api/UserConsumablesListTest.php b/tests/Feature/Users/Api/UserConsumablesListTest.php
index 1e6e420937..7d81c8ebb4 100644
--- a/tests/Feature/Users/Api/UserConsumablesListTest.php
+++ b/tests/Feature/Users/Api/UserConsumablesListTest.php
@@ -76,6 +76,38 @@ class UserConsumablesListTest extends TestCase
$this->assertNotNull($response['prev_page_url']);
}
+ public function test_search_filters_by_consumable_name_and_pivot_note(): void
+ {
+ $user = User::factory()->create();
+ $pens = Consumable::factory()->create(['name' => 'Ballpoint Pens']);
+ $paper = Consumable::factory()->create(['name' => 'Copier Paper']);
+
+ DB::table('consumables_users')->insert([
+ ['consumable_id' => $pens->id, 'assigned_to' => $user->id, 'created_at' => now(), 'note' => 'urgent'],
+ ['consumable_id' => $paper->id, 'assigned_to' => $user->id, 'created_at' => now(), 'note' => 'routine'],
+ ]);
+
+ $caller = User::factory()->viewUsers()->viewConsumables()->create();
+
+ $byName = $this->actingAsForApi($caller)
+ ->getJson(route('api.users.consumableslist', ['user' => $user->id, 'search' => 'Ballpoint']))
+ ->assertOk()
+ ->json();
+ $this->assertSame(1, $byName['total']);
+
+ $byNote = $this->actingAsForApi($caller)
+ ->getJson(route('api.users.consumableslist', ['user' => $user->id, 'search' => 'urgent']))
+ ->assertOk()
+ ->json();
+ $this->assertSame(1, $byNote['total']);
+
+ $miss = $this->actingAsForApi($caller)
+ ->getJson(route('api.users.consumableslist', ['user' => $user->id, 'search' => 'zzz-nomatch']))
+ ->assertOk()
+ ->json();
+ $this->assertSame(0, $miss['total']);
+ }
+
public function test_endpoint_resolves_last_unit_cost_in_one_grouped_query(): void
{
// Regression test for the reason this endpoint exists at all.
diff --git a/tests/Feature/Users/Api/UserLicensesListTest.php b/tests/Feature/Users/Api/UserLicensesListTest.php
new file mode 100644
index 0000000000..e6e92b802d
--- /dev/null
+++ b/tests/Feature/Users/Api/UserLicensesListTest.php
@@ -0,0 +1,157 @@
+<?php
+
+namespace Tests\Feature\Users\Api;
+
+use App\Models\License;
+use App\Models\LicenseSeat;
+use App\Models\User;
+use Illuminate\Support\Facades\DB;
+use Tests\TestCase;
+
+class UserLicensesListTest extends TestCase
+{
+ public function test_endpoint_requires_view_permission(): void
+ {
+ $user = User::factory()->create();
+
+ $this->actingAsForApi(User::factory()->create())
+ ->getJson(route('api.users.licenselist', ['user' => $user->id]))
+ ->assertForbidden();
+ }
+
+ public function test_endpoint_returns_one_row_per_seat_assignment(): void
+ {
+ $user = User::factory()->create();
+ $officeSuite = License::factory()->create(['name' => 'Office Suite', 'seats' => 5]);
+ $editor = License::factory()->create(['name' => 'Editor', 'seats' => 5]);
+
+ LicenseSeat::factory()->count(2)->for($officeSuite)->assignedToUser($user)->create();
+ LicenseSeat::factory()->for($editor)->assignedToUser($user)->create();
+
+ $response = $this->actingAsForApi(User::factory()->viewUsers()->viewLicenses()->create())
+ ->getJson(route('api.users.licenselist', ['user' => $user->id]))
+ ->assertOk()
+ ->json();
+
+ $this->assertSame(3, $response['total']);
+ $this->assertCount(3, $response['rows']);
+ }
+
+ public function test_endpoint_paginates_and_reports_metadata(): void
+ {
+ $user = User::factory()->create();
+ $license = License::factory()->create(['seats' => 20]);
+ LicenseSeat::factory()->count(5)->for($license)->assignedToUser($user)->create();
+
+ $response = $this->actingAsForApi(User::factory()->viewUsers()->viewLicenses()->create())
+ ->getJson(route('api.users.licenselist', ['user' => $user->id, 'limit' => 2, 'page' => 2]))
+ ->assertOk()
+ ->json();
+
+ $this->assertSame(5, $response['total']);
+ $this->assertCount(2, $response['rows']);
+ $this->assertSame(2, $response['per_page']);
+ $this->assertSame(2, $response['current_page']);
+ $this->assertSame(3, $response['total_pages']);
+ }
+
+ public function test_row_shape_exposes_seat_id_and_license_metadata(): void
+ {
+ $user = User::factory()->create();
+ $license = License::factory()->create([
+ 'name' => 'Adobe Creative Cloud',
+ 'purchase_cost' => '600.00',
+ 'purchase_order' => 'PO-77',
+ 'order_number' => 'ORDER-42',
+ 'seats' => 3,
+ ]);
+ $seat = LicenseSeat::factory()->for($license)->assignedToUser($user)->create();
+
+ $response = $this->actingAsForApi(User::factory()->viewUsers()->viewLicenses()->create())
+ ->getJson(route('api.users.licenselist', ['user' => $user->id]))
+ ->assertOk()
+ ->json();
+
+ $row = $response['rows'][0];
+ $this->assertSame((int) $seat->id, $row['id']);
+ $this->assertSame((int) $license->id, $row['license']['id']);
+ $this->assertSame('Adobe Creative Cloud', $row['license']['name']);
+ $this->assertSame('PO-77', $row['purchase_order']);
+ $this->assertSame('ORDER-42', $row['order_number']);
+ }
+
+ public function test_search_matches_license_name_purchase_order_and_order_number(): void
+ {
+ $user = User::factory()->create();
+ $adobe = License::factory()->create(['name' => 'Adobe CC', 'purchase_order' => 'PO-ADOBE-1', 'order_number' => 'ADOBE-ORD-42', 'seats' => 3]);
+ $office = License::factory()->create(['name' => 'MS Office', 'purchase_order' => 'PO-MS-2', 'order_number' => 'MS-ORD-99', 'seats' => 3]);
+
+ LicenseSeat::factory()->for($adobe)->assignedToUser($user)->create();
+ LicenseSeat::factory()->for($office)->assignedToUser($user)->create();
+
+ $caller = User::factory()->viewUsers()->viewLicenses()->create();
+
+ $byName = $this->actingAsForApi($caller)
+ ->getJson(route('api.users.licenselist', ['user' => $user->id, 'search' => 'Adobe']))
+ ->assertOk()
+ ->json();
+ $this->assertSame(1, $byName['total']);
+
+ $byPO = $this->actingAsForApi($caller)
+ ->getJson(route('api.users.licenselist', ['user' => $user->id, 'search' => 'PO-MS']))
+ ->assertOk()
+ ->json();
+ $this->assertSame(1, $byPO['total']);
+
+ $byOrder = $this->actingAsForApi($caller)
+ ->getJson(route('api.users.licenselist', ['user' => $user->id, 'search' => 'ADOBE-ORD']))
+ ->assertOk()
+ ->json();
+ $this->assertSame(1, $byOrder['total']);
+ }
+
+ public function test_search_includes_serial_only_for_callers_with_view_keys(): void
+ {
+ $user = User::factory()->create();
+ $license = License::factory()->create(['name' => 'Adobe CC', 'serial' => 'SECRET-KEY-ABC', 'seats' => 3]);
+ LicenseSeat::factory()->for($license)->assignedToUser($user)->create();
+
+ $withoutKeys = User::factory()->viewUsers()->viewLicenses()->create();
+ $withoutKeysResult = $this->actingAsForApi($withoutKeys)
+ ->getJson(route('api.users.licenselist', ['user' => $user->id, 'search' => 'SECRET-KEY']))
+ ->assertOk()
+ ->json();
+ $this->assertSame(0, $withoutKeysResult['total'], 'Callers without viewKeys must not be able to filter licenses by serial via search');
+
+ $withKeys = User::factory()->superuser()->create();
+ $withKeysResult = $this->actingAsForApi($withKeys)
+ ->getJson(route('api.users.licenselist', ['user' => $user->id, 'search' => 'SECRET-KEY']))
+ ->assertOk()
+ ->json();
+ $this->assertSame(1, $withKeysResult['total']);
+ }
+
+ public function test_endpoint_ignores_seats_assigned_to_other_users(): void
+ {
+ $target = User::factory()->create();
+ $bystander = User::factory()->create();
+ $license = License::factory()->create(['seats' => 5]);
+
+ LicenseSeat::factory()->for($license)->assignedToUser($target)->create();
+ LicenseSeat::factory()->count(3)->for($license)->assignedToUser($bystander)->create();
+ DB::table('license_seats')->insert([
+ 'license_id' => $license->id,
+ 'assigned_to' => null,
+ 'asset_id' => null,
+ 'created_at' => now(),
+ 'updated_at' => now(),
+ ]);
+
+ $response = $this->actingAsForApi(User::factory()->viewUsers()->viewLicenses()->create())
+ ->getJson(route('api.users.licenselist', ['user' => $target->id]))
+ ->assertOk()
+ ->json();
+
+ $this->assertSame(1, $response['total']);
+ }
+}