Skip to content

Commit 676161a

Browse files
committed
fix(api): authorize target environment moves
1 parent cf63c7d commit 676161a

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

bootstrap/helpers/api.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Database\Eloquent\Collection;
99
use Illuminate\Database\Eloquent\Model;
1010
use Illuminate\Http\Request;
11+
use Illuminate\Support\Facades\Gate;
1112
use Illuminate\Validation\Rule;
1213

1314
function getTeamIdFromToken()
@@ -202,6 +203,8 @@ function moveResourceToEnvironment(Request $request, $resource, string $resource
202203
return response()->json(['message' => 'Target environment not found or not owned by your team.'], 404);
203204
}
204205

206+
Gate::authorize('update', $newEnvironment);
207+
205208
if ($resource->environment_id === $newEnvironment->id) {
206209
return response()->json(['message' => "$resourceType is already in this environment."], 400);
207210
}

tests/Feature/MoveResourceApiTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use App\Models\Team;
1212
use App\Models\User;
1313
use Illuminate\Foundation\Testing\RefreshDatabase;
14+
use Illuminate\Support\Facades\Gate;
1415

1516
uses(RefreshDatabase::class);
1617

@@ -123,6 +124,36 @@
123124
$response->assertStatus(404);
124125
});
125126

127+
test('returns 403 when target environment update is denied', function () {
128+
$application = Application::factory()->create([
129+
'environment_id' => $this->environment->id,
130+
'destination_id' => $this->destination->id,
131+
'destination_type' => $this->destination->getMorphClass(),
132+
]);
133+
134+
Gate::before(function (User $user, string $ability, array $arguments) {
135+
$target = $arguments[0] ?? null;
136+
137+
if ($ability === 'update' && $target instanceof Environment && $target->is($this->targetEnvironment)) {
138+
return false;
139+
}
140+
141+
return null;
142+
});
143+
144+
$response = $this->withHeaders([
145+
'Authorization' => 'Bearer '.$this->bearerToken,
146+
'Content-Type' => 'application/json',
147+
])->postJson("/api/v1/applications/{$application->uuid}/move", [
148+
'environment_uuid' => $this->targetEnvironment->uuid,
149+
]);
150+
151+
$response->assertForbidden();
152+
153+
$application->refresh();
154+
expect($application->environment_id)->toBe($this->environment->id);
155+
});
156+
126157
test('returns 400 when application is already in the target environment', function () {
127158
$application = Application::factory()->create([
128159
'environment_id' => $this->environment->id,

0 commit comments

Comments
 (0)