fix(models): correct UserSetting import namespace and add test coverage
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Auth;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class EmailNotificationTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_verified_user_is_redirected(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->actingAs($user)
|
||||
->post(route('verification.send'))
|
||||
->assertRedirect(route('dashboard', absolute: false));
|
||||
}
|
||||
|
||||
public function test_unverified_user_gets_notification(): void
|
||||
{
|
||||
$user = User::factory()->unverified()->create();
|
||||
|
||||
$this->actingAs($user)
|
||||
->post(route('verification.send'))
|
||||
->assertSessionHas('status', 'verification-link-sent');
|
||||
}
|
||||
}
|
||||
@@ -55,4 +55,13 @@ class EmailVerificationTest extends TestCase
|
||||
|
||||
$this->assertFalse($user->fresh()->hasVerifiedEmail());
|
||||
}
|
||||
|
||||
public function test_verified_user_is_redirected_from_prompt(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->get('/verify-email');
|
||||
|
||||
$response->assertRedirect(route('dashboard', absolute: false));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Auth;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class LoginRequestTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_throttles_too_many_attempts(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
for ($i = 0; $i < 6; $i++) {
|
||||
$this->post('/login', [
|
||||
'email' => $user->email,
|
||||
'password' => 'wrong-password',
|
||||
]);
|
||||
}
|
||||
|
||||
$this->post('/login', [
|
||||
'email' => $user->email,
|
||||
'password' => 'password',
|
||||
])->assertSessionHasErrors('email');
|
||||
}
|
||||
|
||||
public function test_successful_login_redirects(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->post('/login', [
|
||||
'email' => $user->email,
|
||||
'password' => 'password',
|
||||
])->assertRedirect(route('dashboard', absolute: false));
|
||||
|
||||
$this->assertAuthenticated();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Auth;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Notifications\ResetPassword;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Tests\TestCase;
|
||||
|
||||
class NewPasswordControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_password_reset_screen_can_be_rendered(): void
|
||||
{
|
||||
Notification::fake();
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->post('/forgot-password', ['email' => $user->email]);
|
||||
|
||||
Notification::assertSentTo($user, ResetPassword::class, function ($notification) {
|
||||
$response = $this->get('/reset-password/'.$notification->token);
|
||||
|
||||
$response->assertStatus(200);
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
public function test_password_can_be_reset(): void
|
||||
{
|
||||
Notification::fake();
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->post('/forgot-password', ['email' => $user->email]);
|
||||
|
||||
Notification::assertSentTo($user, ResetPassword::class, function ($notification) use ($user) {
|
||||
$response = $this->post('/reset-password', [
|
||||
'token' => $notification->token,
|
||||
'email' => $user->email,
|
||||
'password' => 'new-password',
|
||||
'password_confirmation' => 'new-password',
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertSessionHasNoErrors()
|
||||
->assertRedirect(route('login'));
|
||||
|
||||
$this->assertTrue(
|
||||
\Hash::check('new-password', $user->fresh()->password)
|
||||
);
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
public function test_password_reset_fails_with_invalid_token(): void
|
||||
{
|
||||
$response = $this->post('/reset-password', [
|
||||
'token' => 'invalid-token',
|
||||
'email' => 'test@example.com',
|
||||
'password' => 'password',
|
||||
'password_confirmation' => 'password',
|
||||
]);
|
||||
|
||||
$response->assertSessionHasErrors('email');
|
||||
}
|
||||
|
||||
public function test_password_reset_validates_required_fields(): void
|
||||
{
|
||||
$response = $this->post('/reset-password', []);
|
||||
|
||||
$response->assertSessionHasErrors(['token', 'email', 'password']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Auth;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Notifications\ResetPassword;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PasswordResetLinkControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_forgot_password_screen_can_be_rendered(): void
|
||||
{
|
||||
$response = $this->get('/forgot-password');
|
||||
|
||||
$response->assertStatus(200);
|
||||
}
|
||||
|
||||
public function test_password_reset_link_can_be_requested(): void
|
||||
{
|
||||
Notification::fake();
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->post('/forgot-password', ['email' => $user->email]);
|
||||
|
||||
Notification::assertSentTo($user, ResetPassword::class);
|
||||
}
|
||||
|
||||
public function test_password_reset_link_fails_with_invalid_email(): void
|
||||
{
|
||||
$response = $this->post('/forgot-password', [
|
||||
'email' => 'nonexistent@example.com',
|
||||
]);
|
||||
|
||||
$response->assertSessionHasErrors('email');
|
||||
}
|
||||
|
||||
public function test_password_reset_link_validates_email(): void
|
||||
{
|
||||
$response = $this->post('/forgot-password', [
|
||||
'email' => '',
|
||||
]);
|
||||
|
||||
$response->assertSessionHasErrors('email');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Auth;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Events\Verified;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
use Tests\TestCase;
|
||||
|
||||
class VerifyEmailControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_already_verified_user_is_redirected(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$verificationUrl = URL::temporarySignedRoute(
|
||||
'verification.verify',
|
||||
now()->addMinutes(60),
|
||||
['id' => $user->id, 'hash' => sha1($user->email)]
|
||||
);
|
||||
|
||||
$response = $this->actingAs($user)->get($verificationUrl);
|
||||
|
||||
$response->assertRedirect(route('dashboard', absolute: false).'?verified=1');
|
||||
}
|
||||
|
||||
public function test_unverified_user_email_can_be_verified(): void
|
||||
{
|
||||
$user = User::factory()->unverified()->create();
|
||||
|
||||
Event::fake();
|
||||
|
||||
$verificationUrl = URL::temporarySignedRoute(
|
||||
'verification.verify',
|
||||
now()->addMinutes(60),
|
||||
['id' => $user->id, 'hash' => sha1($user->email)]
|
||||
);
|
||||
|
||||
$response = $this->actingAs($user)->get($verificationUrl);
|
||||
|
||||
Event::assertDispatched(Verified::class);
|
||||
$this->assertTrue($user->fresh()->hasVerifiedEmail());
|
||||
$response->assertRedirect(route('dashboard', absolute: false).'?verified=1');
|
||||
}
|
||||
|
||||
public function test_email_not_verified_with_invalid_hash(): void
|
||||
{
|
||||
$user = User::factory()->unverified()->create();
|
||||
|
||||
$verificationUrl = URL::temporarySignedRoute(
|
||||
'verification.verify',
|
||||
now()->addMinutes(60),
|
||||
['id' => $user->id, 'hash' => sha1('wrong-email')]
|
||||
);
|
||||
|
||||
$this->actingAs($user)->get($verificationUrl);
|
||||
|
||||
$this->assertFalse($user->fresh()->hasVerifiedEmail());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Games;
|
||||
|
||||
use App\Features\Consoles\Models\Console;
|
||||
use App\Features\Games\Models\Game;
|
||||
use App\Features\Games\Models\RomFile;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Tests\TestCase;
|
||||
|
||||
class GameControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_game_edit_returns_404_for_invalid_slug(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->actingAs($user)
|
||||
->get(route('games.edit', 'nonexistent'))
|
||||
->assertNotFound();
|
||||
}
|
||||
|
||||
public function test_game_update_returns_404_for_invalid_slug(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->actingAs($user)
|
||||
->put(route('games.update', 'nonexistent'), ['title' => 'Test'])
|
||||
->assertNotFound();
|
||||
}
|
||||
|
||||
public function test_game_destroy_returns_404_for_invalid_slug(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->actingAs($user)
|
||||
->delete(route('games.destroy', 'nonexistent'))
|
||||
->assertNotFound();
|
||||
}
|
||||
|
||||
public function test_game_can_be_deleted_with_rom_files(): void
|
||||
{
|
||||
Storage::fake('local');
|
||||
$user = User::factory()->create();
|
||||
$console = Console::factory()->create();
|
||||
$game = Game::factory()->create(['console_id' => $console->id]);
|
||||
|
||||
RomFile::factory()->create([
|
||||
'game_id' => $game->id,
|
||||
'file_path' => 'roms/nes/game.nes',
|
||||
]);
|
||||
|
||||
Storage::disk('local')->put('roms/nes/game.nes', 'rom-data');
|
||||
|
||||
$this->actingAs($user)
|
||||
->delete(route('games.destroy', $game->slug))
|
||||
->assertRedirect();
|
||||
|
||||
$this->assertDatabaseMissing('games', ['id' => $game->id]);
|
||||
}
|
||||
|
||||
public function test_game_can_be_deleted_with_cover(): void
|
||||
{
|
||||
Storage::fake('local');
|
||||
$user = User::factory()->create();
|
||||
$console = Console::factory()->create();
|
||||
$game = Game::factory()->create([
|
||||
'console_id' => $console->id,
|
||||
'cover_path' => 'covers/nes/game-cover.jpg',
|
||||
]);
|
||||
|
||||
Storage::disk('local')->put('covers/nes/game-cover.jpg', 'cover-data');
|
||||
|
||||
$this->actingAs($user)
|
||||
->delete(route('games.destroy', $game->slug))
|
||||
->assertRedirect();
|
||||
|
||||
$this->assertDatabaseMissing('games', ['id' => $game->id]);
|
||||
}
|
||||
|
||||
public function test_game_download_returns_file(): void
|
||||
{
|
||||
Storage::fake('local');
|
||||
$user = User::factory()->create();
|
||||
$console = Console::factory()->create();
|
||||
$game = Game::factory()->create(['console_id' => $console->id]);
|
||||
|
||||
$romFile = RomFile::factory()->create([
|
||||
'game_id' => $game->id,
|
||||
'file_path' => 'roms/nes/game.nes',
|
||||
'file_name' => 'game.nes',
|
||||
]);
|
||||
|
||||
Storage::disk('local')->put('roms/nes/game.nes', 'rom-data');
|
||||
|
||||
$this->actingAs($user)
|
||||
->get(route('games.download', [$game->slug, $romFile->id]))
|
||||
->assertOk();
|
||||
}
|
||||
|
||||
public function test_game_download_returns_404_for_invalid_rom(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$console = Console::factory()->create();
|
||||
$game = Game::factory()->create(['console_id' => $console->id]);
|
||||
|
||||
$this->actingAs($user)
|
||||
->get(route('games.download', [$game->slug, 99999]))
|
||||
->assertNotFound();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Jobs;
|
||||
|
||||
use App\Features\Consoles\Models\Console;
|
||||
use App\Features\Games\Models\Game;
|
||||
use App\Features\Scraper\Services\ScraperService;
|
||||
use App\Jobs\ScrapeCoverJob;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ScrapeCoverJobTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private ScraperService|MockObject $scraper;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->scraper = $this->createMock(ScraperService::class);
|
||||
}
|
||||
|
||||
public function test_job_updates_cover_when_match_found(): void
|
||||
{
|
||||
Storage::fake('local');
|
||||
$console = Console::factory()->create(['slug' => 'nes']);
|
||||
$game = Game::factory()->create(['console_id' => $console->id]);
|
||||
$game->load('console');
|
||||
|
||||
$match = new \stdClass();
|
||||
$match->coverUrl = 'https://example.com/cover.jpg';
|
||||
|
||||
$this->scraper->expects($this->once())
|
||||
->method('search')
|
||||
->willReturn(collect([$match]));
|
||||
|
||||
$this->scraper->expects($this->once())
|
||||
->method('downloadCover')
|
||||
->willReturn('covers/nes/super-mario-cover.jpg');
|
||||
|
||||
$job = new ScrapeCoverJob($game);
|
||||
$job->handle($this->scraper);
|
||||
|
||||
$game->refresh();
|
||||
$this->assertEquals('covers/nes/super-mario-cover.jpg', $game->cover_path);
|
||||
}
|
||||
|
||||
public function test_job_does_nothing_when_no_results(): void
|
||||
{
|
||||
$game = Game::factory()->create();
|
||||
|
||||
$this->scraper->expects($this->once())
|
||||
->method('search')
|
||||
->willReturn(collect());
|
||||
|
||||
$this->scraper->expects($this->never())
|
||||
->method('downloadCover');
|
||||
|
||||
$job = new ScrapeCoverJob($game);
|
||||
$job->handle($this->scraper);
|
||||
|
||||
$game->refresh();
|
||||
$this->assertNull($game->cover_path);
|
||||
}
|
||||
|
||||
public function test_job_does_nothing_when_no_cover_url(): void
|
||||
{
|
||||
$game = Game::factory()->create();
|
||||
|
||||
$match = new \stdClass();
|
||||
$match->coverUrl = null;
|
||||
|
||||
$this->scraper->expects($this->once())
|
||||
->method('search')
|
||||
->willReturn(collect([$match]));
|
||||
|
||||
$this->scraper->expects($this->never())
|
||||
->method('downloadCover');
|
||||
|
||||
$job = new ScrapeCoverJob($game);
|
||||
$job->handle($this->scraper);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Scraper;
|
||||
|
||||
use App\Features\Consoles\Models\Console;
|
||||
use App\Features\Games\Models\Game;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ScraperControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_search_requires_authentication(): void
|
||||
{
|
||||
$this->getJson(route('scraper.search', ['query' => 'mario']))
|
||||
->assertRedirect();
|
||||
}
|
||||
|
||||
public function test_search_validates_query(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->actingAs($user)
|
||||
->getJson(route('scraper.search', ['query' => '']))
|
||||
->assertSessionHasErrors('query');
|
||||
}
|
||||
|
||||
public function test_search_validates_min_query_length(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->actingAs($user)
|
||||
->getJson(route('scraper.search', ['query' => 'a']))
|
||||
->assertSessionHasErrors('query');
|
||||
}
|
||||
|
||||
public function test_search_returns_results(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
Http::fake([
|
||||
'https://id.twitch.tv/oauth2/token' => Http::response(['access_token' => 'fake-token']),
|
||||
'https://api.igdb.com/v4/games' => Http::response([
|
||||
[
|
||||
'id' => 1,
|
||||
'name' => 'Super Mario Bros',
|
||||
'cover' => null,
|
||||
'release_dates' => [],
|
||||
],
|
||||
]),
|
||||
]);
|
||||
|
||||
config([
|
||||
'services.igdb.client_id' => 'test-id',
|
||||
'services.igdb.client_secret' => 'test-secret',
|
||||
]);
|
||||
|
||||
$this->actingAs($user)
|
||||
->getJson(route('scraper.search', ['query' => 'mario']))
|
||||
->assertOk()
|
||||
->assertJsonCount(1, 'results');
|
||||
}
|
||||
|
||||
public function test_apply_requires_authentication(): void
|
||||
{
|
||||
$this->post(route('scraper.apply', 'some-game'))
|
||||
->assertRedirect(route('login'));
|
||||
}
|
||||
|
||||
public function test_apply_validates_cover_url(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$game = Game::factory()->create();
|
||||
|
||||
$this->actingAs($user)
|
||||
->post(route('scraper.apply', $game->slug), [])
|
||||
->assertSessionHasErrors('cover_url');
|
||||
}
|
||||
|
||||
public function test_apply_downloads_and_updates_cover(): void
|
||||
{
|
||||
Storage::fake('local');
|
||||
$user = User::factory()->create();
|
||||
$game = Game::factory()->create();
|
||||
$game->load('console');
|
||||
|
||||
Http::fake([
|
||||
'https://example.com/cover.jpg' => Http::response('image-data'),
|
||||
]);
|
||||
|
||||
$this->actingAs($user)
|
||||
->post(route('scraper.apply', $game->slug), [
|
||||
'cover_url' => 'https://example.com/cover.jpg',
|
||||
])
|
||||
->assertRedirect();
|
||||
|
||||
$game->refresh();
|
||||
$this->assertNotNull($game->cover_path);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,8 @@
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Features\Consoles\Models\Console;
|
||||
use App\Features\Games\Models\Game;
|
||||
use App\Features\Games\Models\RomFile;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
@@ -88,4 +90,29 @@ class UploadTest extends TestCase
|
||||
|
||||
Storage::disk('local')->assertExists("roms/{$console->slug}/game.nes");
|
||||
}
|
||||
|
||||
public function test_upload_store_skips_duplicate_rom_files(): void
|
||||
{
|
||||
Storage::fake('local');
|
||||
$user = User::factory()->create();
|
||||
$console = Console::factory()->create(['rom_extensions' => ['nes']]);
|
||||
|
||||
$content = str_repeat('x', 1024);
|
||||
$hash = hash('sha256', $content);
|
||||
|
||||
$game = Game::factory()->create(['console_id' => $console->id]);
|
||||
RomFile::factory()->create([
|
||||
'game_id' => $game->id,
|
||||
'file_hash' => $hash,
|
||||
]);
|
||||
|
||||
$file = UploadedFile::fake()->createWithContent('game.nes', $content);
|
||||
|
||||
$this->actingAs($user)
|
||||
->post(route('upload.store'), [
|
||||
'console_id' => $console->id,
|
||||
'rom_files' => [$file],
|
||||
])
|
||||
->assertRedirect();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user