v7.0.0-beta.4 release
This commit is contained in:
1 parent
05db17887d
commit
1db039ff34
1545 files changed
+136783
-11634
No files matched your search
@@ -0,0 +1,183 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Extension\IdentityVerification;
|
||||
|
||||
use App\Contracts\Extension\IdentityVerificationInterface;
|
||||
use App\Extension\HookManager;
|
||||
use App\Extension\IdentityVerification\DTO\VerificationChallenge;
|
||||
use App\Extension\IdentityVerification\DTO\VerificationResult;
|
||||
use App\Extension\IdentityVerification\IdentityVerificationManager;
|
||||
use App\Models\User;
|
||||
use Carbon\Carbon;
|
||||
use InvalidArgumentException;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* IdentityVerificationManager 테스트.
|
||||
*
|
||||
* 프로바이더 등록/해제/resolveForPurpose/필터 훅 통과 후 병합 동작을 검증.
|
||||
*/
|
||||
class IdentityVerificationManagerTest extends TestCase
|
||||
{
|
||||
private IdentityVerificationManager $manager;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
HookManager::resetAll();
|
||||
$this->manager = new IdentityVerificationManager();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
HookManager::resetAll();
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function test_register_and_get_provider(): void
|
||||
{
|
||||
$provider = $this->makeProvider('dummy', true, true);
|
||||
$this->manager->register($provider);
|
||||
|
||||
$this->assertTrue($this->manager->has('dummy'));
|
||||
$this->assertSame($provider, $this->manager->get('dummy'));
|
||||
}
|
||||
|
||||
public function test_unregister_removes_provider(): void
|
||||
{
|
||||
$provider = $this->makeProvider('dummy', true, true);
|
||||
$this->manager->register($provider);
|
||||
$this->manager->unregister('dummy');
|
||||
|
||||
$this->assertFalse($this->manager->has('dummy'));
|
||||
}
|
||||
|
||||
public function test_get_unknown_throws(): void
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$this->manager->get('nonexistent');
|
||||
}
|
||||
|
||||
public function test_all_runs_through_filter_hook(): void
|
||||
{
|
||||
$mail = $this->makeProvider('g7:core.mail', true, true);
|
||||
$this->manager->register($mail);
|
||||
|
||||
$injected = $this->makeProvider('plugin:fake', true, true);
|
||||
HookManager::addFilter('core.identity.registered_providers', function (array $providers) use ($injected) {
|
||||
$providers[$injected->getId()] = $injected;
|
||||
|
||||
return $providers;
|
||||
});
|
||||
|
||||
$all = $this->manager->all();
|
||||
|
||||
$this->assertArrayHasKey('g7:core.mail', $all);
|
||||
$this->assertArrayHasKey('plugin:fake', $all);
|
||||
}
|
||||
|
||||
public function test_resolve_for_purpose_returns_provider_supporting_purpose(): void
|
||||
{
|
||||
$mail = $this->makeProvider('g7:core.mail', true, true);
|
||||
$this->manager->register($mail);
|
||||
|
||||
$resolved = $this->manager->resolveForPurpose('signup');
|
||||
|
||||
$this->assertSame('g7:core.mail', $resolved->getId());
|
||||
}
|
||||
|
||||
public function test_resolve_for_purpose_falls_back_when_default_unsupported(): void
|
||||
{
|
||||
$unsupported = $this->makeProvider('g7:core.mail', true, false);
|
||||
$supporter = $this->makeProvider('plugin:kcp', true, true);
|
||||
|
||||
$this->manager->register($unsupported);
|
||||
$this->manager->register($supporter);
|
||||
|
||||
$resolved = $this->manager->resolveForPurpose('signup');
|
||||
|
||||
$this->assertSame('plugin:kcp', $resolved->getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* IdentityVerificationInterface 더블을 생성합니다.
|
||||
*/
|
||||
private function makeProvider(string $id, bool $available, bool $supportsAll): IdentityVerificationInterface
|
||||
{
|
||||
return new class($id, $available, $supportsAll) implements IdentityVerificationInterface
|
||||
{
|
||||
public function __construct(
|
||||
private string $id,
|
||||
private bool $available,
|
||||
private bool $supportsAll,
|
||||
) {}
|
||||
|
||||
public function getId(): string
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getLabel(): string
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getChannels(): array
|
||||
{
|
||||
return ['email'];
|
||||
}
|
||||
|
||||
public function getRenderHint(): string
|
||||
{
|
||||
return 'text_code';
|
||||
}
|
||||
|
||||
public function supportsPurpose(string $purpose): bool
|
||||
{
|
||||
return $this->supportsAll;
|
||||
}
|
||||
|
||||
public function isAvailable(): bool
|
||||
{
|
||||
return $this->available;
|
||||
}
|
||||
|
||||
public function requestChallenge(User|array $target, array $context = []): VerificationChallenge
|
||||
{
|
||||
return new VerificationChallenge(
|
||||
id: 'dummy-id',
|
||||
providerId: $this->id,
|
||||
purpose: $context['purpose'] ?? 'sensitive_action',
|
||||
channel: 'email',
|
||||
targetHash: str_repeat('0', 64),
|
||||
expiresAt: Carbon::now()->addMinutes(15),
|
||||
renderHint: 'text_code',
|
||||
);
|
||||
}
|
||||
|
||||
public function verify(string $challengeId, array $input, array $context = []): VerificationResult
|
||||
{
|
||||
return VerificationResult::success(
|
||||
challengeId: $challengeId,
|
||||
providerId: $this->id,
|
||||
verifiedAt: Carbon::now(),
|
||||
);
|
||||
}
|
||||
|
||||
public function cancel(string $challengeId): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getSettingsSchema(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function withConfig(array $config): static
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Extension\IdentityVerification\Providers;
|
||||
|
||||
use App\Contracts\Repositories\IdentityVerificationLogRepositoryInterface;
|
||||
use App\Enums\IdentityVerificationStatus;
|
||||
use App\Extension\IdentityVerification\Providers\MailIdentityProvider;
|
||||
use App\Models\IdentityVerificationLog;
|
||||
use Database\Seeders\IdentityMessageDefinitionSeeder;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* MailIdentityProvider 테스트.
|
||||
*
|
||||
* Challenge 발행 / 검증 성공 / 잘못된 코드 / 만료 / 재시도 초과 / 취소 경로를 검증합니다.
|
||||
*/
|
||||
class MailIdentityProviderTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private MailIdentityProvider $provider;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
Notification::fake();
|
||||
Mail::fake();
|
||||
// dispatchMessage 가 IdentityMessageDefinition/Template 을 조회하므로 시더 필수.
|
||||
$this->seed(IdentityMessageDefinitionSeeder::class);
|
||||
$this->provider = $this->app->make(MailIdentityProvider::class);
|
||||
}
|
||||
|
||||
public function test_provider_metadata(): void
|
||||
{
|
||||
$this->assertSame('g7:core.mail', $this->provider->getId());
|
||||
$this->assertContains('email', $this->provider->getChannels());
|
||||
$this->assertTrue($this->provider->supportsPurpose('signup'));
|
||||
$this->assertTrue($this->provider->supportsPurpose('sensitive_action'));
|
||||
}
|
||||
|
||||
public function test_request_challenge_creates_log_and_sends_notification(): void
|
||||
{
|
||||
$challenge = $this->provider->requestChallenge(
|
||||
['email' => 'user@example.com'],
|
||||
['purpose' => 'signup', 'ip_address' => '127.0.0.1'],
|
||||
);
|
||||
|
||||
$this->assertSame('g7:core.mail', $challenge->providerId);
|
||||
$this->assertSame('email', $challenge->channel);
|
||||
$this->assertSame('text_code', $challenge->renderHint);
|
||||
$this->assertSame(hash('sha256', 'user@example.com'), $challenge->targetHash);
|
||||
|
||||
$log = IdentityVerificationLog::find($challenge->id);
|
||||
$this->assertNotNull($log);
|
||||
$this->assertSame(IdentityVerificationStatus::Sent, $log->status);
|
||||
$this->assertArrayHasKey('code_hash', $log->metadata ?? []);
|
||||
}
|
||||
|
||||
public function test_request_challenge_password_reset_uses_link_render_hint(): void
|
||||
{
|
||||
$challenge = $this->provider->requestChallenge(
|
||||
['email' => 'user@example.com'],
|
||||
['purpose' => 'password_reset'],
|
||||
);
|
||||
|
||||
$this->assertSame('link', $challenge->renderHint);
|
||||
|
||||
$log = IdentityVerificationLog::find($challenge->id);
|
||||
$this->assertArrayHasKey('link_token_hash', $log->metadata ?? []);
|
||||
}
|
||||
|
||||
public function test_verify_with_correct_code_succeeds(): void
|
||||
{
|
||||
/** @var IdentityVerificationLogRepositoryInterface $repo */
|
||||
$repo = $this->app->make(IdentityVerificationLogRepositoryInterface::class);
|
||||
|
||||
$challenge = $this->provider->requestChallenge(
|
||||
['email' => 'user@example.com'],
|
||||
['purpose' => 'sensitive_action'],
|
||||
);
|
||||
|
||||
// 테스트 목적으로 메타데이터에서 알려진 코드의 해시를 직접 주입
|
||||
$knownCode = '123456';
|
||||
$repo->updateById($challenge->id, [
|
||||
'metadata' => ['code_hash' => password_hash($knownCode, PASSWORD_BCRYPT)],
|
||||
'status' => IdentityVerificationStatus::Sent->value,
|
||||
]);
|
||||
|
||||
$result = $this->provider->verify($challenge->id, ['code' => $knownCode]);
|
||||
|
||||
$this->assertTrue($result->success);
|
||||
$this->assertNotNull($result->verifiedAt);
|
||||
|
||||
$log = IdentityVerificationLog::find($challenge->id);
|
||||
$this->assertSame(IdentityVerificationStatus::Verified, $log->status);
|
||||
$this->assertNotNull($log->verification_token);
|
||||
}
|
||||
|
||||
public function test_verify_with_wrong_code_increments_attempts(): void
|
||||
{
|
||||
/** @var IdentityVerificationLogRepositoryInterface $repo */
|
||||
$repo = $this->app->make(IdentityVerificationLogRepositoryInterface::class);
|
||||
|
||||
$challenge = $this->provider->requestChallenge(
|
||||
['email' => 'user@example.com'],
|
||||
['purpose' => 'sensitive_action'],
|
||||
);
|
||||
|
||||
$repo->updateById($challenge->id, [
|
||||
'metadata' => ['code_hash' => password_hash('123456', PASSWORD_BCRYPT)],
|
||||
'status' => IdentityVerificationStatus::Sent->value,
|
||||
]);
|
||||
|
||||
$result = $this->provider->verify($challenge->id, ['code' => '999999']);
|
||||
|
||||
$this->assertFalse($result->success);
|
||||
$this->assertSame('INVALID_CODE', $result->failureCode);
|
||||
|
||||
$log = IdentityVerificationLog::find($challenge->id);
|
||||
$this->assertSame(1, $log->attempts);
|
||||
}
|
||||
|
||||
public function test_cancel_marks_status_as_cancelled(): void
|
||||
{
|
||||
$challenge = $this->provider->requestChallenge(
|
||||
['email' => 'user@example.com'],
|
||||
['purpose' => 'sensitive_action'],
|
||||
);
|
||||
|
||||
$this->assertTrue($this->provider->cancel($challenge->id));
|
||||
|
||||
$log = IdentityVerificationLog::find($challenge->id);
|
||||
$this->assertSame(IdentityVerificationStatus::Cancelled, $log->status);
|
||||
}
|
||||
|
||||
public function test_request_challenge_fails_without_email(): void
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->provider->requestChallenge([], ['purpose' => 'signup']);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user