-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserTest.php
More file actions
349 lines (294 loc) · 10.4 KB
/
UserTest.php
File metadata and controls
349 lines (294 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
namespace Pdsinterop\PhpSolid\Tests;
require_once(__DIR__ . "/test-config.php");
use Pdsinterop\PhpSolid\User;
use Pdsinterop\PhpSolid\Db;
class UserTest extends \PHPUnit\Framework\TestCase
{
protected function setUp(): void
{
$futureTimestamp = new \DateTime();
$futureTimestamp->add(new \DateInterval('P120M'));
$statements = [
'DROP TABLE IF EXISTS allowedClients',
'DROP TABLE IF EXISTS userStorage',
'DROP TABLE IF EXISTS verify',
'DROP TABLE IF EXISTS users',
'CREATE TABLE IF NOT EXISTS allowedClients (
userId VARCHAR(255) NOT NULL PRIMARY KEY,
clientId VARCHAR(255) NOT NULL
)',
'CREATE TABLE IF NOT EXISTS userStorage (
userId VARCHAR(255) NOT NULL PRIMARY KEY,
storageUrl VARCHAR(255) NOT NULL
)',
'CREATE TABLE IF NOT EXISTS verify (
code VARCHAR(255) NOT NULL PRIMARY KEY,
data TEXT NOT NULL
)',
'CREATE TABLE IF NOT EXISTS users (
user_id VARCHAR(255) NOT NULL PRIMARY KEY,
email TEXT NOT NULL,
password TEXT NOT NULL,
data TEXT
)',
'INSERT INTO verify VALUES("test1", \'{"expires": 0, "hello": "world", "code": "test1"}\')',
'INSERT INTO verify VALUES("test2", \'{"expires": ' . $futureTimestamp->getTimestamp() . ', "hello": "world", "code": "test2"}\')'
];
Db::connect();
try {
// create tables
foreach($statements as $statement){
Db::$pdo->exec($statement);
}
} catch(\PDOException $e) {
echo $e->getMessage();
}
}
public function testSaveVerifyToken() {
$beforeExpires = new \DateTime();
$beforeExpires->add(new \DateInterval('PT29M'));
$afterExpires = new \DateTime();
$afterExpires->add(new \DateInterval('PT31M'));
$token = User::saveVerifyToken("verify", [
"hello" => "world"
]);
$this->assertTrue($token['expires'] > $beforeExpires->getTimestamp());
$this->assertTrue($token['expires'] < $afterExpires->getTimestamp());
$storedToken = User::getVerifyToken($token['code']);
$this->assertEquals($storedToken['hello'], "world");
}
public function testSavePasswordResetToken() {
$beforeExpires = new \DateTime();
$beforeExpires->add(new \DateInterval('PT29M'));
$afterExpires = new \DateTime();
$afterExpires->add(new \DateInterval('PT31M'));
$token = User::saveVerifyToken("verify", [
"hello" => "world"
]);
$this->assertTrue($token['expires'] > $beforeExpires->getTimestamp());
$this->assertTrue($token['expires'] < $afterExpires->getTimestamp());
$storedToken = User::getVerifyToken($token['code']);
$this->assertEquals($storedToken['hello'], "world");
}
public function testSaveAccountDeleteToken() {
$beforeExpires = new \DateTime();
$beforeExpires->add(new \DateInterval('PT29M'));
$afterExpires = new \DateTime();
$afterExpires->add(new \DateInterval('PT31M'));
$token = User::saveVerifyToken("verify", [
"hello" => "world"
]);
$this->assertTrue($token['expires'] > $beforeExpires->getTimestamp());
$this->assertTrue($token['expires'] < $afterExpires->getTimestamp());
$storedToken = User::getVerifyToken($token['code']);
$this->assertEquals($storedToken['hello'], "world");
}
public function testExpiredToken() {
$token = User::getVerifyToken("test1");
$this->assertFalse($token);
}
public function testNonExpiredToken() {
$token = User::getVerifyToken("test2");
$this->assertEquals($token['hello'], "world");
}
public function testCreateUser() {
$newUser = [
"password" => "hello123!@#ABC",
"email" => "user@example.com",
"hello" => "world"
];
$createdUser = User::createUser($newUser);
$this->assertEquals($createdUser['email'], "user@example.com");
$this->assertTrue(isset($createdUser['webId']));
$this->assertTrue(isset($createdUser['userId']));
$this->assertTrue(strlen($createdUser['userId']) === 32);
$canLogIn = User::checkPassword($newUser['email'], $newUser['password']);
$this->assertTrue($canLogIn);
}
public function testGetUser() {
$newUser = [
"password" => "hello123!@#ABC",
"email" => "user2@example.com",
"hello" => "world"
];
$createdUser = User::createUser($newUser);
$userByEmail = User::getUser($newUser['email']);
$this->assertEquals($userByEmail['webId'], $createdUser['webId']);
$this->assertEquals($userByEmail['hello'], 'world');
$this->assertTrue(isset($userByEmail['allowedClients']));
$this->assertEquals($userByEmail['issuer'], "https://solid.example.com");
}
public function testGetUserById() {
$newUser = [
"password" => "hello123!@#ABC",
"email" => "user3@example.com",
"hello" => "world"
];
$createdUser = User::createUser($newUser);
$userById = User::getUserById($createdUser['userId']);
$this->assertEquals($userById['webId'], $createdUser['webId']);
$this->assertEquals($userById['hello'], 'world');
$this->assertTrue(isset($userById['allowedClients']));
$this->assertEquals($userById['issuer'], "https://solid.example.com");
}
public function testSetPasswordNonExistingUser() {
$result = User::setUserPassword("not_here@example.com", "hello123!@#ABC");
$this->assertFalse($result);
}
public function testSetWeakPassword() {
$newUser = [
"password" => "hello123!@#ABC",
"email" => "user4@example.com",
"hello" => "world"
];
$createdUser = User::createUser($newUser);
$result = User::setUserPassword($newUser['email'], "a");
$this->assertFalse($result);
}
public function testLogin() {
$newUser = [
"password" => "hello123!@#ABC",
"email" => "user5@example.com",
"hello" => "world"
];
$createdUser = User::createUser($newUser);
$canLogIn = User::checkPassword($newUser['email'], $newUser['password']);
$this->assertTrue($canLogIn);
}
public function testLoginFailed() {
$newUser = [
"password" => "hello123!@#ABC",
"email" => "user6@example.com",
"hello" => "world"
];
$createdUser = User::createUser($newUser);
$canLogIn = User::checkPassword($newUser['email'], "something else");
$this->assertFalse($canLogIn);
}
public function testSetStrongPassword() {
$newUser = [
"password" => "hello123!@#ABC",
"email" => "user7@example.com",
"hello" => "world"
];
$createdUser = User::createUser($newUser);
$result = User::setUserPassword($newUser['email'], "this is a strong password because it is long enough");
$this->assertTrue($result);
}
public function testLoginAfterChange() {
$newUser = [
"password" => "hello123!@#ABC",
"email" => "user8@example.com",
"hello" => "world"
];
$createdUser = User::createUser($newUser);
$canLogIn = User::checkPassword($newUser['email'], $newUser['password']);
$this->assertTrue($canLogIn);
$newPassword = "this is a strong password because it is long enough";
$result = User::setUserPassword($newUser['email'], $newPassword);
$this->assertTrue($result);
$canLogIn = User::checkPassword($newUser['email'], "something else");
$this->assertFalse($canLogIn);
$canLogIn = User::checkPassword($newUser['email'], $newUser['password']);
$this->assertFalse($canLogIn);
$canLogIn = User::checkPassword($newUser['email'], $newPassword);
$this->assertTrue($canLogIn);
}
public function testUserStorage() {
$newUser = [
"password" => "hello123!@#ABC",
"email" => "user9@example.com",
"hello" => "world"
];
$createdUser = User::createUser($newUser);
$storageUrl = "https://storage.example.com";
User::setStorage($createdUser['userId'], $storageUrl);
$savedStorage = User::getStorage($createdUser['userId']);
$this->assertTrue(in_array($storageUrl, $savedStorage));
$user = User::getUser($newUser['email']);
$this->assertTrue(in_array($storageUrl, $user['storage']));
}
public function testUserExistsById() {
$newUser = [
"password" => "hello123!@#ABC",
"email" => "user10@example.com",
"hello" => "world"
];
$createdUser = User::createUser($newUser);
$userExists = User::userIdExists($createdUser['userId']);
$this->assertTrue($userExists);
}
public function testUserDoesNotExistsById() {
$userExists = User::userIdExists("foo");
$this->assertFalse($userExists);
}
public function testUserExistsByEmail() {
$newUser = [
"password" => "hello123!@#ABC",
"email" => "user11@example.com",
"hello" => "world"
];
$createdUser = User::createUser($newUser);
$userExists = User::userEmailExists($newUser['email']);
$this->assertTrue($userExists);
}
public function testUserDoesNotExistsByEmail() {
$userExists = User::userEmailExists("foo@example.com");
$this->assertFalse($userExists);
}
public function testAllowClientForUser() {
$newUser = [
"password" => "hello123!@#ABC",
"email" => "user11@example.com",
"hello" => "world"
];
$createdUser = User::createUser($newUser);
$clientId = "12345";
$result = User::allowClientForUser($clientId, $createdUser['userId']);
$this->assertTrue($result);
$allowedClients = User::getAllowedClients($createdUser['userId']);
$this->assertTrue(in_array($clientId, $allowedClients));
$user = User::getUser($newUser['email']);
$this->assertTrue(in_array($clientId, $user['allowedClients']));
}
public function testDeleteAccount() {
$newUser = [
"password" => "hello123!@#ABC",
"email" => "user11@example.com",
"hello" => "world"
];
$createdUser = User::createUser($newUser);
$clientId = "12345";
$result = User::allowClientForUser($clientId, $createdUser['userId']);
$this->assertTrue(User::userIdExists($createdUser['userId']));
$this->assertTrue(User::userEmailExists($newUser['email']));
$allowedClients = User::getAllowedClients($createdUser['userId']);
$this->assertTrue(in_array($clientId, $allowedClients));
User::deleteAccount($newUser['email']);
$this->assertFalse(User::userIdExists($createdUser['userId']));
$this->assertFalse(User::userEmailExists($newUser['email']));
$allowedClients = User::getAllowedClients($createdUser['userId']);
$this->assertEmpty($allowedClients);
}
public function testCleanup() {
// empty the verify table first so we have dependable numbers
$query = Db::$pdo->prepare('DELETE FROM verify WHERE NOT code=""');
$query->execute();
$token1 = User::saveVerifyToken("verify", [
"hello" => "world",
"expires" => time() - 10
]);
$query = Db::$pdo->prepare('SELECT count(*) AS count FROM verify');
$query->execute();
$result = $query->fetchAll();
$beforeCleanup = $result[0]['count'];
$this->assertEquals(1, $beforeCleanup);
User::cleanupTokens();
$query = Db::$pdo->prepare('SELECT count(*) AS count FROM verify');
$query->execute();
$result = $query->fetchAll();
$afterCleanup = $result[0]['count'];
$this->assertEquals(0, $afterCleanup);
}
}