Passed
Push — master ( b28c40...f61e74 )
by Petr
08:06
created

FileTest::testAccountManipulation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 24
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 43
rs 9.536
1
<?php
2
3
namespace SourcesTests\Files\Volume;
4
5
6
use CommonTestClass;
7
use kalanis\kw_auth\AuthException;
8
use kalanis\kw_auth\Data\FileUser;
9
use kalanis\kw_auth\Sources\Files\Volume\File;
10
use kalanis\kw_auth\Statuses\Always;
11
use kalanis\kw_locks\LockException;
12
13
14
class FileTest extends CommonTestClass
15
{
16
    protected $sourcePath = '';
17
18
    protected function setUp(): void
19
    {
20
        $this->sourcePath = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . '.passcomb';
21
    }
22
23
    /**
24
     * @throws AuthException
25
     * @throws LockException
26
     */
27
    public function testDataOnly(): void
28
    {
29
        $lib = $this->fileSources();
30
        $this->assertEmpty($lib->getDataOnly('does not exist'));
31
        $user = $lib->getDataOnly('manager');
32
        $this->assertNotEmpty($user);
33
        $this->assertEquals('Manage', $user->getDisplayName());
34
    }
35
36
    /**
37
     * @throws AuthException
38
     * @throws LockException
39
     */
40
    public function testAuthenticate(): void
41
    {
42
        $lib = $this->fileSources();
43
        $this->assertEmpty($lib->authenticate('manager', ['password' => 'thisisnotreal']));
44
        $user = $lib->authenticate('manager', ['password' => 'valid']);
45
        $this->assertNotEmpty($user);
46
        $this->assertEquals('Manage', $user->getDisplayName());
47
    }
48
49
    /**
50
     * @throws AuthException
51
     * @throws LockException
52
     */
53
    public function testAuthenticateNoPass(): void
54
    {
55
        $lib = $this->fileSources();
56
        $this->expectException(AuthException::class);
57
        $lib->authenticate('manager', []);
58
    }
59
60
    /**
61
     * @throws AuthException
62
     * @throws LockException
63
     */
64
    public function testAccountManipulation(): void
65
    {
66
        $lib = $this->fileSources();
67
        $user = $this->wantedUser();
68
69
        // create
70
        $lib->createAccount($user, 'here to set');
71
        // check data
72
        $saved = $lib->getDataOnly($user->getAuthName());
73
        $this->assertEquals('Testing another', $saved->getDisplayName());
74
        $this->assertEquals('why_here', $saved->getDir());
75
        $this->assertEquals(3, $saved->getClass());
76
77
        // check login
78
        $this->assertNotEmpty($lib->authenticate($user->getAuthName(), ['password' => 'here to set']));
79
80
        // update
81
        $user->setData(
82
            $user->getAuthId(),
83
            $user->getAuthName(),
84
            $user->getGroup(),
85
            2,
86
            444,
87
            'WheĐn yoĐu dđo nođt knđow',
88
            $user->getDir()
89
        );
90
        $lib->updateAccount($user);
91
92
        // check data - again with new values
93
        $saved = $lib->getDataOnly($user->getAuthName());
94
        $this->assertEquals('When you do not know', $saved->getDisplayName());
95
        $this->assertEquals(2, $saved->getClass());
96
97
        // update password
98
        $lib->updatePassword($user->getAuthName(), 'another pass');
99
        // check login
100
        $this->assertEmpty($lib->authenticate($user->getAuthName(), ['password' => 'here to set']));
101
        $this->assertNotEmpty($lib->authenticate($user->getAuthName(), ['password' => 'another pass']));
102
103
        // remove
104
        $lib->deleteAccount($user->getAuthName());
105
        // check for existence
106
        $this->assertEmpty($lib->getDataOnly($user->getAuthName()));
107
    }
108
109
    /**
110
     * @throws AuthException
111
     * @throws LockException
112
     */
113
    public function testCreateFail(): void
114
    {
115
        $lib = $this->fileSources();
116
        $user = $this->wantedUser();
117
        $this->expectException(AuthException::class);
118
        $lib->createAccount($user, '');
119
    }
120
121
    /**
122
     * @throws AuthException
123
     * @throws LockException
124
     */
125
    public function testAllUsers(): void
126
    {
127
        $lib = $this->fileSources();
128
        $data = $lib->readAccounts();
129
        $this->assertEquals(1, $data[0]->getClass());
130
        $this->assertEquals('manager', $data[1]->getAuthName());
131
    }
132
133
    /**
134
     * Contains a full comedy/tragedy of work with locks
135
     * @throws LockException
136
     * @return File
137
     */
138
    protected function fileSources(): File
139
    {
140
        return new File(
141
            new \MockModes(),
142
            new Always(),
143
            $this->getLockPath(),
144
            $this->sourcePath
145
        );
146
    }
147
148
    protected function wantedUser(): FileUser
149
    {
150
        $user = new FileUser();
151
        $user->setData(600, 'another', 0, 0, 555, 'Testing another', 'why_here');
152
        return $user;
153
    }
154
}
155