dir = sys_get_temp_dir().'/inhaus-fb-'.bin2hex(random_bytes(4)); mkdir($this->dir); $this->app->instance(LicenseStore::class, new LicenseStore($this->dir.'/license.json')); $this->app->instance(InstalledVersion::class, new InstalledVersion($this->dir.'/.version')); app(InstalledVersion::class)->save(str_repeat('9', 40)); Cache::put('feedback.endpoints', ['bugreport', 'featurerequest', 'telemetry'], 60); } protected function tearDown(): void { Filesystem::deleteDirectory($this->dir); parent::tearDown(); } /** * @return array */ private function payload(): array { return ['title' => 'Export bricht ab', 'body' => 'Ab etwa 5000 Zeilen passiert nichts mehr.', 'severity' => 'high']; } public function test_request_body_and_headers_with_license(): void { app(LicenseStore::class)->setLicense('lizenz@firma.de', 'KEY-1'); Http::fake([self::URL => Http::response(['ok' => true, 'reference' => 'BUG-8F3K2Q', 'id' => 'x'], 201)]); $user = User::factory()->admin()->create(); $this->actingAs($user); Livewire::test(Feedback::class) ->set('type', 'bugreport') ->set('subject', 'Export bricht ab') ->set('body', 'Ab etwa 5000 Zeilen passiert nichts mehr.') ->set('severity', 'high') ->set('email', 'rueckfrage@firma.de') ->call('submit') ->assertHasNoErrors() ->assertSet('lastReference', 'BUG-8F3K2Q') ->assertSet('subject', '') ->assertSee('BUG-8F3K2Q'); Http::assertSent(function (Request $request) { $data = $request->data(); return $request->url() === self::URL && $request->method() === 'POST' && $request->hasHeader('X-License-Email', 'lizenz@firma.de') && $request->hasHeader('Authorization', 'Bearer KEY-1') && strlen($request->header('Idempotency-Key')[0]) === 32 && $data['title'] === 'Export bricht ab' && $data['email'] === 'rueckfrage@firma.de' && $data['severity'] === 'high' && $data['app_version'] === Version::VERSION && $data['current_sha'] === str_repeat('9', 40) && $data['os'] !== '' && isset($data['metadata']['php']) && ! isset($data['name']); }); $feedback = VendorFeedback::query()->sole(); $this->assertSame('sent', $feedback->status); $this->assertSame('BUG-8F3K2Q', $feedback->reference); } public function test_no_license_headers_without_license(): void { Http::fake([self::URL => Http::response(['ok' => true, 'reference' => 'BUG-1'], 201)]); app(FeedbackOutbox::class)->submit(null, 'bugreport', $this->payload()); Http::assertSent(fn (Request $request) => ! $request->hasHeader('X-License-Email') && ! $request->hasHeader('Authorization') && ! $request->hasHeader('X-Device-Id')); } public function test_expired_license_does_not_prevent_sending(): void { app(LicenseStore::class)->setLicense('lizenz@firma.de', 'ABGELAUFEN'); app(LicenseStore::class)->rememberStatus(null, 'license_expired'); Http::fake([self::URL => Http::response(['ok' => true, 'reference' => 'BUG-2'], 201)]); $result = app(FeedbackOutbox::class)->submit(null, 'bugreport', $this->payload()); $this->assertSame(FeedbackResult::SENT, $result->status); Http::assertSentCount(1); } public function test_422_shows_error_on_field_and_keeps_input(): void { Http::fake([self::URL => Http::response(['code' => 'validation_failed', 'errors' => ['title' => ['Der Betreff ist zu lang.']]], 422)]); $this->actingAs(User::factory()->admin()->create()); Livewire::test(Feedback::class) ->set('type', 'bugreport') ->set('subject', 'Betreff') ->set('body', 'Eine ausreichend lange Beschreibung.') ->call('submit') ->assertHasErrors(['subject']) ->assertSet('subject', 'Betreff') ->assertSet('body', 'Eine ausreichend lange Beschreibung.'); $this->assertSame(0, VendorFeedback::query()->count()); } public function test_429_names_the_waiting_time_and_keeps_input(): void { Http::fake([self::URL => Http::response(['code' => 'rate_limited'], 429, ['Retry-After' => '600'])]); $this->actingAs(User::factory()->admin()->create()); Livewire::test(Feedback::class) ->set('type', 'bugreport') ->set('subject', 'Betreff') ->set('body', 'Eine ausreichend lange Beschreibung.') ->call('submit') ->assertSet('notice', fn (string $notice) => str_contains($notice, 'in etwa 10 Minute(n)')) ->assertSet('body', 'Eine ausreichend lange Beschreibung.'); Http::assertSentCount(1); $this->assertSame(0, VendorFeedback::query()->count()); } public function test_404_hides_the_channel_permanently(): void { Http::fake([self::URL => Http::response(['code' => 'not_found'], 404)]); $this->actingAs(User::factory()->admin()->create()); Livewire::test(Feedback::class) ->set('type', 'bugreport') ->set('subject', 'Betreff') ->set('body', 'Eine ausreichend lange Beschreibung.') ->call('submit') ->assertSet('type', 'featurerequest') ->assertSet('body', 'Eine ausreichend lange Beschreibung.'); $this->assertSame(['featurerequest' => 'Funktionswunsch'], app(FeedbackChannels::class)->available()); } public function test_only_enabled_channels_are_offered(): void { Cache::put('feedback.endpoints', ['featurerequest', 'telemetry'], 60); $this->assertSame(['featurerequest' => 'Funktionswunsch'], app(FeedbackChannels::class)->available()); } public function test_channels_are_read_from_software_list(): void { Cache::forget('feedback.endpoints'); Http::fake(['api.loheide.cloud/v1/software' => Http::response(['software' => [ ['slug' => 'andere', 'endpoints' => ['bugreport']], ['slug' => 'mitarbeiterportal', 'endpoints' => ['bugreport', 'telemetry']], ]])]); $this->assertSame(['bugreport' => 'Fehlerbericht'], app(FeedbackChannels::class)->available()); } public function test_503_queues_and_retries_with_same_idempotency_key(): void { Http::fakeSequence(self::URL) ->push(['code' => 'maintenance'], 503) ->push(['code' => 'maintenance'], 503) ->push(['ok' => true, 'reference' => 'BUG-SPAET'], 201); $result = app(FeedbackOutbox::class)->submit(null, 'bugreport', $this->payload()); $this->assertSame(FeedbackResult::RETRY, $result->status); $feedback = VendorFeedback::query()->sole(); $this->assertSame('pending', $feedback->status); $this->assertSame(1, $feedback->attempts); // Noch nicht fällig. $this->assertSame(0, app(FeedbackOutbox::class)->processDue()); $this->travel(2)->minutes(); app(FeedbackOutbox::class)->processDue(); $this->travel(10)->minutes(); app(FeedbackOutbox::class)->processDue(); $feedback->refresh(); $this->assertSame('sent', $feedback->status); $this->assertSame('BUG-SPAET', $feedback->reference); $keys = collect(Http::recorded())->map(fn ($pair) => $pair[0]->header('Idempotency-Key')[0])->unique(); $this->assertCount(1, $keys, 'Wiederholungen müssen denselben Idempotency-Key senden.'); } public function test_gives_up_after_five_attempts_and_informs_the_user(): void { Http::fake([self::URL => Http::response('', 503)]); $user = User::factory()->admin()->create(); app(FeedbackOutbox::class)->submit($user, 'bugreport', $this->payload()); for ($i = 0; $i < 6; $i++) { $this->travel(2)->hours(); app(FeedbackOutbox::class)->processDue(); } $feedback = VendorFeedback::query()->sole(); $this->assertSame('discarded', $feedback->status); $this->assertSame(5, $feedback->attempts); Http::assertSentCount(5); $this->assertSame(1, $user->notifications()->count()); } public function test_network_failure_is_queued(): void { Http::fake([self::URL => fn () => throw new ConnectionException('offline')]); $result = app(FeedbackOutbox::class)->submit(null, 'bugreport', $this->payload()); $this->assertSame(FeedbackResult::RETRY, $result->status); $this->assertSame('pending', VendorFeedback::query()->sole()->status); } public function test_409_is_not_shown_as_error_but_retried(): void { Http::fake([self::URL => Http::response(['code' => 'in_progress'], 409)]); $result = app(FeedbackOutbox::class)->submit(null, 'bugreport', $this->payload()); $this->assertSame(FeedbackResult::RETRY, $result->status); $this->assertTrue(VendorFeedback::query()->sole()->next_attempt_at->isFuture()); } public function test_page_shows_technical_info_and_privacy_notice_and_is_admin_only(): void { $this->actingAs(User::factory()->create())->get('/admin/rueckmeldung')->assertForbidden(); $this->actingAs(User::factory()->admin()->create()) ->get('/admin/rueckmeldung') ->assertOk() ->assertSee('Diese technischen Angaben werden mitgesendet') ->assertSee(Version::VERSION) ->assertSee(str_repeat('9', 40)) ->assertSee('Mit dem Absenden werden Daten an api.loheide.cloud übertragen.') ->assertDontSee('die von Ihnen angehängten Dateien') ->assertDontSee('Ihre Lizenzdaten, damit die Meldung'); } }