Loheide-eu-mitarbeiterplatt.../tests/Feature/Mobile/AppConnectTest.php
Friederich Loheide b3e2b525e1
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 6m10s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Failing after 6m3s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 7m33s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 7m25s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m28s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m27s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m26s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m27s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 44s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m12s
Herzen, Reichweite und App-Anbindung (0.30.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 12:24:11 +00:00

104 lines
5 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Tests\Feature\Mobile;
use App\Models\User;
use App\Notifications\PortalNotification;
use App\Push\AppDevice;
use App\Push\ExpoPush;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
class AppConnectTest extends TestCase
{
use RefreshDatabase;
private function pkce(): array
{
$verifier = str_repeat('a1B2c3D4', 6);
return [$verifier, rtrim(strtr(base64_encode(hash('sha256', $verifier, true)), '+/', '-_'), '=')];
}
public function test_config_is_public(): void
{
$this->getJson('/api/v1/config')->assertOk()
->assertJsonStructure(['name', 'primary_color', 'logo_url', 'icon_url', 'login' => ['password', 'browser', 'providers'], 'connect_url']);
}
public function test_browser_connect_hands_out_a_one_time_code_for_the_app(): void
{
$user = User::factory()->create();
[$verifier, $challenge] = $this->pkce();
$query = ['redirect_uri' => 'inhaus://verbunden', 'device_name' => 'Pixel 9', 'code_challenge' => $challenge, 'state' => 'xyz'];
$this->get('/app/verbinden?'.http_build_query($query))->assertRedirect('/login');
$this->actingAs($user)->get('/app/verbinden?'.http_build_query($query))->assertOk();
$location = $this->actingAs($user)->post('/app/verbinden', $query, ['X-Inertia' => 'true'])
->assertStatus(409)->headers->get('X-Inertia-Location');
$this->assertStringStartsWith('inhaus://verbunden?code=', $location);
$this->assertStringContainsString('state=xyz', $location);
$this->assertStringNotContainsString('|', $location);
parse_str((string) parse_url($location, PHP_URL_QUERY), $params);
// Falscher Prüfwert: nichts – und der Code ist danach verbraucht
$this->postJson('/api/v1/token/exchange', ['code' => $params['code'], 'code_verifier' => str_repeat('x', 48)])->assertUnprocessable();
$this->postJson('/api/v1/token/exchange', ['code' => $params['code'], 'code_verifier' => $verifier])->assertUnprocessable();
$location = $this->actingAs($user)->post('/app/verbinden', $query, ['X-Inertia' => 'true'])->headers->get('X-Inertia-Location');
parse_str((string) parse_url($location, PHP_URL_QUERY), $params);
$this->postJson('/api/v1/token/exchange', ['code' => $params['code'], 'code_verifier' => $verifier])
->assertCreated()->assertJsonPath('user.id', $user->id)->assertJsonStructure(['token']);
$this->assertSame('Pixel 9', $user->tokens()->first()->name);
}
public function test_only_app_schemes_are_accepted_as_redirect(): void
{
$user = User::factory()->create();
[, $challenge] = $this->pkce();
foreach (['https://evil.example/cb', 'javascript:alert(1)', 'exp://192.168.0.2:8081'] as $uri) {
$this->actingAs($user)->get('/app/verbinden?'.http_build_query(['redirect_uri' => $uri, 'code_challenge' => $challenge, 'state' => 's']))->assertStatus(400);
}
config(['inhaus.app_schemes' => ['inhaus', 'exp']]);
$this->actingAs($user)->get('/app/verbinden?'.http_build_query(['redirect_uri' => 'exp://192.168.0.2:8081/--/verbunden', 'code_challenge' => $challenge, 'state' => 's']))->assertOk();
}
public function test_push_devices_receive_notifications_via_expo(): void
{
$user = User::factory()->create();
$headers = ['Authorization' => 'Bearer '.$user->createToken('App')->plainTextToken];
$this->postJson('/api/v1/push/devices', ['token' => 'kein-token'], $headers)->assertUnprocessable();
$this->postJson('/api/v1/push/devices', ['token' => 'ExponentPushToken[abc123]', 'platform' => 'android'], $headers)->assertCreated();
$this->postJson('/api/v1/push/devices', ['token' => 'ExponentPushToken[weg]', 'platform' => 'ios'], $headers)->assertCreated();
Http::fake([ExpoPush::ENDPOINT => Http::response(['data' => [
['status' => 'ok', 'id' => '1'],
['status' => 'error', 'details' => ['error' => 'DeviceNotRegistered']],
]])]);
$user->notify(new PortalNotification('Neue Stelle', 'Pflegefachkraft', '/stellen/1'));
Http::assertSent(fn ($request) => $request->url() === ExpoPush::ENDPOINT
&& $request[0]['to'] === 'ExponentPushToken[abc123]' && $request[0]['data']['url'] === '/stellen/1');
$this->assertSame(['ExponentPushToken[abc123]'], AppDevice::query()->pluck('push_token')->all());
$this->deleteJson('/api/v1/push/devices', ['token' => 'ExponentPushToken[abc123]'], $headers)->assertOk();
$this->assertSame(0, AppDevice::query()->count());
}
public function test_revoking_access_removes_app_devices(): void
{
$user = User::factory()->create();
$token = $user->createToken('App');
AppDevice::query()->create(['user_id' => $user->id, 'token_id' => $token->accessToken->id, 'push_token' => 'ExponentPushToken[x]']);
$token->accessToken->delete();
$this->assertSame(0, AppDevice::query()->count());
}
}