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

FilesTest::testAccountManipulation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 60
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 36
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 60
rs 9.344

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\FileCertUser;
9
use kalanis\kw_auth\Data\FileGroup;
10
use kalanis\kw_auth\Sources\Files\Volume\Files;
11
use kalanis\kw_auth\Statuses\Always;
12
use kalanis\kw_locks\LockException;
13
14
15
class FilesTest extends CommonTestClass
16
{
17
    protected $sourcePath = '';
18
19
    protected function setUp(): void
20
    {
21
        $this->sourcePath = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'data';
22
    }
23
24
    /**
25
     * @throws AuthException
26
     * @throws LockException
27
     */
28
    public function testDataOnly(): void
29
    {
30
        $lib = $this->fileSources();
31
        $this->assertEmpty($lib->getDataOnly('does not exist'));
32
        $user = $lib->getDataOnly('manager');
33
        $this->assertNotEmpty($user);
34
        $this->assertEquals('Manage', $user->getDisplayName());
35
    }
36
37
    /**
38
     * @throws AuthException
39
     * @throws LockException
40
     */
41
    public function testAuthenticate(): void
42
    {
43
        $lib = $this->fileSources();
44
        $this->assertEmpty($lib->authenticate('manager', ['password' => 'thisisnotreal']));
45
        $user = $lib->authenticate('manager', ['password' => 'valid']);
46
        $this->assertNotEmpty($user);
47
        $this->assertEquals('Manage', $user->getDisplayName());
48
    }
49
50
    /**
51
     * @throws AuthException
52
     * @throws LockException
53
     */
54
    public function testAuthenticateNoPass(): void
55
    {
56
        $lib = $this->fileSources();
57
        $this->expectException(AuthException::class);
58
        $lib->authenticate('manager', []);
59
    }
60
61
    /**
62
     * @throws AuthException
63
     * @throws LockException
64
     */
65
    public function testAccountManipulation(): void
66
    {
67
        $lib = $this->fileSources();
68
        $user = $this->wantedUser();
69
70
        // create
71
        $lib->createAccount($user, 'here to set');
72
        // check data
73
        $saved = $lib->getDataOnly($user->getAuthName());
74
        $this->assertEquals('Testing another', $saved->getDisplayName());
75
        $this->assertEquals('why_here', $saved->getDir());
76
        $this->assertEquals(3, $saved->getClass());
77
78
        // check login
79
        $this->assertNotEmpty($lib->authenticate($user->getAuthName(), ['password' => 'here to set']));
80
81
        // update
82
        $user->setData(
83
            $user->getAuthId(),
84
            $user->getAuthName(),
85
            $user->getGroup(),
86
            2,
87
            7,
88
            'WheĐn yoĐu dđo nođt knđow',
89
            $user->getDir()
90
        );
91
        $user->addCertInfo('==public key for accessing that content==', 'hidden salt');
92
        $lib->updateAccount($user);
93
        $lib->updateCertKeys($user->getAuthName(), $user->getPubKey(), $user->getPubSalt());
94
95
        // update name
96
        $user->setData(
97
            $user->getAuthId(),
98
            'changed name',
99
            $user->getGroup(),
100
            $user->getClass(),
101
            $user->getStatus(),
102
            $user->getDisplayName(),
103
            $user->getDir()
104
        );
105
        $lib->updateAccount($user);
106
107
        // check data - again with new values
108
        $saved = $lib->getCertData($user->getAuthName());
109
        $this->assertEquals('When you do not know', $saved->getDisplayName());
110
        $this->assertEquals(2, $saved->getClass());
111
        $this->assertEquals($user->getPubKey(), $saved->getPubKey());
112
        $this->assertEquals($user->getPubSalt(), $saved->getPubSalt());
113
114
115
        // update password
116
        $lib->updatePassword($user->getAuthName(), 'another pass');
117
        // check login
118
        $this->assertEmpty($lib->authenticate($user->getAuthName(), ['password' => 'here to set']));
119
        $this->assertNotEmpty($lib->authenticate($user->getAuthName(), ['password' => 'another pass']));
120
121
        // remove
122
        $lib->deleteAccount($user->getAuthName());
123
        // check for existence
124
        $this->assertEmpty($lib->getDataOnly($user->getAuthName()));
125
    }
126
127
    /**
128
     * @throws AuthException
129
     * @throws LockException
130
     * AuthId is not correct but auth name is
131
     */
132
    public function testAccountUpdateFail(): void
133
    {
134
        $lib = $this->fileSources();
135
        $user = new FileCertUser();
136
        $user->setData(600, 'worker', 0, 0, null, 'Die on set', 'so_here');
137
138
        $this->expectException(AuthException::class);
139
        $lib->updateAccount($user);
140
    }
141
142
    /**
143
     * @throws AuthException
144
     * @throws LockException
145
     */
146
    public function testCreateFail(): void
147
    {
148
        $lib = $this->fileSources();
149
        $user = $this->wantedUser();
150
        $this->expectException(AuthException::class);
151
        $lib->createAccount($user, '');
152
    }
153
154
    /**
155
     * @throws AuthException
156
     * @throws LockException
157
     */
158
    public function testAllUsers(): void
159
    {
160
        $lib = $this->fileSources();
161
        $data = $lib->readAccounts();
162
        $this->assertEquals(1, $data[0]->getClass());
163
        $this->assertEquals('manager', $data[1]->getAuthName());
164
    }
165
166
    /**
167
     * Contains a full comedy/tragedy of work with locks
168
     * @throws LockException
169
     * @return Files
170
     */
171
    protected function fileSources(): Files
172
    {
173
        return new Files(
174
            new \MockModes(),
175
            new Always(),
176
            $this->getLockPath(),
177
            $this->sourcePath
178
        );
179
    }
180
181
    protected function wantedUser(): FileCertUser
182
    {
183
        $user = new FileCertUser();
184
        $user->setData(1003, 'another', 0, 0, 12, 'Testing another', 'why_here');
185
        return $user;
186
    }
187
188
    /**
189
     * @throws AuthException
190
     * @throws LockException
191
     */
192
    public function testGroupManipulation(): void
193
    {
194
        $lib = $this->fileSources();
195
        $group = $this->wantedGroup();
196
197
        // create
198
        $lib->createGroup($group);
199
        // check data
200
        $saved = $lib->getGroupDataOnly($group->getGroupId());
201
        $this->assertEquals('another', $saved->getGroupName());
202
        $this->assertEquals('Testing group', $saved->getGroupDesc());
203
        $this->assertEquals(1001, $saved->getGroupAuthorId());
204
205
        // update
206
        $group->setData(
207
            $group->getGroupId(),
208
            $group->getGroupName(),
209
            1002,
210
            'WheĐn yoĐu dđo nođt knđow',
211
            777,
212
            [32, 15, 21, 0]
213
        );
214
        $lib->updateGroup($group);
215
216
        // check data - again with new values
217
        $saved = $lib->getGroupDataOnly($group->getGroupId());
218
        $this->assertEquals('When you do not know', $saved->getGroupDesc()); // overwrite this
219
        $this->assertEquals(1001, $saved->getGroupAuthorId()); // cannot overwrite this
220
        $this->assertEquals([32, 15, 21], $saved->getGroupParents()); // will be filtered
221
222
        // remove
223
        $lib->deleteGroup($group->getGroupId());
224
        // check for existence
225
        $this->assertEmpty($lib->getGroupDataOnly($group->getGroupId()));
226
    }
227
228
    /**
229
     * @throws AuthException
230
     * @throws LockException
231
     */
232
    public function testCreateGroupFail(): void
233
    {
234
        $lib = $this->fileSources();
235
        $group = $this->wantedGroup('');
236
        $this->expectException(AuthException::class);
237
        $lib->createGroup($group);
238
    }
239
240
    /**
241
     * @throws AuthException
242
     * @throws LockException
243
     */
244
    public function testDeleteGroupFail(): void
245
    {
246
        $lib = $this->fileSources();
247
        $this->expectException(AuthException::class);
248
        $lib->deleteGroup(1);
249
    }
250
251
    /**
252
     * @throws AuthException
253
     * @throws LockException
254
     */
255
    public function testAllGroups(): void
256
    {
257
        $lib = $this->fileSources();
258
        $data = $lib->readGroup();
259
        $this->assertEquals('Maintainers', $data[0]->getGroupDesc());
260
        $this->assertEquals(1000, $data[1]->getGroupAuthorId());
261
    }
262
263
    protected function wantedGroup($name = 'another'): FileGroup
264
    {
265
        $user = new FileGroup();
266
        $user->setData(3, $name, 1001, 'Testing group', 666);
267
        return $user;
268
    }
269
}
270