Files
einundzwanzig-app/tests/Feature/Api/VenueWriteApiTest.php
T
HolgerHatGarKeineNode 3b93e22e95 **Add authenticated API endpoints for managing Meetups, Cities, Venues, and Lecturers**
-  Introduced `store`, `update`, `mine`, and `mineShow` endpoints for `Meetups`, `Cities`, `Venues`, and `Lecturers` with validation and authorization.
- 🔒 Added `Policies` for `Meetups`, `Cities`, `Venues`, and `Lecturers` leveraging `ChecksCreatorOwnership` for ownership checks.
- 🌐 Created `Resources` for structured API responses: `MeetupResource`, `CityResource`, `VenueResource`, and `LecturerResource`.
-  Added dedicated `Request` classes for input validation: `Store` and `Update` variants for all models.
- 🛠️ Updated controllers to support new functionalities with localized error messages and proper HTTP responses.
2026-06-08 01:58:37 +02:00

84 lines
2.4 KiB
PHP

<?php
use App\Models\City;
use App\Models\User;
use App\Models\Venue;
use Laravel\Sanctum\Sanctum;
it('rejects a guest', function () {
$this->postJson('/api/venues', [
'name' => 'Bitcoin Hub',
'city_id' => City::factory()->create()->id,
])->assertUnauthorized();
});
it('lets an authenticated user create', function () {
Sanctum::actingAs($user = User::factory()->create());
$this->postJson('/api/venues', [
'name' => 'Bitcoin Hub',
'city_id' => City::factory()->create()->id,
'street' => 'Satoshi Street 21',
])->assertCreated();
$this->assertDatabaseHas('venues', [
'name' => 'Bitcoin Hub',
'created_by' => $user->id,
]);
});
it('fails validation', function () {
Sanctum::actingAs(User::factory()->create());
$this->postJson('/api/venues', [])
->assertUnprocessable()
->assertJsonValidationErrors(['name', 'city_id']);
});
it('lets the owner update', function () {
Sanctum::actingAs($user = User::factory()->create());
$model = Venue::factory()->create(['created_by' => $user->id]);
$this->patchJson('/api/venues/'.$model->id, [
'name' => 'Orange Hub',
])
->assertSuccessful()
->assertJsonPath('data.name', 'Orange Hub');
});
it('forbids updating someone elses', function () {
$owner = User::factory()->create();
$model = Venue::factory()->create(['created_by' => $owner->id]);
Sanctum::actingAs(User::factory()->create());
$this->patchJson('/api/venues/'.$model->id, [
'name' => 'Orange Hub',
])->assertForbidden();
});
it('returns only own in mine index', function () {
Sanctum::actingAs($user = User::factory()->create());
$other = User::factory()->create();
Venue::factory()->count(2)->create(['created_by' => $user->id]);
Venue::factory()->create(['created_by' => $other->id]);
$response = $this->getJson('/api/my-venues');
$response->assertSuccessful();
expect($response->json('data'))->toHaveCount(2);
collect($response->json('data'))->each(
fn ($venue) => expect($venue['created_by'])->toBe($user->id)
);
});
it('forbids viewing someone elses in mine show', function () {
$owner = User::factory()->create();
$model = Venue::factory()->create(['created_by' => $owner->id]);
Sanctum::actingAs(User::factory()->create());
$this->getJson('/api/my-venues/'.$model->id)->assertForbidden();
});