'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()); } }