PasswordableBehaviorTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. public function setUp() {
  13. parent::setUp();
  14. Configure::write('Passwordable.auth', 'AuthTest');
  15. $this->User = ClassRegistry::init('ToolsUser');
  16. if (isset($this->User->validate['pwd'])) {
  17. unset($this->User->validate['pwd']);
  18. }
  19. if (isset($this->User->validate['pwd_repeat'])) {
  20. unset($this->User->validate['pwd_repeat']);
  21. }
  22. if (isset($this->User->validate['pwd_current'])) {
  23. unset($this->User->validate['pwd_current']);
  24. }
  25. if (isset($this->User->order)) {
  26. unset($this->User->order);
  27. }
  28. $this->User->create();
  29. $data = array(
  30. 'id' => '5',
  31. 'name' => 'admin',
  32. 'password' => Security::hash('some', null, true),
  33. 'role_id' => '1'
  34. );
  35. $this->User->set($data);
  36. $res = $this->User->save();
  37. $this->assertTrue((bool)$res);
  38. Router::setRequestInfo(new CakeRequest(null, false));
  39. }
  40. /**
  41. * Tear-down method. Resets environment state.
  42. */
  43. public function tearDown() {
  44. unset($this->User);
  45. parent::tearDown();
  46. ClassRegistry::flush();
  47. }
  48. public function testObject() {
  49. $this->User->Behaviors->load('Tools.Passwordable', array());
  50. $this->assertInstanceOf('PasswordableBehavior', $this->User->Behaviors->Passwordable);
  51. $res = $this->User->Behaviors->attached('Passwordable');
  52. $this->assertTrue($res);
  53. }
  54. /**
  55. * make sure validation is triggered correctly
  56. */
  57. public function testValidate() {
  58. $this->User->Behaviors->load('Tools.Passwordable', array());
  59. $this->User->create();
  60. $data = array(
  61. 'pwd' => '123456',
  62. );
  63. $this->User->set($data);
  64. $is = $this->User->save();
  65. //debug($this->User->validationErrors);
  66. $this->assertFalse($is);
  67. $this->assertEquals(array('pwd_repeat'), array_keys($this->User->validationErrors));
  68. $this->User->create();
  69. $data = array(
  70. 'pwd' => '1234ab',
  71. 'pwd_repeat' => '123456'
  72. );
  73. $this->User->set($data);
  74. $is = $this->User->save();
  75. //debug($this->User->validationErrors);
  76. $this->assertFalse($is);
  77. $this->assertEquals(array(__('valErrPwdNotMatch')), $this->User->validationErrors['pwd_repeat']);
  78. $this->User->create();
  79. $data = array(
  80. 'pwd' => '123456',
  81. 'pwd_repeat' => '123456'
  82. );
  83. $this->User->set($data);
  84. //debug($this->User->validate);
  85. $is = $this->User->validates();
  86. $this->assertTrue(!empty($is));
  87. }
  88. /**
  89. * test that confirm false does not require confirmation
  90. */
  91. public function testValidateNoConfirm() {
  92. $this->User->Behaviors->load('Tools.Passwordable', array('confirm'=>false));
  93. $this->User->create();
  94. $data = array(
  95. 'pwd' => '123456',
  96. );
  97. $this->User->set($data);
  98. $is = $this->User->save();
  99. //debug($is);
  100. $this->assertTrue(!empty($is));
  101. }
  102. /**
  103. * Trigger validation and update process if no values are entered but are required
  104. */
  105. public function testValidateRequired() {
  106. $this->User->Behaviors->load('Tools.Passwordable');
  107. $this->User->create();
  108. $data = array(
  109. 'pwd' => '',
  110. 'pwd_repeat' => ''
  111. );
  112. $this->User->set($data);
  113. $is = $this->User->save();
  114. $this->assertFalse($is);
  115. $this->assertEquals(array('pwd', 'pwd_repeat'), array_keys($this->User->validationErrors));
  116. }
  117. /**
  118. * validation and update process gets skipped if no values are entered
  119. */
  120. public function testValidateNotRequired() {
  121. $this->User->Behaviors->load('Tools.Passwordable', array('require' => false));
  122. $this->User->create();
  123. $data = array(
  124. 'name' => 'foo', // we need at least one field besides the password on CREATE
  125. 'pwd' => '',
  126. 'pwd_repeat' => ''
  127. );
  128. $this->User->set($data);
  129. $is = $this->User->save();
  130. $this->assertTrue((bool)$is);
  131. $this->assertEquals(array('name', 'id'), array_keys($is['ToolsUser']));
  132. $id = $this->User->id;
  133. $data = array(
  134. 'id' => $id,
  135. 'pwd' => '',
  136. 'pwd_repeat' => ''
  137. );
  138. $this->User->set($data);
  139. $is = $this->User->save();
  140. $this->assertTrue((bool)$is);
  141. $this->assertEquals(array('id'), array_keys($is['ToolsUser']));
  142. }
  143. /**
  144. * PasswordableBehaviorTest::testValidateEmptyWithCurrentPassword()
  145. *
  146. * @return void
  147. */
  148. public function testValidateEmptyWithCurrentPassword() {
  149. $this->User->Behaviors->load('Tools.Passwordable', array('current' => true));
  150. $this->User->create();
  151. $data = array(
  152. 'id' => '123',
  153. 'pwd' => '',
  154. 'pwd_repeat' => '',
  155. 'pwd_current' => '123',
  156. );
  157. $this->User->set($data);
  158. $is = $this->User->save();
  159. //debug($this->User->validationErrors);
  160. $this->assertFalse($is);
  161. $this->assertEquals(array('pwd', 'pwd_repeat', 'pwd_current'), array_keys($this->User->validationErrors));
  162. $this->tearDown();
  163. $this->setUp();
  164. $this->User->Behaviors->load('Tools.Passwordable', array('require' => false, 'current'=>true));
  165. $this->User->create();
  166. $data = array(
  167. 'name' => 'foo',
  168. 'pwd' => '',
  169. 'pwd_repeat' => '',
  170. 'pwd_current' => '',
  171. );
  172. $is = $this->User->save($data);
  173. $this->assertTrue(!empty($is));
  174. }
  175. /**
  176. * test aliases for field names
  177. */
  178. public function testDifferentFieldNames() {
  179. $this->User->Behaviors->load('Tools.Passwordable', array(
  180. 'formField' => 'passw',
  181. 'formFieldRepeat' => 'passw_repeat',
  182. 'formFieldCurrent' => 'passw_current',
  183. ));
  184. $this->User->create();
  185. $data = array(
  186. 'passw' => '123456',
  187. 'passw_repeat' => '123456'
  188. );
  189. $this->User->set($data);
  190. //debug($this->User->data);
  191. $is = $this->User->save();
  192. $this->assertTrue(!empty($is));
  193. }
  194. /**
  195. * assert that allowSame false does not allow storing the same password as previously entered
  196. */
  197. public function testNotSame() {
  198. $this->User->Behaviors->load('Tools.Passwordable', array(
  199. 'formField' => 'passw',
  200. 'formFieldRepeat' => 'passw_repeat',
  201. 'formFieldCurrent' => 'passw_current',
  202. 'allowSame' => false,
  203. 'current' => true,
  204. //'userModel' => 'ToolsUser'
  205. ));
  206. $this->User->create();
  207. $data = array(
  208. 'id' => '5',
  209. 'passw_current' => 'some',
  210. 'passw' => 'some',
  211. 'passw_repeat' => 'some'
  212. );
  213. $this->User->set($data);
  214. $is = $this->User->save();
  215. //debug($this->User->validationErrors);
  216. $this->assertFalse($is);
  217. $this->User->create();
  218. $data = array(
  219. 'id' => '5',
  220. 'passw_current' => 'some',
  221. 'passw' => 'new',
  222. 'passw_repeat' => 'new'
  223. );
  224. $this->User->set($data);
  225. $is = $this->User->save();
  226. $this->assertTrue(!empty($is));
  227. }
  228. /**
  229. * assert that allowSame false does not allow storing the same password as previously entered
  230. */
  231. public function testNotSameWithoutCurrentField() {
  232. $this->User->Behaviors->load('Tools.Passwordable', array(
  233. 'formField' => 'passw',
  234. 'formFieldRepeat' => 'passw_repeat',
  235. 'allowSame' => false,
  236. 'current' => false
  237. ));
  238. $this->User->create();
  239. $data = array(
  240. 'passw' => 'some',
  241. 'passw_repeat' => 'some'
  242. );
  243. $this->User->set($data);
  244. $is = $this->User->save();
  245. $this->assertTrue((bool)$is);
  246. $id = $is[$this->User->alias]['id'];
  247. $this->User->create();
  248. $data = array(
  249. 'id' => $id,
  250. 'passw' => 'some',
  251. 'passw_repeat' => 'some'
  252. );
  253. $this->User->set($data);
  254. $is = $this->User->save();
  255. $this->assertFalse((bool)$is);
  256. $this->User->create();
  257. $data = array(
  258. 'id' => $id,
  259. 'passw' => 'new',
  260. 'passw_repeat' => 'new'
  261. );
  262. $this->User->set($data);
  263. $is = $this->User->save();
  264. $this->assertTrue((bool)$is);
  265. }
  266. /**
  267. * needs faking of pwd check...
  268. */
  269. public function testValidateCurrent() {
  270. $this->assertFalse($this->User->Behaviors->attached('Passwordable'));
  271. $this->User->create();
  272. $data = array(
  273. 'name' => 'xyz',
  274. 'password' => Security::hash('some', null, true));
  275. $res = $this->User->save($data);
  276. $this->assertTrue(!empty($res));
  277. $uid = (String)$this->User->id;
  278. $this->User->Behaviors->load('Tools.Passwordable', array('current' => true));
  279. $this->User->create();
  280. $data = array(
  281. 'id' => $uid,
  282. 'pwd' => '1234',
  283. 'pwd_repeat' => '123456',
  284. //'pwd_current' => '',
  285. );
  286. $this->User->set($data);
  287. $this->assertTrue($this->User->Behaviors->attached('Passwordable'));
  288. $is = $this->User->save();
  289. $this->assertFalse($is);
  290. $this->User->create();
  291. $data = array(
  292. 'id' => $uid,
  293. 'pwd_current' => 'somex',
  294. 'pwd' => '123456',
  295. 'pwd_repeat' => '123456'
  296. );
  297. $this->User->set($data);
  298. $is = $this->User->save();
  299. $this->assertFalse($is);
  300. $this->User->create();
  301. $data = array(
  302. 'id' => $uid,
  303. 'pwd_current' => 'some',
  304. 'pwd' => '123456',
  305. 'pwd_repeat' => '123456'
  306. );
  307. $this->User->set($data);
  308. $is = $this->User->save();
  309. $this->assertTrue(!empty($is));
  310. }
  311. /**
  312. * Test cake2.4 passwordHasher feature
  313. *
  314. * @return void
  315. */
  316. public function testPasswordHasher() {
  317. $this->skipIf((float)Configure::version() < 2.4, 'Needs 2.4 and above');
  318. $this->User->Behaviors->load('Tools.Passwordable', array(
  319. 'formField' => 'pwd',
  320. 'formFieldRepeat' => 'pwd_repeat',
  321. 'allowSame' => false,
  322. 'current' => false,
  323. 'passwordHasher' => 'Complex',
  324. ));
  325. $this->User->create();
  326. $data = array(
  327. 'pwd' => 'some',
  328. 'pwd_repeat' => 'some'
  329. );
  330. $this->User->set($data);
  331. $res = $this->User->save();
  332. $this->assertTrue((bool)$res);
  333. $uid = (String)$this->User->id;
  334. $this->User->Behaviors->load('Tools.Passwordable', array('current' => true));
  335. $this->User->create();
  336. $data = array(
  337. 'id' => $uid,
  338. 'pwd' => '1234',
  339. 'pwd_repeat' => '123456',
  340. //'pwd_current' => '',
  341. );
  342. $this->User->set($data);
  343. $this->assertTrue($this->User->Behaviors->attached('Passwordable'));
  344. $is = $this->User->save();
  345. $this->assertFalse($is);
  346. $this->User->create();
  347. $data = array(
  348. 'id' => $uid,
  349. 'pwd_current' => 'somex',
  350. 'pwd' => '123456',
  351. 'pwd_repeat' => '123456'
  352. );
  353. $this->User->set($data);
  354. $is = $this->User->save();
  355. $this->assertFalse($is);
  356. $this->User->create();
  357. $data = array(
  358. 'id' => $uid,
  359. 'pwd_current' => 'some',
  360. 'pwd' => '123456',
  361. 'pwd_repeat' => '123456'
  362. );
  363. $this->User->set($data);
  364. $is = $this->User->save();
  365. $this->assertTrue(!empty($is));
  366. }
  367. /**
  368. * PasswordableBehaviorTest::testBlowfish()
  369. *
  370. * @return void
  371. */
  372. public function testBlowfish() {
  373. //Configure::write('Security.salt', 'Cf1f11ePArKlBJomM0F6aJ');
  374. /*
  375. $this->assertFalse($this->User->Behaviors->attached('Passwordable'));
  376. $this->User->create();
  377. $data = array(
  378. 'name' => 'xyz',
  379. 'password' => Security::hash('some', 'blowfish'));
  380. $res = $this->User->save($data);
  381. $this->assertTrue(!empty($res));
  382. $uid = (String)$this->User->id;
  383. */
  384. $this->User->Behaviors->load('Tools.Passwordable', array(
  385. 'formField' => 'pwd',
  386. 'formFieldRepeat' => 'pwd_repeat',
  387. 'allowSame' => false,
  388. 'current' => false,
  389. //'userModel' => 'ToolsUser',
  390. 'authType' => 'Blowfish',
  391. ));
  392. $this->User->create();
  393. $data = array(
  394. 'pwd' => 'some',
  395. 'pwd_repeat' => 'some'
  396. );
  397. $this->User->set($data);
  398. $res = $this->User->save();
  399. $this->assertTrue((bool)$res);
  400. $uid = (String)$this->User->id;
  401. $this->User->Behaviors->load('Tools.Passwordable', array('current' => true));
  402. $this->User->create();
  403. $data = array(
  404. 'id' => $uid,
  405. 'pwd' => '1234',
  406. 'pwd_repeat' => '123456',
  407. //'pwd_current' => '',
  408. );
  409. $this->User->set($data);
  410. $this->assertTrue($this->User->Behaviors->attached('Passwordable'));
  411. $is = $this->User->save();
  412. $this->assertFalse($is);
  413. $this->User->create();
  414. $data = array(
  415. 'id' => $uid,
  416. 'pwd_current' => 'somex',
  417. 'pwd' => '123456',
  418. 'pwd_repeat' => '123456'
  419. );
  420. $this->User->set($data);
  421. $is = $this->User->save();
  422. $this->assertFalse($is);
  423. $this->User->create();
  424. $data = array(
  425. 'id' => $uid,
  426. 'pwd_current' => 'some',
  427. 'pwd' => '123456',
  428. 'pwd_repeat' => '123456'
  429. );
  430. $this->User->set($data);
  431. $is = $this->User->save();
  432. $this->assertTrue(!empty($is));
  433. }
  434. }
  435. /**
  436. * 2011-11-03 ms
  437. */
  438. class AuthTestComponent extends AuthComponent {
  439. }
  440. if (!class_exists('SimplePasswordHasher')) {
  441. class SimplePasswordHasher {
  442. }
  443. }
  444. class ComplexPasswordHasher extends SimplePasswordHasher {
  445. }