Passed
Push — master ( d309df...7ec489 )
by Petr
02:37
created

FilesTest   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 398
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 165
c 1
b 0
f 0
dl 0
loc 398
rs 10
wmc 23

23 Methods

Rating   Name   Duplication   Size   Complexity  
A testAllGroups() 0 6 1
A testCreateFail() 0 6 1
A testAllUsers() 0 6 1
A testDeleteAccountOnEmptyInstance() 0 7 1
A testCreateAccountOnEmptyInstance() 0 13 1
A testNotExistsData() 0 6 1
A testGroupManipulation() 0 31 1
A testAuthenticate() 0 7 1
A testDeleteGroupFail() 0 5 1
A testUpdateAccountOnEmptyInstance() 0 7 1
A partialFileSources() 0 15 1
A wantedUser() 0 5 1
A emptyFileSources() 0 8 1
A testUpdatePasswordOnEmptyInstance() 0 6 1
A testAccountUpdateFail() 0 8 1
A testUpdateCertsOnEmptyInstance() 0 6 1
A testAuthenticateNoPass() 0 5 1
A testDeleteAccountOnPartialInstance() 0 7 1
A fileSources() 0 29 1
A wantedGroup() 0 5 1
A testDataOnly() 0 7 1
A testCreateGroupFail() 0 6 1
A testAccountManipulation() 0 58 1
1
<?php
2
3
namespace SourcesTests\Files\Storage;
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\Interfaces\IFile;
11
use kalanis\kw_auth\Sources\Files\Storage\Files;
12
use kalanis\kw_locks\LockException;
13
use kalanis\kw_storage\Storage\Key\DefaultKey;
14
use kalanis\kw_storage\Storage\Storage;
15
use kalanis\kw_storage\Storage\Target\Memory;
16
17
18
class FilesTest extends CommonTestClass
19
{
20
    protected $sourcePath = 'data';
21
22
    /**
23
     * @throws AuthException
24
     * @throws LockException
25
     */
26
    public function testNotExistsData(): void
27
    {
28
        $lib = $this->emptyFileSources();
29
        $this->assertNull($lib->getDataOnly('does not exist'));
30
        $this->assertNull($lib->getCertData('does not exist'));
31
        $this->assertNull($lib->authenticate('does not exist', ['password' => 'not need']));
32
    }
33
34
    /**
35
     * @throws AuthException
36
     * @throws LockException
37
     */
38
    public function testDataOnly(): void
39
    {
40
        $lib = $this->fileSources();
41
        $this->assertEmpty($lib->getDataOnly('does not exist'));
42
        $user = $lib->getDataOnly('manager');
43
        $this->assertNotEmpty($user);
44
        $this->assertEquals('Manage', $user->getDisplayName());
45
    }
46
47
    /**
48
     * @throws AuthException
49
     * @throws LockException
50
     */
51
    public function testAuthenticate(): void
52
    {
53
        $lib = $this->fileSources();
54
        $this->assertEmpty($lib->authenticate('manager', ['password' => 'thisisnotreal']));
55
        $user = $lib->authenticate('manager', ['password' => 'valid']);
56
        $this->assertNotEmpty($user);
57
        $this->assertEquals('Manage', $user->getDisplayName());
58
    }
59
60
    /**
61
     * @throws AuthException
62
     * @throws LockException
63
     */
64
    public function testAuthenticateNoPass(): void
65
    {
66
        $lib = $this->fileSources();
67
        $this->expectException(AuthException::class);
68
        $lib->authenticate('manager', []);
69
    }
70
71
    /**
72
     * @throws AuthException
73
     * @throws LockException
74
     */
75
    public function testCreateAccountOnEmptyInstance(): void
76
    {
77
        $lib = $this->emptyFileSources();
78
        $user = $this->wantedUser();
79
80
        // create
81
        $lib->createAccount($user, 'here to set');
82
83
        // check data
84
        $saved = $lib->getDataOnly($user->getAuthName());
85
        $this->assertEquals('Testing another', $saved->getDisplayName());
86
        $this->assertEquals('why_here', $saved->getDir());
87
        $this->assertEquals(3, $saved->getClass());
88
    }
89
90
    /**
91
     * @throws AuthException
92
     * @throws LockException
93
     */
94
    public function testUpdateAccountOnEmptyInstance(): void
95
    {
96
        $lib = $this->emptyFileSources();
97
        $user = $this->wantedUser();
98
99
        $this->expectException(AuthException::class);
100
        $lib->updateAccount($user);
101
    }
102
103
    /**
104
     * @throws AuthException
105
     * @throws LockException
106
     */
107
    public function testUpdatePasswordOnEmptyInstance(): void
108
    {
109
        $lib = $this->emptyFileSources();
110
111
        $this->expectException(AuthException::class);
112
        $lib->updatePassword('someone', 'not important');
113
    }
114
115
    /**
116
     * @throws AuthException
117
     * @throws LockException
118
     */
119
    public function testUpdateCertsOnEmptyInstance(): void
120
    {
121
        $lib = $this->emptyFileSources();
122
123
        $this->expectException(AuthException::class);
124
        $lib->updateCertKeys('someone', 'can be empty in this case', 'not important');
125
    }
126
127
    /**
128
     * @throws AuthException
129
     * @throws LockException
130
     */
131
    public function testDeleteAccountOnEmptyInstance(): void
132
    {
133
        $lib = $this->emptyFileSources();
134
        $user = $this->wantedUser();
135
136
        $this->expectException(AuthException::class);
137
        $lib->deleteAccount($user->getAuthName());
138
    }
139
140
    /**
141
     * @throws AuthException
142
     * @throws LockException
143
     */
144
    public function testDeleteAccountOnPartialInstance(): void
145
    {
146
        $lib = $this->partialFileSources();
147
        $user = $this->wantedUser();
148
149
        $this->expectException(AuthException::class);
150
        $lib->deleteAccount($user->getAuthName());
151
    }
152
153
    /**
154
     * @throws AuthException
155
     * @throws LockException
156
     */
157
    public function testAccountManipulation(): void
158
    {
159
        $lib = $this->fileSources();
160
        $user = $this->wantedUser();
161
162
        // create
163
        $lib->createAccount($user, 'here to set');
164
        // check data
165
        $saved = $lib->getDataOnly($user->getAuthName());
166
        $this->assertEquals('Testing another', $saved->getDisplayName());
167
        $this->assertEquals('why_here', $saved->getDir());
168
        $this->assertEquals(3, $saved->getClass());
169
170
        // check login
171
        $this->assertNotEmpty($lib->authenticate($user->getAuthName(), ['password' => 'here to set']));
172
173
        // update
174
        $user->setData(
175
            $user->getAuthId(),
176
            $user->getAuthName(),
177
            $user->getGroup(),
178
            2,
179
            'WheĐn yoĐu dđo nođt knđow',
180
            $user->getDir()
181
        );
182
        $user->addCertInfo('==public key for accessing that content==', 'hidden salt');
183
        $lib->updateAccount($user);
184
        $lib->updateCertKeys($user->getAuthName(), $user->getPubKey(), $user->getPubSalt());
185
186
        // update name
187
        $user->setData(
188
            $user->getAuthId(),
189
            'changed name',
190
            $user->getGroup(),
191
            $user->getClass(),
192
            $user->getDisplayName(),
193
            $user->getDir()
194
        );
195
        $lib->updateAccount($user);
196
197
        // check data - again with new values
198
        $saved = $lib->getCertData($user->getAuthName());
199
        $this->assertEquals('When you do not know', $saved->getDisplayName());
200
        $this->assertEquals(2, $saved->getClass());
201
        $this->assertEquals($user->getPubKey(), $saved->getPubKey());
202
        $this->assertEquals($user->getPubSalt(), $saved->getPubSalt());
203
204
205
        // update password
206
        $lib->updatePassword($user->getAuthName(), 'another pass');
207
        // check login
208
        $this->assertEmpty($lib->authenticate($user->getAuthName(), ['password' => 'here to set']));
209
        $this->assertNotEmpty($lib->authenticate($user->getAuthName(), ['password' => 'another pass']));
210
211
        // remove
212
        $lib->deleteAccount($user->getAuthName());
213
        // check for existence
214
        $this->assertEmpty($lib->getDataOnly($user->getAuthName()));
215
    }
216
217
    /**
218
     * @throws AuthException
219
     * @throws LockException
220
     * AuthId is not correct but auth name is
221
     */
222
    public function testAccountUpdateFail(): void
223
    {
224
        $lib = $this->fileSources();
225
        $user = new FileCertUser();
226
        $user->setData(600, 'worker', 0, 0, 'Die on set', 'so_here');
227
228
        $this->expectException(AuthException::class);
229
        $lib->updateAccount($user);
230
    }
231
232
    /**
233
     * @throws AuthException
234
     * @throws LockException
235
     */
236
    public function testCreateFail(): void
237
    {
238
        $lib = $this->fileSources();
239
        $user = $this->wantedUser();
240
        $this->expectException(AuthException::class);
241
        $lib->createAccount($user, '');
242
    }
243
244
    /**
245
     * @throws AuthException
246
     * @throws LockException
247
     */
248
    public function testAllUsers(): void
249
    {
250
        $lib = $this->fileSources();
251
        $data = $lib->readAccounts();
252
        $this->assertEquals(1, $data[0]->getClass());
253
        $this->assertEquals('manager', $data[1]->getAuthName());
254
    }
255
256
    /**
257
     * Contains a full comedy/tragedy of work with locks
258
     * @throws LockException
259
     * @return Files
260
     */
261
    protected function fileSources(): Files
262
    {
263
        $storage = new Storage(new DefaultKey(), new Memory());
264
        $storage->write($this->sourcePath . DIRECTORY_SEPARATOR . IFile::PASS_FILE,
265
            'owner:1000:0:1:Owner:/data/:' . "\r\n"
266
            . 'manager:1001:1:2:Manage:/data/:' . "\r\n"
267
            . '# commented out' . "\r\n"
268
            . 'worker:1002:1:3:Worker:/data/:' . "\r\n"
269
            // last line is intentionally empty one
270
        );
271
        $storage->write($this->sourcePath . DIRECTORY_SEPARATOR . IFile::SHADE_FILE,
272
            'owner:M2FjMjZhMjc3MGY4MzUxYjYyN2YzMzI1NjRkNTVlYmM4N2U5N2Y3ODI2NDAwMjY0MTZmMTI0NTliOTFlMTUxZQ==:0:9999999999:7:x:' . "\r\n"
273
            . 'manager:ZWZmNzQwODIxZDhjNzRkMjZlZTIzYjQ2ODBiNDA1YTA5MWY0ZjdkNWVhNzk2NDAxZTZkODY3NDhmMjg0MzE4Yw==:0:9999999999:salt_hash:x:' . "\r\n"
274
            . '# commented out' . "\r\n"
275
            . 'worker:M2FjMjZhMjc3MGY4MzUxYjYyN2YzMzI1NjRkNTVlYmM4N2U5N2Y3ODI2NDAwMjY0MTZmMTI0NTliOTFlMTUxZQ==:0:9999999999:salt_key:x:' . "\r\n"
276
            // last line is intentionally empty one
277
        );
278
        $storage->write($this->sourcePath . DIRECTORY_SEPARATOR . IFile::GROUP_FILE,
279
            '0:root:1000:Maintainers:' . "\r\n"
280
            . '1:admin:1000:Administrators:' . "\r\n"
281
            . '# commented out' . "\r\n"
282
            . '2:user:1000:All users:' . "\r\n"
283
            // last line is intentionally empty one
284
        );
285
        return new Files(
286
            $storage,
287
            new \MockModes(),
288
            $this->getLockPath(),
289
            $this->sourcePath
290
        );
291
    }
292
293
    /**
294
     * Contains a partial files - no groups or shadow files
295
     * @throws LockException
296
     * @return Files
297
     */
298
    protected function partialFileSources(): Files
299
    {
300
        $storage = new Storage(new DefaultKey(), new Memory());
301
        $storage->write($this->sourcePath . DIRECTORY_SEPARATOR . IFile::PASS_FILE,
302
            'owner:1000:0:1:Owner:/data/:' . "\r\n"
303
            . 'manager:1001:1:2:Manage:/data/:' . "\r\n"
304
            . '# commented out' . "\r\n"
305
            . 'worker:1002:1:3:Worker:/data/:' . "\r\n"
306
            // last line is intentionally empty one
307
        );
308
        return new Files(
309
            $storage,
310
            new \MockModes(),
311
            $this->getLockPath(),
312
            $this->sourcePath
313
        );
314
    }
315
316
    /**
317
     * Contains a full comedy/tragedy of work with locks
318
     * @throws LockException
319
     * @return Files
320
     */
321
    protected function emptyFileSources(): Files
322
    {
323
        $storage = new Storage(new DefaultKey(), new Memory());
324
        return new Files(
325
            $storage,
326
            new \MockModes(),
327
            $this->getLockPath(),
328
            $this->sourcePath
329
        );
330
    }
331
332
    protected function wantedUser(): FileCertUser
333
    {
334
        $user = new FileCertUser();
335
        $user->setData(1003, 'another', 0, 0, 'Testing another', 'why_here');
336
        return $user;
337
    }
338
339
    /**
340
     * @throws AuthException
341
     * @throws LockException
342
     */
343
    public function testGroupManipulation(): void
344
    {
345
        $lib = $this->fileSources();
346
        $group = $this->wantedGroup();
347
348
        // create
349
        $lib->createGroup($group);
350
        // check data
351
        $saved = $lib->getGroupDataOnly($group->getGroupId());
352
        $this->assertEquals('another', $saved->getGroupName());
353
        $this->assertEquals('Testing group', $saved->getGroupDesc());
354
        $this->assertEquals(1001, $saved->getGroupAuthorId());
355
356
        // update
357
        $group->setData(
358
            $group->getGroupId(),
359
            $group->getGroupName(),
360
            1002,
361
            'WheĐn yoĐu dđo nođt knđow'
362
        );
363
        $lib->updateGroup($group);
364
365
        // check data - again with new values
366
        $saved = $lib->getGroupDataOnly($group->getGroupId());
367
        $this->assertEquals('When you do not know', $saved->getGroupDesc()); // overwrite this
368
        $this->assertEquals(1001, $saved->getGroupAuthorId()); // cannot overwrite this
369
370
        // remove
371
        $lib->deleteGroup($group->getGroupId());
372
        // check for existence
373
        $this->assertEmpty($lib->getGroupDataOnly($group->getGroupId()));
374
    }
375
376
    /**
377
     * @throws AuthException
378
     * @throws LockException
379
     */
380
    public function testCreateGroupFail(): void
381
    {
382
        $lib = $this->fileSources();
383
        $group = $this->wantedGroup('');
384
        $this->expectException(AuthException::class);
385
        $lib->createGroup($group);
386
    }
387
388
    /**
389
     * @throws AuthException
390
     * @throws LockException
391
     */
392
    public function testDeleteGroupFail(): void
393
    {
394
        $lib = $this->fileSources();
395
        $this->expectException(AuthException::class);
396
        $lib->deleteGroup(1);
397
    }
398
399
    /**
400
     * @throws AuthException
401
     * @throws LockException
402
     */
403
    public function testAllGroups(): void
404
    {
405
        $lib = $this->fileSources();
406
        $data = $lib->readGroup();
407
        $this->assertEquals('Maintainers', $data[0]->getGroupDesc());
408
        $this->assertEquals(1000, $data[1]->getGroupAuthorId());
409
    }
410
411
    protected function wantedGroup($name = 'another'): FileGroup
412
    {
413
        $user = new FileGroup();
414
        $user->setData(3, $name, 1001, 'Testing group');
415
        return $user;
416
    }
417
}
418