mirror of
https://github.com/HolgerHatGarKeineNode/einundzwanzig-app.git
synced 2026-06-16 16:20:30 +00:00
✨ **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.
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Lecturer;
|
||||
use App\Models\User;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
it('rejects a guest', function () {
|
||||
$this->postJson('/api/lecturers', [
|
||||
'name' => 'Saifedean Ammous',
|
||||
])->assertUnauthorized();
|
||||
});
|
||||
|
||||
it('lets an authenticated user create', function () {
|
||||
Sanctum::actingAs($user = User::factory()->create());
|
||||
|
||||
$this->postJson('/api/lecturers', [
|
||||
'name' => 'Saifedean Ammous',
|
||||
])
|
||||
->assertCreated()
|
||||
->assertJsonPath('data.name', 'Saifedean Ammous');
|
||||
|
||||
$this->assertDatabaseHas('lecturers', [
|
||||
'name' => 'Saifedean Ammous',
|
||||
'created_by' => $user->id,
|
||||
]);
|
||||
});
|
||||
|
||||
it('fails validation', function () {
|
||||
Sanctum::actingAs(User::factory()->create());
|
||||
|
||||
$this->postJson('/api/lecturers', [])
|
||||
->assertUnprocessable()
|
||||
->assertJsonValidationErrors(['name']);
|
||||
});
|
||||
|
||||
it('lets the owner update', function () {
|
||||
Sanctum::actingAs($user = User::factory()->create());
|
||||
$lecturer = Lecturer::factory()->create(['created_by' => $user->id]);
|
||||
|
||||
$this->patchJson('/api/lecturers/'.$lecturer->id, [
|
||||
'name' => 'Knut Svanholm',
|
||||
])
|
||||
->assertSuccessful()
|
||||
->assertJsonPath('data.name', 'Knut Svanholm');
|
||||
});
|
||||
|
||||
it('forbids updating someone elses', function () {
|
||||
$owner = User::factory()->create();
|
||||
$lecturer = Lecturer::factory()->create(['created_by' => $owner->id]);
|
||||
|
||||
Sanctum::actingAs(User::factory()->create());
|
||||
|
||||
$this->patchJson('/api/lecturers/'.$lecturer->id, [
|
||||
'name' => 'Knut Svanholm',
|
||||
])->assertForbidden();
|
||||
});
|
||||
|
||||
it('returns only own in mine index', function () {
|
||||
Sanctum::actingAs($user = User::factory()->create());
|
||||
$other = User::factory()->create();
|
||||
|
||||
Lecturer::factory()->count(2)->create(['created_by' => $user->id]);
|
||||
Lecturer::factory()->create(['created_by' => $other->id]);
|
||||
|
||||
$response = $this->getJson('/api/my-lecturers');
|
||||
|
||||
$response->assertSuccessful();
|
||||
expect($response->json('data'))->toHaveCount(2);
|
||||
collect($response->json('data'))->each(
|
||||
fn ($lecturer) => expect($lecturer['created_by'])->toBe($user->id)
|
||||
);
|
||||
});
|
||||
|
||||
it('forbids viewing someone elses in mine show', function () {
|
||||
$owner = User::factory()->create();
|
||||
$lecturer = Lecturer::factory()->create(['created_by' => $owner->id]);
|
||||
|
||||
Sanctum::actingAs(User::factory()->create());
|
||||
|
||||
$this->getJson('/api/my-lecturers/'.$lecturer->id)
|
||||
->assertForbidden();
|
||||
});
|
||||
Reference in New Issue
Block a user