Skip to content

A free open source IT asset/license management system

Added bulk action options tests

snipe8ea374937f56
diff --git a/tests/Feature/AssetModels/Api/BulkSelectableTest.php b/tests/Feature/AssetModels/Api/BulkSelectableTest.php
new file mode 100644
index 0000000000..db72d60150
--- /dev/null
+++ b/tests/Feature/AssetModels/Api/BulkSelectableTest.php
@@ -0,0 +1,77 @@
+<?php
+
+namespace Tests\Feature\AssetModels\Api;
+
+use App\Models\Asset;
+use App\Models\AssetModel;
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * Verifies the per-row eligibility flags that drive the models index bulk-actions
+ * dropdown. `available_actions.bulk_selectable.edit` gates the bulk-edit action;
+ * `available_actions.bulk_selectable.delete` gates the bulk-delete action. The
+ * dropdown intersects these keys across all selected rows.
+ */
+class BulkSelectableTest extends TestCase
+{
+ public function test_clean_model_supports_both_edit_and_delete()
+ {
+ $model = AssetModel::factory()->create();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.models.show', $model))
+ ->assertOk()
+ ->assertJsonPath('available_actions.bulk_selectable.edit', true)
+ ->assertJsonPath('available_actions.bulk_selectable.delete', true);
+ }
+
+ public function test_soft_deleted_model_supports_neither()
+ {
+ // api.models.show excludes soft-deleted rows, so we assert via the
+ // index endpoint with status=deleted which does return them.
+ $model = AssetModel::factory()->create(['name' => 'unique-'.uniqid()]);
+ $model->delete();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.models.index', ['status' => 'deleted', 'search' => $model->name]))
+ ->assertOk()
+ ->assertJsonPath('rows.0.id', $model->id)
+ ->assertJsonPath('rows.0.available_actions.bulk_selectable.edit', false)
+ ->assertJsonPath('rows.0.available_actions.bulk_selectable.delete', false);
+ }
+
+ public function test_model_with_assigned_assets_cannot_be_deleted()
+ {
+ $model = AssetModel::factory()->create();
+ Asset::factory()->create(['model_id' => $model->id]);
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.models.show', $model))
+ ->assertOk()
+ ->assertJsonPath('available_actions.bulk_selectable.edit', true)
+ ->assertJsonPath('available_actions.bulk_selectable.delete', false);
+ }
+
+ public function test_user_without_edit_permission_gets_edit_false()
+ {
+ $model = AssetModel::factory()->create();
+
+ $this->actingAsForApi(User::factory()->viewAssetModels()->deleteAssetModels()->create())
+ ->getJson(route('api.models.show', $model))
+ ->assertOk()
+ ->assertJsonPath('available_actions.bulk_selectable.edit', false)
+ ->assertJsonPath('available_actions.bulk_selectable.delete', true);
+ }
+
+ public function test_user_without_delete_permission_gets_delete_false()
+ {
+ $model = AssetModel::factory()->create();
+
+ $this->actingAsForApi(User::factory()->viewAssetModels()->editAssetModels()->create())
+ ->getJson(route('api.models.show', $model))
+ ->assertOk()
+ ->assertJsonPath('available_actions.bulk_selectable.edit', true)
+ ->assertJsonPath('available_actions.bulk_selectable.delete', false);
+ }
+}
diff --git a/tests/Feature/Assets/Api/BulkSelectableTest.php b/tests/Feature/Assets/Api/BulkSelectableTest.php
new file mode 100644
index 0000000000..f7a15aea77
--- /dev/null
+++ b/tests/Feature/Assets/Api/BulkSelectableTest.php
@@ -0,0 +1,133 @@
+<?php
+
+namespace Tests\Feature\Assets\Api;
+
+use App\Models\Asset;
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * Verifies the per-row eligibility flags that drive the hardware index and every
+ * assets-tab embed's bulk-actions dropdown. `available_actions.bulk_selectable`
+ * exposes 8 keys (edit, maintenance, checkout, checkin, audit, delete, labels,
+ * restore); the dropdown intersects them across selected rows. Status filtering
+ * that the pre-refactor hand-rolled widget did via a `status_type` prop now falls
+ * out of these per-row flags: deleted rows only surface `restore`, assigned rows
+ * hide `checkout`/`delete`, unassigned rows hide `checkin`, etc.
+ */
+class BulkSelectableTest extends TestCase
+{
+ public function test_clean_unassigned_asset_supports_edit_maintenance_checkout_audit_delete_labels()
+ {
+ $asset = Asset::factory()->create();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.assets.show', $asset))
+ ->assertOk()
+ ->assertJsonPath('available_actions.bulk_selectable.edit', true)
+ ->assertJsonPath('available_actions.bulk_selectable.maintenance', true)
+ ->assertJsonPath('available_actions.bulk_selectable.checkout', true)
+ ->assertJsonPath('available_actions.bulk_selectable.checkin', false)
+ ->assertJsonPath('available_actions.bulk_selectable.audit', true)
+ ->assertJsonPath('available_actions.bulk_selectable.delete', true)
+ ->assertJsonPath('available_actions.bulk_selectable.labels', true)
+ ->assertJsonPath('available_actions.bulk_selectable.restore', false);
+ }
+
+ public function test_assigned_asset_hides_checkout_and_delete_and_shows_checkin()
+ {
+ $assignee = User::factory()->create();
+ $asset = Asset::factory()->assignedToUser($assignee)->create();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.assets.show', $asset))
+ ->assertOk()
+ ->assertJsonPath('available_actions.bulk_selectable.edit', true)
+ ->assertJsonPath('available_actions.bulk_selectable.checkout', false)
+ ->assertJsonPath('available_actions.bulk_selectable.checkin', true)
+ ->assertJsonPath('available_actions.bulk_selectable.delete', false)
+ ->assertJsonPath('available_actions.bulk_selectable.labels', true)
+ ->assertJsonPath('available_actions.bulk_selectable.restore', false);
+ }
+
+ public function test_soft_deleted_asset_surfaces_only_restore()
+ {
+ $asset = Asset::factory()->create();
+ $asset->delete();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.assets.show', $asset))
+ ->assertOk()
+ ->assertJsonPath('available_actions.bulk_selectable.edit', false)
+ ->assertJsonPath('available_actions.bulk_selectable.maintenance', false)
+ ->assertJsonPath('available_actions.bulk_selectable.checkout', false)
+ ->assertJsonPath('available_actions.bulk_selectable.checkin', false)
+ ->assertJsonPath('available_actions.bulk_selectable.audit', false)
+ ->assertJsonPath('available_actions.bulk_selectable.delete', false)
+ ->assertJsonPath('available_actions.bulk_selectable.labels', false)
+ ->assertJsonPath('available_actions.bulk_selectable.restore', true);
+ }
+
+ public function test_user_without_edit_permission_gets_edit_and_maintenance_false()
+ {
+ $asset = Asset::factory()->create();
+
+ $this->actingAsForApi(User::factory()->viewAssets()->create())
+ ->getJson(route('api.assets.show', $asset))
+ ->assertOk()
+ ->assertJsonPath('available_actions.bulk_selectable.edit', false)
+ ->assertJsonPath('available_actions.bulk_selectable.maintenance', false);
+ }
+
+ public function test_user_without_checkout_permission_gets_checkout_false()
+ {
+ $asset = Asset::factory()->create();
+
+ $this->actingAsForApi(User::factory()->viewAssets()->create())
+ ->getJson(route('api.assets.show', $asset))
+ ->assertOk()
+ ->assertJsonPath('available_actions.bulk_selectable.checkout', false);
+ }
+
+ public function test_user_without_checkin_permission_gets_checkin_false()
+ {
+ $assignee = User::factory()->create();
+ $asset = Asset::factory()->assignedToUser($assignee)->create();
+
+ $this->actingAsForApi(User::factory()->viewAssets()->create())
+ ->getJson(route('api.assets.show', $asset))
+ ->assertOk()
+ ->assertJsonPath('available_actions.bulk_selectable.checkin', false);
+ }
+
+ public function test_user_without_audit_permission_gets_audit_false()
+ {
+ $asset = Asset::factory()->create();
+
+ $this->actingAsForApi(User::factory()->viewAssets()->create())
+ ->getJson(route('api.assets.show', $asset))
+ ->assertOk()
+ ->assertJsonPath('available_actions.bulk_selectable.audit', false);
+ }
+
+ public function test_user_without_delete_permission_gets_delete_false()
+ {
+ $asset = Asset::factory()->create();
+
+ $this->actingAsForApi(User::factory()->viewAssets()->create())
+ ->getJson(route('api.assets.show', $asset))
+ ->assertOk()
+ ->assertJsonPath('available_actions.bulk_selectable.delete', false);
+ }
+
+ public function test_user_without_create_permission_gets_restore_false()
+ {
+ $asset = Asset::factory()->create();
+ $asset->delete();
+
+ $this->actingAsForApi(User::factory()->viewAssets()->create())
+ ->getJson(route('api.assets.show', $asset))
+ ->assertOk()
+ ->assertJsonPath('available_actions.bulk_selectable.restore', false);
+ }
+}
diff --git a/tests/Feature/Users/Api/BulkSelectableTest.php b/tests/Feature/Users/Api/BulkSelectableTest.php
new file mode 100644
index 0000000000..665f1470b3
--- /dev/null
+++ b/tests/Feature/Users/Api/BulkSelectableTest.php
@@ -0,0 +1,141 @@
+<?php
+
+namespace Tests\Feature\Users\Api;
+
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * Verifies the per-row eligibility flags that drive the users index bulk-actions
+ * dropdown. `available_actions.bulk_selectable` exposes 6 keys (edit,
+ * send_assigned, delete, merge, bulkpasswordreset, print); the dropdown
+ * intersects them across selected rows. Per-row gates in UsersTransformer mirror
+ * the actual per-row filters in BulkUsersController::edit (email presence for
+ * send_assigned; email + activated + not ldap_import for bulkpasswordreset).
+ */
+class BulkSelectableTest extends TestCase
+{
+ public function test_clean_user_supports_all_six_actions()
+ {
+ $user = User::factory()->create([
+ 'email' => '[email protected]',
+ 'activated' => 1,
+ 'ldap_import' => 0,
+ ]);
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.users.show', $user))
+ ->assertOk()
+ ->assertJsonPath('available_actions.bulk_selectable.edit', true)
+ ->assertJsonPath('available_actions.bulk_selectable.send_assigned', true)
+ ->assertJsonPath('available_actions.bulk_selectable.delete', true)
+ ->assertJsonPath('available_actions.bulk_selectable.merge', true)
+ ->assertJsonPath('available_actions.bulk_selectable.bulkpasswordreset', true)
+ ->assertJsonPath('available_actions.bulk_selectable.print', true);
+ }
+
+ public function test_soft_deleted_user_supports_none()
+ {
+ // api.users.show excludes soft-deleted rows, so we assert via the
+ // index endpoint with deleted=true which does return them.
+ $user = User::factory()->create([
+ 'username' => 'unique-'.uniqid(),
+ 'email' => '[email protected]',
+ 'activated' => 1,
+ 'ldap_import' => 0,
+ ]);
+ $user->delete();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.users.index', ['deleted' => 'true', 'search' => $user->username]))
+ ->assertOk()
+ ->assertJsonPath('rows.0.id', $user->id)
+ ->assertJsonPath('rows.0.available_actions.bulk_selectable.edit', false)
+ ->assertJsonPath('rows.0.available_actions.bulk_selectable.send_assigned', false)
+ ->assertJsonPath('rows.0.available_actions.bulk_selectable.delete', false)
+ ->assertJsonPath('rows.0.available_actions.bulk_selectable.merge', false)
+ ->assertJsonPath('rows.0.available_actions.bulk_selectable.bulkpasswordreset', false)
+ ->assertJsonPath('rows.0.available_actions.bulk_selectable.print', false);
+ }
+
+ public function test_user_without_email_cannot_send_assigned_or_password_reset()
+ {
+ $user = User::factory()->create([
+ 'email' => '',
+ 'activated' => 1,
+ 'ldap_import' => 0,
+ ]);
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.users.show', $user))
+ ->assertOk()
+ ->assertJsonPath('available_actions.bulk_selectable.send_assigned', false)
+ ->assertJsonPath('available_actions.bulk_selectable.bulkpasswordreset', false)
+ ->assertJsonPath('available_actions.bulk_selectable.edit', true)
+ ->assertJsonPath('available_actions.bulk_selectable.delete', true)
+ ->assertJsonPath('available_actions.bulk_selectable.print', true);
+ }
+
+ public function test_deactivated_user_cannot_receive_password_reset()
+ {
+ $user = User::factory()->create([
+ 'email' => '[email protected]',
+ 'activated' => 0,
+ 'ldap_import' => 0,
+ ]);
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.users.show', $user))
+ ->assertOk()
+ ->assertJsonPath('available_actions.bulk_selectable.bulkpasswordreset', false)
+ ->assertJsonPath('available_actions.bulk_selectable.send_assigned', true);
+ }
+
+ public function test_ldap_imported_user_cannot_receive_password_reset()
+ {
+ $user = User::factory()->create([
+ 'email' => '[email protected]',
+ 'activated' => 1,
+ 'ldap_import' => 1,
+ ]);
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.users.show', $user))
+ ->assertOk()
+ ->assertJsonPath('available_actions.bulk_selectable.bulkpasswordreset', false)
+ ->assertJsonPath('available_actions.bulk_selectable.send_assigned', true);
+ }
+
+ public function test_user_without_edit_permission_gets_edit_and_send_assigned_false()
+ {
+ $user = User::factory()->create([
+ 'email' => '[email protected]',
+ 'activated' => 1,
+ 'ldap_import' => 0,
+ ]);
+
+ $this->actingAsForApi(User::factory()->viewUsers()->create())
+ ->getJson(route('api.users.show', $user))
+ ->assertOk()
+ ->assertJsonPath('available_actions.bulk_selectable.edit', false)
+ ->assertJsonPath('available_actions.bulk_selectable.send_assigned', false)
+ ->assertJsonPath('available_actions.bulk_selectable.print', true)
+ ->assertJsonPath('available_actions.bulk_selectable.bulkpasswordreset', true);
+ }
+
+ public function test_user_without_delete_permission_gets_delete_and_merge_false()
+ {
+ $user = User::factory()->create([
+ 'email' => '[email protected]',
+ 'activated' => 1,
+ 'ldap_import' => 0,
+ ]);
+
+ $this->actingAsForApi(User::factory()->viewUsers()->create())
+ ->getJson(route('api.users.show', $user))
+ ->assertOk()
+ ->assertJsonPath('available_actions.bulk_selectable.delete', false)
+ ->assertJsonPath('available_actions.bulk_selectable.merge', false)
+ ->assertJsonPath('available_actions.bulk_selectable.print', true);
+ }
+}