Failed Conditions
Pull Request — master (#16)
by Adrien
03:09
created

HasOtpTest.php$0 ➔ testRevokeSecret()   A

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 1
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 OTPHP\Factory;
9
use OTPHP\TOTPInterface;
10
use PHPUnit\Framework\TestCase;
11
12
final class HasOtpTest extends TestCase
13
{
14
    private \Ecodev\Felix\Model\HasOtp $user;
15
16
    protected function setUp(): void
17
    {
18
        $this->user = new class() implements \Ecodev\Felix\Model\HasOtp {
19
            use HasOtp;
20
21
            public function getLogin(): ?string
22
            {
23
                return '[email protected]';
24
            }
25
        };
26
    }
27
28
    public function testCreateOtpSecret(): void
29
    {
30
        self::assertNull($this->user->getOtpUri(), 'should have no OTP secret at first');
31
        self::assertFalse($this->user->isOtp(), 'should have OTP disabled at first');
32
33
        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

33
        self::/** @scrutinizer ignore-call */ 
34
              expectExceptionMessage('Cannot enable OTP without a secret');
Loading history...
34
        $this->user->setOtp(true);
35
36
        $this->user->createOtpSecret('felix.lan');
37
        $otp1 = $this->user->getOtpUri();
38
        self::assertIsString($otp1);
39
        self::assertStringStartsWith('otpauth://totp/', $otp1, 'TOTP provisionning URI was generated and stored');
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

39
        self::assertStringStartsWith('otpauth://totp/', /** @scrutinizer ignore-type */ $otp1, 'TOTP provisionning URI was generated and stored');
Loading history...
40
41
        $this->user->createOtpSecret('felix.lan');
42
        $otp2 = $this->user->getOtpUri();
43
        self::assertIsString($otp2);
44
        self::assertNotSame($otp1, $otp2, 'TOTP provisionning URI was changed');
45
46
        $this->user->setOtp(true);
47
        self::assertTrue($this->user->isOtp());
48
    }
49
50
    public function testRevokeSecret(): void
51
    {
52
        $this->user->createOtpSecret('felix.lan');
53
        $this->user->revokeOtpSecret();
54
55
        self::assertFalse($this->user->isOtp());
56
        self::assertNull($this->user->getOtpUri());
57
    }
58
59
    public function testVerifySecret(): void
60
    {
61
        $this->user->setOtp(false);
62
        self::assertFalse($this->user->verifyOtp('123456'), 'Cannot verify OTP with 2FA disabled');
63
64
        $this->user->createOtpSecret('felix.lan');
65
        $this->user->setOtp(true);
66
67
        self::assertFalse($this->user->verifyOtp('123456'), 'Wrong OTP given');
68
69
        $uri = $this->user->getOtpUri();
70
        self::assertNotNull($uri);
71
72
        $otp = Factory::loadFromProvisioningUri($uri);
0 ignored issues
show
Bug introduced by
It seems like $uri can also be of type null; however, parameter $uri of OTPHP\Factory::loadFromProvisioningUri() 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

72
        $otp = Factory::loadFromProvisioningUri(/** @scrutinizer ignore-type */ $uri);
Loading history...
73
        self::assertInstanceOf(TOTPInterface::class, $otp);
74
        self::assertTrue($this->user->verifyOtp($otp->now()), 'Correct OTP given');
0 ignored issues
show
Bug introduced by
The method now() does not exist on OTPHP\HOTP. ( Ignorable by Annotation )

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

74
        self::assertTrue($this->user->verifyOtp($otp->/** @scrutinizer ignore-call */ now()), 'Correct OTP given');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
    }
76
}
77