Passed
Pull Request — master (#28)
by Fran
08:31
created

SecurityTest::testSecurityUserManagement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 50
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 41
nc 1
nop 0
dl 0
loc 50
rs 9.264
c 0
b 0
f 0
1
<?php
2
3
namespace PSFS\tests\base;
4
5
use Exception;
6
use PHPUnit\Framework\Attributes\Depends;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\Attributes\Depends 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...
7
use PHPUnit\Framework\TestCase;
8
use PSFS\base\exception\GeneratorException;
9
use PSFS\base\Request;
10
use PSFS\base\Security;
11
use PSFS\base\Template;
12
use PSFS\base\types\helpers\AuthHelper;
13
use PSFS\services\AdminServices;
14
15
/**
16
 * Class SecurityTest
17
 * @package PSFS\tests\base
18
 * @runInSeparateProcess
19
 */
20
class SecurityTest extends TestCase
21
{
22
    /**
23
     * Test to check if the Logger has been created successful
24
     * @return Security
25
     */
26
    public function getInstance(): Security
27
    {
28
        global $_SESSION;
29
        if (null === $_SESSION) {
30
            $_SESSION = [];
31
        }
32
        $instance = Security::getInstance(true);
33
        Security::setTest(false);
34
35
        $this->assertNotNull($instance, 'Security instance is null');
36
        $this->assertInstanceOf(Security::class, $instance, 'Instance is different than expected');
37
        return $instance;
38
    }
39
40
    /**
41
     * Test basic static functionality for Security class
42
     */
43
    public function testSecurityBasics(): Security
44
    {
45
        $security = $this->getInstance();
46
        $this->assertInstanceOf(Security::class, $security);
47
48
        $profiles = $security->getAdminProfiles();
49
        $this->assertArrayHasKey(AuthHelper::ADMIN_ID_TOKEN, $profiles, 'Malformed array');
50
        $this->assertArrayHasKey(AuthHelper::MANAGER_ID_TOKEN, $profiles, 'Malformed array');
51
        $this->assertArrayHasKey(AuthHelper::USER_ID_TOKEN, $profiles, 'Malformed array');
52
53
        $cleanProfiles = $security->getAdminCleanProfiles();
54
        $this->assertNotEmpty($cleanProfiles, 'Malformed security profiles array');
55
        $this->assertTrue(in_array(AuthHelper::ADMIN_ID_TOKEN, $cleanProfiles, true), 'Key not exists');
56
        $this->assertTrue(in_array(AuthHelper::MANAGER_ID_TOKEN, $cleanProfiles, true), 'Key not exists');
57
        $this->assertTrue(in_array(AuthHelper::USER_ID_TOKEN, $cleanProfiles, true), 'Key not exists');
58
        return $security;
59
    }
60
61
    /**
62
     * @return Security
63
     * @throws GeneratorException
64
     */
65
    #[Depends('testSecurityBasics')]
66
    public function testSecurityUserManagement(): Security
67
    {
68
        $user = [
69
            'username' => uniqid('test', true),
70
            'password' => uniqid('test', true),
71
            'profile' => AuthHelper::ADMIN_ID_TOKEN,
72
        ];
73
        $security = $this->getInstance();
74
        $security->saveUser($user);
75
76
        $this->assertFileExists(CONFIG_DIR . DIRECTORY_SEPARATOR . 'admins.json', 'Error trying to save admins');
77
        $this->assertNull($security->getUser());
78
        $this->assertNull($security->getAdmin());
79
        $this->assertTrue($security->canDo('something'));
80
        $this->assertFalse($security->isLogged());
81
        $this->assertFalse($security->isAdmin());
82
        $this->assertFalse($security->isManager());
83
        $this->assertFalse($security->isUser());
84
        $this->assertFalse($security->isSuperAdmin());
85
86
        $security->updateUser($user);
87
        $this->assertNotNull($security->getUser(), 'An error occurred when update user in session');
88
        $this->assertFalse($security->checkAdmin(uniqid('test', true), uniqid('error', true), true), 'Error checking admin user');
89
        $this->assertNull($security->getAdmin(), 'Wrong admin parser');
90
91
        $_COOKIE[AuthHelper::generateProfileHash()] = AuthHelper::encrypt($user['username'] . ':' . $user['password'], AuthHelper::ADMIN_ID_TOKEN);
92
        Request::getInstance()->init();
93
        $this->assertTrue($security->checkAdmin(null, null, true), 'An error occurred verifying the admin user');
94
        AdminServices::setTest(true);
95
        $admins = AdminServices::getInstance()->getAdmins();
96
        $this->assertArrayHasKey($user['username'], $admins, 'Admin is not into credentials file');
97
        $this->assertEquals($user['profile'], $admins[$user['username']]['profile'], 'Admin user with different profile');
98
        $admin = $security->getAdmin();
99
        $this->assertNotNull($admin, 'An error ocurred gathering the admin user');
100
        $this->assertEquals($admin['alias'], $user['username'], 'Wrong data gathered from admins.json');
101
        $this->assertEquals($admin['profile'], $user['profile'], 'Wrong profile gathered from admins.json');
102
        $this->assertTrue($security->isSuperAdmin(), 'Wrong checking for super admin profile');
103
        $this->assertTrue($security->isLogged());
104
        $this->assertTrue($security->isAdmin());
105
        $this->assertFalse($security->isManager());
106
        $this->assertFalse($security->isUser());
107
108
        $security->updateSession(true);
109
        $this->assertNotEmpty($security->getSessionKey(AuthHelper::ADMIN_ID_TOKEN), 'Error saving sessions');
110
111
        $security->deleteUser($user['username']);
112
        $this->assertTrue($security->checkAdmin($user['username'], $user['password'], true), 'Error checking admin user');
113
114
        return $security;
115
116
    }
117
118
    /**
119
     * @param Security $security
120
     * @throws Exception
121
     */
122
    #[Depends('testSecurityUserManagement')]
123
    public function testSessionHandler(Security $security)
124
    {
125
126
        $testValue = random_int(0, 1e5);
127
        $security->setSessionKey('test', $testValue);
128
        $this->assertNotNull($security->getSessionKey('test'), 'Error trying to gather the session key');
129
        $this->assertEquals($security->getSessionKey('test'), $testValue, 'The session key value is not the same than expected');
130
131
        $flashValue = 'test value for flash';
132
        $security->setFlash('flash_test', $flashValue);
133
        $security->updateSession();
134
        $this->assertNotEmpty($security->getFlashes(), 'Flash key not saved');
135
        $gatherData = $security->getFlash('flash_test');
136
        $this->assertNotNull($gatherData, 'Error trying to gather the flash key');
137
        $this->assertEquals($flashValue, $gatherData, 'Error gathering the flash data, there is not the same data than expected');
138
        $security->clearFlashes();
139
        $this->assertNull($security->getFlash('flash_test'), 'Flash key not deleted');
140
        $this->assertEmpty($security->getFlashes(), 'Flash with data yet');
141
        $security->closeSession();
142
    }
143
144
    /**
145
     * @throws Exception
146
     */
147
    public function testCheckNotAuthorizedRoute(): void
148
    {
149
        Template::setTest(true);
150
        $this->assertNotEmpty($this->getInstance()->notAuthorized('test'), 'Fail to render the redirection template');
151
        Template::setTest(false);
152
    }
153
154
}
155