PasswordableBehaviorTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. * validation and update process gets skipped if no values are entered
  104. */
  105. public function testValidateEmpty() {
  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. //debug($this->User->validationErrors);
  115. $this->assertFalse($is);
  116. $this->assertEquals(array('pwd', 'pwd_repeat'), array_keys($this->User->validationErrors));
  117. }
  118. /**
  119. * PasswordableBehaviorTest::testValidateEmptyWithCurrentPassword()
  120. *
  121. * @return void
  122. */
  123. public function testValidateEmptyWithCurrentPassword() {
  124. $this->User->Behaviors->load('Tools.Passwordable', array('current'=>true));
  125. $this->User->create();
  126. $data = array(
  127. 'id' => '123',
  128. 'pwd' => '',
  129. 'pwd_repeat' => '',
  130. 'pwd_current' => '123',
  131. );
  132. $this->User->set($data);
  133. $is = $this->User->save();
  134. //debug($this->User->validationErrors);
  135. $this->assertFalse($is);
  136. $this->assertEquals(array('pwd', 'pwd_repeat', 'pwd_current'), array_keys($this->User->validationErrors));
  137. $this->tearDown();
  138. $this->setUp();
  139. $this->User->Behaviors->load('Tools.Passwordable', array('allowEmpty'=>true, 'current'=>true));
  140. $this->User->create();
  141. $data = array(
  142. 'name' => 'foo',
  143. 'pwd' => '',
  144. 'pwd_repeat' => '',
  145. 'pwd_current' => '',
  146. );
  147. $is = $this->User->save($data);
  148. $this->assertTrue(!empty($is));
  149. }
  150. /**
  151. * test aliases for field names
  152. */
  153. public function testDifferentFieldNames() {
  154. $this->User->Behaviors->load('Tools.Passwordable', array(
  155. 'formField' => 'passw',
  156. 'formFieldRepeat' => 'passw_repeat',
  157. 'formFieldCurrent' => 'passw_current',
  158. ));
  159. $this->User->create();
  160. $data = array(
  161. 'passw' => '123456',
  162. 'passw_repeat' => '123456'
  163. );
  164. $this->User->set($data);
  165. //debug($this->User->data);
  166. $is = $this->User->save();
  167. $this->assertTrue(!empty($is));
  168. }
  169. /**
  170. * assert that allowSame false does not allow storing the same password as previously entered
  171. */
  172. public function testNotSame() {
  173. $this->User->Behaviors->load('Tools.Passwordable', array(
  174. 'formField' => 'passw',
  175. 'formFieldRepeat' => 'passw_repeat',
  176. 'formFieldCurrent' => 'passw_current',
  177. 'allowSame' => false,
  178. 'current' => true,
  179. //'userModel' => 'ToolsUser'
  180. ));
  181. $this->User->create();
  182. $data = array(
  183. 'id' => '5',
  184. 'passw_current' => 'some',
  185. 'passw' => 'some',
  186. 'passw_repeat' => 'some'
  187. );
  188. $this->User->set($data);
  189. $is = $this->User->save();
  190. //debug($this->User->validationErrors);
  191. $this->assertFalse($is);
  192. $this->User->create();
  193. $data = array(
  194. 'id' => '5',
  195. 'passw_current' => 'some',
  196. 'passw' => 'new',
  197. 'passw_repeat' => 'new'
  198. );
  199. $this->User->set($data);
  200. $is = $this->User->save();
  201. $this->assertTrue(!empty($is));
  202. }
  203. /**
  204. * assert that allowSame false does not allow storing the same password as previously entered
  205. */
  206. public function testNotSameWithoutCurrentField() {
  207. $this->User->Behaviors->load('Tools.Passwordable', array(
  208. 'formField' => 'passw',
  209. 'formFieldRepeat' => 'passw_repeat',
  210. 'allowSame' => false,
  211. 'current' => false
  212. ));
  213. $this->User->create();
  214. $data = array(
  215. 'passw' => 'some',
  216. 'passw_repeat' => 'some'
  217. );
  218. $this->User->set($data);
  219. $is = $this->User->save();
  220. $this->assertTrue((bool)$is);
  221. $id = $is[$this->User->alias]['id'];
  222. $this->User->create();
  223. $data = array(
  224. 'id' => $id,
  225. 'passw' => 'some',
  226. 'passw_repeat' => 'some'
  227. );
  228. $this->User->set($data);
  229. $is = $this->User->save();
  230. $this->assertFalse((bool)$is);
  231. $this->User->create();
  232. $data = array(
  233. 'id' => $id,
  234. 'passw' => 'new',
  235. 'passw_repeat' => 'new'
  236. );
  237. $this->User->set($data);
  238. $is = $this->User->save();
  239. $this->assertTrue((bool)$is);
  240. }
  241. /**
  242. * needs faking of pwd check...
  243. */
  244. public function testValidateCurrent() {
  245. $this->assertFalse($this->User->Behaviors->attached('Passwordable'));
  246. $this->User->create();
  247. $data = array(
  248. 'name' => 'xyz',
  249. 'password' => Security::hash('some', null, true));
  250. $res = $this->User->save($data);
  251. $this->assertTrue(!empty($res));
  252. $uid = (String)$this->User->id;
  253. $this->User->Behaviors->load('Tools.Passwordable', array('current' => true));
  254. $this->User->create();
  255. $data = array(
  256. 'id' => $uid,
  257. 'pwd' => '1234',
  258. 'pwd_repeat' => '123456',
  259. //'pwd_current' => '',
  260. );
  261. $this->User->set($data);
  262. $this->assertTrue($this->User->Behaviors->attached('Passwordable'));
  263. $is = $this->User->save();
  264. $this->assertFalse($is);
  265. $this->User->create();
  266. $data = array(
  267. 'id' => $uid,
  268. 'pwd_current' => 'somex',
  269. 'pwd' => '123456',
  270. 'pwd_repeat' => '123456'
  271. );
  272. $this->User->set($data);
  273. $is = $this->User->save();
  274. $this->assertFalse($is);
  275. $this->User->create();
  276. $data = array(
  277. 'id' => $uid,
  278. 'pwd_current' => 'some',
  279. 'pwd' => '123456',
  280. 'pwd_repeat' => '123456'
  281. );
  282. $this->User->set($data);
  283. $is = $this->User->save();
  284. $this->assertTrue(!empty($is));
  285. }
  286. /**
  287. * PasswordableBehaviorTest::testPasswordHasher()
  288. *
  289. * @return void
  290. */
  291. public function testPasswordHasher() {
  292. $this->User->Behaviors->load('Tools.Passwordable', array(
  293. 'formField' => 'pwd',
  294. 'formFieldRepeat' => 'pwd_repeat',
  295. 'allowSame' => false,
  296. 'current' => false,
  297. 'passwordHasher' => 'Complex',
  298. ));
  299. $this->User->create();
  300. $data = array(
  301. 'pwd' => 'some',
  302. 'pwd_repeat' => 'some'
  303. );
  304. $this->User->set($data);
  305. $res = $this->User->save();
  306. $this->assertTrue((bool)$res);
  307. $uid = (String)$this->User->id;
  308. $this->User->Behaviors->load('Tools.Passwordable', array('current' => true));
  309. $this->User->create();
  310. $data = array(
  311. 'id' => $uid,
  312. 'pwd' => '1234',
  313. 'pwd_repeat' => '123456',
  314. //'pwd_current' => '',
  315. );
  316. $this->User->set($data);
  317. $this->assertTrue($this->User->Behaviors->attached('Passwordable'));
  318. $is = $this->User->save();
  319. $this->assertFalse($is);
  320. $this->User->create();
  321. $data = array(
  322. 'id' => $uid,
  323. 'pwd_current' => 'somex',
  324. 'pwd' => '123456',
  325. 'pwd_repeat' => '123456'
  326. );
  327. $this->User->set($data);
  328. $is = $this->User->save();
  329. $this->assertFalse($is);
  330. $this->User->create();
  331. $data = array(
  332. 'id' => $uid,
  333. 'pwd_current' => 'some',
  334. 'pwd' => '123456',
  335. 'pwd_repeat' => '123456'
  336. );
  337. $this->User->set($data);
  338. $is = $this->User->save();
  339. $this->assertTrue(!empty($is));
  340. }
  341. /**
  342. * PasswordableBehaviorTest::testBlowfish()
  343. *
  344. * @return void
  345. */
  346. public function testBlowfish() {
  347. Configure::write('Security.salt', 'Cf1f11ePArKlBJomM0F6aJ');
  348. /*
  349. $this->assertFalse($this->User->Behaviors->attached('Passwordable'));
  350. $this->User->create();
  351. $data = array(
  352. 'name' => 'xyz',
  353. 'password' => Security::hash('some', 'blowfish'));
  354. $res = $this->User->save($data);
  355. $this->assertTrue(!empty($res));
  356. $uid = (String)$this->User->id;
  357. */
  358. $this->User->Behaviors->load('Tools.Passwordable', array(
  359. 'formField' => 'pwd',
  360. 'formFieldRepeat' => 'pwd_repeat',
  361. 'allowSame' => false,
  362. 'current' => false,
  363. //'userModel' => 'ToolsUser',
  364. 'authType' => 'Blowfish',
  365. ));
  366. $this->User->create();
  367. $data = array(
  368. 'pwd' => 'some',
  369. 'pwd_repeat' => 'some'
  370. );
  371. $this->User->set($data);
  372. $res = $this->User->save();
  373. $this->assertTrue((bool)$res);
  374. $uid = (String)$this->User->id;
  375. $this->User->Behaviors->load('Tools.Passwordable', array('current' => true));
  376. $this->User->create();
  377. $data = array(
  378. 'id' => $uid,
  379. 'pwd' => '1234',
  380. 'pwd_repeat' => '123456',
  381. //'pwd_current' => '',
  382. );
  383. $this->User->set($data);
  384. $this->assertTrue($this->User->Behaviors->attached('Passwordable'));
  385. $is = $this->User->save();
  386. $this->assertFalse($is);
  387. $this->User->create();
  388. $data = array(
  389. 'id' => $uid,
  390. 'pwd_current' => 'somex',
  391. 'pwd' => '123456',
  392. 'pwd_repeat' => '123456'
  393. );
  394. $this->User->set($data);
  395. $is = $this->User->save();
  396. $this->assertFalse($is);
  397. $this->User->create();
  398. $data = array(
  399. 'id' => $uid,
  400. 'pwd_current' => 'some',
  401. 'pwd' => '123456',
  402. 'pwd_repeat' => '123456'
  403. );
  404. $this->User->set($data);
  405. $is = $this->User->save();
  406. $this->assertTrue(!empty($is));
  407. }
  408. }
  409. /**
  410. * 2011-11-03 ms
  411. */
  412. class AuthTestComponent extends AuthComponent {
  413. }
  414. class ToolsUser extends CakeTestModel {
  415. }
  416. class ComplexPasswordHasher extends SimplePasswordHasher {
  417. }