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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
|
<?php
namespace Psecio\Gatekeeper;
class UserModelTest extends \Psecio\Gatekeeper\Base
{
private $permissions = array(1, 2, 3);
private $groups = array(1, 2, 3);
private function buildPermissionGroupUserMock()
{
$user = $this->getMockBuilder('\Psecio\Gatekeeper\UserModel')
->disableOriginalConstructor()
->setMethods(array('grantPermissions', 'grantGroups'))
->getMock();
return $user;
}
private function buildMysqlDataSourceMock($method = 'save')
{
$ds = $this->getMockBuilder('\Psecio\Gatekeeper\DataSource\Mysql')
->disableOriginalConstructor()
->setMethods(array($method))
->getMock();
return $ds;
}
/**
* Test that a 0 is returned when no throttle record is found (null)
*/
public function testFindThrottleAttemptsNoLogin()
{
$return = (object)array('attempts' => null);
$ds = $this->buildMock($return);
$user = new UserModel($ds);
$result = $user->findAttemptsByUser(1);
$this->assertEquals(0, $result);
}
/**
* Check to be sure the blocked logic works correctly
*/
public function testUserIsBlocked()
{
$return = (object)array('status' => ThrottleModel::STATUS_BLOCKED);
$ds = $this->buildMock($return);
$user = new UserModel($ds);
$this->assertTrue($user->isBanned());
}
/**
* Test that the user does have the permission
*/
public function testUserHasPermission()
{
$return = (object)array('id' => 1234, 'name' => 'perm1');
$ds = $this->buildMock($return);
$user = new UserModel($ds);
$this->assertTrue($user->hasPermission(1234));
}
public function testUserInGroup()
{
$return = (object)array('id' => 1234, 'name' => 'group1', 'groupId' => 1234);
$ds = $this->buildMock($return);
$user = new UserModel($ds);
$this->assertTrue($user->inGroup(1234));
}
/**
* Test that false is returned if the group found is invalid (no ID)
*/
public function testUserInGroupInvalid()
{
$return = (object)array('id' => null, 'name' => 'group1', 'groupId' => 1234);
$ds = $this->buildMock($return);
$user = new UserModel($ds);
$this->assertFalse($user->inGroup(1234));
}
/**
* Test the clearing of the password reset handling
*/
public function testClearPasswordResetCode()
{
$ds = $this->buildMock(true, 'save');
$user = new UserModel($ds, array('id' => 1234));
$user->resetCode = sha1(mt_rand());
$user->resetCodeTimeout = date('m.d.Y H:i:s');
$user->clearPasswordResetCode();
$this->assertTrue(
$user->resetCode === null && $user->resetCodeTimeout === null
);
}
/**
* Test that when the user isn't valid (no ID), false is returned
*/
public function testClearPasswordResetCodeBadUser()
{
$ds = $this->buildMock(true, 'save');
$user = new UserModel($ds);
$this->assertFalse($user->clearPasswordResetCode());
}
/**
* Test the hash checking on password resets for a valid situation
*/
public function testCheckPasswordResetCodeValid()
{
$code = sha1(mt_rand());
$ds = $this->buildMock(true, 'save');
$user = new UserModel($ds, array('id' => 1234));
$user->resetCode = $code;
$user->resetCodeTimeout = date('Y/m/d H:i:s', strtotime('+1 day'));
$this->assertTrue($user->checkResetPasswordCode($code));
$this->assertTrue(
$user->resetCode === null && $user->resetCodeTimeout === null
);
}
/**
* If an invalid user is defined (no ID), false is returned
*/
public function testCheckPasswordResetCodeBadUser()
{
$ds = $this->buildMock(true, 'save');
$user = new UserModel($ds);
$this->assertFalse($user->checkResetPasswordCode('hash'));
}
/**
* Test that an exception is thrown when the reset code is not present
*
* @expectedException \Psecio\Gatekeeper\Exception\PasswordResetInvalid
*/
public function testCheckPasswordResetInvalidCode()
{
$code = sha1(mt_rand());
$ds = $this->buildMock(true, 'save');
$user = new UserModel($ds, array('id' => 1234));
$user->checkResetPasswordCode('code');
}
/**
* Test that an exception is thrown when the reset code has timed out
*
* @expectedException \Psecio\Gatekeeper\Exception\PasswordResetTimeout
*/
public function testCheckPasswordResetCodeTimeout()
{
$code = sha1(mt_rand());
$ds = $this->buildMock(true, 'save');
$user = new UserModel($ds, array('id' => 1234));
$user->resetCode = $code;
$user->resetCodeTimeout = date('Y/m/d H:i:s', strtotime('-1 day'));
$this->assertTrue($user->checkResetPasswordCode($code));
}
/**
* Test the password code generation
*/
public function testPasswordResetCodeGeneration()
{
$ds = $this->buildMock(true, 'save');
$user = new UserModel($ds, array('id' => 1234));
// Defaults to 80, use a custom 100 too
$this->assertEquals(strlen($user->getResetPasswordCode()), 80);
$this->assertEquals(strlen($user->getResetPasswordCode(100)), 100);
}
/**
* Test that an invalid user (no ID) returns false on code generation
* (the user is required as it saves the code when it generates it)
*/
public function testPasswordResetCodeGenerationBadUser()
{
$ds = $this->buildMock(true, 'save');
$user = new UserModel($ds);
// Defaults to 80, use a custom 100 too
$this->assertFalse($user->getResetPasswordCode());
}
/**
* Test that a user is successfully deactivated (status change)
*/
public function testDeactivateUserValid()
{
$ds = $this->buildMock(true, 'save');
$user = new UserModel($ds, array('id' => 1234));
$user->deactivate();
$this->assertEquals($user->status, $user::STATUS_INACTIVE);
}
/**
* Test that false is returned when the user is invalid (no ID)
*/
public function testDeactivateUserInvalid()
{
$ds = $this->buildMock(true, 'save');
$user = new UserModel($ds);
$this->assertFalse($user->deactivate());
}
/**
* Test that a user is successfully activated (status change)
*/
public function testActivateUserValid()
{
$ds = $this->buildMock(true, 'save');
$user = new UserModel($ds, array('id' => 1234));
$user->activate();
$this->assertEquals($user->status, $user::STATUS_ACTIVE);
}
/**
* Test that false is returned when the user is invalid (no ID)
*/
public function testActivateUserInvalid()
{
$ds = $this->buildMock(true, 'save');
$user = new UserModel($ds);
$this->assertFalse($user->activate());
}
/**
* Test that a group can be added by using a Group ID
*/
public function testAddGroupByIdValid()
{
$ds = $this->buildMock(true, 'save');
$user = new UserModel($ds);
$this->assertTrue($user->addGroup(1));
}
/**
* Test that a group can be added by using a Group model
*/
public function testAddGroupByModelValid()
{
$ds = $this->buildMock(true, 'save');
$user = new UserModel($ds);
$group = new GroupModel($ds, array('id' => 1234));
$this->assertTrue($user->addGroup($group));
}
/**
* Test that a permission can be added by ID
*/
public function testAddPermissionByIdValid()
{
$ds = $this->buildMock(true, 'save');
$user = new UserModel($ds);
$this->assertTrue($user->addPermission(1));
}
/**
* Test that a permission can be added by Permission model instance
*/
public function testAddPermissionByModelValid()
{
$ds = $this->buildMock(true, 'save');
$user = new UserModel($ds);
$perm = new PermissionModel($ds, array('id' => 1234));
$this->assertTrue($user->addPermission($perm));
}
/**
* Test the location of a record by username, mocked fetch
*/
public function testFindByUsername()
{
$username = 'ccornutt';
$data = array(
array('username' => $username)
);
$ds = $this->buildMysqlDataSourceMock('fetch');
$ds->method('fetch')
->willReturn($data);
$user = new UserModel($ds);
$user->findByUsername($username);
$this->assertEquals($user->username, $username);
}
/**
* Test that a password needs a rehash
* In this case, it's a plain-text password that needs hashing
*/
public function testPasswordNeedsRehash()
{
$password = 'test1234';
$ds = $this->buildMock(true);
$user = new UserModel($ds);
$postPassword = $user->prePassword($password);
$this->assertNotEquals($password, $postPassword);
}
/**
* Test that a true is returned when you add valid permissions by ID
*/
public function testGrantPermissionsByIdValid()
{
$ds = $this->buildMysqlDataSourceMock();
$ds->method('save')->willReturn(true);
$perms = array(1, 2, 3);
$user = new UserModel($ds);
$this->assertTrue($user->grantPermissions($perms));
}
/**
* Try to add a set of permissions with a failed return (false)
*/
public function testGrantPermissionsByIdInalid()
{
$ds = $this->buildMysqlDataSourceMock();
$ds->method('save')->willReturn(false);
$user = new UserModel($ds);
$this->assertFalse($user->grantPermissions(array(1, 2, 3)));
}
/**
* Test that it understsands how to grant permissions by model instances too
*/
public function testGrantPermissionsByModelValid()
{
$ds = $this->buildMysqlDataSourceMock();
$ds->method('save')->willReturn(true);
$perms = array(
new PermissionModel($ds, array('id' => 1)),
new PermissionModel($ds, array('id' => 2)),
new PermissionModel($ds, array('id' => 3))
);
$user = new UserModel($ds);
$this->assertTrue($user->grantPermissions($perms));
}
/**
* Test the addition of group accesss by ID
*/
public function testGrantGroupsByIdValid()
{
$ds = $this->buildMysqlDataSourceMock();
$ds->method('save')->willReturn(true);
$groups = array(1, 2, 3);
$user = new UserModel($ds);
$this->assertTrue($user->grantGroups($groups));
}
/**
* Test the addition of group accesss by ID with failure
*/
public function testGrantGroupsByIdInvalid()
{
$ds = $this->buildMysqlDataSourceMock();
$ds->method('save')->willReturn(false);
$user = new UserModel($ds);
$this->assertFalse($user->grantGroups(array(1, 2, 3)));
}
/**
* Test the addition of group accesss by Group model
*/
public function testGrantGroupsByModelValid()
{
$ds = $this->buildMysqlDataSourceMock();
$ds->method('save')->willReturn(true);
$groups = array(
new GroupModel($ds, array('id' => 1)),
new GroupModel($ds, array('id' => 2)),
new GroupModel($ds, array('id' => 3))
);
$user = new UserModel($ds);
$this->assertTrue($user->grantGroups($groups));
}
/**
* Test using the generic "grant" method to grant both permissions and groups
*/
public function testGrantGroupsAndPermissionsAllValid()
{
$user = $this->buildPermissionGroupUserMock();
$user->method('grantPermissions')->willReturn(true);
$user->method('grantGroups')->willReturn(true);
$result = $user->grant(array(
'permissions' => $this->permissions,
'groups' => $this->groups
));
$this->assertTrue($result);
}
/**
* Test the addition of groups, but with a failure in adding
*/
public function testGrantGroupsInvalid()
{
$user = $this->buildPermissionGroupUserMock();
$user->method('grantPermissions')->willReturn(true);
$user->method('grantGroups')->willReturn(false);
$result = $user->grant(array(
'permissions' => $this->permissions,
'groups' => $this->groups
));
$this->assertFalse($result);
}
/**
* Test the addition of permissions, but with a failure in adding
*/
public function testGrantPermissionsInvalid()
{
$user = $this->buildPermissionGroupUserMock();
$user->method('grantPermissions')->willReturn(false);
$user->method('grantGroups')->willReturn(true);
$result = $user->grant(array(
'permissions' => $this->permissions,
'groups' => $this->groups
));
$this->assertFalse($result);
}
/**
* Test the addition of a security question (valid)
*/
public function testAddSecurityQuestionValid()
{
$ds = $this->buildMock(true, 'save');
$user = new UserModel($ds);
$result = $user->addSecurityQuestion(array(
'question' => 'Question 1',
'answer' => 'Answer 1'
));
$this->assertTrue($result);
}
/**
* Testing the addition of the security question with
* no data being provided
*
* @expectedException \InvalidArgumentException
*/
public function testAddSecurityQuestionNoData()
{
$ds = $this->buildMock(true, 'save');
$user = new UserModel($ds);
$result = $user->addSecurityQuestion(array());
}
/**
* Test that an exception is thrown when the password is the
* same as the security question
*
* @expectedException \InvalidArgumentException
*/
public function testAddSecurityQuestionSameAsPassword()
{
$ds = $this->buildMock(true, 'save');
$passwordHash = password_hash('mypass', PASSWORD_DEFAULT);
$user = new UserModel($ds, array('password' => $passwordHash));
$result = $user->addSecurityQuestion(array(
'question' => 'Question #1',
'answer' => 'mypass'
));
}
/**
* Test finding a user by ID
*/
public function testFindUserById()
{
$userId = 1234;
$ds = $this->buildMock(true);
$user = new UserModel($ds, ['id' => $userId, 'username' => 'testuser1']);
$ds = $this->buildMock($user, 'find');
$user = new UserModel($ds);
$result = $user->findByUserId($userId);
$this->assertTrue($result->id === $userId);
$this->assertTrue($result->username === 'testuser1');
}
}
|