A free open source IT asset/license management system
Added tests
snipef390eac1531c
diff --git a/tests/Feature/Fmcs/AdvancedSearchFmcsScopingTest.php b/tests/Feature/Fmcs/AdvancedSearchFmcsScopingTest.phpnew file mode 100644index 0000000000..cbdb152408--- /dev/null+++ b/tests/Feature/Fmcs/AdvancedSearchFmcsScopingTest.php@@ -0,0 +1,226 @@+<?php++namespace Tests\Feature\Fmcs;++use App\Models\Asset;+use App\Models\Company;+use App\Models\Location;+use App\Models\User;+use Tests\TestCase;++/**+ * Cross-tenant tests for the advanced-search filter payload on Users and+ * Hardware under all three FMCS modes (strict, floater, location-scoped).+ *+ * Users is called out separately because User-to-Company resolves+ * through the company_user pivot rather than the companyable_id column+ * every other model uses.+ */+class AdvancedSearchFmcsScopingTest extends TestCase+{+ protected Company $companyA;++ protected Company $companyB;++ protected function setUp(): void+ {+ parent::setUp();+ [$this->companyA, $this->companyB] = Company::factory()->count(2)->create();+ }++ /**+ * Build a non-superuser caller pinned to company A with the requested+ * view permissions. Mirrors HttpCrossCompanyLeakTest's helper so the+ * caller shape is identical across both suites.+ */+ private function callerScopedToCompanyA(array $viewPermissionKeys): User+ {+ $permissions = [];+ foreach ($viewPermissionKeys as $key) {+ $permissions[$key.'.view'] = '1';+ }++ return $this->companyA->users()->save(+ User::factory()->make(['permissions' => json_encode($permissions)])+ );+ }++ private function idsFromResponse(string $endpoint, array $query, User $caller): array+ {+ return collect(+ $this->actingAsForApi($caller)+ ->getJson($endpoint.'?'.http_build_query($query))+ ->assertOk()+ ->json('rows')+ )->pluck('id')->all();+ }++ // ----- Users -----++ public function test_users_location_filter_cannot_see_other_company_user_under_strict_fmcs(): void+ {+ $this->settings->enableMultipleFullCompanySupport();+ $this->settings->disableFloaterMode();++ $location = Location::factory()->create();+ $this->companyB->users()->save(+ User::factory()->make(['location_id' => $location->id, 'first_name' => 'Hidden'])+ );+ $caller = $this->callerScopedToCompanyA(['users']);++ $ids = $this->idsFromResponse(route('api.users.index'), [+ 'filter' => json_encode(['location' => (string) $location->id]),+ ], $caller);++ $this->assertNotContains(+ User::where('first_name', 'Hidden')->value('id'),+ $ids,+ 'Company B user at the filtered location leaked to a company A caller under strict FMCS.'+ );+ }++ public function test_users_location_filter_cannot_see_other_company_user_under_floater_fmcs(): void+ {+ $this->settings->enableFloaterMode();++ $location = Location::factory()->create();+ $this->companyB->users()->save(+ User::factory()->make(['location_id' => $location->id, 'first_name' => 'Hidden'])+ );+ $caller = $this->callerScopedToCompanyA(['users']);++ $ids = $this->idsFromResponse(route('api.users.index'), [+ 'filter' => json_encode(['location' => (string) $location->id]),+ ], $caller);++ $this->assertNotContains(+ User::where('first_name', 'Hidden')->value('id'),+ $ids,+ 'Company B user at the filtered location leaked to a company A caller under floater FMCS.'+ );+ }++ public function test_users_companies_filter_cannot_name_other_company_under_strict_fmcs(): void+ {+ // The companies filter runs through the company_user pivot via+ // applyCustomFilter. A caller in company A asking for company B+ // explicitly should still see zero rows because the base pivot+ // scope limits the universe to users in company A.+ $this->settings->enableMultipleFullCompanySupport();+ $this->settings->disableFloaterMode();++ $this->companyB->users()->save(User::factory()->make(['first_name' => 'Hidden']));+ $caller = $this->callerScopedToCompanyA(['users']);++ $ids = $this->idsFromResponse(route('api.users.index'), [+ 'filter' => json_encode(['companies' => (string) $this->companyB->id]),+ ], $caller);++ $this->assertNotContains(+ User::where('first_name', 'Hidden')->value('id'),+ $ids,+ 'Hostile companies=B filter leaked a company B user to a company A caller.'+ );+ }++ public function test_users_location_filter_cannot_see_cross_company_user_under_location_scoping(): void+ {+ $this->settings->enableScopedLocationsWithFullMultipleCompanySupport();+ $this->settings->disableFloaterMode();++ $location = Location::factory()->create();+ $this->companyB->users()->save(+ User::factory()->make(['location_id' => $location->id, 'first_name' => 'Hidden'])+ );+ $caller = $this->callerScopedToCompanyA(['users']);++ $ids = $this->idsFromResponse(route('api.users.index'), [+ 'filter' => json_encode(['location' => (string) $location->id]),+ ], $caller);++ $this->assertNotContains(+ User::where('first_name', 'Hidden')->value('id'),+ $ids,+ 'Company B user leaked to a company A caller under location-scoped FMCS.'+ );+ }++ // ----- Hardware (Asset) -----++ public function test_hardware_company_filter_cannot_name_other_company_under_strict_fmcs(): void+ {+ $this->settings->enableMultipleFullCompanySupport();+ $this->settings->disableFloaterMode();++ $hiddenAsset = Asset::factory()->for($this->companyB)->create();+ $caller = $this->callerScopedToCompanyA(['assets']);++ $ids = $this->idsFromResponse(route('api.assets.index'), [+ 'filter' => json_encode(['company' => (string) $this->companyB->id]),+ ], $caller);++ $this->assertNotContains(+ $hiddenAsset->id,+ $ids,+ 'Hostile company=B filter leaked a company B asset to a company A caller under strict FMCS.'+ );+ }++ public function test_hardware_company_filter_cannot_name_other_company_under_floater_fmcs(): void+ {+ $this->settings->enableFloaterMode();++ $hiddenAsset = Asset::factory()->for($this->companyB)->create();+ $caller = $this->callerScopedToCompanyA(['assets']);++ $ids = $this->idsFromResponse(route('api.assets.index'), [+ 'filter' => json_encode(['company' => (string) $this->companyB->id]),+ ], $caller);++ $this->assertNotContains(+ $hiddenAsset->id,+ $ids,+ 'Hostile company=B filter leaked a company B asset to a company A caller under floater FMCS.'+ );+ }++ public function test_hardware_location_filter_cannot_see_other_company_asset_under_strict_fmcs(): void+ {+ $this->settings->enableMultipleFullCompanySupport();+ $this->settings->disableFloaterMode();++ $location = Location::factory()->create();+ $hiddenAsset = Asset::factory()->for($this->companyB)->create(['location_id' => $location->id]);+ $caller = $this->callerScopedToCompanyA(['assets']);++ $ids = $this->idsFromResponse(route('api.assets.index'), [+ 'filter' => json_encode(['location' => (string) $location->id]),+ ], $caller);++ $this->assertNotContains(+ $hiddenAsset->id,+ $ids,+ 'Company B asset at the filtered location leaked to a company A caller under strict FMCS.'+ );+ }++ public function test_hardware_location_filter_cannot_see_cross_company_asset_under_location_scoping(): void+ {+ $this->settings->enableScopedLocationsWithFullMultipleCompanySupport();+ $this->settings->disableFloaterMode();++ $location = Location::factory()->create();+ $hiddenAsset = Asset::factory()->for($this->companyB)->create(['location_id' => $location->id]);+ $caller = $this->callerScopedToCompanyA(['assets']);++ $ids = $this->idsFromResponse(route('api.assets.index'), [+ 'filter' => json_encode(['location' => (string) $location->id]),+ ], $caller);++ $this->assertNotContains(+ $hiddenAsset->id,+ $ids,+ 'Company B asset leaked to a company A caller under location-scoped FMCS.'+ );+ }+}diff --git a/tests/Feature/SavedSearches/Api/SavedSearchStoreTest.php b/tests/Feature/SavedSearches/Api/SavedSearchStoreTest.phpnew file mode 100644index 0000000000..12a06e2568--- /dev/null+++ b/tests/Feature/SavedSearches/Api/SavedSearchStoreTest.php@@ -0,0 +1,153 @@+<?php++namespace Tests\Feature\SavedSearches\Api;++use App\Models\Accessory;+use App\Models\SavedSearch;+use App\Models\User;+use PHPUnit\Framework\Attributes\DataProvider;+use Tests\TestCase;++/**+ * Regression coverage for the two bits of saved-search behavior that+ * are most likely to break in a future refactor:+ *+ * 1. Name uniqueness is per-user, not global.+ * 2. `model` metadata must be a loadable Eloquent subclass.+ */+class SavedSearchStoreTest extends TestCase+{+ public function test_two_users_can_use_the_same_name_on_the_same_listing(): void+ {+ $adminA = User::factory()->create();+ $adminB = User::factory()->create();++ $this->actingAsForApi($adminA)+ ->postJson(route('api.saved-searches.store'), [+ 'table_name' => 'accessoriesListingTable',+ 'name' => 'Weekly audit',+ 'query_string' => 'filter[category]=1',+ ])+ ->assertCreated();++ $this->actingAsForApi($adminB)+ ->postJson(route('api.saved-searches.store'), [+ 'table_name' => 'accessoriesListingTable',+ 'name' => 'Weekly audit',+ 'query_string' => 'filter[category]=2',+ ])+ ->assertCreated();++ $this->assertSame(1, SavedSearch::where('created_by', $adminA->id)->count());+ $this->assertSame(1, SavedSearch::where('created_by', $adminB->id)->count());+ }++ public function test_same_user_cannot_reuse_a_name_on_the_same_listing(): void+ {+ $admin = User::factory()->create();++ $this->actingAsForApi($admin)+ ->postJson(route('api.saved-searches.store'), [+ 'table_name' => 'accessoriesListingTable',+ 'name' => 'Weekly audit',+ 'query_string' => 'filter[category]=1',+ ])+ ->assertCreated();++ $this->actingAsForApi($admin)+ ->postJson(route('api.saved-searches.store'), [+ 'table_name' => 'accessoriesListingTable',+ 'name' => 'Weekly audit',+ 'query_string' => 'filter[category]=99',+ ])+ ->assertStatus(422);++ $this->assertSame(1, SavedSearch::where('created_by', $admin->id)->count());+ }++ public function test_same_user_can_reuse_a_name_on_a_different_listing(): void+ {+ $admin = User::factory()->create();++ $this->actingAsForApi($admin)+ ->postJson(route('api.saved-searches.store'), [+ 'table_name' => 'accessoriesListingTable',+ 'name' => 'Weekly audit',+ 'query_string' => 'filter[category]=1',+ ])+ ->assertCreated();++ $this->actingAsForApi($admin)+ ->postJson(route('api.saved-searches.store'), [+ 'table_name' => 'assetsListingTable',+ 'name' => 'Weekly audit',+ 'query_string' => 'filter[category]=1',+ ])+ ->assertCreated();++ $this->assertSame(2, SavedSearch::where('created_by', $admin->id)->count());+ }++ public function test_model_metadata_accepts_a_real_eloquent_class(): void+ {+ $admin = User::factory()->create();++ $this->actingAsForApi($admin)+ ->postJson(route('api.saved-searches.store'), [+ 'table_name' => 'accessoriesListingTable',+ 'model' => Accessory::class,+ 'name' => 'Weekly audit',+ 'query_string' => 'filter[category]=1',+ ])+ ->assertCreated();++ $this->assertSame(Accessory::class, SavedSearch::first()->model);+ }++ public function test_model_metadata_accepts_null(): void+ {+ $admin = User::factory()->create();++ $this->actingAsForApi($admin)+ ->postJson(route('api.saved-searches.store'), [+ 'table_name' => 'accessoriesListingTable',+ 'model' => null,+ 'name' => 'Weekly audit',+ 'query_string' => 'filter[category]=1',+ ])+ ->assertCreated();++ $this->assertNull(SavedSearch::first()->model);+ }++ #[DataProvider('provideInvalidModelStrings')]+ public function test_model_metadata_rejects_non_eloquent_strings(string $invalid): void+ {+ $admin = User::factory()->create();++ $this->actingAsForApi($admin)+ ->postJson(route('api.saved-searches.store'), [+ 'table_name' => 'accessoriesListingTable',+ 'model' => $invalid,+ 'name' => 'Weekly audit',+ 'query_string' => 'filter[category]=1',+ ])+ ->assertStatus(422)+ ->assertJsonPath('status', 'error')+ ->assertJsonStructure(['messages' => ['model']]);++ $this->assertSame(0, SavedSearch::count());+ }++ public static function provideInvalidModelStrings(): array+ {+ return [+ 'nonexistent class' => ['App\\Models\\DoesNotExist'],+ 'not a class at all' => ['<script>alert(1)</script>'],+ // Random non-Eloquent FQCN that IS loadable. Guards+ // against a future weakening of the subclass check to+ // just `class_exists`.+ 'non-eloquent class' => ['Illuminate\\Support\\Str'],+ ];+ }+}