|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\Assets\Ui; |
| 4 | + |
| 5 | +use App\Models\Asset; |
| 6 | +use App\Models\User; |
| 7 | +use Tests\TestCase; |
| 8 | + |
| 9 | +/** |
| 10 | + * Regression tests for a broken-access-control bug where |
| 11 | + * BulkAssetsController::restore() (POST /hardware/bulkrestore) used |
| 12 | + * $this->authorize('update', Asset::class), letting any user with |
| 13 | + * assets.edit undo an admin's soft-delete by sending the ID directly. |
| 14 | + * Every other restore code path in the codebase gated on |
| 15 | + * authorize('delete', ...): |
| 16 | + * |
| 17 | + * - AssetsController::getRestore (single-asset web) |
| 18 | + * - Api\AssetsController::restore (single-asset API) |
| 19 | + * - BulkAssetsController::edit() 'restore' preview branch |
| 20 | + * |
| 21 | + * The fix aligns the bulk POST handler with the rest by gating on the |
| 22 | + * `delete` ability at both the class level and per-instance level. |
| 23 | + */ |
| 24 | +class BulkRestoreAuthorizationTest extends TestCase |
| 25 | +{ |
| 26 | + public function test_user_with_edit_but_no_delete_cannot_bulk_restore() |
| 27 | + { |
| 28 | + $asset = Asset::factory()->deleted()->create(); |
| 29 | + |
| 30 | + $user = User::factory()->viewAssets()->editAssets()->create(); |
| 31 | + |
| 32 | + $this->actingAs($user) |
| 33 | + ->post(route('hardware/bulkrestore'), [ |
| 34 | + 'ids' => [$asset->id], |
| 35 | + ]) |
| 36 | + ->assertForbidden(); |
| 37 | + |
| 38 | + $asset->refresh(); |
| 39 | + $this->assertNotNull($asset->deleted_at, 'Asset must remain soft-deleted after a forbidden restore attempt.'); |
| 40 | + } |
| 41 | + |
| 42 | + public function test_user_with_only_view_cannot_bulk_restore() |
| 43 | + { |
| 44 | + $asset = Asset::factory()->deleted()->create(); |
| 45 | + |
| 46 | + $user = User::factory()->viewAssets()->create(); |
| 47 | + |
| 48 | + $this->actingAs($user) |
| 49 | + ->post(route('hardware/bulkrestore'), [ |
| 50 | + 'ids' => [$asset->id], |
| 51 | + ]) |
| 52 | + ->assertForbidden(); |
| 53 | + |
| 54 | + $asset->refresh(); |
| 55 | + $this->assertNotNull($asset->deleted_at); |
| 56 | + } |
| 57 | + |
| 58 | + public function test_user_with_delete_permission_can_bulk_restore() |
| 59 | + { |
| 60 | + $asset = Asset::factory()->deleted()->create(); |
| 61 | + |
| 62 | + $user = User::factory()->viewAssets()->deleteAssets()->create(); |
| 63 | + |
| 64 | + $this->actingAs($user) |
| 65 | + ->post(route('hardware/bulkrestore'), [ |
| 66 | + 'ids' => [$asset->id], |
| 67 | + ]) |
| 68 | + ->assertRedirect(route('hardware.index')); |
| 69 | + |
| 70 | + $asset->refresh(); |
| 71 | + $this->assertNull($asset->deleted_at, 'Asset should be restored (deleted_at cleared) after an authorized bulk restore.'); |
| 72 | + } |
| 73 | + |
| 74 | + public function test_bulk_restore_skips_nonexistent_ids_instead_of_500() |
| 75 | + { |
| 76 | + // Prior code did Asset::withTrashed()->find($id)->restore() with no |
| 77 | + // null guard, so any invalid or forged ID crashed the whole request. |
| 78 | + $realAsset = Asset::factory()->deleted()->create(); |
| 79 | + |
| 80 | + $user = User::factory()->viewAssets()->deleteAssets()->create(); |
| 81 | + |
| 82 | + $this->actingAs($user) |
| 83 | + ->post(route('hardware/bulkrestore'), [ |
| 84 | + 'ids' => [$realAsset->id, 99999999], |
| 85 | + ]) |
| 86 | + ->assertRedirect(route('hardware.index')); |
| 87 | + |
| 88 | + $realAsset->refresh(); |
| 89 | + $this->assertNull($realAsset->deleted_at); |
| 90 | + } |
| 91 | +} |
0 commit comments