|
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
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
class FileTest extends AStorageTests |
|
16
|
|
|
{ |
|
17
|
|
|
protected $sourcePath = '.passcomb'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @throws AuthException |
|
21
|
|
|
* @throws LockException |
|
22
|
|
|
*/ |
|
23
|
|
|
public function testNotExistsData(): void |
|
24
|
|
|
{ |
|
25
|
|
|
$lib = $this->emptyFileSources(); |
|
26
|
|
|
$this->assertNull($lib->getDataOnly('does not exist')); |
|
27
|
|
|
$this->assertNull($lib->authenticate('does not exist', ['password' => 'not need'])); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @throws AuthException |
|
32
|
|
|
* @throws LockException |
|
33
|
|
|
*/ |
|
34
|
|
|
public function testDataOnly(): void |
|
35
|
|
|
{ |
|
36
|
|
|
$lib = $this->fileSources(); |
|
37
|
|
|
$this->assertEmpty($lib->getDataOnly('does not exist')); |
|
38
|
|
|
$user = $lib->getDataOnly('manager'); |
|
39
|
|
|
$this->assertNotEmpty($user); |
|
40
|
|
|
$this->assertEquals('Manage', $user->getDisplayName()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @throws AuthException |
|
45
|
|
|
* @throws LockException |
|
46
|
|
|
*/ |
|
47
|
|
|
public function testAuthenticate(): void |
|
48
|
|
|
{ |
|
49
|
|
|
$lib = $this->fileSources(); |
|
50
|
|
|
$this->assertEmpty($lib->authenticate('manager', ['password' => 'thisisnotreal'])); |
|
51
|
|
|
$user = $lib->authenticate('manager', ['password' => 'valid']); |
|
52
|
|
|
$this->assertNotEmpty($user); |
|
53
|
|
|
$this->assertEquals('Manage', $user->getDisplayName()); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @throws AuthException |
|
58
|
|
|
* @throws LockException |
|
59
|
|
|
*/ |
|
60
|
|
|
public function testAuthenticateNoPass(): void |
|
61
|
|
|
{ |
|
62
|
|
|
$lib = $this->fileSources(); |
|
63
|
|
|
$this->expectException(AuthException::class); |
|
64
|
|
|
$lib->authenticate('manager', []); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @throws AuthException |
|
69
|
|
|
* @throws LockException |
|
70
|
|
|
*/ |
|
71
|
|
|
public function testCreateAccountOnEmptyInstance(): void |
|
72
|
|
|
{ |
|
73
|
|
|
$lib = $this->emptyFileSources(); |
|
74
|
|
|
$user = $this->wantedUser(); |
|
75
|
|
|
|
|
76
|
|
|
// create |
|
77
|
|
|
$lib->createAccount($user, 'here to set'); |
|
78
|
|
|
|
|
79
|
|
|
// check data |
|
80
|
|
|
$saved = $lib->getDataOnly($user->getAuthName()); |
|
81
|
|
|
$this->assertEquals('Testing another', $saved->getDisplayName()); |
|
82
|
|
|
$this->assertEquals('why_here', $saved->getDir()); |
|
83
|
|
|
$this->assertEquals(3, $saved->getClass()); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @throws AuthException |
|
88
|
|
|
* @throws LockException |
|
89
|
|
|
*/ |
|
90
|
|
|
public function testUpdateAccountOnEmptyInstance(): void |
|
91
|
|
|
{ |
|
92
|
|
|
$lib = $this->emptyFileSources(); |
|
93
|
|
|
$user = $this->wantedUser(); |
|
94
|
|
|
|
|
95
|
|
|
// update |
|
96
|
|
|
$this->expectException(AuthException::class); |
|
97
|
|
|
$lib->updateAccount($user); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @throws AuthException |
|
102
|
|
|
* @throws LockException |
|
103
|
|
|
*/ |
|
104
|
|
|
public function testUpdatePasswordOnEmptyInstance(): void |
|
105
|
|
|
{ |
|
106
|
|
|
$lib = $this->emptyFileSources(); |
|
107
|
|
|
$user = $this->wantedUser(); |
|
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
// update |
|
110
|
|
|
$this->expectException(AuthException::class); |
|
111
|
|
|
$lib->updatePassword('Some user', 'not important'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @throws AuthException |
|
116
|
|
|
* @throws LockException |
|
117
|
|
|
*/ |
|
118
|
|
|
public function testRemoveAccountOnEmptyInstance(): void |
|
119
|
|
|
{ |
|
120
|
|
|
$lib = $this->emptyFileSources(); |
|
121
|
|
|
$user = $this->wantedUser(); |
|
122
|
|
|
|
|
123
|
|
|
// delete |
|
124
|
|
|
$lib->deleteAccount($user->getAuthName()); |
|
125
|
|
|
$this->assertNull($lib->getDataOnly($user->getAuthName())); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @throws AuthException |
|
130
|
|
|
* @throws LockException |
|
131
|
|
|
*/ |
|
132
|
|
|
public function testAccountManipulation(): void |
|
133
|
|
|
{ |
|
134
|
|
|
$lib = $this->fileSources(); |
|
135
|
|
|
$user = $this->wantedUser(); |
|
136
|
|
|
|
|
137
|
|
|
// create |
|
138
|
|
|
$lib->createAccount($user, 'here to set'); |
|
139
|
|
|
// check data |
|
140
|
|
|
$saved = $lib->getDataOnly($user->getAuthName()); |
|
141
|
|
|
$this->assertEquals('Testing another', $saved->getDisplayName()); |
|
142
|
|
|
$this->assertEquals('why_here', $saved->getDir()); |
|
143
|
|
|
$this->assertEquals(3, $saved->getClass()); |
|
144
|
|
|
|
|
145
|
|
|
// check login |
|
146
|
|
|
$this->assertNotEmpty($lib->authenticate($user->getAuthName(), ['password' => 'here to set'])); |
|
147
|
|
|
|
|
148
|
|
|
// update |
|
149
|
|
|
$user->setData( |
|
150
|
|
|
$user->getAuthId(), |
|
151
|
|
|
$user->getAuthName(), |
|
152
|
|
|
$user->getGroup(), |
|
153
|
|
|
2, |
|
154
|
|
|
'WheĐn yoĐu dđo nođt knđow', |
|
155
|
|
|
$user->getDir() |
|
156
|
|
|
); |
|
157
|
|
|
$lib->updateAccount($user); |
|
158
|
|
|
|
|
159
|
|
|
// check data - again with new values |
|
160
|
|
|
$saved = $lib->getDataOnly($user->getAuthName()); |
|
161
|
|
|
$this->assertEquals('When you do not know', $saved->getDisplayName()); |
|
162
|
|
|
$this->assertEquals(2, $saved->getClass()); |
|
163
|
|
|
|
|
164
|
|
|
// update password |
|
165
|
|
|
$lib->updatePassword($user->getAuthName(), 'another pass'); |
|
166
|
|
|
// check login |
|
167
|
|
|
$this->assertEmpty($lib->authenticate($user->getAuthName(), ['password' => 'here to set'])); |
|
168
|
|
|
$this->assertNotEmpty($lib->authenticate($user->getAuthName(), ['password' => 'another pass'])); |
|
169
|
|
|
|
|
170
|
|
|
// remove |
|
171
|
|
|
$lib->deleteAccount($user->getAuthName()); |
|
172
|
|
|
// check for existence |
|
173
|
|
|
$this->assertEmpty($lib->getDataOnly($user->getAuthName())); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @throws AuthException |
|
178
|
|
|
* @throws LockException |
|
179
|
|
|
*/ |
|
180
|
|
|
public function testCreateFail(): void |
|
181
|
|
|
{ |
|
182
|
|
|
$lib = $this->fileSources(); |
|
183
|
|
|
$user = $this->wantedUser(); |
|
184
|
|
|
$this->expectException(AuthException::class); |
|
185
|
|
|
$lib->createAccount($user, ''); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @throws AuthException |
|
190
|
|
|
* @throws LockException |
|
191
|
|
|
*/ |
|
192
|
|
|
public function testAllUsers(): void |
|
193
|
|
|
{ |
|
194
|
|
|
$lib = $this->fileSources(); |
|
195
|
|
|
$data = $lib->readAccounts(); |
|
196
|
|
|
$this->assertEquals(1, $data[0]->getClass()); |
|
197
|
|
|
$this->assertEquals('manager', $data[1]->getAuthName()); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Contains a full comedy/tragedy of work with locks |
|
202
|
|
|
* @throws LockException |
|
203
|
|
|
* @throws AuthException |
|
204
|
|
|
* @return File |
|
205
|
|
|
*/ |
|
206
|
|
|
protected function fileSources(): File |
|
207
|
|
|
{ |
|
208
|
|
|
$storage = new Storage(new DefaultKey(), new Memory()); |
|
209
|
|
|
$file = new File( |
|
210
|
|
|
$storage, |
|
211
|
|
|
new \MockModes(), |
|
212
|
|
|
$this->getLockPath(), |
|
213
|
|
|
$this->sourcePath |
|
214
|
|
|
); |
|
215
|
|
|
$storage->write($this->sourcePath, |
|
216
|
|
|
'1000:owner:$2y$10$6-bucFamnK5BTGbojaWw3!HzzHOlUNnN6PF3Y9qHQIdE8FmQKv/eq:0:1:Owner:/data/:' . "\r\n" |
|
217
|
|
|
. '1001:manager:$2y$10$G1Fo0udxqekABHkzUQubfuD8AjgD/5O9F9v3E0qYG2TI0BfZAkyz2:1:2:Manage:/data/:' . "\r\n" |
|
218
|
|
|
. '# commented out' . "\r\n" |
|
219
|
|
|
. '1002:worker:$2y$10$6.bucFamnK5BTGbojaWw3.HpzHOlQUnN6PF3Y9qHQIdE8FmQKv/eq:1:3:Worker:/data/:' . "\r\n" |
|
220
|
|
|
// last line is intentionally empty one |
|
221
|
|
|
); |
|
222
|
|
|
return $file; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Contains a full comedy/tragedy of work with locks |
|
227
|
|
|
* @throws LockException |
|
228
|
|
|
* @return File |
|
229
|
|
|
*/ |
|
230
|
|
|
protected function emptyFileSources(): File |
|
231
|
|
|
{ |
|
232
|
|
|
return new File( |
|
233
|
|
|
new Storage(new DefaultKey(), new Memory()), |
|
234
|
|
|
new \MockModes(), |
|
235
|
|
|
$this->getLockPath(), |
|
236
|
|
|
$this->sourcePath |
|
237
|
|
|
); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
protected function wantedUser(): FileUser |
|
241
|
|
|
{ |
|
242
|
|
|
$user = new FileUser(); |
|
243
|
|
|
$user->setData(600, 'another', 0, 0, 'Testing another', 'why_here'); |
|
244
|
|
|
return $user; |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|