|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\PredefinedKits\Ui; |
| 4 | + |
| 5 | +use App\Models\Accessory; |
| 6 | +use App\Models\AssetModel; |
| 7 | +use App\Models\Consumable; |
| 8 | +use App\Models\License; |
| 9 | +use App\Models\PredefinedKit; |
| 10 | +use App\Models\User; |
| 11 | +use Tests\TestCase; |
| 12 | + |
| 13 | +/** |
| 14 | + * Web sibling of tests/Feature/PredefinedKits/Api/UpdateKitItemsTest. |
| 15 | + * FD-56594 added `findOrFail` + `authorize('view', $object)` to the four |
| 16 | + * update handlers on `Api\PredefinedKitsController`, but the parallel |
| 17 | + * handlers on the web `Kits\PredefinedKitsController` (`updateLicense`, |
| 18 | + * `updateModel`, `updateAccessory`, `updateConsumable`) were not covered |
| 19 | + * by that fix. A `kits.edit` holder could re-point an existing pivot at |
| 20 | + * an object id they cannot read directly, including one in another |
| 21 | + * company. This locks in the follow-up guards. |
| 22 | + */ |
| 23 | +class UpdateKitItemsAuthorizationTest extends TestCase |
| 24 | +{ |
| 25 | + // ------------------------------------------------------------------------- |
| 26 | + // Licenses |
| 27 | + // ------------------------------------------------------------------------- |
| 28 | + |
| 29 | + public function test_web_update_kit_license_requires_view_permission_on_target_license() |
| 30 | + { |
| 31 | + $kit = PredefinedKit::factory()->create(); |
| 32 | + $existingLicense = License::factory()->create(); |
| 33 | + $kit->licenses()->attach($existingLicense->id, ['quantity' => 1]); |
| 34 | + $pivotId = $kit->licenses()->wherePivot('license_id', $existingLicense->id)->first()->pivot->id; |
| 35 | + |
| 36 | + // Attacker points the pivot at a different license they cannot view. |
| 37 | + $targetLicense = License::factory()->create(); |
| 38 | + |
| 39 | + $this->actingAs(User::factory()->editPredefinedKits()->create()) |
| 40 | + ->put(route('kits.licenses.update', ['kit' => $kit->id, 'license_id' => $existingLicense->id]), [ |
| 41 | + 'pivot_id' => $pivotId, |
| 42 | + 'license_id' => $targetLicense->id, |
| 43 | + 'quantity' => 5, |
| 44 | + ]) |
| 45 | + ->assertForbidden(); |
| 46 | + |
| 47 | + $this->assertDatabaseMissing('kits_licenses', [ |
| 48 | + 'kit_id' => $kit->id, |
| 49 | + 'license_id' => $targetLicense->id, |
| 50 | + ]); |
| 51 | + $this->assertDatabaseHas('kits_licenses', [ |
| 52 | + 'kit_id' => $kit->id, |
| 53 | + 'license_id' => $existingLicense->id, |
| 54 | + 'quantity' => 1, |
| 55 | + ]); |
| 56 | + } |
| 57 | + |
| 58 | + public function test_web_update_kit_license_succeeds_when_view_permission_granted() |
| 59 | + { |
| 60 | + $kit = PredefinedKit::factory()->create(); |
| 61 | + $existingLicense = License::factory()->create(); |
| 62 | + $kit->licenses()->attach($existingLicense->id, ['quantity' => 1]); |
| 63 | + $pivotId = $kit->licenses()->wherePivot('license_id', $existingLicense->id)->first()->pivot->id; |
| 64 | + |
| 65 | + $targetLicense = License::factory()->create(); |
| 66 | + |
| 67 | + $this->actingAs(User::factory()->editPredefinedKits()->viewLicenses()->create()) |
| 68 | + ->put(route('kits.licenses.update', ['kit' => $kit->id, 'license_id' => $existingLicense->id]), [ |
| 69 | + 'pivot_id' => $pivotId, |
| 70 | + 'license_id' => $targetLicense->id, |
| 71 | + 'quantity' => 5, |
| 72 | + ]) |
| 73 | + ->assertRedirect(route('kits.edit', $kit->id)); |
| 74 | + |
| 75 | + $this->assertDatabaseHas('kits_licenses', [ |
| 76 | + 'kit_id' => $kit->id, |
| 77 | + 'license_id' => $targetLicense->id, |
| 78 | + 'quantity' => 5, |
| 79 | + ]); |
| 80 | + } |
| 81 | + |
| 82 | + // ------------------------------------------------------------------------- |
| 83 | + // Models |
| 84 | + // ------------------------------------------------------------------------- |
| 85 | + |
| 86 | + public function test_web_update_kit_model_requires_view_permission_on_target_model() |
| 87 | + { |
| 88 | + $kit = PredefinedKit::factory()->create(); |
| 89 | + $existingModel = AssetModel::factory()->create(); |
| 90 | + $kit->models()->attach($existingModel->id, ['quantity' => 1]); |
| 91 | + $pivotId = $kit->models()->wherePivot('model_id', $existingModel->id)->first()->pivot->id; |
| 92 | + |
| 93 | + $targetModel = AssetModel::factory()->create(); |
| 94 | + |
| 95 | + $this->actingAs(User::factory()->editPredefinedKits()->create()) |
| 96 | + ->put(route('kits.models.update', ['kit' => $kit->id, 'model_id' => $existingModel->id]), [ |
| 97 | + 'pivot_id' => $pivotId, |
| 98 | + 'model_id' => $targetModel->id, |
| 99 | + 'quantity' => 5, |
| 100 | + ]) |
| 101 | + ->assertForbidden(); |
| 102 | + |
| 103 | + $this->assertDatabaseMissing('kits_models', [ |
| 104 | + 'kit_id' => $kit->id, |
| 105 | + 'model_id' => $targetModel->id, |
| 106 | + ]); |
| 107 | + } |
| 108 | + |
| 109 | + // ------------------------------------------------------------------------- |
| 110 | + // Accessories |
| 111 | + // ------------------------------------------------------------------------- |
| 112 | + |
| 113 | + public function test_web_update_kit_accessory_requires_view_permission_on_target_accessory() |
| 114 | + { |
| 115 | + $kit = PredefinedKit::factory()->create(); |
| 116 | + $existingAccessory = Accessory::factory()->create(); |
| 117 | + $kit->accessories()->attach($existingAccessory->id, ['quantity' => 1]); |
| 118 | + $pivotId = $kit->accessories()->wherePivot('accessory_id', $existingAccessory->id)->first()->pivot->id; |
| 119 | + |
| 120 | + $targetAccessory = Accessory::factory()->create(); |
| 121 | + |
| 122 | + $this->actingAs(User::factory()->editPredefinedKits()->create()) |
| 123 | + ->put(route('kits.accessories.update', ['kit' => $kit->id, 'accessory_id' => $existingAccessory->id]), [ |
| 124 | + 'pivot_id' => $pivotId, |
| 125 | + 'accessory_id' => $targetAccessory->id, |
| 126 | + 'quantity' => 5, |
| 127 | + ]) |
| 128 | + ->assertForbidden(); |
| 129 | + |
| 130 | + $this->assertDatabaseMissing('kits_accessories', [ |
| 131 | + 'kit_id' => $kit->id, |
| 132 | + 'accessory_id' => $targetAccessory->id, |
| 133 | + ]); |
| 134 | + } |
| 135 | + |
| 136 | + // ------------------------------------------------------------------------- |
| 137 | + // Consumables |
| 138 | + // ------------------------------------------------------------------------- |
| 139 | + |
| 140 | + public function test_web_update_kit_consumable_requires_view_permission_on_target_consumable() |
| 141 | + { |
| 142 | + $kit = PredefinedKit::factory()->create(); |
| 143 | + $existingConsumable = Consumable::factory()->create(); |
| 144 | + $kit->consumables()->attach($existingConsumable->id, ['quantity' => 1]); |
| 145 | + $pivotId = $kit->consumables()->wherePivot('consumable_id', $existingConsumable->id)->first()->pivot->id; |
| 146 | + |
| 147 | + $targetConsumable = Consumable::factory()->create(); |
| 148 | + |
| 149 | + $this->actingAs(User::factory()->editPredefinedKits()->create()) |
| 150 | + ->put(route('kits.consumables.update', ['kit' => $kit->id, 'consumable_id' => $existingConsumable->id]), [ |
| 151 | + 'pivot_id' => $pivotId, |
| 152 | + 'consumable_id' => $targetConsumable->id, |
| 153 | + 'quantity' => 5, |
| 154 | + ]) |
| 155 | + ->assertForbidden(); |
| 156 | + |
| 157 | + $this->assertDatabaseMissing('kits_consumables', [ |
| 158 | + 'kit_id' => $kit->id, |
| 159 | + 'consumable_id' => $targetConsumable->id, |
| 160 | + ]); |
| 161 | + } |
| 162 | +} |
0 commit comments