PasswordableBehaviorTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. <?php
  2. App::uses('ComponentCollection', 'Controller');
  3. App::uses('AuthComponent', 'Controller/Component');
  4. App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
  5. class PasswordableBehaviorTest extends CakeTestCase {
  6. public $fixtures = array(
  7. 'plugin.tools.tools_user', 'plugin.tools.role',
  8. );
  9. /**
  10. * SetUp method
  11. *
  12. * @return void
  13. */
  14. public function setUp() {
  15. parent::setUp();
  16. Configure::write('Passwordable.auth', 'AuthTest');
  17. $this->User = ClassRegistry::init('Tools.ToolsUser');
  18. if (isset($this->User->validate['pwd'])) {
  19. unset($this->User->validate['pwd']);
  20. }
  21. if (isset($this->User->validate['pwd_repeat'])) {
  22. unset($this->User->validate['pwd_repeat']);
  23. }
  24. if (isset($this->User->validate['pwd_current'])) {
  25. unset($this->User->validate['pwd_current']);
  26. }
  27. if (isset($this->User->order)) {
  28. unset($this->User->order);
  29. }
  30. $this->User->create();
  31. $data = array(
  32. 'id' => '5',
  33. 'name' => 'admin',
  34. 'password' => Security::hash('somepwd', null, true),
  35. 'role_id' => '1'
  36. );
  37. $this->User->set($data);
  38. $res = $this->User->save();
  39. $this->assertTrue((bool)$res);
  40. Router::setRequestInfo(new CakeRequest(null, false));
  41. }
  42. /**
  43. * PasswordableBehaviorTest::testObject()
  44. *
  45. * @return void
  46. */
  47. public function testObject() {
  48. $this->User->Behaviors->load('Tools.Passwordable', array());
  49. $this->assertInstanceOf('PasswordableBehavior', $this->User->Behaviors->Passwordable);
  50. $res = $this->User->Behaviors->loaded('Passwordable');
  51. $this->assertTrue($res);
  52. }
  53. /**
  54. * Make sure validation is triggered correctly
  55. *
  56. * @return void
  57. */
  58. public function testValidate() {
  59. $this->User->Behaviors->load('Tools.Passwordable', array());
  60. $this->User->create();
  61. $data = array(
  62. 'pwd' => '123456',
  63. );
  64. $this->User->set($data);
  65. $is = $this->User->save();
  66. //debug($this->User->validationErrors);
  67. $this->assertFalse($is);
  68. $this->assertEquals(array('pwd_repeat'), array_keys($this->User->validationErrors));
  69. $this->User->create();
  70. $data = array(
  71. 'pwd' => '1234ab',
  72. 'pwd_repeat' => '123456'
  73. );
  74. $this->User->set($data);
  75. $is = $this->User->save();
  76. //debug($this->User->validationErrors);
  77. $this->assertFalse($is);
  78. $this->assertEquals(array(__('valErrPwdNotMatch')), $this->User->validationErrors['pwd_repeat']);
  79. $this->User->create();
  80. $data = array(
  81. 'pwd' => '123456',
  82. 'pwd_repeat' => '123456'
  83. );
  84. $this->User->set($data);
  85. //debug($this->User->validate);
  86. $is = $this->User->validates();
  87. $this->assertTrue(!empty($is));
  88. }
  89. /**
  90. * Test that confirm false does not require confirmation
  91. *
  92. * @return void
  93. */
  94. public function testValidateNoConfirm() {
  95. $this->User->Behaviors->load('Tools.Passwordable', array('confirm' => false));
  96. $this->User->create();
  97. $data = array(
  98. 'pwd' => '123456',
  99. );
  100. $this->User->set($data);
  101. $is = $this->User->save();
  102. //debug($is);
  103. $this->assertTrue(!empty($is));
  104. }
  105. /**
  106. * Trigger validation and update process if no values are entered but are required
  107. *
  108. * @return void
  109. */
  110. public function testValidateRequired() {
  111. $this->User->Behaviors->load('Tools.Passwordable');
  112. $this->User->create();
  113. $data = array(
  114. 'pwd' => '',
  115. 'pwd_repeat' => ''
  116. );
  117. $this->User->set($data);
  118. $is = $this->User->save();
  119. $this->assertFalse($is);
  120. $this->assertEquals(array('pwd', 'pwd_repeat'), array_keys($this->User->validationErrors));
  121. }
  122. /**
  123. * Validation and update process gets skipped if no values are entered
  124. *
  125. * @return void
  126. */
  127. public function testValidateNotRequired() {
  128. $this->User->Behaviors->load('Tools.Passwordable', array('require' => false));
  129. $this->User->create();
  130. $data = array(
  131. 'name' => 'foo', // we need at least one field besides the password on CREATE
  132. 'pwd' => '',
  133. 'pwd_repeat' => ''
  134. );
  135. $this->User->set($data);
  136. $is = $this->User->save();
  137. $this->assertTrue((bool)$is);
  138. $this->assertEquals(array('name', 'id'), array_keys($is[$this->User->alias]));
  139. $id = $this->User->id;
  140. $data = array(
  141. 'id' => $id,
  142. 'pwd' => '',
  143. 'pwd_repeat' => ''
  144. );
  145. $this->User->set($data);
  146. $is = $this->User->save();
  147. $this->assertTrue((bool)$is);
  148. $this->assertEquals(array('id'), array_keys($is[$this->User->alias]));
  149. }
  150. /**
  151. * PasswordableBehaviorTest::testValidateEmptyWithCurrentPassword()
  152. *
  153. * @return void
  154. */
  155. public function testValidateEmptyWithCurrentPassword() {
  156. $this->User->Behaviors->load('Tools.Passwordable', array('current' => true));
  157. $this->User->create();
  158. $data = array(
  159. 'id' => '123',
  160. 'pwd' => '',
  161. 'pwd_repeat' => '',
  162. 'pwd_current' => '123456',
  163. );
  164. $this->User->set($data);
  165. $is = $this->User->save();
  166. //debug($this->User->validationErrors);
  167. $this->assertFalse($is);
  168. $this->assertEquals(array('pwd', 'pwd_repeat', 'pwd_current'), array_keys($this->User->validationErrors));
  169. $this->tearDown();
  170. $this->setUp();
  171. $this->User->Behaviors->load('Tools.Passwordable', array('require' => false, 'current' => true));
  172. $this->User->create();
  173. $data = array(
  174. 'name' => 'foo',
  175. 'pwd' => '',
  176. 'pwd_repeat' => '',
  177. 'pwd_current' => '',
  178. );
  179. $is = $this->User->save($data);
  180. $this->assertTrue(!empty($is));
  181. }
  182. /**
  183. * Test aliases for field names
  184. */
  185. public function testDifferentFieldNames() {
  186. $this->User->Behaviors->load('Tools.Passwordable', array(
  187. 'formField' => 'passw',
  188. 'formFieldRepeat' => 'passw_repeat',
  189. 'formFieldCurrent' => 'passw_current',
  190. ));
  191. $this->User->create();
  192. $data = array(
  193. 'passw' => '123456',
  194. 'passw_repeat' => '123456'
  195. );
  196. $this->User->set($data);
  197. //debug($this->User->data);
  198. $is = $this->User->save();
  199. $this->assertTrue(!empty($is));
  200. }
  201. /**
  202. * Assert that allowSame false does not allow storing the same password as previously entered
  203. */
  204. public function testNotSame() {
  205. $this->User->Behaviors->load('Tools.Passwordable', array(
  206. 'formField' => 'passw',
  207. 'formFieldRepeat' => 'passw_repeat',
  208. 'formFieldCurrent' => 'passw_current',
  209. 'allowSame' => false,
  210. 'current' => true,
  211. //'userModel' => 'ToolsUser'
  212. ));
  213. $this->User->create();
  214. $data = array(
  215. 'id' => '5',
  216. 'passw_current' => 'something',
  217. 'passw' => 'somepwd',
  218. 'passw_repeat' => 'somepwd'
  219. );
  220. $this->User->set($data);
  221. $is = $this->User->save();
  222. //debug($this->User->validationErrors);
  223. $this->assertFalse($is);
  224. $this->User->create();
  225. $data = array(
  226. 'id' => '5',
  227. 'passw_current' => 'somepwd',
  228. 'passw' => 'newpwd',
  229. 'passw_repeat' => 'newpwd'
  230. );
  231. $this->User->set($data);
  232. $is = $this->User->save();
  233. $this->assertTrue(!empty($is));
  234. }
  235. /**
  236. * Assert that allowSame false does not allow storing the same password as previously entered
  237. */
  238. public function testNotSameWithoutCurrentField() {
  239. $this->User->Behaviors->load('Tools.Passwordable', array(
  240. 'formField' => 'passw',
  241. 'formFieldRepeat' => 'passw_repeat',
  242. 'allowSame' => false,
  243. 'current' => false
  244. ));
  245. $this->User->create();
  246. $data = array(
  247. 'passw' => 'somepwd',
  248. 'passw_repeat' => 'somepwd'
  249. );
  250. $this->User->set($data);
  251. $is = $this->User->save();
  252. $this->assertTrue((bool)$is);
  253. $id = $is[$this->User->alias]['id'];
  254. $this->User->create();
  255. $data = array(
  256. 'id' => $id,
  257. 'passw' => 'somepwd',
  258. 'passw_repeat' => 'somepwd'
  259. );
  260. $this->User->set($data);
  261. $is = $this->User->save();
  262. $this->assertFalse((bool)$is);
  263. $this->User->create();
  264. $data = array(
  265. 'id' => $id,
  266. 'passw' => 'newpwd',
  267. 'passw_repeat' => 'newpwd'
  268. );
  269. $this->User->set($data);
  270. $is = $this->User->save();
  271. $this->assertTrue((bool)$is);
  272. }
  273. /**
  274. * Needs faking of pwd check...
  275. */
  276. public function testValidateCurrent() {
  277. $this->assertFalse($this->User->Behaviors->loaded('Passwordable'));
  278. $this->User->create();
  279. $data = array(
  280. 'name' => 'xyz',
  281. 'password' => Security::hash('somepwd', null, true));
  282. $res = $this->User->save($data);
  283. $this->assertTrue(!empty($res));
  284. $uid = (string)$this->User->id;
  285. $this->User->Behaviors->load('Tools.Passwordable', array('current' => true));
  286. $this->User->create();
  287. $data = array(
  288. 'id' => $uid,
  289. 'pwd' => '123456',
  290. 'pwd_repeat' => '12345678',
  291. //'pwd_current' => '',
  292. );
  293. $this->User->set($data);
  294. $this->assertTrue($this->User->Behaviors->loaded('Passwordable'));
  295. $is = $this->User->save();
  296. $this->assertFalse($is);
  297. $this->User->create();
  298. $data = array(
  299. 'id' => $uid,
  300. 'pwd_current' => 'somepwdx',
  301. 'pwd' => '123456',
  302. 'pwd_repeat' => '123456'
  303. );
  304. $this->User->set($data);
  305. $is = $this->User->save();
  306. $this->assertFalse($is);
  307. $this->User->create();
  308. $data = array(
  309. 'id' => $uid,
  310. 'name' => 'Yeah',
  311. 'pwd_current' => 'somepwd',
  312. 'pwd' => '123456',
  313. 'pwd_repeat' => '123456'
  314. );
  315. $this->User->set($data);
  316. // Test whitelist setting - only "password" needs to gets auto-added
  317. $is = $this->User->save(null, true, array('id', 'pwd', 'pwd_repeat', 'pwd_current'));
  318. $this->assertTrue(!empty($is));
  319. $user = $this->User->get($uid);
  320. // The password is updated, the name not
  321. $this->assertSame($is['ToolsUser']['password'], $user['ToolsUser']['password']);
  322. $this->assertSame('xyz', $user['ToolsUser']['name']);
  323. // Proof that we manually need to add pwd, pwd_repeat etc due to a bug in CakePHP<=2.4 allowing behaviors to only modify saving,
  324. // not validating of additional whitelist fields. Validation for those will be just skipped, no matter what the behavior tries
  325. // to set.
  326. $this->User->create();
  327. $data = array(
  328. 'id' => $uid,
  329. 'name' => 'Yeah',
  330. 'pwd_current' => '123', // Obviously wrong
  331. 'pwd' => 'some', // Too short
  332. 'pwd_repeat' => 'somex' // Don't match
  333. );
  334. $this->User->set($data);
  335. // Test whitelist setting - only "password" gets auto-added, pwd, pwd_repeat etc need to be added manually
  336. // NOTE that I had to remove the code for adding those fields from the behavior (as it was not functional)
  337. // So of course, this won't work now as expected. But feel free to try to add them in the behavior. Results will be the same.
  338. $is = $this->User->save(null, true, array('id', 'name'));
  339. if ((float)Configure::version() >= 2.5) {
  340. // Validation errors triggered - as expected
  341. $this->assertFalse($is);
  342. $this->assertSame(array('pwd', 'pwd_repeat', 'pwd_current'), array_keys($this->User->validationErrors));
  343. return;
  344. }
  345. // Save is successful
  346. $this->assertTrue(!empty($is));
  347. $user = $this->User->get($uid);
  348. $this->assertSame('Yeah', $user['ToolsUser']['name']);
  349. // The password is not updated, the name is
  350. $this->assertSame($is['ToolsUser']['password'], $user['ToolsUser']['password']);
  351. $this->assertSame('Yeah', $user['ToolsUser']['name']);
  352. }
  353. /**
  354. * Test cake2.4 passwordHasher feature
  355. *
  356. * @return void
  357. */
  358. public function testPasswordHasher() {
  359. $this->skipIf((float)Configure::version() < 2.4, 'Needs 2.4 and above');
  360. $this->User->Behaviors->load('Tools.Passwordable', array(
  361. 'formField' => 'pwd',
  362. 'formFieldRepeat' => 'pwd_repeat',
  363. 'allowSame' => false,
  364. 'current' => false,
  365. 'passwordHasher' => 'Complex',
  366. ));
  367. $this->User->create();
  368. $data = array(
  369. 'pwd' => 'somepwd',
  370. 'pwd_repeat' => 'somepwd'
  371. );
  372. $this->User->set($data);
  373. $res = $this->User->save();
  374. $this->assertTrue((bool)$res);
  375. $uid = (string)$this->User->id;
  376. $this->User->Behaviors->load('Tools.Passwordable', array('current' => true));
  377. $this->User->create();
  378. $data = array(
  379. 'id' => $uid,
  380. 'pwd' => '123456',
  381. 'pwd_repeat' => '12345678',
  382. //'pwd_current' => '',
  383. );
  384. $this->User->set($data);
  385. $this->assertTrue($this->User->Behaviors->loaded('Passwordable'));
  386. $is = $this->User->save();
  387. $this->assertFalse($is);
  388. $this->User->create();
  389. $data = array(
  390. 'id' => $uid,
  391. 'pwd_current' => 'somepwdx',
  392. 'pwd' => '123456',
  393. 'pwd_repeat' => '123456'
  394. );
  395. $this->User->set($data);
  396. $is = $this->User->save();
  397. $this->assertFalse($is);
  398. $this->User->create();
  399. $data = array(
  400. 'id' => $uid,
  401. 'pwd_current' => 'somepwd',
  402. 'pwd' => '123456',
  403. 'pwd_repeat' => '123456'
  404. );
  405. $this->User->set($data);
  406. $is = $this->User->save();
  407. $this->assertTrue(!empty($is));
  408. }
  409. /**
  410. * PasswordableBehaviorTest::testBlowfish()
  411. *
  412. * @return void
  413. */
  414. public function testBlowfish() {
  415. $this->User->Behaviors->load('Tools.Passwordable', array(
  416. 'allowSame' => false,
  417. 'current' => false,
  418. 'authType' => 'Blowfish',
  419. ));
  420. $this->User->create();
  421. $data = array(
  422. 'pwd' => 'somepwd',
  423. 'pwd_repeat' => 'somepwd'
  424. );
  425. $this->User->set($data);
  426. $res = $this->User->save();
  427. $this->assertTrue((bool)$res);
  428. $uid = (string)$this->User->id;
  429. $this->User->Behaviors->load('Tools.Passwordable', array('current' => true));
  430. $this->User->create();
  431. $data = array(
  432. 'id' => $uid,
  433. 'pwd' => '123456',
  434. 'pwd_repeat' => '12345678',
  435. );
  436. $this->User->set($data);
  437. $this->assertTrue($this->User->Behaviors->loaded('Passwordable'));
  438. $is = $this->User->save();
  439. $this->assertFalse($is);
  440. $this->User->create();
  441. $data = array(
  442. 'id' => $uid,
  443. 'pwd_current' => 'somepwdx',
  444. 'pwd' => '123456',
  445. 'pwd_repeat' => '123456'
  446. );
  447. $this->User->set($data);
  448. $is = $this->User->save();
  449. $this->assertFalse($is);
  450. $this->User->create();
  451. $data = array(
  452. 'id' => $uid,
  453. 'pwd_current' => 'somepwd',
  454. 'pwd' => '123456',
  455. 'pwd_repeat' => '123456'
  456. );
  457. $this->User->set($data);
  458. $is = $this->User->save();
  459. $this->assertTrue(!empty($is));
  460. }
  461. /**
  462. * PasswordableBehaviorTest::testSettings()
  463. *
  464. * @return void
  465. */
  466. public function testSettings() {
  467. // Pwd min and max length
  468. $this->User->Behaviors->load('Tools.Passwordable', array(
  469. 'allowSame' => false,
  470. 'current' => false,
  471. 'minLength' => 3,
  472. 'maxLength' => 6,
  473. ));
  474. $this->User->create();
  475. $data = array(
  476. 'pwd' => '123',
  477. 'pwd_repeat' => '123'
  478. );
  479. $this->User->set($data);
  480. $res = $this->User->save();
  481. $this->assertTrue((bool)$res);
  482. $uid = (string)$this->User->id;
  483. $this->User->create();
  484. $data = array(
  485. 'pwd' => '12345678',
  486. 'pwd_repeat' => '12345678'
  487. );
  488. $this->User->set($data);
  489. $res = $this->User->save();
  490. $this->assertFalse($res);
  491. $expected = array(
  492. 'pwd' => array(__('valErrBetweenCharacters %s %s', 3, 6)),
  493. 'pwd_repeat' => array(__('valErrBetweenCharacters %s %s', 3, 6))
  494. );
  495. $this->assertEquals($expected, $this->User->validationErrors);
  496. }
  497. }
  498. /**
  499. */
  500. class AuthTestComponent extends AuthComponent {
  501. }
  502. if (!class_exists('SimplePasswordHasher')) {
  503. class SimplePasswordHasher {
  504. }
  505. }
  506. class ComplexPasswordHasher extends SimplePasswordHasher {
  507. }