Failed Conditions
Pull Request — master (#16)
by Adrien
05:43
created

HasOtpTest::testRevokeSecret()

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
nc 1
nop 0
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');
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCa...xpectExceptionMessage() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
        self::/** @scrutinizer ignore-call */ 
32
              expectExceptionMessage('Cannot enable OTP without a secret');
Loading history...
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');
0 ignored issues
show
Bug introduced by
It seems like $otp1 can also be of type null; however, parameter $string of PHPUnit\Framework\Assert::assertStringStartsWith() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
        self::assertStringStartsWith('otpauth://totp/', /** @scrutinizer ignore-type */ $otp1, '6 digits TOTP using SHA1 valid 30s');
Loading history...
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