extend(TestCase::class) ->use(RefreshDatabase::class) ->beforeEach(function () { // EnsureSetupComplete middleware redirects to /setup unless an admin exists. User::factory()->admin()->create(['email' => 'admin-setup@test.com']); }) ->in('Feature', 'Browser', 'Screenshots'); /* |-------------------------------------------------------------------------- | Functions |-------------------------------------------------------------------------- | | While Pest is very powerful out-of-the-box, you may have some testing code specific to your | project that you don't want to repeat in every file. Here you can also expose helpers as | global functions to help you to reduce the number of lines of code in your test files. | */ /** * A page of SealShare, once it can be used: loaded, with Alpine and Livewire started. Shared by * every file under tests/Browser, so a browser test needs no visit() of its own to define it. */ function ready(mixed $page): mixed { return $page->waitForEvent('networkidle') ->assertScript("document.readyState === 'complete' && typeof window.Alpine !== 'undefined' && typeof window.Livewire !== 'undefined'"); } /** * A chunk of a registered file, encrypted with its share's key and its header's nonce prefix. */ function encryptedChunk(ShareFile $file, string $plaintext, int $index, bool $isLast): string { $encryption = app(FileEncryptionService::class); $header = $encryption->parseHeader(file_get_contents(app(ShareService::class)->storedFilePath($file), false, null, 0, FileEncryptionService::HEADER_LENGTH)); return $encryption->encryptChunk($plaintext, $file->share->encryption_key, $header['noncePrefix'], $index, $isLast); } /** * Encrypt a whole file in the SEALCHK2 format, as the uploader's browser does chunk by chunk. */ function encryptTestFile(string $sourcePath, string $destinationPath, string $key, int $chunkSize): void { $encryption = new FileEncryptionService; $header = $encryption->createHeader($chunkSize); $noncePrefix = $encryption->parseHeader($header)['noncePrefix']; $plaintext = (string) file_get_contents($sourcePath); $chunkCount = $encryption->chunkCount(strlen($plaintext), $chunkSize); $chunks = array_map( fn (int $index): string => $encryption->encryptChunk(substr($plaintext, $index * $chunkSize, $chunkSize), $key, $noncePrefix, $index, $index === $chunkCount - 1), range(0, $chunkCount - 1), ); file_put_contents($destinationPath, $header.implode('', $chunks)); }