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