Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions tests/Feature/ExampleTest.php

This file was deleted.

55 changes: 55 additions & 0 deletions tests/Feature/Http/Controllers/DashboardControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

use Database\Factories\TorrentFactory;
use Database\Factories\UserFactory;
use Illuminate\Http\UploadedFile;

it('shows the upload form to a guest', function () {
$this->get(route('dashboard'))
->assertOk()
->assertSee('Click to upload')
->assertSee(route('upload'));
});

it('does not show the torrent list to a guest', function () {
$this->get(route('dashboard'))
->assertOk()
->assertDontSee('You have not uploaded any torrents.');
});

it('shows a signed in user their uploaded torrents', function () {
$user = UserFactory::new()->create();
$torrent = TorrentFactory::new()->create(['filename' => 'example.bin']);
$user->torrents()->attach($torrent);

$this->actingAs($user)->get(route('dashboard'))
->assertOk()
->assertSee('example.bin')
->assertSee(route('details', ['torrent' => $torrent->hash]));
});

it('tells a signed in user when they have uploaded nothing', function () {
$this->actingAs(UserFactory::new()->create())
->get(route('dashboard'))
->assertOk()
->assertSee('You have not uploaded any torrents.');
});

it('reports why an upload was rejected', function () {
$this->from(route('dashboard'))
->post(route('upload'), [
'torrent' => UploadedFile::fake()->createWithContent('bad.torrent', 'this is not bencoded'),
])
->assertRedirect(route('dashboard'));

$this->followingRedirects()
->from(route('dashboard'))
->post(route('upload'), [
'torrent' => UploadedFile::fake()->createWithContent('bad.torrent', 'this is not bencoded'),
])
->assertOk()
->assertSee('That torrent could not be uploaded')
->assertSee('Invalid torrent file provided.');
});
47 changes: 47 additions & 0 deletions tests/Feature/Http/Controllers/DetailsControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

use Database\Factories\TorrentFactory;

it('shows everything the details page promises about a torrent', function () {
$torrent = TorrentFactory::new()->create([
'filename' => 'example.bin',
'size' => 1048576,
'downloads' => 1234,
]);

$this->get(route('details', ['torrent' => $torrent->hash]))
->assertOk()
->assertSee($torrent->hash)
->assertSee('example.bin')
->assertSee('1.00 MB')
->assertSee('1,234')
->assertSee(route('download', ['torrent' => $torrent->hash]));
});

it('falls back to the hash when the original filename is unknown', function () {
$torrent = TorrentFactory::new()->create(['filename' => null]);

$this->get(route('details', ['torrent' => $torrent->hash]))
->assertOk()
->assertSee('Unknown')
->assertSee($torrent->hash);
});

it('is reachable by a guest', function () {
$torrent = TorrentFactory::new()->create();

$this->get(route('details', ['torrent' => $torrent->hash]))->assertOk();
});

it('looks a torrent up by its hash rather than its id', function () {
$torrent = TorrentFactory::new()->create();

$this->get("/torrents/{$torrent->id}")->assertNotFound();
$this->get("/torrents/{$torrent->hash}")->assertOk();
});

it('returns a 404 for a hash that was never uploaded', function () {
$this->get(route('details', ['torrent' => str_repeat('a', 40)]))->assertNotFound();
});
88 changes: 88 additions & 0 deletions tests/Feature/Http/Controllers/DownloadControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

use App\Models\Torrent;
use Database\Factories\TorrentFactory;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;

beforeEach(fn () => Storage::fake('torrents'));

/** Put a torrent on the disk the same way the upload pipeline would. */
function storedTorrent(string $contents = 'd8:announce0:e'): Torrent
{
$torrent = TorrentFactory::new()->create(['downloads' => 0]);

Storage::disk('torrents')->put("{$torrent->hash}.torrent", $contents);

return $torrent;
}

it('serves the stored file under the torrent hash', function () {
$torrent = storedTorrent('the-original-bytes');

$response = $this->get(route('download', ['torrent' => $torrent->hash]))
->assertOk()
->assertDownload("{$torrent->hash}.torrent");

expect($response->streamedContent())->toBe('the-original-bytes');
});

it('counts every download', function () {
$torrent = storedTorrent();

foreach (range(1, 3) as $expected) {
$this->get(route('download', ['torrent' => $torrent->hash]))->assertOk();

expect($torrent->refresh()->downloads)->toBe($expected);
}
});

it('is reachable by a guest', function () {
$torrent = storedTorrent();

$this->get(route('download', ['torrent' => $torrent->hash]))->assertOk();
});

it('returns a 404 for a hash that was never uploaded', function () {
$this->get(route('download', ['torrent' => str_repeat('a', 40)]))->assertNotFound();

Storage::disk('torrents')->assertDirectoryEmpty('');
});

it('returns a 404 when the row outlived its cached file', function () {
$torrent = TorrentFactory::new()->create(['downloads' => 0]);

$this->get(route('download', ['torrent' => $torrent->hash]))->assertNotFound();
});

it('does not count a download it could not serve', function () {
$torrent = TorrentFactory::new()->create(['downloads' => 7]);

$this->get(route('download', ['torrent' => $torrent->hash]))->assertNotFound();

expect($torrent->refresh()->downloads)->toBe(7);
});

it('logs the hash whose cached file has gone missing', function () {
Log::spy();

$torrent = TorrentFactory::new()->create();

$this->get(route('download', ['torrent' => $torrent->hash]))->assertNotFound();

Log::shouldHaveReceived('error')
->once()
->with('Cached torrent file is missing.', ['hash' => $torrent->hash]);
});

it('does not log when the torrent is served normally', function () {
Log::spy();

$torrent = storedTorrent();

$this->get(route('download', ['torrent' => $torrent->hash]))->assertOk();

Log::shouldNotHaveReceived('error');
});
9 changes: 0 additions & 9 deletions tests/Feature/Http/Controllers/HomeControllerTest.php

This file was deleted.

Loading