Failed Conditions
Pull Request — master (#16)
by Sylvain
03:12
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 OTPHP\Factory;
9
use PHPUnit\Framework\TestCase;
10
11
final class HasOtpTest extends TestCase
12
{
13
    private \Ecodev\Felix\Model\HasOtp $user;
14
15
    protected function setUp(): void
16
    {
17
        $this->user = new class() implements \Ecodev\Felix\Model\HasOtp {
18
            use HasOtp;
19
20
            public function getLogin(): ?string
21
            {
22
                return '[email protected]';
23
            }
24
        };
25
    }
26
27
    public function testCreateOtpSecret(): void
28
    {
29
        self::assertNull($this->user->getOtpUri(), 'should have no OTP secret at first');
30
        self::assertFalse($this->user->isOtp(), 'should have OTP disabled at first');
31
32
        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

32
        self::/** @scrutinizer ignore-call */ 
33
              expectExceptionMessage('Cannot enable OTP without a secret');
Loading history...
33
        $this->user->setOtp(true);
34
35
        $this->user->createOtpSecret('felix.lan');
36
        $otp1 = $this->user->getOtpUri();
37
        self::assertIsString($otp1);
38
        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

38
        self::assertStringStartsWith('otpauth://totp/', /** @scrutinizer ignore-type */ $otp1, 'TOTP provisionning URI was generated and stored');
Loading history...
39
40
        $this->user->createOtpSecret('felix.lan');
41
        $otp2 = $this->user->getOtpUri();
42
        self::assertIsString($otp2);
43
        self::assertNotSame($otp1, $otp2, 'TOTP provisionning URI was 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
    public function testVerifySecret(): void
59
    {
60
        $this->user->setOtp(false);
61
        self::assertFalse($this->user->verifyOtp('123456'), 'Cannot verify OTP with 2FA disabled');
62
63
        $this->user->createOtpSecret('felix.lan');
64
        $this->user->setOtp(true);
65
66
        self::assertFalse($this->user->verifyOtp('123456'), 'Wrong OTP given');
67
68
        $otp = Factory::loadFromProvisioningUri($this->user->getOtpUri());
0 ignored issues
show
Bug introduced by
It seems like $this->user->getOtpUri() 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

68
        $otp = Factory::loadFromProvisioningUri(/** @scrutinizer ignore-type */ $this->user->getOtpUri());
Loading history...
69
70
        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

70
        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...
71
    }
72
}
73