Loheide-eu-mitarbeiterplatt.../tests/Feature/Mobile/ApiTest.php
Friederich Loheide f11a3e24b3
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m54s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 4m0s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m54s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m50s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m48s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m38s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m56s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 7m5s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m17s
Phase 8 Mobil: REST-API, Web-Push, installierbare App (0.25.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 08:20:07 +00:00

99 lines
4.8 KiB
PHP

<?php
namespace Tests\Feature\Mobile;
use App\Models\Role;
use App\Models\User;
use App\Notifications\PortalNotification;
use App\Plugins\PluginAccess;
use App\Support\Permissions\Audiences;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Inhaus\Plugins\Events\Models\Event;
use Inhaus\Plugins\News\Models\NewsPost;
use Tests\TestCase;
class ApiTest extends TestCase
{
use RefreshDatabase;
private function bearer(User $user): array
{
return ['Authorization' => 'Bearer '.$user->createToken('Test')->plainTextToken, 'Accept' => 'application/json'];
}
public function test_apps_sign_in_with_password_and_can_revoke(): void
{
$user = User::factory()->create(['email' => 'ada@example.org', 'password' => 'geheim123']);
$this->postJson('/api/v1/token', ['email' => 'ada@example.org', 'password' => 'falsch', 'device_name' => 'iPhone'])->assertUnprocessable();
$token = $this->postJson('/api/v1/token', ['email' => 'ada@example.org', 'password' => 'geheim123', 'device_name' => 'iPhone'])
->assertCreated()->assertJsonPath('user.email', 'ada@example.org')->json('token');
$this->withToken($token)->getJson('/api/v1/me')->assertOk()->assertJsonPath('user.id', $user->id);
$this->withToken($token)->deleteJson('/api/v1/token')->assertOk();
$this->app['auth']->forgetGuards();
$this->withToken($token)->getJson('/api/v1/me')->assertUnauthorized();
}
public function test_tokens_of_deactivated_people_stop_working(): void
{
$user = User::factory()->create();
$headers = $this->bearer($user);
$user->update(['is_active' => false]);
$this->getJson('/api/v1/me', $headers)->assertUnauthorized();
}
public function test_notifications_people_and_search(): void
{
$user = User::factory()->create();
$colleague = User::factory()->create(['first_name' => 'Zora', 'last_name' => 'Zeller', 'name' => 'Zora Zeller', 'department' => 'Einkauf']);
$user->notify(new PortalNotification('Hallo', 'Willkommen', '/'));
$headers = $this->bearer($user);
$id = $this->getJson('/api/v1/notifications', $headers)->assertOk()->assertJsonPath('data.0.title', 'Hallo')->assertJsonPath('meta.unread', 1)->json('data.0.id');
$this->postJson("/api/v1/notifications/{$id}/read", [], $headers)->assertOk();
$this->getJson('/api/v1/notifications', $headers)->assertJsonPath('meta.unread', 0);
$this->getJson('/api/v1/people?q=Zeller', $headers)->assertOk()->assertJsonPath('data.0.id', $colleague->id);
$this->getJson("/api/v1/people/{$colleague->id}", $headers)->assertOk()->assertJsonPath('data.department', 'Einkauf');
$this->getJson('/api/v1/search?q=Zeller', $headers)->assertOk()->assertJsonStructure(['query', 'groups']);
}
public function test_news_and_events_follow_the_same_visibility_rules(): void
{
$user = User::factory()->create();
$headers = $this->bearer($user);
$pflege = Role::query()->create(['name' => 'Pflege']);
$public = NewsPost::factory()->create(['title' => 'Für alle', 'published_at' => now()->subHour()]);
$event = Event::factory()->create(['title' => 'Nur Pflege', 'starts_at' => now()->addDay(), 'is_published' => true]);
Audiences::sync('event', $event->id, ['role' => [(string) $pflege->id]]);
$this->getJson('/api/v1/news', $headers)->assertOk()->assertJsonPath('data.0.title', 'Für alle');
$this->getJson("/api/v1/news/{$public->slug}", $headers)->assertOk()->assertJsonStructure(['data' => ['blocks', 'text', 'url']]);
$this->getJson('/api/v1/events', $headers)->assertOk()->assertJsonCount(0, 'data');
$this->getJson("/api/v1/events/{$event->id}", $headers)->assertNotFound();
// Rollen-Ausschluss eines Moduls gilt auch für die API
$excluded = Role::query()->create(['name' => 'Extern']);
app(PluginAccess::class)->exclude('news', [$excluded->id]);
$outsider = User::factory()->withRoles([$excluded])->create();
$this->app['auth']->forgetGuards();
$this->getJson('/api/v1/news', $this->bearer($outsider))->assertForbidden();
}
public function test_profile_creates_and_removes_app_tokens(): void
{
$user = User::factory()->create();
$token = $this->actingAs($user)->postJson('/profil/apps', ['name' => 'Diensthandy'])->assertOk()->json('token');
$this->get('/profil/bearbeiten')->assertInertia(fn ($page) => $page->where('tokens.0.name', 'Diensthandy'));
$this->app['auth']->forgetGuards();
$this->withToken($token)->getJson('/api/v1/me')->assertOk();
$this->actingAs($user)->delete('/profil/apps/'.$user->tokens()->first()->id)->assertRedirect();
$this->assertSame(0, $user->tokens()->count());
}
}