PasswordableBehaviorTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. App::uses('ComponentCollection', 'Controller');
  3. class PasswordableBehaviorTest extends CakeTestCase {
  4. public $fixtures = array(
  5. 'core.user',
  6. );
  7. /**
  8. * setUp method
  9. */
  10. public function setUp() {
  11. parent::setUp();
  12. Configure::write('Passwordable.auth', 'AuthTest');
  13. $this->User = ClassRegistry::init('User');
  14. if (isset($this->User->validate['pwd'])) {
  15. unset($this->User->validate['pwd']);
  16. }
  17. if (isset($this->User->validate['pwd_repeat'])) {
  18. unset($this->User->validate['pwd_repeat']);
  19. }
  20. if (isset($this->User->validate['pwd_current'])) {
  21. unset($this->User->validate['pwd_current']);
  22. }
  23. if (isset($this->User->order)) {
  24. unset($this->User->order);
  25. }
  26. }
  27. /**
  28. * Tear-down method. Resets environment state.
  29. */
  30. public function tearDown() {
  31. unset($this->User);
  32. parent::tearDown();
  33. ClassRegistry::flush();
  34. }
  35. public function testObject() {
  36. $this->User->Behaviors->load('Tools.Passwordable', array());
  37. $this->assertInstanceOf('PasswordableBehavior', $this->User->Behaviors->Passwordable);
  38. $res = $this->User->Behaviors->attached('Passwordable');
  39. $this->assertTrue($res);
  40. }
  41. /**
  42. * make sure validation is triggered correctly
  43. */
  44. public function testValidate() {
  45. $this->User->Behaviors->load('Tools.Passwordable', array());
  46. $this->User->create();
  47. $data = array(
  48. 'pwd' => '123456',
  49. );
  50. $this->User->set($data);
  51. $is = $this->User->save();
  52. //debug($this->User->validationErrors); ob_flush();
  53. $this->assertFalse($is);
  54. $this->assertEquals(array('pwd_repeat'), array_keys($this->User->validationErrors));
  55. $this->User->create();
  56. $data = array(
  57. 'pwd' => '1234ab',
  58. 'pwd_repeat' => '123456'
  59. );
  60. $this->User->set($data);
  61. $is = $this->User->save();
  62. //debug($this->User->validationErrors); ob_flush();
  63. $this->assertFalse($is);
  64. $this->assertEquals(array(__('valErrPwdNotMatch')), $this->User->validationErrors['pwd_repeat']);
  65. $this->User->create();
  66. $data = array(
  67. 'pwd' => '123456',
  68. 'pwd_repeat' => '123456'
  69. );
  70. $this->User->set($data);
  71. //debug($this->User->validate);
  72. $is = $this->User->validates();
  73. $this->assertTrue(!empty($is));
  74. }
  75. /**
  76. * test that confirm false does not require confirmation
  77. */
  78. public function testValidateNoConfirm() {
  79. $this->User->Behaviors->load('Tools.Passwordable', array('confirm'=>false));
  80. $this->User->create();
  81. $data = array(
  82. 'pwd' => '123456',
  83. );
  84. $this->User->set($data);
  85. $is = $this->User->save();
  86. debug($is); ob_flush();
  87. $this->assertTrue(!empty($is));
  88. }
  89. /**
  90. * validation and update process gets skipped if no values are entered
  91. */
  92. public function testValidateEmpty() {
  93. $this->User->Behaviors->load('Tools.Passwordable');
  94. $this->User->create();
  95. $data = array(
  96. 'pwd' => '',
  97. 'pwd_repeat' => ''
  98. );
  99. $this->User->set($data);
  100. $is = $this->User->save();
  101. debug($this->User->validationErrors); ob_flush();
  102. $this->assertFalse($is);
  103. $this->assertEquals(array('pwd', 'pwd_repeat'), array_keys($this->User->validationErrors));
  104. }
  105. public function testValidateEmptyWithCurrentPassword() {
  106. $this->User->Behaviors->load('Tools.Passwordable', array('current'=>true));
  107. $this->User->create();
  108. $data = array(
  109. 'id' => 123,
  110. 'pwd' => '',
  111. 'pwd_repeat' => '',
  112. 'pwd_current' => '123',
  113. );
  114. $this->User->set($data);
  115. $is = $this->User->save();
  116. //debug($this->User->validationErrors); ob_flush();
  117. $this->assertFalse($is);
  118. $this->assertEquals(array('pwd', 'pwd_repeat', 'pwd_current'), array_keys($this->User->validationErrors));
  119. $this->tearDown();
  120. $this->setUp();
  121. $this->User->Behaviors->load('Tools.Passwordable', array('allowEmpty'=>true, 'current'=>true));
  122. $this->User->create();
  123. $data = array(
  124. 'user' => 'foo',
  125. 'pwd' => '',
  126. 'pwd_repeat' => '',
  127. 'pwd_current' => '',
  128. );
  129. $is = $this->User->save($data);
  130. $this->assertTrue(!empty($is));
  131. }
  132. /**
  133. * test aliases for field names
  134. */
  135. public function testDifferentFieldNames() {
  136. $this->User->Behaviors->load('Tools.Passwordable', array(
  137. 'formField' => 'passw',
  138. 'formFieldRepeat' => 'passw_repeat',
  139. 'formFieldCurrent' => 'passw_current',
  140. ));
  141. $this->User->create();
  142. $data = array(
  143. 'passw' => '123456',
  144. 'passw_repeat' => '123456'
  145. );
  146. $this->User->set($data);
  147. //debug($this->User->data);
  148. $is = $this->User->save();
  149. $this->assertTrue(!empty($is));
  150. }
  151. /**
  152. * assert that allowSame false does not allow storing the same password as previously entered
  153. */
  154. public function testNotSame() {
  155. $this->User->Behaviors->load('Tools.Passwordable', array(
  156. 'formField' => 'passw',
  157. 'formFieldRepeat' => 'passw_repeat',
  158. 'formFieldCurrent' => 'passw_current',
  159. 'allowSame' => false,
  160. 'current' => true
  161. ));
  162. $this->User->create();
  163. $data = array(
  164. 'id' => 5,
  165. 'passw_current' => 'some',
  166. 'passw' => 'some',
  167. 'passw_repeat' => 'some'
  168. );
  169. $this->User->set($data);
  170. $is = $this->User->save();
  171. debug($this->User->validationErrors);
  172. $this->assertFalse($is);
  173. $this->User->create();
  174. $data = array(
  175. 'id' => 5,
  176. 'passw_current' => 'some',
  177. 'passw' => 'new',
  178. 'passw_repeat' => 'new'
  179. );
  180. $this->User->set($data);
  181. $is = $this->User->save();
  182. $this->assertTrue(!empty($is));
  183. }
  184. /**
  185. * assert that allowSame false does not allow storing the same password as previously entered
  186. */
  187. public function testNotSameWithoutCurrentField() {
  188. $this->User->Behaviors->load('Tools.Passwordable', array(
  189. 'formField' => 'passw',
  190. 'formFieldRepeat' => 'passw_repeat',
  191. 'allowSame' => false,
  192. 'current' => false
  193. ));
  194. $this->User->create();
  195. $data = array(
  196. 'passw' => 'some',
  197. 'passw_repeat' => 'some'
  198. );
  199. $this->User->set($data);
  200. $is = $this->User->save();
  201. $this->assertTrue((bool)$is);
  202. $id = $is['User']['id'];
  203. $this->User->create();
  204. $data = array(
  205. 'id' => $id,
  206. 'passw' => 'some',
  207. 'passw_repeat' => 'some'
  208. );
  209. $this->User->set($data);
  210. $is = $this->User->save();
  211. $this->assertFalse((bool)$is);
  212. $this->User->create();
  213. $data = array(
  214. 'id' => $id,
  215. 'passw' => 'new',
  216. 'passw_repeat' => 'new'
  217. );
  218. $this->User->set($data);
  219. $is = $this->User->save();
  220. $this->assertTrue((bool)$is);
  221. }
  222. /**
  223. * needs faking of pwd check...
  224. */
  225. public function testValidateCurrent() {
  226. $this->assertFalse($this->User->Behaviors->attached('Passwordable'));
  227. $this->User->create();
  228. $data = array('user'=>'xyz', 'password'=>Security::hash('some', null, true));
  229. $res = $this->User->save($data);
  230. $this->assertTrue(!empty($res));
  231. $uid = $this->User->id;
  232. $this->User->Behaviors->load('Tools.Passwordable', array('current'=>true));
  233. $this->User->create();
  234. $data = array(
  235. 'id' => $uid,
  236. 'pwd' => '1234',
  237. 'pwd_repeat' => '123456',
  238. //'pwd_current' => '',
  239. );
  240. $this->User->set($data);
  241. $this->assertTrue($this->User->Behaviors->attached('Passwordable'));
  242. $is = $this->User->save();
  243. $this->assertFalse($is);
  244. $this->User->create();
  245. $data = array(
  246. 'id' => $uid,
  247. 'pwd_current' => 'somex',
  248. 'pwd' => '123456',
  249. 'pwd_repeat' => '123456'
  250. );
  251. $this->User->set($data);
  252. $is = $this->User->save();
  253. $this->assertFalse($is);
  254. $this->User->create();
  255. $data = array(
  256. 'id' => $uid,
  257. 'pwd_current' => 'some',
  258. 'pwd' => '123456',
  259. 'pwd_repeat' => '123456'
  260. );
  261. $this->User->set($data);
  262. $is = $this->User->save();
  263. $this->assertTrue(!empty($is));
  264. }
  265. }
  266. /**
  267. * FAKER!
  268. * 2011-11-03 ms
  269. */
  270. class AuthTestComponent {
  271. public function identify($request, $response) {
  272. $user = $request->data['User'];
  273. if ($user['id'] == '5' && $user['password'] === 'some') {
  274. return true;
  275. }
  276. return false;
  277. }
  278. }