1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BasicTests; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use CommonTestClass; |
7
|
|
|
use kalanis\kw_address_handler\Handler; |
8
|
|
|
use kalanis\kw_address_handler\Sources as HandlerSources; |
9
|
|
|
use kalanis\kw_auth\Auth; |
10
|
|
|
use kalanis\kw_auth\AuthException; |
11
|
|
|
use kalanis\kw_auth\AuthTree; |
12
|
|
|
use kalanis\kw_auth\Interfaces; |
13
|
|
|
use kalanis\kw_auth\Methods; |
14
|
|
|
use kalanis\kw_auth\Mode\KwOrig; |
15
|
|
|
use kalanis\kw_auth\Sources; |
16
|
|
|
use kalanis\kw_locks\LockException; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
class AuthTest extends CommonTestClass |
20
|
|
|
{ |
21
|
|
|
public function testStatical(): void |
22
|
|
|
{ |
23
|
|
|
$this->assertEmpty(Auth::getTree()); |
24
|
|
|
Auth::fill( |
25
|
|
|
new Methods\Everytime(null, null) |
26
|
|
|
); |
27
|
|
|
$this->assertNotEmpty(Auth::getTree()); |
28
|
|
|
|
29
|
|
|
$this->assertEmpty(Auth::getAuthenticator()); |
30
|
|
|
$this->assertEmpty(Auth::getAuth()); |
31
|
|
|
$this->assertEmpty(Auth::getAccounts()); |
32
|
|
|
$this->assertEmpty(Auth::getClasses()); |
33
|
|
|
$this->assertEmpty(Auth::getGroups()); |
34
|
|
|
Auth::setAuthenticator(new XAUser()); |
35
|
|
|
Auth::setAuth(new XAAuth()); |
36
|
|
|
Auth::setAccounts(new XAAccounts()); |
37
|
|
|
Auth::setClasses(new XAClasses()); |
38
|
|
|
Auth::setGroups(new XAGroups()); |
39
|
|
|
$this->assertNotEmpty(Auth::getAuthenticator()); |
40
|
|
|
$this->assertNotEmpty(Auth::getAuth()); |
41
|
|
|
$this->assertNotEmpty(Auth::getAccounts()); |
42
|
|
|
$this->assertNotEmpty(Auth::getClasses()); |
43
|
|
|
$this->assertNotEmpty(Auth::getGroups()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @throws AuthException |
48
|
|
|
* @throws LockException |
49
|
|
|
*/ |
50
|
|
|
public function testTree(): void |
51
|
|
|
{ |
52
|
|
|
$tree = new AuthTree(); |
53
|
|
|
$this->assertEmpty($tree->getMethod()); |
54
|
|
|
|
55
|
|
|
// this is what should be in bootstrap |
56
|
|
|
$tree->setTree( |
57
|
|
|
new Methods\HttpDigest( |
58
|
|
|
$this->fileSources(), |
59
|
|
|
new Methods\Everytime(null, null), |
60
|
|
|
new \MockCredentials([ |
61
|
|
|
Methods\HttpDigest::INPUT_METHOD => 'PUT', |
62
|
|
|
Methods\HttpDigest::INPUT_DIGEST => '0123456789qwertzuiopasdfghjklyxcvbnm--', |
63
|
|
|
]) |
64
|
|
|
) |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
// now run that |
68
|
|
|
$this->assertEmpty($tree->getMethod()); |
|
|
|
|
69
|
|
|
$tree->findMethod(new \MockCredentials()); |
70
|
|
|
$this->assertNotEmpty($tree->getMethod()); |
71
|
|
|
$this->assertTrue($tree->getMethod()->isAuthorized()); |
72
|
|
|
$this->assertEquals('Debug', $tree->getMethod()->getLoggedUser()->getAuthName()); |
73
|
|
|
|
74
|
|
|
// tree with data from url |
75
|
|
|
$tree->setTree(new Methods\UrlCerts( |
76
|
|
|
$this->fileSources(), |
77
|
|
|
null, |
78
|
|
|
new Handler(new HandlerSources\Address('//abcdef/ghi/jkl')) |
79
|
|
|
)); |
80
|
|
|
$this->assertEmpty($tree->getMethod()); |
|
|
|
|
81
|
|
|
$tree->findMethod(new \MockCredentials()); |
82
|
|
|
$this->assertEmpty($tree->getMethod()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Contains a full comedy/tragedy of work with locks |
87
|
|
|
* @throws LockException |
88
|
|
|
* @return Sources\Files |
89
|
|
|
*/ |
90
|
|
|
protected function fileSources(): Sources\Files |
91
|
|
|
{ |
92
|
|
|
return new Sources\Files( |
93
|
|
|
new KwOrig('yxcvbnmasdfghjklqwertzuiop0123456789'), |
94
|
|
|
$this->getLockPath(), |
95
|
|
|
__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'data' |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
class XAUser implements Interfaces\IUser |
102
|
|
|
{ |
103
|
|
|
|
104
|
|
|
public function setData(int $authId, string $authName, int $authGroup, int $authClass, string $displayName, string $dir): void |
105
|
|
|
{ |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function getAuthId(): int |
109
|
|
|
{ |
110
|
|
|
return 0; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function getAuthName(): string |
114
|
|
|
{ |
115
|
|
|
return ''; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function getGroup(): int |
119
|
|
|
{ |
120
|
|
|
return 0; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function getClass(): int |
124
|
|
|
{ |
125
|
|
|
return 0; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function getDisplayName(): string |
129
|
|
|
{ |
130
|
|
|
return ''; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function getDir(): string |
134
|
|
|
{ |
135
|
|
|
return ''; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
class XAAuth implements Interfaces\IAuth |
141
|
|
|
{ |
142
|
|
|
public function getDataOnly(string $userName): ?Interfaces\IUser |
143
|
|
|
{ |
144
|
|
|
return null; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function authenticate(string $userName, array $params = []): ?Interfaces\IUser |
148
|
|
|
{ |
149
|
|
|
return null; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
class XAAccounts implements Interfaces\IAccessAccounts |
155
|
|
|
{ |
156
|
|
|
public function createAccount(Interfaces\IUser $user, string $password): void |
157
|
|
|
{ |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function readAccounts(): array |
161
|
|
|
{ |
162
|
|
|
return []; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function updateAccount(Interfaces\IUser $user): void |
166
|
|
|
{ |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function updatePassword(string $userName, string $passWord): void |
170
|
|
|
{ |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function deleteAccount(string $userName): void |
174
|
|
|
{ |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
class XAGroups implements Interfaces\IAccessGroups |
180
|
|
|
{ |
181
|
|
|
|
182
|
|
|
public function createGroup(Interfaces\IGroup $group): void |
183
|
|
|
{ |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function getGroupDataOnly(int $groupId): ?Interfaces\IGroup |
187
|
|
|
{ |
188
|
|
|
return null; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function readGroup(): array |
192
|
|
|
{ |
193
|
|
|
return []; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function updateGroup(Interfaces\IGroup $group): void |
197
|
|
|
{ |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function deleteGroup(int $groupId): void |
201
|
|
|
{ |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
|
206
|
|
|
class XAClasses implements Interfaces\IAccessClasses |
207
|
|
|
{ |
208
|
|
|
public function readClasses(): array |
209
|
|
|
{ |
210
|
|
|
return []; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
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.