Port the Fuc 0.12.0 frontend and default-off VIP plugin. Include the previously approved password-reset token revocation. Deploy only to an independent FUC-XB test instance; no production cutover.
147 lines
5.6 KiB
PHP
147 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Services\Auth;
|
|
|
|
use App\Models\User;
|
|
use App\Services\Auth\LoginService;
|
|
use App\Utils\CacheKey;
|
|
use App\Utils\Helper;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Tests\TestCase;
|
|
|
|
class LoginServiceTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private LoginService $service;
|
|
|
|
protected function beforeRefreshingDatabase(): void
|
|
{
|
|
config([
|
|
'app.env' => '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('[email protected]', '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('[email protected]', 'old-password');
|
|
$subscriptionToken = $user->token;
|
|
$user->createToken('first-device');
|
|
$user->createToken('second-device');
|
|
$other = $this->createUser('[email protected]', '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('[email protected]', '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('[email protected]', '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' => '[email protected]',
|
|
'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(),
|
|
]);
|
|
}
|
|
}
|