UserTest::testGetFullName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 12
rs 10
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published
9
 * by the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
declare(strict_types=1);
22
23
namespace App\Tests\Entity\UserSystem;
24
25
use App\Entity\UserSystem\U2FKey;
26
use App\Entity\UserSystem\User;
27
use App\Entity\UserSystem\WebauthnKey;
28
use DateTime;
29
use Doctrine\Common\Collections\Collection;
30
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
use Ramsey\Uuid\Uuid;
32
use Webauthn\TrustPath\EmptyTrustPath;
33
34
class UserTest extends TestCase
35
{
36
    public function testGetFullName(): void
37
    {
38
        $user = new User();
39
        $user->setName('username');
40
        $user->setFirstName('John');
41
        $user->setLastName('Doe');
42
43
        $this->assertSame('John Doe', $user->getFullName(false));
44
        $this->assertSame('John Doe (@username)', $user->getFullName(true));
45
46
        $user->setLastName('');
47
        $this->assertSame('John (@username)', $user->getFullName(true));
48
    }
49
50
    public function googleAuthenticatorEnabledDataProvider(): array
51
    {
52
        return [
53
            [null, false],
54
            ['', false],
55
            ['SSSk38498', true],
56
        ];
57
    }
58
59
    /**
60
     * @dataProvider googleAuthenticatorEnabledDataProvider
61
     */
62
    public function testIsGoogleAuthenticatorEnabled(?string $secret, bool $expected): void
63
    {
64
        $user = new User();
65
        $user->setGoogleAuthenticatorSecret($secret);
66
        $this->assertSame($expected, $user->isGoogleAuthenticatorEnabled());
67
    }
68
69
    /**
70
     * @requires PHPUnit 8
71
     */
72
    public function testSetBackupCodes(): void
73
    {
74
        $user = new User();
75
        $this->assertNull($user->getBackupCodesGenerationDate());
76
77
        $codes = ['test', 'invalid', 'test'];
78
        $user->setBackupCodes($codes);
79
        // Backup Codes generation date must be changed!
80
        $this->assertInstanceOf(\DateTime::class, $user->getBackupCodesGenerationDate());
81
        $this->assertSame($codes, $user->getBackupCodes());
82
83
        //Test what happens if we delete the backup keys
84
        $user->setBackupCodes([]);
85
        $this->assertEmpty($user->getBackupCodes());
86
        $this->assertNull($user->getBackupCodesGenerationDate());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $user->getBackupCodesGenerationDate() targeting App\Entity\UserSystem\Us...upCodesGenerationDate() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
87
    }
88
89
    public function testIsBackupCode(): void
90
    {
91
        $user = new User();
92
        $codes = ['aaaa', 'bbbb', 'cccc', 'dddd'];
93
        $user->setBackupCodes($codes);
94
95
        $this->assertTrue($user->isBackupCode('aaaa'));
96
        $this->assertTrue($user->isBackupCode('cccc'));
97
98
        $this->assertFalse($user->isBackupCode(''));
99
        $this->assertFalse($user->isBackupCode('zzzz'));
100
    }
101
102
    public function testInvalidateBackupCode(): void
103
    {
104
        $user = new User();
105
        $codes = ['aaaa', 'bbbb', 'cccc', 'dddd'];
106
        $user->setBackupCodes($codes);
107
108
        //Ensure the code is valid
109
        $this->assertTrue($user->isBackupCode('aaaa'));
110
        $this->assertTrue($user->isBackupCode('bbbb'));
111
        //Invalidate code, afterwards the code has to be invalid!
112
        $user->invalidateBackupCode('bbbb');
113
        $this->assertFalse($user->isBackupCode('bbbb'));
114
        $this->assertTrue($user->isBackupCode('aaaa'));
115
116
        //No exception must happen, when we try to invalidate an not existing backup key!
117
        $user->invalidateBackupCode('zzzz');
118
    }
119
120
    public function testInvalidateTrustedDeviceTokens(): void
121
    {
122
        $user = new User();
123
        $old_value = $user->getTrustedTokenVersion();
124
        //To invalidate the token, the new value must be bigger than the old value
125
        $user->invalidateTrustedDeviceTokens();
126
        $this->assertGreaterThan($old_value, $user->getTrustedTokenVersion());
127
    }
128
129
    public function testIsWebauthnEnabled(): void
130
    {
131
        $user = new User();
132
        $user->addWebauthnKey(new WebauthnKey(
133
            "Test",
134
            "Test",
135
            [],
136
            "Test",
137
            new EmptyTrustPath(),
138
            Uuid::fromDateTime(new \DateTime()),
139
            "",
140
            "",
141
            0
142
        ));
143
        $this->assertTrue($user->isWebAuthnAuthenticatorEnabled());
144
145
        $result = $user->getWebauthnKeys();
146
        if($result instanceof Collection){
147
            $result->clear();
148
        }
149
        $this->assertFalse($user->isWebAuthnAuthenticatorEnabled());
150
    }
151
}
152