Skip to content

A free open source IT asset/license management system

More tests

snipe196590c6fd1c
diff --git a/tests/Feature/Accessories/Ui/BulkFulfillAccessoryTest.php b/tests/Feature/Accessories/Ui/BulkFulfillAccessoryTest.php
new file mode 100644
index 0000000000..5057916ce7
--- /dev/null
+++ b/tests/Feature/Accessories/Ui/BulkFulfillAccessoryTest.php
@@ -0,0 +1,207 @@
+<?php
+
+namespace Tests\Feature\Accessories\Ui;
+
+use App\Enums\CheckoutRequestState;
+use App\Models\Accessory;
+use App\Models\CheckoutRequest;
+use App\Models\User;
+use Illuminate\Support\Facades\Notification;
+use Tests\TestCase;
+
+/**
+ * Bulk-fulfill flow for the Accessory type. Same per-row/partial-
+ * success contract every user-target bulk-fulfill controller uses
+ * (consumables and licenses have their own mirror tests since the
+ * qty semantics differ slightly). Screen is reached from the
+ * "Fulfill Multiple" button on /requests when >=2 pending requests
+ * exist for the same accessory.
+ */
+class BulkFulfillAccessoryTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+ Notification::fake();
+ }
+
+ public function test_requires_checkout_permission_on_accessories(): void
+ {
+ // Endpoint gate: caller without checkoutAccessories should
+ // be blocked before they can see the pending queue.
+ $accessory = Accessory::factory()->create(['qty' => 10]);
+
+ $this->actingAs(User::factory()->create())
+ ->get(route('accessories.fulfill-requests.create', $accessory))
+ ->assertForbidden();
+ }
+
+ public function test_show_lists_all_pending_requests_for_the_accessory(): void
+ {
+ $admin = User::factory()->checkoutAccessories()->create();
+ $accessory = Accessory::factory()->create(['qty' => 10]);
+ $alice = User::factory()->create();
+ $bob = User::factory()->create();
+
+ $aliceRequest = CheckoutRequest::factory()->create([
+ 'user_id' => $alice->id,
+ 'requestable_id' => $accessory->id,
+ 'requestable_type' => Accessory::class,
+ 'quantity' => 2,
+ ]);
+ $bobRequest = CheckoutRequest::factory()->create([
+ 'user_id' => $bob->id,
+ 'requestable_id' => $accessory->id,
+ 'requestable_type' => Accessory::class,
+ 'quantity' => 3,
+ ]);
+
+ $this->actingAs($admin)
+ ->get(route('accessories.fulfill-requests.create', $accessory))
+ ->assertOk()
+ ->assertViewHas('pendingRequests', function ($pending) use ($aliceRequest, $bobRequest) {
+ $ids = collect($pending)->pluck('id')->all();
+
+ return count($ids) === 2
+ && in_array($aliceRequest->id, $ids, true)
+ && in_array($bobRequest->id, $ids, true);
+ });
+ }
+
+ public function test_fulfills_only_ticked_rows_and_leaves_others_pending(): void
+ {
+ // Opt-in semantics: rows admin didn't check stay pending
+ // on the queue for a later pass.
+ $admin = User::factory()->checkoutAccessories()->create();
+ $accessory = Accessory::factory()->create(['qty' => 10]);
+ $alice = User::factory()->create();
+ $bob = User::factory()->create();
+
+ $aliceRequest = CheckoutRequest::factory()->create([
+ 'user_id' => $alice->id,
+ 'requestable_id' => $accessory->id,
+ 'requestable_type' => Accessory::class,
+ 'quantity' => 2,
+ ]);
+ $bobRequest = CheckoutRequest::factory()->create([
+ 'user_id' => $bob->id,
+ 'requestable_id' => $accessory->id,
+ 'requestable_type' => Accessory::class,
+ 'quantity' => 3,
+ ]);
+
+ $this->actingAs($admin)
+ ->post(route('accessories.fulfill-requests.store', $accessory), [
+ 'enabled_requests' => [$aliceRequest->id],
+ 'user_id' => [$aliceRequest->id => $alice->id],
+ 'qty' => [$aliceRequest->id => 2],
+ 'notes' => [$aliceRequest->id => ''],
+ ])
+ ->assertRedirect(route('requests.index'));
+
+ $this->assertSame(CheckoutRequestState::Fulfilled, $aliceRequest->fresh()->state);
+ $this->assertSame(CheckoutRequestState::Pending, $bobRequest->fresh()->state);
+ }
+
+ public function test_partial_qty_leaves_row_pending_with_counter_advanced(): void
+ {
+ // Admin fulfills 1 of a 3-request. State stays pending;
+ // fulfilled_quantity advances so the next pass knows what's
+ // still owed. Original quantity stays immutable.
+ $admin = User::factory()->checkoutAccessories()->create();
+ $accessory = Accessory::factory()->create(['qty' => 10]);
+ $requester = User::factory()->create();
+
+ $request = CheckoutRequest::factory()->create([
+ 'user_id' => $requester->id,
+ 'requestable_id' => $accessory->id,
+ 'requestable_type' => Accessory::class,
+ 'quantity' => 3,
+ ]);
+
+ $this->actingAs($admin)
+ ->post(route('accessories.fulfill-requests.store', $accessory), [
+ 'enabled_requests' => [$request->id],
+ 'user_id' => [$request->id => $requester->id],
+ 'qty' => [$request->id => 1],
+ 'notes' => [$request->id => ''],
+ ])
+ ->assertRedirect(route('requests.index'));
+
+ $fresh = $request->fresh();
+ $this->assertSame(CheckoutRequestState::Pending, $fresh->state);
+ $this->assertSame(3, $fresh->quantity);
+ $this->assertSame(1, $fresh->fulfilled_quantity);
+ $this->assertSame(2, $fresh->remainingQuantity());
+ }
+
+ public function test_stale_or_already_fulfilled_row_is_silently_skipped(): void
+ {
+ // Between page load and submit, another admin already
+ // fulfilled or canceled the request. The bulk-fulfill
+ // controller should skip that row (per-row partial success)
+ // without failing the whole batch.
+ $admin = User::factory()->checkoutAccessories()->create();
+ $accessory = Accessory::factory()->create(['qty' => 10]);
+ $alice = User::factory()->create();
+ $bob = User::factory()->create();
+
+ $canceledRequest = CheckoutRequest::factory()->create([
+ 'user_id' => $alice->id,
+ 'requestable_id' => $accessory->id,
+ 'requestable_type' => Accessory::class,
+ 'quantity' => 1,
+ 'canceled_at' => now(),
+ ]);
+ $stillPending = CheckoutRequest::factory()->create([
+ 'user_id' => $bob->id,
+ 'requestable_id' => $accessory->id,
+ 'requestable_type' => Accessory::class,
+ 'quantity' => 1,
+ ]);
+
+ $this->actingAs($admin)
+ ->post(route('accessories.fulfill-requests.store', $accessory), [
+ 'enabled_requests' => [$canceledRequest->id, $stillPending->id],
+ 'user_id' => [
+ $canceledRequest->id => $alice->id,
+ $stillPending->id => $bob->id,
+ ],
+ 'qty' => [
+ $canceledRequest->id => 1,
+ $stillPending->id => 1,
+ ],
+ 'notes' => [],
+ ])
+ ->assertRedirect(route('requests.index'));
+
+ $this->assertSame(CheckoutRequestState::Canceled, $canceledRequest->fresh()->state);
+ $this->assertSame(CheckoutRequestState::Fulfilled, $stillPending->fresh()->state);
+ }
+
+ public function test_empty_selection_redirects_back_with_error(): void
+ {
+ // Submit without ticking any rows: nothing to do. Redirect
+ // back to the fulfill screen with a "no rows selected"
+ // flash rather than confusing the admin with a bare
+ // success redirect.
+ $admin = User::factory()->checkoutAccessories()->create();
+ $accessory = Accessory::factory()->create(['qty' => 10]);
+ CheckoutRequest::factory()->create([
+ 'user_id' => User::factory()->create()->id,
+ 'requestable_id' => $accessory->id,
+ 'requestable_type' => Accessory::class,
+ 'quantity' => 1,
+ ]);
+
+ $this->actingAs($admin)
+ ->post(route('accessories.fulfill-requests.store', $accessory), [
+ // no enabled_requests
+ 'user_id' => [],
+ 'qty' => [],
+ 'notes' => [],
+ ])
+ ->assertRedirect(route('accessories.fulfill-requests.create', $accessory))
+ ->assertSessionHas('error');
+ }
+}
diff --git a/tests/Feature/AssetModels/Ui/BulkFulfillAssetModelTest.php b/tests/Feature/AssetModels/Ui/BulkFulfillAssetModelTest.php
new file mode 100644
index 0000000000..59d5e16e51
--- /dev/null
+++ b/tests/Feature/AssetModels/Ui/BulkFulfillAssetModelTest.php
@@ -0,0 +1,156 @@
+<?php
+
+namespace Tests\Feature\AssetModels\Ui;
+
+use App\Enums\CheckoutRequestState;
+use App\Models\Asset;
+use App\Models\AssetModel;
+use App\Models\CheckoutRequest;
+use App\Models\Statuslabel;
+use App\Models\User;
+use Illuminate\Support\Facades\Notification;
+use Tests\TestCase;
+
+/**
+ * Bulk-fulfill flow for AssetModel. Distinct from the qty-tracked
+ * types because each row hands out a specific Asset OF the model
+ * (not a qty count). The controller iterates ticked rows and
+ * lockForUpdates each target asset before checkout - the double-
+ * assignment race and hand-crafted-POST collision cases are both
+ * covered by the per-row assigned_to guard.
+ */
+class BulkFulfillAssetModelTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+ Notification::fake();
+ }
+
+ public function test_requires_asset_checkout_permission(): void
+ {
+ // Model requests fulfill via asset checkout, so the gate
+ // is checkoutAssets (not a nonexistent models.checkout).
+ $model = AssetModel::factory()->create();
+
+ $this->actingAs(User::factory()->create())
+ ->get(route('models.fulfill-requests.create', $model))
+ ->assertForbidden();
+ }
+
+ public function test_fulfills_ticked_rows_with_distinct_assets(): void
+ {
+ // Happy path: two rows tick with different asset ids from
+ // the pool; both check out cleanly to their requesters.
+ $admin = User::factory()->checkoutAssets()->create();
+ $model = AssetModel::factory()->create();
+ $status = Statuslabel::factory()->rtd()->create();
+ $assetA = Asset::factory()->create(['model_id' => $model->id, 'status_id' => $status->id]);
+ $assetB = Asset::factory()->create(['model_id' => $model->id, 'status_id' => $status->id]);
+ $alice = User::factory()->create();
+ $bob = User::factory()->create();
+
+ $aliceReq = CheckoutRequest::factory()->create([
+ 'user_id' => $alice->id,
+ 'requestable_id' => $model->id,
+ 'requestable_type' => AssetModel::class,
+ ]);
+ $bobReq = CheckoutRequest::factory()->create([
+ 'user_id' => $bob->id,
+ 'requestable_id' => $model->id,
+ 'requestable_type' => AssetModel::class,
+ ]);
+
+ $this->actingAs($admin)
+ ->post(route('models.fulfill-requests.store', $model), [
+ 'enabled_requests' => [$aliceReq->id, $bobReq->id],
+ 'asset_id' => [
+ $aliceReq->id => $assetA->id,
+ $bobReq->id => $assetB->id,
+ ],
+ 'notes' => [],
+ ])
+ ->assertRedirect(route('requests.index'));
+
+ $this->assertSame(CheckoutRequestState::Fulfilled, $aliceReq->fresh()->state);
+ $this->assertSame(CheckoutRequestState::Fulfilled, $bobReq->fresh()->state);
+ $this->assertSame($alice->id, (int) $assetA->fresh()->assigned_to);
+ $this->assertSame($bob->id, (int) $assetB->fresh()->assigned_to);
+ }
+
+ public function test_double_assignment_of_same_asset_wins_first_row_only(): void
+ {
+ // Two rows accidentally point at the SAME asset id
+ // (admin misclicked auto-pick, or hand-crafted POST, or
+ // JS drift). Sequential per-row lockForUpdate + assigned_to
+ // check means row 1 checks out, row 2 sees the asset now
+ // taken and skips with row_asset_taken. Second requester's
+ // request stays pending on the queue.
+ $admin = User::factory()->checkoutAssets()->create();
+ $model = AssetModel::factory()->create();
+ $status = Statuslabel::factory()->rtd()->create();
+ $asset = Asset::factory()->create(['model_id' => $model->id, 'status_id' => $status->id]);
+ $alice = User::factory()->create();
+ $bob = User::factory()->create();
+
+ $aliceReq = CheckoutRequest::factory()->create([
+ 'user_id' => $alice->id,
+ 'requestable_id' => $model->id,
+ 'requestable_type' => AssetModel::class,
+ ]);
+ $bobReq = CheckoutRequest::factory()->create([
+ 'user_id' => $bob->id,
+ 'requestable_id' => $model->id,
+ 'requestable_type' => AssetModel::class,
+ ]);
+
+ $this->actingAs($admin)
+ ->post(route('models.fulfill-requests.store', $model), [
+ 'enabled_requests' => [$aliceReq->id, $bobReq->id],
+ 'asset_id' => [
+ $aliceReq->id => $asset->id,
+ $bobReq->id => $asset->id,
+ ],
+ 'notes' => [],
+ ])
+ ->assertRedirect(route('requests.index'));
+
+ // First row wins the asset.
+ $this->assertSame(CheckoutRequestState::Fulfilled, $aliceReq->fresh()->state);
+ $this->assertSame($alice->id, (int) $asset->fresh()->assigned_to);
+
+ // Second row's request stays pending - the admin can pick
+ // a different asset (or wait for stock) on a later pass.
+ $this->assertSame(CheckoutRequestState::Pending, $bobReq->fresh()->state);
+ }
+
+ public function test_asset_from_a_different_model_is_rejected(): void
+ {
+ // Guard against hand-crafted POSTs pointing at an asset
+ // that isn't of this model (would silently check out the
+ // wrong physical unit).
+ $admin = User::factory()->checkoutAssets()->create();
+ $model = AssetModel::factory()->create();
+ $otherModel = AssetModel::factory()->create();
+ $status = Statuslabel::factory()->rtd()->create();
+ $offModelAsset = Asset::factory()->create(['model_id' => $otherModel->id, 'status_id' => $status->id]);
+ $requester = User::factory()->create();
+
+ $request = CheckoutRequest::factory()->create([
+ 'user_id' => $requester->id,
+ 'requestable_id' => $model->id,
+ 'requestable_type' => AssetModel::class,
+ ]);
+
+ $this->actingAs($admin)
+ ->post(route('models.fulfill-requests.store', $model), [
+ 'enabled_requests' => [$request->id],
+ 'asset_id' => [$request->id => $offModelAsset->id],
+ 'notes' => [],
+ ])
+ ->assertRedirect(route('requests.index'));
+
+ $this->assertSame(CheckoutRequestState::Pending, $request->fresh()->state);
+ $this->assertNull($offModelAsset->fresh()->assigned_to);
+ }
+}
diff --git a/tests/Feature/CheckoutRequests/StateMachineTest.php b/tests/Feature/CheckoutRequests/StateMachineTest.php
index ec1629d4f8..388dcc450b 100644
--- a/tests/Feature/CheckoutRequests/StateMachineTest.php
+++ b/tests/Feature/CheckoutRequests/StateMachineTest.php
@@ -182,4 +182,137 @@ class StateMachineTest extends TestCase
$this->assertNull($asset->isRequestedBy($user));
}
+
+ public function test_partial_fulfillment_advances_counter_and_leaves_pending(): void
+ {
+ // Admin fulfills 3 of a 5-requested row. State stays
+ // pending because there's still 2 outstanding; counter
+ // advances so the next fulfillment knows how much is left.
+ $request = CheckoutRequest::factory()->create([
+ 'user_id' => User::factory()->create()->id,
+ 'requestable_id' => Accessory::factory()->create()->id,
+ 'requestable_type' => Accessory::class,
+ 'quantity' => 5,
+ ]);
+
+ FulfillCheckoutRequestAction::run($request, null, 3);
+ $request->refresh();
+
+ $this->assertSame(CheckoutRequestState::Pending, $request->state);
+ $this->assertSame(3, $request->fulfilled_quantity);
+ $this->assertSame(2, $request->remainingQuantity());
+ $this->assertNull($request->fulfilled_at);
+ $this->assertTrue($request->isPartiallyFulfilled());
+ }
+
+ public function test_full_fulfillment_across_multiple_partial_calls_closes_the_row(): void
+ {
+ // Two partial fulfillments that together hit the requested
+ // qty flip the row to fulfilled. fulfilled_at is set only
+ // on the pass that pushes the counter past the threshold.
+ $request = CheckoutRequest::factory()->create([
+ 'user_id' => User::factory()->create()->id,
+ 'requestable_id' => Accessory::factory()->create()->id,
+ 'requestable_type' => Accessory::class,
+ 'quantity' => 4,
+ ]);
+
+ FulfillCheckoutRequestAction::run($request, null, 1);
+ $request->refresh();
+ $this->assertSame(CheckoutRequestState::Pending, $request->state);
+ $this->assertNull($request->fulfilled_at);
+
+ FulfillCheckoutRequestAction::run($request, null, 3);
+ $request->refresh();
+
+ $this->assertSame(CheckoutRequestState::Fulfilled, $request->state);
+ $this->assertSame(4, $request->fulfilled_quantity);
+ $this->assertSame(0, $request->remainingQuantity());
+ $this->assertNotNull($request->fulfilled_at);
+ $this->assertFalse($request->isPartiallyFulfilled());
+ }
+
+ public function test_overfulfill_caps_at_requested_quantity(): void
+ {
+ // Admin somehow submits a qty larger than what was
+ // requested (misclicked, hand-crafted POST). The action
+ // caps at the remainder so fulfilled_quantity never
+ // exceeds quantity - the audit trail preserves the
+ // original ask exactly.
+ $request = CheckoutRequest::factory()->create([
+ 'user_id' => User::factory()->create()->id,
+ 'requestable_id' => Accessory::factory()->create()->id,
+ 'requestable_type' => Accessory::class,
+ 'quantity' => 2,
+ ]);
+
+ FulfillCheckoutRequestAction::run($request, null, 99);
+ $request->refresh();
+
+ $this->assertSame(CheckoutRequestState::Fulfilled, $request->state);
+ $this->assertSame(2, $request->fulfilled_quantity);
+ }
+
+ public function test_zero_qty_fulfillment_is_a_noop(): void
+ {
+ // Belt: an event with qty=0 (edge case, shouldn't happen
+ // from a real checkout but callers might slip a 0 through)
+ // must not advance the counter or flip the state.
+ $request = CheckoutRequest::factory()->create([
+ 'user_id' => User::factory()->create()->id,
+ 'requestable_id' => Accessory::factory()->create()->id,
+ 'requestable_type' => Accessory::class,
+ 'quantity' => 3,
+ ]);
+
+ FulfillCheckoutRequestAction::run($request, null, 0);
+ $request->refresh();
+
+ $this->assertSame(CheckoutRequestState::Pending, $request->state);
+ $this->assertSame(0, $request->fulfilled_quantity);
+ }
+
+ public function test_null_qty_fulfillment_consumes_the_full_remainder(): void
+ {
+ // Backwards compat: single-target checkout controllers that
+ // predate partial-fulfillment tracking call the action
+ // without a qty. The action assumes "this fulfillment
+ // covers the whole thing" so the request closes in one
+ // pass. Keeps the old behavior for callers not opting into
+ // the new partial semantics.
+ $request = CheckoutRequest::factory()->create([
+ 'user_id' => User::factory()->create()->id,
+ 'requestable_id' => Accessory::factory()->create()->id,
+ 'requestable_type' => Accessory::class,
+ 'quantity' => 7,
+ ]);
+
+ FulfillCheckoutRequestAction::run($request);
+ $request->refresh();
+
+ $this->assertSame(CheckoutRequestState::Fulfilled, $request->state);
+ $this->assertSame(7, $request->fulfilled_quantity);
+ }
+
+ public function test_partial_row_still_counts_as_requested_by_for_duplicate_guard(): void
+ {
+ // Requestable::isRequestedBy gates the "you already have an
+ // open request" duplicate check. A partially-fulfilled row
+ // is still open (state=pending), so it must still count -
+ // otherwise the user could file a new request while the
+ // partial is outstanding and end up with two open rows for
+ // the same item.
+ $user = User::factory()->create();
+ $accessory = Accessory::factory()->create();
+ $request = CheckoutRequest::factory()->create([
+ 'user_id' => $user->id,
+ 'requestable_id' => $accessory->id,
+ 'requestable_type' => Accessory::class,
+ 'quantity' => 5,
+ ]);
+
+ FulfillCheckoutRequestAction::run($request, null, 2);
+
+ $this->assertNotNull($accessory->isRequestedBy($user));
+ }
}
diff --git a/tests/Feature/Consumables/Ui/BulkFulfillConsumableTest.php b/tests/Feature/Consumables/Ui/BulkFulfillConsumableTest.php
new file mode 100644
index 0000000000..e0033ae84f
--- /dev/null
+++ b/tests/Feature/Consumables/Ui/BulkFulfillConsumableTest.php
@@ -0,0 +1,94 @@
+<?php
+
+namespace Tests\Feature\Consumables\Ui;
+
+use App\Enums\CheckoutRequestState;
+use App\Models\CheckoutRequest;
+use App\Models\Consumable;
+use App\Models\User;
+use Illuminate\Support\Facades\Notification;
+use Tests\TestCase;
+
+/**
+ * Bulk-fulfill flow for Consumables. Same per-row/partial-success
+ * contract every user-target bulk-fulfill controller uses; see
+ * BulkFulfillAccessoryTest for the canonical shape.
+ */
+class BulkFulfillConsumableTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+ Notification::fake();
+ }
+
+ public function test_requires_checkout_permission_on_consumables(): void
+ {
+ $consumable = Consumable::factory()->create(['qty' => 10]);
+
+ $this->actingAs(User::factory()->create())
+ ->get(route('consumables.fulfill-requests.create', $consumable))
+ ->assertForbidden();
+ }
+
+ public function test_fulfills_only_ticked_rows(): void
+ {
+ $admin = User::factory()->checkoutConsumables()->create();
+ $consumable = Consumable::factory()->create(['qty' => 10]);
+ $alice = User::factory()->create();
+ $bob = User::factory()->create();
+
+ $aliceRequest = CheckoutRequest::factory()->create([
+ 'user_id' => $alice->id,
+ 'requestable_id' => $consumable->id,
+ 'requestable_type' => Consumable::class,
+ 'quantity' => 2,
+ ]);
+ $bobRequest = CheckoutRequest::factory()->create([
+ 'user_id' => $bob->id,
+ 'requestable_id' => $consumable->id,
+ 'requestable_type' => Consumable::class,
+ 'quantity' => 3,
+ ]);
+
+ $this->actingAs($admin)
+ ->post(route('consumables.fulfill-requests.store', $consumable), [
+ 'enabled_requests' => [$aliceRequest->id],
+ 'user_id' => [$aliceRequest->id => $alice->id],
+ 'qty' => [$aliceRequest->id => 2],
+ 'notes' => [$aliceRequest->id => ''],
+ ])
+ ->assertRedirect(route('requests.index'));
+
+ $this->assertSame(CheckoutRequestState::Fulfilled, $aliceRequest->fresh()->state);
+ $this->assertSame(CheckoutRequestState::Pending, $bobRequest->fresh()->state);
+ }
+
+ public function test_partial_qty_leaves_row_pending_with_counter_advanced(): void
+ {
+ $admin = User::factory()->checkoutConsumables()->create();
+ $consumable = Consumable::factory()->create(['qty' => 10]);
+ $requester = User::factory()->create();
+
+ $request = CheckoutRequest::factory()->create([
+ 'user_id' => $requester->id,
+ 'requestable_id' => $consumable->id,
+ 'requestable_type' => Consumable::class,
+ 'quantity' => 4,
+ ]);
+
+ $this->actingAs($admin)
+ ->post(route('consumables.fulfill-requests.store', $consumable), [
+ 'enabled_requests' => [$request->id],
+ 'user_id' => [$request->id => $requester->id],
+ 'qty' => [$request->id => 2],
+ 'notes' => [$request->id => ''],
+ ])
+ ->assertRedirect(route('requests.index'));
+
+ $fresh = $request->fresh();
+ $this->assertSame(CheckoutRequestState::Pending, $fresh->state);
+ $this->assertSame(2, $fresh->fulfilled_quantity);
+ $this->assertSame(2, $fresh->remainingQuantity());
+ }
+}
diff --git a/tests/Feature/Requests/Ui/AssetRequestIndexTest.php b/tests/Feature/Requests/Ui/AssetRequestIndexTest.php
index 312eb150af..6877092058 100644
--- a/tests/Feature/Requests/Ui/AssetRequestIndexTest.php
+++ b/tests/Feature/Requests/Ui/AssetRequestIndexTest.php
@@ -16,7 +16,10 @@ class AssetRequestIndexTest extends TestCase
public function test_privileged_user_gets_the_shell_page()
{
- $this->actingAs(User::factory()->viewAssets()->create())
+ // Gate is viewAnyCheckoutRequests - satisfied by checkout
+ // perm on any of the five checkoutable types. viewAssets
+ // alone wouldn't pass (that's the view perm, not checkout).
+ $this->actingAs(User::factory()->checkoutAssets()->create())
->get(route('requests.index'))
->assertOk();
}
diff --git a/tests/Feature/Requests/Ui/CancelRequestTest.php b/tests/Feature/Requests/Ui/CancelRequestTest.php
index 5bd91f813f..8cd854d70e 100644
--- a/tests/Feature/Requests/Ui/CancelRequestTest.php
+++ b/tests/Feature/Requests/Ui/CancelRequestTest.php
@@ -29,8 +29,11 @@ class CancelRequestTest extends TestCase
);
}
- public function test_non_admin_cannot_use_cancel_by_admin_to_cancel_another_users_request(): void
+ public function test_non_admin_cannot_cancel_another_users_request_via_requestinguser_param(): void
{
+ // Admin-cancel is inferred from a requestingUser param that
+ // differs from the auth id. Non-admin callers who hand-craft
+ // that URL get rejected server-side.
$asset = Asset::factory()->create();
$victim = User::factory()->create();
CheckoutRequest::factory()->create(['requestable_id' => $asset->id, 'requestable_type' => Asset::class, 'user_id' => $victim->id]);
@@ -39,7 +42,6 @@ class CancelRequestTest extends TestCase
->post(route('account/request-item', [
'itemType' => 'asset',
'itemId' => $asset->id,
- 'cancel_by_admin' => '1',
'requestingUser' => $victim->id,
]))
->assertRedirect()
@@ -53,42 +55,35 @@ class CancelRequestTest extends TestCase
);
}
- public function test_user_with_own_pending_request_cannot_cancel_another_users_request_via_requestinguser_param(): void
+ public function test_non_admin_with_own_pending_request_is_blocked_when_trying_admin_cancel_url(): void
{
+ // Actor has their own open request AND passes a different
+ // requestingUser id (the victim's). Since actor isn't
+ // admin, the whole request is rejected - neither their own
+ // nor the victim's request is canceled. This is the
+ // security-friendlier behavior compared to the old shape
+ // (which silently self-canceled and ignored the URL noise).
$asset = Asset::factory()->create();
$actor = User::factory()->create();
$victim = User::factory()->create();
- CheckoutRequest::factory()->create(['requestable_id' => $asset->id, 'requestable_type' => Asset::class, 'user_id' => $actor->id]);
- CheckoutRequest::factory()->create(['requestable_id' => $asset->id, 'requestable_type' => Asset::class, 'user_id' => $victim->id]);
+ $actorRequest = CheckoutRequest::factory()->create(['requestable_id' => $asset->id, 'requestable_type' => Asset::class, 'user_id' => $actor->id]);
+ $victimRequest = CheckoutRequest::factory()->create(['requestable_id' => $asset->id, 'requestable_type' => Asset::class, 'user_id' => $victim->id]);
$this->actingAs($actor)
->post(route('account/request-item', [
'itemType' => 'asset',
'itemId' => $asset->id,
- 'cancel_by_admin' => '0',
'requestingUser' => $victim->id,
]))
- ->assertRedirect();
-
- // Actor's own request is cancelled
- $this->assertNotNull(
- CheckoutRequest::where('requestable_id', $asset->id)
- ->where('user_id', $actor->id)
- ->whereNotNull('canceled_at')
- ->first()
- );
+ ->assertRedirect()
+ ->assertSessionHas('error');
- // Victim's request is untouched
- $this->assertNull(
- CheckoutRequest::where('requestable_id', $asset->id)
- ->where('user_id', $victim->id)
- ->whereNotNull('canceled_at')
- ->first()
- );
+ $this->assertNull($actorRequest->fresh()->canceled_at);
+ $this->assertNull($victimRequest->fresh()->canceled_at);
}
- public function test_superuser_can_cancel_another_users_request_via_cancel_by_admin(): void
+ public function test_superuser_can_cancel_another_users_request(): void
{
$asset = Asset::factory()->create();
$victim = User::factory()->create();
@@ -98,7 +93,6 @@ class CancelRequestTest extends TestCase
->post(route('account/request-item', [
'itemType' => 'asset',
'itemId' => $asset->id,
- 'cancel_by_admin' => '1',
'requestingUser' => $victim->id,
]))
->assertRedirect();
@@ -170,7 +164,7 @@ class CancelRequestTest extends TestCase
Log::shouldHaveReceived('warning')->once();
}
- public function test_admin_can_cancel_another_users_request_via_cancel_by_admin(): void
+ public function test_admin_can_cancel_another_users_request(): void
{
$asset = Asset::factory()->create();
$victim = User::factory()->create();
@@ -180,7 +174,6 @@ class CancelRequestTest extends TestCase
->post(route('account/request-item', [
'itemType' => 'asset',
'itemId' => $asset->id,
- 'cancel_by_admin' => '1',
'requestingUser' => $victim->id,
]))
->assertRedirect();