Phase 8 Mobil: REST-API, Web-Push, installierbare App (0.25.0)
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
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
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
232dc5c59c
commit
f11a3e24b3
201 changed files with 3604 additions and 427 deletions
99
tests/Feature/Mobile/ApiTest.php
Normal file
99
tests/Feature/Mobile/ApiTest.php
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
<?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());
|
||||
}
|
||||
}
|
||||
118
tests/Feature/Mobile/WebPushTest.php
Normal file
118
tests/Feature/Mobile/WebPushTest.php
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature\Mobile;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Notifications\PortalNotification;
|
||||
use App\Push\PushSubscription;
|
||||
use App\Push\SendWebPush;
|
||||
use App\Push\WebPush;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\Client\Request;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
use Tests\TestCase;
|
||||
|
||||
class WebPushTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* Schlüsselpaar eines „Browsers“ (User Agent) wie beim Abonnieren.
|
||||
*
|
||||
* @return array{0: \OpenSSLAsymmetricKey, 1: string, 2: string}
|
||||
*/
|
||||
private function browserKeys(): array
|
||||
{
|
||||
$key = openssl_pkey_new(['curve_name' => 'prime256v1', 'private_key_type' => OPENSSL_KEYTYPE_EC]);
|
||||
|
||||
return [$key, WebPush::base64UrlEncode(WebPush::rawPublicKey($key)), WebPush::base64UrlEncode(random_bytes(16))];
|
||||
}
|
||||
|
||||
/** Entschlüsseln wie ein Browser (RFC 8291) */
|
||||
private function decrypt(string $body, \OpenSSLAsymmetricKey $uaPrivate, string $uaPublic, string $auth): string
|
||||
{
|
||||
$salt = substr($body, 0, 16);
|
||||
$idLength = ord($body[20]);
|
||||
$asPublic = substr($body, 21, $idLength);
|
||||
$cipher = substr($body, 21 + $idLength);
|
||||
|
||||
$shared = openssl_pkey_derive(WebPush::publicKeyFromRaw($asPublic), $uaPrivate, 32);
|
||||
$ikm = hash_hkdf('sha256', $shared, 32, "WebPush: info\0".WebPush::base64UrlDecode($uaPublic).$asPublic, WebPush::base64UrlDecode($auth));
|
||||
$cek = hash_hkdf('sha256', $ikm, 16, "Content-Encoding: aes128gcm\0", $salt);
|
||||
$nonce = hash_hkdf('sha256', $ikm, 12, "Content-Encoding: nonce\0", $salt);
|
||||
|
||||
$plain = openssl_decrypt(substr($cipher, 0, -16), 'aes-128-gcm', $cek, OPENSSL_RAW_DATA, $nonce, substr($cipher, -16));
|
||||
$this->assertNotFalse($plain, 'Entschlüsselung fehlgeschlagen');
|
||||
|
||||
return rtrim($plain, "\x00\x02");
|
||||
}
|
||||
|
||||
public function test_payload_is_encrypted_per_rfc8291_and_signed_with_vapid(): void
|
||||
{
|
||||
[$uaPrivate, $uaPublic, $auth] = $this->browserKeys();
|
||||
$user = User::factory()->create();
|
||||
$subscription = PushSubscription::query()->create([
|
||||
'user_id' => $user->id, 'endpoint' => 'https://push.example.org/send/abc', 'endpoint_hash' => hash('sha256', 'x'),
|
||||
'public_key' => $uaPublic, 'auth_token' => $auth,
|
||||
]);
|
||||
|
||||
Http::fake(['push.example.org/*' => Http::response('', 201)]);
|
||||
$push = app(WebPush::class);
|
||||
$this->assertTrue($push->send($subscription, ['title' => 'Freigabe', 'body' => 'Ümläute & mehr']));
|
||||
|
||||
Http::assertSent(function (Request $request) use ($uaPrivate, $uaPublic, $auth, $push) {
|
||||
$this->assertSame('aes128gcm', $request->header('Content-Encoding')[0]);
|
||||
$payload = json_decode($this->decrypt($request->body(), $uaPrivate, $uaPublic, $auth), true);
|
||||
$this->assertSame('Ümläute & mehr', $payload['body']);
|
||||
|
||||
// VAPID: JWT mit ES256, prüfbar mit dem öffentlichen Schlüssel
|
||||
preg_match('/^vapid t=([^,]+), k=(.+)$/', $request->header('Authorization')[0], $m);
|
||||
$this->assertSame($push->publicKey(), $m[2]);
|
||||
[$header, $claims, $signature] = explode('.', $m[1]);
|
||||
$this->assertSame('https://push.example.org', json_decode(WebPush::base64UrlDecode($claims), true)['aud']);
|
||||
$raw = WebPush::base64UrlDecode($signature);
|
||||
$int = fn (string $v) => "\x02".chr(strlen($v = ltrim($v, "\0")) + (ord($v[0]) > 0x7F ? 1 : 0)).(ord($v[0]) > 0x7F ? "\0" : '').$v;
|
||||
$der = $int(substr($raw, 0, 32)).$int(substr($raw, 32));
|
||||
$der = "\x30".chr(strlen($der)).$der;
|
||||
$this->assertSame(1, openssl_verify($header.'.'.$claims, $der, WebPush::publicKeyFromRaw(WebPush::base64UrlDecode($m[2])), OPENSSL_ALGO_SHA256));
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
public function test_devices_subscribe_and_receive_portal_notifications(): void
|
||||
{
|
||||
Queue::fake();
|
||||
[, $uaPublic, $auth] = $this->browserKeys();
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->actingAs($user)->get('/')->assertInertia(fn ($page) => $page->where('push.key', app(WebPush::class)->publicKey()));
|
||||
$this->postJson('/push/abo', ['endpoint' => 'http://unsicher.example.org/x', 'keys' => ['p256dh' => $uaPublic, 'auth' => $auth]])->assertUnprocessable();
|
||||
$this->postJson('/push/abo', ['endpoint' => 'https://push.example.org/send/1', 'keys' => ['p256dh' => $uaPublic, 'auth' => $auth]])->assertOk();
|
||||
$this->assertSame(1, PushSubscription::query()->where('user_id', $user->id)->count());
|
||||
|
||||
$user->notify(new PortalNotification('Beitrag freigegeben', '„Sommerfest“ ist online.', '/neuigkeiten/sommerfest'));
|
||||
Queue::assertPushed(SendWebPush::class, fn (SendWebPush $job) => $job->userId === $user->id && $job->payload['url'] === '/neuigkeiten/sommerfest');
|
||||
|
||||
$this->deleteJson('/push/abo', ['endpoint' => 'https://push.example.org/send/1'])->assertOk();
|
||||
$this->assertSame(0, PushSubscription::query()->count());
|
||||
|
||||
// Ohne Abonnement: nur die Glocke
|
||||
Queue::fake();
|
||||
$user->notify(new PortalNotification('Noch einer'));
|
||||
Queue::assertNotPushed(SendWebPush::class);
|
||||
}
|
||||
|
||||
public function test_expired_subscriptions_are_removed(): void
|
||||
{
|
||||
[, $uaPublic, $auth] = $this->browserKeys();
|
||||
$user = User::factory()->create();
|
||||
PushSubscription::query()->create(['user_id' => $user->id, 'endpoint' => 'https://push.example.org/weg', 'endpoint_hash' => hash('sha256', 'weg'), 'public_key' => $uaPublic, 'auth_token' => $auth]);
|
||||
Http::fake(['*' => Http::response('', 410)]);
|
||||
|
||||
(new SendWebPush($user->id, ['title' => 'x']))->handle(app(WebPush::class));
|
||||
|
||||
$this->assertSame(0, PushSubscription::query()->count());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue