1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EcodevTests\Felix\Model\Traits; |
6
|
|
|
|
7
|
|
|
use Ecodev\Felix\Model\Traits\HasOtp; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
final class HasOtpTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
private \Ecodev\Felix\Model\HasOtp $user; |
13
|
|
|
|
14
|
|
|
protected function setUp(): void |
15
|
|
|
{ |
16
|
|
|
$this->user = new class() implements \Ecodev\Felix\Model\HasOtp { |
17
|
|
|
use HasOtp; |
18
|
|
|
|
19
|
|
|
public function getLogin(): ?string |
20
|
|
|
{ |
21
|
|
|
return '[email protected]'; |
22
|
|
|
} |
23
|
|
|
}; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testCreateOtpSecret(): void |
27
|
|
|
{ |
28
|
|
|
self::assertNull($this->user->getOtpUri(), 'should have no OTP secret at first'); |
29
|
|
|
self::assertFalse($this->user->isOtp(), 'should have OTP disabled at first'); |
30
|
|
|
|
31
|
|
|
self::expectExceptionMessage('Cannot enable OTP without a secret'); |
|
|
|
|
32
|
|
|
$this->user->setOtp(true); |
33
|
|
|
|
34
|
|
|
$this->user->createOtpSecret('felix.lan', 30, 'sha1', 6); |
35
|
|
|
$otp1 = $this->user->getOtpUri(); |
36
|
|
|
self::assertIsString($otp1); |
37
|
|
|
self::assertStringStartsWith('otpauth://totp/', $otp1, '6 digits TOTP using SHA1 valid 30s'); |
|
|
|
|
38
|
|
|
|
39
|
|
|
$this->user->createOtpSecret('felix.lan', 40, 'sha256', 8); |
40
|
|
|
$otp2 = $this->user->getOtpUri(); |
41
|
|
|
self::assertIsString($otp2); |
42
|
|
|
self::assertStringStartsWith('otpauth://totp/', $otp2, '8 digits TOTP using SHA256 valid 40s'); |
43
|
|
|
self::assertNotSame($otp1, $otp2, 'user OTP URI must have been changed'); |
44
|
|
|
|
45
|
|
|
$this->user->setOtp(true); |
46
|
|
|
self::assertTrue($this->user->isOtp()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testRevokeSecret(): void |
50
|
|
|
{ |
51
|
|
|
$this->user->createOtpSecret('felix.lan'); |
52
|
|
|
$this->user->revokeOtpSecret(); |
53
|
|
|
|
54
|
|
|
self::assertFalse($this->user->isOtp()); |
55
|
|
|
self::assertNull($this->user->getOtpUri()); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|