'testing', 'database.default' => 'sqlite', 'database.connections.sqlite.database' => ':memory:', 'cache.default' => 'array', 'cache.stores.redis' => ['driver' => 'array'], ]); DB::purge('sqlite'); Cache::forgetDriver(['redis', 'array']); \Illuminate\Support\Facades\Http::preventStrayRequests(); \Illuminate\Support\Facades\Queue::fake(); \Illuminate\Support\Facades\Mail::fake(); } protected function setUp(): void { parent::setUp(); Cache::flush(); $this->service = app(LoginService::class); } public function test_reset_password_rejects_missing_cached_email_code(): void { $user = $this->createUser('victim@example.com', 'old-password'); $user->createToken('existing-device'); [$success, $result] = $this->service->resetPassword($user->email, '', 'new-password'); $this->assertFalse($success); $this->assertSame(400, $result[0]); $user->refresh(); $this->assertTrue(password_verify('old-password', $user->password)); $this->assertSame(1, $user->tokens()->count()); } public function test_reset_password_accepts_matching_cached_email_code(): void { $user = $this->createUser('user@example.com', 'old-password'); $subscriptionToken = $user->token; $user->createToken('first-device'); $user->createToken('second-device'); $other = $this->createUser('other@example.com', 'other-password'); $other->createToken('unrelated-device'); Cache::put(CacheKey::get('EMAIL_VERIFY_CODE', $user->email), 123456, 300); [$success, $result] = $this->service->resetPassword($user->email, '123456', 'new-password'); $this->assertTrue($success); $this->assertTrue($result); $user->refresh(); $this->assertTrue(password_verify('new-password', $user->password)); $this->assertNull(Cache::get(CacheKey::get('EMAIL_VERIFY_CODE', $user->email))); $this->assertSame(0, $user->tokens()->count()); $this->assertSame(1, $other->tokens()->count()); $this->assertSame($subscriptionToken, $user->token); } public function test_wrong_code_preserves_password_and_login_sessions(): void { $user = $this->createUser('wrong-code@example.com', 'old-password'); $user->createToken('existing-device'); Cache::put(CacheKey::get('EMAIL_VERIFY_CODE', $user->email), 123456, 300); [$success] = $this->service->resetPassword($user->email, '654321', 'new-password'); $this->assertFalse($success); $this->assertTrue(password_verify('old-password', $user->fresh()->password)); $this->assertSame(1, $user->tokens()->count()); $this->assertSame(123456, Cache::get(CacheKey::get('EMAIL_VERIFY_CODE', $user->email))); } public function test_revocation_failure_rolls_back_password_change(): void { if (DB::getDriverName() !== 'sqlite') { $this->markTestSkipped('SQLite trigger supplies the isolated failure injection'); } $user = $this->createUser('rollback@example.com', 'old-password'); $user->createToken('existing-device'); Cache::put(CacheKey::get('EMAIL_VERIFY_CODE', $user->email), 123456, 300); DB::unprepared("CREATE TEMP TRIGGER reject_reset_token_delete BEFORE DELETE ON personal_access_tokens BEGIN SELECT RAISE(ABORT, 'test revocation failure'); END"); try { $this->service->resetPassword($user->email, '123456', 'new-password'); $this->fail('The failed revocation must not report a successful reset'); } catch (\Illuminate\Database\QueryException $error) { $this->assertStringContainsString('test revocation failure', $error->getMessage()); } finally { DB::unprepared('DROP TRIGGER reject_reset_token_delete'); } $this->assertTrue(password_verify('old-password', $user->fresh()->password)); $this->assertSame(1, $user->tokens()->count()); $this->assertSame(123456, Cache::get(CacheKey::get('EMAIL_VERIFY_CODE', $user->email))); } public function test_forget_password_validation_rejects_boolean_email_code(): void { $validator = Validator::make([ 'email' => 'victim@example.com', 'password' => 'new-password', 'email_code' => false, ], [ 'email' => 'required|email:strict', 'password' => 'required|min:8', 'email_code' => 'required|digits:6', ]); $this->assertTrue($validator->fails()); $this->assertArrayHasKey('email_code', $validator->errors()->toArray()); } private function createUser(string $email, string $password): User { return User::query()->create([ 'email' => $email, 'password' => password_hash($password, PASSWORD_DEFAULT), 'uuid' => Helper::guid(true), 'token' => Helper::guid(true), 'created_at' => time(), 'updated_at' => time(), ]); } }