previousState = is_file($file) ? file_get_contents($file) : null; $state = json_decode($this->previousState ?? '{}', true) ?: []; $state['chat'] = ['enabled' => true]; file_put_contents($file, json_encode($state)); parent::setUp(); Storage::fake('local'); Storage::fake('uploads'); } protected function tearDown(): void { $file = storage_path('app/plugins.json'); $this->previousState === null ? @unlink($file) : file_put_contents($file, $this->previousState); parent::tearDown(); } private function dm(User $a, User $b): int { return $this->actingAs($a)->postJson('/chat/api/direkt', ['user_id' => $b->id])->assertOk()->json('id'); } public function test_direct_messages_unread_and_privacy(): void { [$ada, $bob, $eve] = User::factory()->count(3)->create()->all(); $id = $this->dm($ada, $bob); $this->assertSame($id, $this->dm($bob, $ada)); // ein Gespräch je Paar $this->actingAs($ada)->postJson("/chat/api/gespraeche/{$id}/nachrichten", ['body' => 'Hallo Bob 👋'])->assertCreated()->assertJsonPath('message.body', 'Hallo Bob 👋'); $this->actingAs($ada)->postJson("/chat/api/gespraeche/{$id}/nachrichten", ['body' => ' '])->assertUnprocessable(); $this->actingAs($bob)->getJson('/chat/api/ungelesen')->assertJsonPath('unread', 1); $this->actingAs($bob)->get('/')->assertInertia(fn (Assert $p) => $p->where('headerItems.0.component', 'chat::Unread')->where('headerItems.0.props.unread', 1)); $messageId = $this->actingAs($bob)->getJson("/chat/api/gespraeche/{$id}/nachrichten")->assertOk()->json('messages.0.id'); $this->actingAs($bob)->postJson("/chat/api/gespraeche/{$id}/gelesen", ['message_id' => $messageId])->assertJsonPath('unread', 0); // Andere – auch die Administration – kommen nicht hinein $this->actingAs($eve)->getJson("/chat/api/gespraeche/{$id}/nachrichten")->assertNotFound(); $this->actingAs(User::factory()->admin()->create())->getJson("/chat/api/gespraeche/{$id}/nachrichten")->assertNotFound(); $this->actingAs($eve)->get("/chat/{$id}")->assertNotFound(); $this->actingAs($ada)->get("/chat/{$id}")->assertOk()->assertInertia(fn (Assert $p) => $p->component('chat::Index')->where('activeId', $id)->has('conversations', 1)); } public function test_attachments_are_private_and_dangerous_files_blocked(): void { [$ada, $bob, $eve] = User::factory()->count(3)->create()->all(); $id = $this->dm($ada, $bob); $this->actingAs($ada)->post("/chat/api/gespraeche/{$id}/nachrichten", ['files' => [UploadedFile::fake()->create('boese.php', 1, 'text/x-php')]], ['Accept' => 'application/json']) ->assertUnprocessable(); $response = $this->actingAs($ada)->post("/chat/api/gespraeche/{$id}/nachrichten", [ 'body' => 'Foto und Plan', 'files' => [UploadedFile::fake()->image('foto.jpg', 400, 300), UploadedFile::fake()->create('Dienstplan.pdf', 50, 'application/pdf')], ], ['Accept' => 'application/json'])->assertCreated(); $image = $response->json('message.attachments.0'); $this->assertTrue($image['image']); $this->assertSame(400, $image['width']); $this->actingAs($bob)->get($image['url'])->assertOk()->assertHeader('X-Content-Type-Options', 'nosniff'); $this->actingAs($eve)->get($image['url'])->assertNotFound(); $this->assertStringContainsString('attachment', $this->actingAs($bob)->get($response->json('message.attachments.1.url'))->headers->get('Content-Disposition')); // Löschen entfernt auch die Dateien $this->actingAs($ada)->deleteJson('/chat/api/nachrichten/'.$response->json('message.id'))->assertOk(); $this->assertSame([], Storage::disk('local')->allFiles('chat')); } public function test_groups_need_the_right_and_admins_manage_members(): void { [$ada, $bob, $cem] = User::factory()->count(3)->create()->all(); $this->actingAs($ada)->postJson('/chat/api/gruppen', ['name' => 'Team', 'members' => [$bob->id]])->assertForbidden(); $role = Role::query()->create(['name' => 'Teamleitung']); $role->grants()->create(['ability' => 'chat.groups.create']); $ada->roles()->attach($role); $id = $this->actingAs($ada)->postJson('/chat/api/gruppen', ['name' => 'Station 3', 'members' => [$bob->id]])->assertCreated()->json('id'); $this->actingAs($bob)->postJson("/chat/api/gespraeche/{$id}/mitglieder", ['members' => [$cem->id]])->assertForbidden(); $this->actingAs($ada)->postJson("/chat/api/gespraeche/{$id}/mitglieder", ['members' => [$cem->id]])->assertOk(); $this->actingAs($cem)->getJson("/chat/api/gespraeche/{$id}/nachrichten")->assertOk()->assertJsonPath('messages.1.kind', 'system'); // Letzte Admin-Person verlässt die Gruppe → Nächste übernimmt $this->actingAs($ada)->deleteJson("/chat/api/gespraeche/{$id}")->assertOk(); $this->actingAs($ada)->getJson("/chat/api/gespraeche/{$id}/nachrichten")->assertNotFound(); $this->assertSame('admin', Conversation::query()->find($id)->participant($bob)->role); $this->actingAs($bob)->deleteJson("/chat/api/gespraeche/{$id}/mitglieder/{$cem->id}")->assertOk(); $this->actingAs($cem)->getJson("/chat/api/gespraeche/{$id}/nachrichten")->assertNotFound(); } public function test_updates_edit_react_and_typing(): void { [$ada, $bob] = User::factory()->count(2)->create()->all(); $id = $this->dm($ada, $bob); $cursor = $this->actingAs($bob)->getJson("/chat/api/neues?offen={$id}")->json('cursor'); $messageId = $this->actingAs($ada)->postJson("/chat/api/gespraeche/{$id}/nachrichten", ['body' => 'Kommst du?'])->json('message.id'); $this->actingAs($ada)->postJson("/chat/api/gespraeche/{$id}/tippt")->assertOk(); $this->actingAs($bob)->getJson('/chat/api/neues?'.http_build_query(['offen' => $id, 'seit' => $cursor, 'nach' => 0])) ->assertJsonPath('messages.0.body', 'Kommst du?')->assertJsonPath('typing.0', $ada->first_name)->assertJsonPath('unread', 1) ->assertJsonPath('conversations.0.id', $id); $this->actingAs($bob)->putJson("/chat/api/nachrichten/{$messageId}", ['body' => 'geändert'])->assertForbidden(); $this->actingAs($ada)->putJson("/chat/api/nachrichten/{$messageId}", ['body' => 'Kommst du heute?'])->assertOk(); $this->actingAs($bob)->postJson("/chat/api/nachrichten/{$messageId}/reaktion", ['emoji' => '👍'])->assertOk(); $this->actingAs($bob)->postJson("/chat/api/nachrichten/{$messageId}/reaktion", ['emoji' => '🍕'])->assertUnprocessable(); $this->actingAs($ada)->getJson("/chat/api/gespraeche/{$id}/nachrichten") ->assertJsonPath('messages.0.body', 'Kommst du heute?')->assertJsonPath('messages.0.edited', true) ->assertJsonPath('messages.0.reactions.0.emoji', '👍')->assertJsonPath('messages.0.reactions.0.mine', false); } public function test_gifs_from_own_collection_and_giphy(): void { [$ada, $bob] = User::factory()->count(2)->create()->all(); $id = $this->dm($ada, $bob); Storage::disk('uploads')->put('chat-gifs/daumen.gif', 'GIF89a'); Gif::query()->create(['path' => 'chat-gifs/daumen.gif', 'title' => 'Daumen hoch', 'tags' => 'super']); $own = $this->actingAs($ada)->getJson('/chat/api/gifs?q=super')->assertOk()->assertJsonPath('giphy', false)->json('gifs.0.url'); $this->actingAs($ada)->postJson("/chat/api/gespraeche/{$id}/nachrichten", ['gif_url' => $own])->assertCreated()->assertJsonPath('message.kind', 'gif'); $giphy = 'https://media2.giphy.com/media/abc123/200.gif?cid=x&rid=200.gif'; $this->actingAs($ada)->postJson("/chat/api/gespraeche/{$id}/nachrichten", ['gif_url' => $giphy])->assertUnprocessable(); $this->actingAs($ada)->postJson("/chat/api/gespraeche/{$id}/nachrichten", ['gif_url' => 'https://evil.example/x.gif'])->assertUnprocessable(); app(Settings::class)->set('chat.giphy_key', Crypt::encryptString('gk')); Http::fake(['api.giphy.com/*' => Http::response(['data' => [['title' => 'Yay', 'images' => ['fixed_height' => ['url' => $giphy, 'width' => '200', 'height' => '200']]]]])]); $this->actingAs($ada)->getJson('/chat/api/gifs?q=yay')->assertJsonPath('gifs.0.source', 'giphy'); $this->actingAs($ada)->postJson("/chat/api/gespraeche/{$id}/nachrichten", ['gif_url' => $giphy])->assertCreated(); } public function test_notifications_once_until_read_and_not_while_open(): void { Notification::fake(); [$ada, $bob] = User::factory()->count(2)->create()->all(); $id = $this->dm($ada, $bob); $this->actingAs($ada)->postJson("/chat/api/gespraeche/{$id}/nachrichten", ['body' => 'Eins']); $this->actingAs($ada)->postJson("/chat/api/gespraeche/{$id}/nachrichten", ['body' => 'Zwei']); Notification::assertSentToTimes($bob, PortalNotification::class, 1); $last = Message::query()->max('id'); $this->actingAs($bob)->postJson("/chat/api/gespraeche/{$id}/gelesen", ['message_id' => $last]); $this->actingAs($bob)->getJson("/chat/api/neues?offen={$id}"); // Bob hat das Gespräch offen $this->actingAs($ada)->postJson("/chat/api/gespraeche/{$id}/nachrichten", ['body' => 'Drei']); Notification::assertSentToTimes($bob, PortalNotification::class, 1); } public function test_realtime_signals_without_content_and_private_channels(): void { app(Settings::class)->setMany(['chat.realtime.enabled' => true, 'chat.realtime.app_id' => '42', 'chat.realtime.key' => 'k', 'chat.realtime.secret' => Crypt::encryptString('s3cret'), 'chat.realtime.host' => 'ws.example.org', 'chat.realtime.port' => 443]); Http::fake(['ws.example.org/*' => Http::response([])]); [$ada, $bob] = User::factory()->count(2)->create()->all(); $id = $this->dm($ada, $bob); $this->actingAs($ada)->postJson("/chat/api/gespraeche/{$id}/nachrichten", ['body' => 'Geheimer Inhalt']); Http::assertSent(fn ($r) => str_starts_with($r->url(), 'https://ws.example.org/apps/42/events?') && str_contains($r->url(), 'auth_signature=') && in_array(Realtime::channel($bob->id), $r['channels'], true) && ! str_contains($r->body(), 'Geheimer')); $this->actingAs($ada)->postJson('/chat/api/echtzeit/anmelden', ['socket_id' => '123.456', 'channel_name' => Realtime::channel($ada->id)]) ->assertOk()->assertJsonPath('auth', 'k:'.hash_hmac('sha256', '123.456:'.Realtime::channel($ada->id), 's3cret')); $this->actingAs($ada)->postJson('/chat/api/echtzeit/anmelden', ['socket_id' => '123.456', 'channel_name' => Realtime::channel($bob->id)])->assertForbidden(); } }