A free open source IT asset/license management system
Updated tests
snipe7c8e1d0c3507
diff --git a/tests/Feature/Dashboard/Api/ActivityWidgetTest.php b/tests/Feature/Dashboard/Api/ActivityWidgetTest.phpindex ae828bb38d..9753c3b70c 100644--- a/tests/Feature/Dashboard/Api/ActivityWidgetTest.php+++ b/tests/Feature/Dashboard/Api/ActivityWidgetTest.php@@ -5,6 +5,7 @@ namespace Tests\Feature\Dashboard\Api;use App\Models\Accessory;use App\Models\Actionlog;use App\Models\Asset;+use App\Models\Component;use App\Models\User;use Laravel\Passport\Passport;use Tests\TestCase;@@ -78,9 +79,13 @@ class ActivityWidgetTest extends TestCase->assertSee('ASSET_ROW_SENTINEL');}- public function test_limit_caps_at_fifty_regardless_of_query_string()+ public function test_limit_caps_regardless_of_query_string(){- Actionlog::factory()->count(60)->create([+ // The widget defaults to 25. The report page (which also+ // reads from this endpoint for scoped viewers) can page+ // deeper. Cap is 500. A hostile caller asking for 10k rows+ // is still capped.+ Actionlog::factory()->count(510)->create(['item_type' => Accessory::class,'item_id' => Accessory::factory()->create()->id,'action_type' => 'checkout',@@ -88,19 +93,21 @@ class ActivityWidgetTest extends TestCasePassport::actingAs(User::factory()->viewAccessories()->create());- $rows = $this->getJson(route('api.dashboard.activity', ['limit' => 500]))+ $rows = $this->getJson(route('api.dashboard.activity', ['limit' => 10_000]))->assertOk()->json('rows');- $this->assertLessThanOrEqual(50, count($rows));+ $this->assertLessThanOrEqual(500, count($rows));}- public function test_search_and_filter_params_do_not_narrow_the_result_set()+ public function test_per_attribute_probe_params_are_ignored(){- // Probe attempt: scoped viewer trying to run a note-text search- // through the dashboard endpoint. The widget does not need it,- // so the endpoint ignores it. Same-caller results should be- // identical with and without the probe.+ // Probe attempt: scoped viewer trying to narrow to a+ // specific action_type / created_by / filter attribute. The+ // widget does not need those, so the endpoint ignores them.+ // Same-caller totals should be identical with and without+ // the probe. Free-text `search` is deliberately excluded from+ // this test since it is supported (see the search test below).$accessory = Accessory::factory()->create();Actionlog::factory()->count(3)->create(['item_type' => Accessory::class,@@ -114,14 +121,109 @@ class ActivityWidgetTest extends TestCase->assertOk()->json('total');$withProbe = $this->getJson(route('api.dashboard.activity', [- 'search' => 'never-matches-anything','filter' => 'never-matches-anything','action_type' => 'delete','created_by' => 999_999,+ 'action_source' => 'never-matches',+ 'remote_ip' => '9.9.9.9',+ 'item_type' => 'App\Models\Asset',]))->assertOk()->json('total');$this->assertSame($unfiltered, $withProbe);}++ public function test_search_narrows_within_viewable_scope()+ {+ // Search runs against Actionlog's Searchable trait AFTER+ // the viewable-type whereIn filter, so a scoped viewer+ // cannot use it to reach rows they were not already+ // allowed to see. Also proves search actually works+ // (the on-page UX would be broken if search were a no-op).+ $accessory = Accessory::factory()->create();++ Actionlog::factory()->create([+ 'item_type' => Accessory::class,+ 'item_id' => $accessory->id,+ 'action_type' => 'checkout',+ 'note' => 'MATCH_ME_SPECIFICALLY',+ ]);+ Actionlog::factory()->create([+ 'item_type' => Accessory::class,+ 'item_id' => $accessory->id,+ 'action_type' => 'checkout',+ 'note' => 'other note',+ ]);++ Passport::actingAs(User::factory()->viewAccessories()->create());++ $matched = $this->getJson(route('api.dashboard.activity', ['search' => 'MATCH_ME_SPECIFICALLY']))+ ->assertOk()+ ->json('total');+ $this->assertSame(1, $matched);+ }++ public function test_search_matches_target_asset_tag_for_component_checkout_row()+ {+ // Regression pin for the Actionlog target-search gap:+ // component checkouts store item=Component + target=Asset,+ // and the trait's searchRelations only walks item_id. The+ // Actionlog::advancedTextSearch() override adds a+ // polymorphic target search so an asset tag entered in the+ // search box finds component-checkout rows targeted at that+ // asset. Uses a positive-membership assertion because+ // Actionlog's built-in searchRelations may also match the+ // asset's own create log via the item_id-keyed relations,+ // and both matches are legitimate.+ $component = Component::factory()->create();+ $asset = Asset::factory()->create(['asset_tag' => 'TAG-DASH-9977']);++ $checkoutLog = Actionlog::factory()->create([+ 'item_type' => Component::class,+ 'item_id' => $component->id,+ 'target_type' => Asset::class,+ 'target_id' => $asset->id,+ 'action_type' => 'checkout',+ ]);++ Passport::actingAs(User::factory()->viewComponents()->create());++ $ids = collect($this->getJson(route('api.dashboard.activity', ['search' => 'TAG-DASH-9977']))+ ->assertOk()+ ->json('rows'))+ ->pluck('id')+ ->all();++ $this->assertContains($checkoutLog->id, $ids);+ }++ public function test_search_cannot_reach_rows_the_caller_could_not_view_unfiltered()+ {+ // The type-filter runs before search, so a scoped viewer+ // searching for a substring that matches an Asset-typed row+ // still gets zero results.+ $asset = Asset::factory()->create();+ $accessory = Accessory::factory()->create();++ Actionlog::factory()->create([+ 'item_type' => Asset::class,+ 'item_id' => $asset->id,+ 'action_type' => 'checkout',+ 'note' => 'CROSS_TYPE_SENTINEL',+ ]);+ Actionlog::factory()->create([+ 'item_type' => Accessory::class,+ 'item_id' => $accessory->id,+ 'action_type' => 'checkout',+ 'note' => 'other note',+ ]);++ Passport::actingAs(User::factory()->viewAccessories()->create());++ $matched = $this->getJson(route('api.dashboard.activity', ['search' => 'CROSS_TYPE_SENTINEL']))+ ->assertOk()+ ->json('total');+ $this->assertSame(0, $matched);+ }}diff --git a/tests/Feature/Dashboard/Api/CategoriesWidgetTest.php b/tests/Feature/Dashboard/Api/CategoriesWidgetTest.phpindex c1453e1944..434f413b79 100644--- a/tests/Feature/Dashboard/Api/CategoriesWidgetTest.php+++ b/tests/Feature/Dashboard/Api/CategoriesWidgetTest.php@@ -71,6 +71,20 @@ class CategoriesWidgetTest extends TestCase$this->assertFalse($row['available_actions']['view']);}+ public function test_stale_sort_column_for_type_the_viewer_cannot_see_falls_back()+ {+ // See LocationsWidgetTest for full rationale. localStorage-+ // persisted bs-table sort state can name a *_count column the+ // current caller does not have. Falling back to name keeps+ // the widget rendering.+ Category::factory()->count(2)->create(['category_type' => 'component']);++ Passport::actingAs(User::factory()->viewComponents()->create());++ $this->getJson(route('api.dashboard.categories', ['sort' => 'accessories_count', 'order' => 'desc']))+ ->assertOk();+ }+public function test_search_and_filter_params_are_ignored(){Category::factory()->count(3)->create(['category_type' => 'accessory']);diff --git a/tests/Feature/Dashboard/Api/CompaniesWidgetTest.php b/tests/Feature/Dashboard/Api/CompaniesWidgetTest.phpindex 6569daa955..36103244d5 100644--- a/tests/Feature/Dashboard/Api/CompaniesWidgetTest.php+++ b/tests/Feature/Dashboard/Api/CompaniesWidgetTest.php@@ -54,8 +54,10 @@ class CompaniesWidgetTest extends TestCase// widening that leaks past what the widget renders. Extend// this list intentionally when adding a widget column, not// accidentally by extending the transformer.- $allowed = ['id', 'name', 'available_actions', 'accessories_count'];- $this->assertSame(sort($allowed) ? $allowed : $allowed, array_values(collect(array_keys($row))->sort()->values()->all()));+ $allowed = ['id', 'name', 'tag_color', 'available_actions', 'accessories_count'];+ sort($allowed);+ $actual = collect(array_keys($row))->sort()->values()->all();+ $this->assertSame($allowed, $actual);$this->assertFalse($row['available_actions']['view']);}@@ -72,6 +74,17 @@ class CompaniesWidgetTest extends TestCase$this->assertTrue($row['available_actions']['view']);}+ public function test_stale_sort_column_for_type_the_viewer_cannot_see_falls_back()+ {+ // See LocationsWidgetTest for full rationale.+ Company::factory()->count(2)->create();++ Passport::actingAs(User::factory()->viewComponents()->create());++ $this->getJson(route('api.dashboard.companies', ['sort' => 'accessories_count', 'order' => 'desc']))+ ->assertOk();+ }+public function test_search_and_filter_params_are_ignored(){Company::factory()->count(3)->create();diff --git a/tests/Feature/Dashboard/Api/LocationsWidgetTest.php b/tests/Feature/Dashboard/Api/LocationsWidgetTest.phpindex 055feae547..aa0ab8605e 100644--- a/tests/Feature/Dashboard/Api/LocationsWidgetTest.php+++ b/tests/Feature/Dashboard/Api/LocationsWidgetTest.php@@ -67,6 +67,22 @@ class LocationsWidgetTest extends TestCase$this->assertFalse($row['available_actions']['view']);}+ public function test_stale_sort_column_for_type_the_viewer_cannot_see_falls_back()+ {+ // Regression: bs-table persists the last-clicked sort column+ // in localStorage, which can name a *_count field the current+ // caller does not have. Before the allowlist tracked the+ // withCount list, that URL blew up with a 42S22 unknown-column+ // error mid-render. Falling back to the default (name) keeps+ // the widget alive.+ Location::factory()->count(2)->create();++ Passport::actingAs(User::factory()->viewComponents()->create());++ $this->getJson(route('api.dashboard.locations', ['sort' => 'accessories_count', 'order' => 'desc']))+ ->assertOk();+ }+public function test_search_and_filter_params_are_ignored(){Location::factory()->count(3)->create();diff --git a/tests/Feature/Reporting/ActivityReportPageAuthTest.php b/tests/Feature/Reporting/ActivityReportPageAuthTest.phpnew file mode 100644index 0000000000..ff0db466f8--- /dev/null+++ b/tests/Feature/Reporting/ActivityReportPageAuthTest.php@@ -0,0 +1,187 @@+<?php++namespace Tests\Feature\Reporting;++use App\Models\User;+use Tests\TestCase;++/**+ * The activity report page (reports/activity) is reachable by two+ * viewer classes with different UI:+ * - reports.view holders get the full page with CSV export and+ * the general search / filter / sort endpoint.+ * - Scoped viewers (canViewUsersAndCheckoutables, no reports.view)+ * arrive through the dashboard Recent Activity widget's View-all+ * button, land on the same template with admin-shaped UI stripped,+ * and read from the narrow api.dashboard.activity endpoint.+ *+ * These tests pin both paths so a future permission change does not+ * quietly 403 the widget click-through or leak the export button.+ */+class ActivityReportPageAuthTest extends TestCase+{+ public function test_permissionless_user_is_forbidden()+ {+ $this->actingAs(User::factory()->create())+ ->get(route('reports.activity'))+ ->assertForbidden();+ }++ public function test_reports_view_holder_reaches_page()+ {+ $this->actingAs(User::factory()->canViewReports()->create())+ ->get(route('reports.activity'))+ ->assertOk();+ }++ public function test_scoped_viewer_reaches_page()+ {+ $this->actingAs(User::factory()->viewComponents()->create())+ ->get(route('reports.activity'))+ ->assertOk();+ }++ public function test_reports_view_holder_sees_csv_export_button()+ {+ $this->actingAs(User::factory()->canViewReports()->create())+ ->get(route('reports.activity'))+ ->assertOk()+ ->assertSee(route('reports.activity.post'));+ }++ public function test_scoped_viewer_also_sees_csv_export_button()+ {+ // Scoped viewers get an export gated to the same viewable+ // types they see on the page. Filtering happens inside+ // postActivityReport.+ $this->actingAs(User::factory()->viewComponents()->create())+ ->get(route('reports.activity'))+ ->assertOk()+ ->assertSee(route('reports.activity.post'));+ }++ public function test_scoped_viewer_page_points_at_narrow_dashboard_endpoint()+ {+ $this->actingAs(User::factory()->viewComponents()->create())+ ->get(route('reports.activity'))+ ->assertOk()+ ->assertSee(route('api.dashboard.activity'))+ ->assertDontSee(route('api.activity.index'));+ }++ public function test_reports_view_holder_page_points_at_general_endpoint()+ {+ $this->actingAs(User::factory()->canViewReports()->create())+ ->get(route('reports.activity'))+ ->assertOk()+ ->assertSee(route('api.activity.index'));+ }++ public function test_permissionless_user_cannot_hit_csv_export_endpoint()+ {+ $this->actingAs(User::factory()->create())+ ->post(route('reports.activity.post'))+ ->assertForbidden();+ }++ public function test_scoped_viewer_csv_export_only_contains_rows_for_viewable_types()+ {+ // A components-only viewer's CSV must contain rows for+ // Component actionlogs and omit rows for Asset / License /+ // Consumable / Accessory actionlogs. Same viewable-type+ // filter the on-page endpoint applies.+ $component = \App\Models\Component::factory()->create();+ $asset = \App\Models\Asset::factory()->create();++ \App\Models\Actionlog::factory()->create([+ 'item_type' => \App\Models\Component::class,+ 'item_id' => $component->id,+ 'action_type' => 'checkout',+ 'note' => 'COMPONENT_ROW_SENTINEL',+ ]);+ \App\Models\Actionlog::factory()->create([+ 'item_type' => \App\Models\Asset::class,+ 'item_id' => $asset->id,+ 'action_type' => 'checkout',+ 'note' => 'ASSET_ROW_SENTINEL',+ ]);++ $body = $this->actingAs(User::factory()->viewComponents()->create())+ ->post(route('reports.activity.post'))+ ->assertOk()+ ->streamedContent();++ $this->assertStringContainsString('COMPONENT_ROW_SENTINEL', $body);+ $this->assertStringNotContainsString('ASSET_ROW_SENTINEL', $body);+ }++ public function test_reports_view_holder_csv_export_contains_every_type()+ {+ // reports.view holders keep the full unfiltered CSV. Guard+ // against a future refactor that accidentally applies the+ // scoped-viewer filter to admin exports.+ $component = \App\Models\Component::factory()->create();+ $asset = \App\Models\Asset::factory()->create();++ \App\Models\Actionlog::factory()->create([+ 'item_type' => \App\Models\Component::class,+ 'item_id' => $component->id,+ 'action_type' => 'checkout',+ 'note' => 'COMPONENT_ROW_SENTINEL',+ ]);+ \App\Models\Actionlog::factory()->create([+ 'item_type' => \App\Models\Asset::class,+ 'item_id' => $asset->id,+ 'action_type' => 'checkout',+ 'note' => 'ASSET_ROW_SENTINEL',+ ]);++ $body = $this->actingAs(User::factory()->canViewReports()->create())+ ->post(route('reports.activity.post'))+ ->assertOk()+ ->streamedContent();++ $this->assertStringContainsString('COMPONENT_ROW_SENTINEL', $body);+ $this->assertStringContainsString('ASSET_ROW_SENTINEL', $body);+ }++ public function test_scoped_viewer_breadcrumb_does_not_link_to_reports_index()+ {+ // reports.index is admin-shaped. A scoped viewer's activity+ // report breadcrumb must not include a "Reports" crumb whose+ // link would 403 on click. Uses the URL with the closing "+ // so plain substring assertions don't collide with the+ // reports.activity URL (which contains reports.index's URL+ // as a prefix).+ $this->actingAs(User::factory()->viewComponents()->create())+ ->get(route('reports.activity'))+ ->assertOk()+ ->assertDontSee(route('reports.index').'"', false);+ }++ public function test_reports_view_holder_breadcrumb_links_to_reports_index()+ {+ $this->actingAs(User::factory()->canViewReports()->create())+ ->get(route('reports.activity'))+ ->assertOk()+ ->assertSee(route('reports.index').'"', false);+ }++ public function test_scoped_viewer_unaccepted_acceptance_breadcrumb_does_not_link_to_reports_index()+ {+ // Same fix retroactively applied to the acceptances report+ // widened in an earlier batch.+ $this->actingAs(User::factory()->viewComponents()->create())+ ->get(route('reports/unaccepted_assets'))+ ->assertOk()+ ->assertDontSee(route('reports.index').'"', false);+ }++ public function test_reports_view_holder_unaccepted_acceptance_breadcrumb_links_to_reports_index()+ {+ $this->actingAs(User::factory()->canViewReports()->create())+ ->get(route('reports/unaccepted_assets'))+ ->assertOk()+ ->assertSee(route('reports.index').'"', false);+ }+}diff --git a/tests/Feature/Reporting/ActivityReportTest.php b/tests/Feature/Reporting/ActivityReportTest.phpindex 756c98847a..d1836cbdd3 100644--- a/tests/Feature/Reporting/ActivityReportTest.php+++ b/tests/Feature/Reporting/ActivityReportTest.php@@ -223,6 +223,39 @@ class ActivityReportTest extends TestCase->assertStatusMessageIs('error');}+ public function test_search_matches_target_asset_tag_on_component_checkout()+ {+ // Component checkouts store item=Component and target=Asset.+ // Actionlog's $searchableRelations only walks item_id, so+ // searching by the target asset's tag missed these rows+ // before the advancedTextSearch() override on Actionlog.+ // Same fix applied here for the general reports.view path.+ // Positive-membership assertion because the built-in+ // item_id-keyed searchRelations may also match the asset's+ // own create log, and both matches are legitimate.+ $actor = User::factory()->superuser()->create();++ $component = \App\Models\Component::factory()->create();+ $targetAsset = Asset::factory()->create(['asset_tag' => 'TARGET-TAG-4242']);++ $matchingLog = Actionlog::factory()->create([+ 'action_type' => 'checkout',+ 'item_type' => \App\Models\Component::class,+ 'item_id' => $component->id,+ 'target_type' => Asset::class,+ 'target_id' => $targetAsset->id,+ ]);++ $ids = collect($this->actingAsForApi($actor)+ ->getJson(route('api.activity.index', ['search' => 'TARGET-TAG-4242']))+ ->assertOk()+ ->json('rows'))+ ->pluck('id')+ ->all();++ $this->assertContains($matchingLog->id, $ids);+ }+public function test_search_matches_action_log_location_name(){// Activity Report eager-loads and shows the location on each