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

FileTest::testAccountManipulation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 23
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 43
rs 9.552
1
<?php
2
3
namespace SourcesTests\Files\Storage;
4
5
6
use kalanis\kw_auth\AuthException;
7
use kalanis\kw_auth\Data\FileUser;
8
use kalanis\kw_auth\Sources\Files\Storage\File;
9
use kalanis\kw_auth\Statuses\Always;
10
use kalanis\kw_locks\LockException;
11
use kalanis\kw_storage\Storage\Key\DefaultKey;
12
use kalanis\kw_storage\Storage\Storage;
13
use kalanis\kw_storage\Storage\Target\Memory;
14
use kalanis\kw_storage\StorageException;
15
16
17
class FileTest extends AStorageTests
18
{
19
    protected $sourcePath = '.passcomb';
20
21
    /**
22
     * @throws AuthException
23
     * @throws LockException
24
     */
25
    public function testNotExistsData(): void
26
    {
27
        $lib = $this->emptyFileSources();
28
        $this->assertNull($lib->getDataOnly('does not exist'));
29
        $this->assertNull($lib->authenticate('does not exist', ['password' => 'not need']));
30
    }
31
32
    /**
33
     * @throws AuthException
34
     * @throws LockException
35
     * @throws StorageException
36
     */
37
    public function testDataOnly(): void
38
    {
39
        $lib = $this->fileSources();
40
        $this->assertEmpty($lib->getDataOnly('does not exist'));
41
        $user = $lib->getDataOnly('manager');
42
        $this->assertNotEmpty($user);
43
        $this->assertEquals('Manage', $user->getDisplayName());
44
    }
45
46
    /**
47
     * @throws AuthException
48
     * @throws LockException
49
     * @throws StorageException
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
     * @throws StorageException
64
     */
65
    public function testAuthenticateNoPass(): void
66
    {
67
        $lib = $this->fileSources();
68
        $this->expectException(AuthException::class);
69
        $lib->authenticate('manager', []);
70
    }
71
72
    /**
73
     * @throws AuthException
74
     * @throws LockException
75
     */
76
    public function testCreateAccountOnEmptyInstance(): void
77
    {
78
        $lib = $this->emptyFileSources();
79
        $user = $this->wantedUser();
80
81
        // create
82
        $lib->createAccount($user, 'here to set');
83
84
        // check data
85
        $saved = $lib->getDataOnly($user->getAuthName());
86
        $this->assertEquals('Testing another', $saved->getDisplayName());
87
        $this->assertEquals('why_here', $saved->getDir());
88
        $this->assertEquals(3, $saved->getClass());
89
    }
90
91
    /**
92
     * @throws AuthException
93
     * @throws LockException
94
     */
95
    public function testUpdateAccountOnEmptyInstance(): void
96
    {
97
        $lib = $this->emptyFileSources();
98
        $user = $this->wantedUser();
99
100
        // update
101
        $this->expectException(AuthException::class);
102
        $lib->updateAccount($user);
103
    }
104
105
    /**
106
     * @throws AuthException
107
     * @throws LockException
108
     */
109
    public function testUpdatePasswordOnEmptyInstance(): void
110
    {
111
        $lib = $this->emptyFileSources();
112
        // update
113
        $this->expectException(AuthException::class);
114
        $lib->updatePassword('Some user', 'not important');
115
    }
116
117
    /**
118
     * @throws AuthException
119
     * @throws LockException
120
     */
121
    public function testRemoveAccountOnEmptyInstance(): void
122
    {
123
        $lib = $this->emptyFileSources();
124
        $user = $this->wantedUser();
125
126
        // delete
127
        $lib->deleteAccount($user->getAuthName());
128
        $this->assertNull($lib->getDataOnly($user->getAuthName()));
129
    }
130
131
    /**
132
     * @throws AuthException
133
     * @throws LockException
134
     * @throws StorageException
135
     */
136
    public function testAccountManipulation(): void
137
    {
138
        $lib = $this->fileSources();
139
        $user = $this->wantedUser();
140
141
        // create
142
        $lib->createAccount($user, 'here to set');
143
        // check data
144
        $saved = $lib->getDataOnly($user->getAuthName());
145
        $this->assertEquals('Testing another', $saved->getDisplayName());
146
        $this->assertEquals('why_here', $saved->getDir());
147
        $this->assertEquals(3, $saved->getClass());
148
149
        // check login
150
        $this->assertNotEmpty($lib->authenticate($user->getAuthName(), ['password' => 'here to set']));
151
152
        // update
153
        $user->setData(
154
            $user->getAuthId(),
155
            $user->getAuthName(),
156
            $user->getGroup(),
157
            2,
158
            3,
159
            'WheĐn yoĐu dđo nođt knđow',
160
            $user->getDir()
161
        );
162
        $lib->updateAccount($user);
163
164
        // check data - again with new values
165
        $saved = $lib->getDataOnly($user->getAuthName());
166
        $this->assertEquals('When you do not know', $saved->getDisplayName());
167
        $this->assertEquals(2, $saved->getClass());
168
169
        // update password
170
        $lib->updatePassword($user->getAuthName(), 'another pass');
171
        // check login
172
        $this->assertEmpty($lib->authenticate($user->getAuthName(), ['password' => 'here to set']));
173
        $this->assertNotEmpty($lib->authenticate($user->getAuthName(), ['password' => 'another pass']));
174
175
        // remove
176
        $lib->deleteAccount($user->getAuthName());
177
        // check for existence
178
        $this->assertEmpty($lib->getDataOnly($user->getAuthName()));
179
    }
180
181
    /**
182
     * @throws AuthException
183
     * @throws LockException
184
     * @throws StorageException
185
     */
186
    public function testCreateFail(): void
187
    {
188
        $lib = $this->fileSources();
189
        $user = $this->wantedUser();
190
        $this->expectException(AuthException::class);
191
        $lib->createAccount($user, '');
192
    }
193
194
    /**
195
     * @throws AuthException
196
     * @throws LockException
197
     * @throws StorageException
198
     */
199
    public function testAllUsers(): void
200
    {
201
        $lib = $this->fileSources();
202
        $data = $lib->readAccounts();
203
        $this->assertEquals(1, $data[0]->getClass());
204
        $this->assertEquals('manager', $data[1]->getAuthName());
205
    }
206
207
    /**
208
     * @throws AuthException
209
     * @throws LockException
210
     */
211
    public function testCreateAccountStorageFail(): void
212
    {
213
        $lib = $this->failedFileSources();
214
        $group = $this->wantedUser();
215
        $this->expectException(AuthException::class);
216
        $lib->createAccount($group, 'somewhere');
217
    }
218
219
    /**
220
     * @throws AuthException
221
     * @throws LockException
222
     */
223
    public function testCreateAccountStorageFailSave(): void
224
    {
225
        $lib = $this->failedFileSources(true);
226
        $group = $this->wantedUser();
227
        $this->expectException(AuthException::class);
228
        $lib->createAccount($group, 'somewhere');
229
    }
230
231
    /**
232
     * @throws AuthException
233
     * @throws LockException
234
     */
235
    public function testReadAccountsStorageFail(): void
236
    {
237
        $lib = $this->failedFileSources();
238
        $this->expectException(AuthException::class);
239
        $lib->readAccounts();
240
    }
241
242
    /**
243
     * @throws AuthException
244
     * @throws LockException
245
     */
246
    public function testUpdateAccountStorageFail(): void
247
    {
248
        $lib = $this->failedFileSources();
249
        $this->expectException(AuthException::class);
250
        $lib->updateAccount($this->wantedUser());
251
    }
252
253
    /**
254
     * @throws AuthException
255
     * @throws LockException
256
     */
257
    public function testUpdateAccountStorageFailSave(): void
258
    {
259
        $lib = $this->failedFileSources(true);
260
        $this->expectException(AuthException::class);
261
        $lib->updateAccount($this->wantedUser());
262
    }
263
264
    /**
265
     * @throws AuthException
266
     * @throws LockException
267
     */
268
    public function testRemoveUserStorageFail(): void
269
    {
270
        $lib = $this->failedFileSources();
271
        $this->assertTrue($lib->deleteAccount('no-one'));
272
    }
273
274
    /**
275
     * @throws AuthException
276
     * @throws LockException
277
     */
278
    public function testRemoveUserStorageFailSave(): void
279
    {
280
        $lib = $this->failedFileSources(true, '1000:owner:some-wanted:0:1:1:Owner:/data/:' . "\r\n" . '1002:worker:some-else:1:3:1:Worker:/data/:' . "\r\n");
281
        $this->expectException(AuthException::class);
282
        $lib->deleteAccount('worker');
283
    }
284
285
    /**
286
     * Contains a full comedy/tragedy of work with locks
287
     * @throws LockException
288
     * @throws StorageException
289
     * @return File
290
     */
291
    protected function fileSources(): File
292
    {
293
        $storage = new Storage(new DefaultKey(), new Memory());
294
        $file = new File(
295
            $storage,
296
            new \MockModes(),
297
            new Always(),
298
            $this->getLockPath(),
299
            $this->sourcePath
300
        );
301
        $storage->write($this->sourcePath,
302
            '1000:owner:$2y$10$6-bucFamnK5BTGbojaWw3!HzzHOlUNnN6PF3Y9qHQIdE8FmQKv/eq:0:1:1:Owner:/data/:' . "\r\n"
303
            . '1001:manager:$2y$10$G1Fo0udxqekABHkzUQubfuD8AjgD/5O9F9v3E0qYG2TI0BfZAkyz2:1:2:1:Manage:/data/:' . "\r\n"
304
            . '# commented out' . "\r\n"
305
            . '1002:worker:$2y$10$6.bucFamnK5BTGbojaWw3.HpzHOlQUnN6PF3Y9qHQIdE8FmQKv/eq:1:3:1:Worker:/data/:' . "\r\n"
306
            // last line is intentionally empty one
307
        );
308
        return $file;
309
    }
310
311
    /**
312
     * @throws LockException
313
     * @return File
314
     */
315
    protected function emptyFileSources(): File
316
    {
317
        return new File(
318
            new Storage(new DefaultKey(), new Memory()),
319
            new \MockModes(),
320
            new Always(),
321
            $this->getLockPath(),
322
            $this->sourcePath
323
        );
324
    }
325
326
    /**
327
     * @param bool $canOpen
328
     * @param string $content
329
     * @throws LockException
330
     * @return File
331
     */
332
    protected function failedFileSources(bool $canOpen = false, string $content = ''): File
333
    {
334
        return new File(
335
            new \XFailedStorage($canOpen, $content),
336
            new \MockModes(),
337
            new Always(),
338
            $this->getLockPath(),
339
            $this->sourcePath
340
        );
341
    }
342
343
    protected function wantedUser(): FileUser
344
    {
345
        $user = new FileUser();
346
        $user->setData(600, 'another', 0, 0, 2,'Testing another', 'why_here');
347
        return $user;
348
    }
349
}
350