Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 anikendra 1
<?php
2
/**
3
 * AuthComponentTest file
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14
 * @package       Cake.Test.Case.Controller.Component
15
 * @since         CakePHP(tm) v 1.2.0.5347
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('Controller', 'Controller');
20
App::uses('AuthComponent', 'Controller/Component');
21
App::uses('AclComponent', 'Controller/Component');
22
App::uses('FormAuthenticate', 'Controller/Component/Auth');
23
 
24
/**
25
 * TestAuthComponent class
26
 *
27
 * @package       Cake.Test.Case.Controller.Component
28
 */
29
class TestAuthComponent extends AuthComponent {
30
 
31
/**
32
 * testStop property
33
 *
34
 * @var bool
35
 */
36
	public $testStop = false;
37
 
38
/**
39
 * Helper method to add/set an authenticate object instance
40
 *
41
 * @param int $index The index at which to add/set the object
42
 * @param Object $object The object to add/set
43
 * @return void
44
 */
45
	public function setAuthenticateObject($index, $object) {
46
		$this->_authenticateObjects[$index] = $object;
47
	}
48
 
49
/**
50
 * Helper method to add/set an authorize object instance
51
 *
52
 * @param int $index The index at which to add/set the object
53
 * @param Object $object The object to add/set
54
 * @return void
55
 */
56
	public function setAuthorizeObject($index, $object) {
57
		$this->_authorizeObjects[$index] = $object;
58
	}
59
 
60
/**
61
 * stop method
62
 *
63
 * @return void
64
 */
65
	protected function _stop($status = 0) {
66
		$this->testStop = true;
67
	}
68
 
69
	public static function clearUser() {
70
		self::$_user = array();
71
	}
72
 
73
}
74
 
75
/**
76
 * AuthUser class
77
 *
78
 * @package       Cake.Test.Case.Controller.Component
79
 */
80
class AuthUser extends CakeTestModel {
81
 
82
/**
83
 * useDbConfig property
84
 *
85
 * @var string
86
 */
87
	public $useDbConfig = 'test';
88
 
89
}
90
 
91
/**
92
 * AuthTestController class
93
 *
94
 * @package       Cake.Test.Case.Controller.Component
95
 */
96
class AuthTestController extends Controller {
97
 
98
/**
99
 * uses property
100
 *
101
 * @var array
102
 */
103
	public $uses = array('AuthUser');
104
 
105
/**
106
 * components property
107
 *
108
 * @var array
109
 */
110
	public $components = array('Session', 'Auth');
111
 
112
/**
113
 * testUrl property
114
 *
115
 * @var mixed
116
 */
117
	public $testUrl = null;
118
 
119
/**
120
 * construct method
121
 *
122
 */
123
	public function __construct($request, $response) {
124
		$request->addParams(Router::parse('/auth_test'));
125
		$request->here = '/auth_test';
126
		$request->webroot = '/';
127
		Router::setRequestInfo($request);
128
		parent::__construct($request, $response);
129
	}
130
 
131
/**
132
 * login method
133
 *
134
 * @return void
135
 */
136
	public function login() {
137
	}
138
 
139
/**
140
 * admin_login method
141
 *
142
 * @return void
143
 */
144
	public function admin_login() {
145
	}
146
 
147
/**
148
 * admin_add method
149
 *
150
 * @return void
151
 */
152
	public function admin_add() {
153
	}
154
 
155
/**
156
 * logout method
157
 *
158
 * @return void
159
 */
160
	public function logout() {
161
	}
162
 
163
/**
164
 * add method
165
 *
166
 * @return void
167
 */
168
	public function add() {
169
		echo "add";
170
	}
171
 
172
/**
173
 * add method
174
 *
175
 * @return void
176
 */
177
	public function camelCase() {
178
		echo "camelCase";
179
	}
180
 
181
/**
182
 * redirect method
183
 *
184
 * @param string|array $url
185
 * @param mixed $status
186
 * @param mixed $exit
187
 * @return void
188
 */
189
	public function redirect($url, $status = null, $exit = true) {
190
		$this->testUrl = Router::url($url);
191
		return false;
192
	}
193
 
194
/**
195
 * isAuthorized method
196
 *
197
 * @return void
198
 */
199
	public function isAuthorized() {
200
	}
201
 
202
}
203
 
204
/**
205
 * AjaxAuthController class
206
 *
207
 * @package       Cake.Test.Case.Controller.Component
208
 */
209
class AjaxAuthController extends Controller {
210
 
211
/**
212
 * components property
213
 *
214
 * @var array
215
 */
216
	public $components = array('Session', 'TestAuth');
217
 
218
/**
219
 * uses property
220
 *
221
 * @var array
222
 */
223
	public $uses = array();
224
 
225
/**
226
 * testUrl property
227
 *
228
 * @var mixed
229
 */
230
	public $testUrl = null;
231
 
232
/**
233
 * beforeFilter method
234
 *
235
 * @return void
236
 */
237
	public function beforeFilter() {
238
		$this->TestAuth->ajaxLogin = 'test_element';
239
		$this->TestAuth->userModel = 'AuthUser';
240
		$this->TestAuth->RequestHandler->ajaxLayout = 'ajax2';
241
	}
242
 
243
/**
244
 * add method
245
 *
246
 * @return void
247
 */
248
	public function add() {
249
		if ($this->TestAuth->testStop !== true) {
250
			echo 'Added Record';
251
		}
252
	}
253
 
254
/**
255
 * redirect method
256
 *
257
 * @param string|array $url
258
 * @param mixed $status
259
 * @param mixed $exit
260
 * @return void
261
 */
262
	public function redirect($url, $status = null, $exit = true) {
263
		$this->testUrl = Router::url($url);
264
		return false;
265
	}
266
 
267
}
268
 
269
/**
270
 * AuthComponentTest class
271
 *
272
 * @package       Cake.Test.Case.Controller.Component
273
 */
274
class AuthComponentTest extends CakeTestCase {
275
 
276
/**
277
 * name property
278
 *
279
 * @var string
280
 */
281
	public $name = 'Auth';
282
 
283
/**
284
 * fixtures property
285
 *
286
 * @var array
287
 */
288
	public $fixtures = array('core.auth_user');
289
 
290
/**
291
 * initialized property
292
 *
293
 * @var bool
294
 */
295
	public $initialized = false;
296
 
297
/**
298
 * setUp method
299
 *
300
 * @return void
301
 */
302
	public function setUp() {
303
		parent::setUp();
304
		Configure::write('Security.salt', 'YJfIxfs2guVoUubWDYhG93b0qyJfIxfs2guwvniR2G0FgaC9mi');
305
		Configure::write('Security.cipherSeed', 770011223369876);
306
 
307
		$request = new CakeRequest(null, false);
308
 
309
		$this->Controller = new AuthTestController($request, $this->getMock('CakeResponse'));
310
 
311
		$collection = new ComponentCollection();
312
		$collection->init($this->Controller);
313
		$this->Auth = new TestAuthComponent($collection);
314
		$this->Auth->request = $request;
315
		$this->Auth->response = $this->getMock('CakeResponse');
316
		AuthComponent::$sessionKey = 'Auth.User';
317
 
318
		$this->Controller->Components->init($this->Controller);
319
 
320
		$this->initialized = true;
321
		Router::reload();
322
		Router::connect('/:controller/:action/*');
323
 
324
		$User = ClassRegistry::init('AuthUser');
325
		$User->updateAll(array('password' => $User->getDataSource()->value(Security::hash('cake', null, true))));
326
	}
327
 
328
/**
329
 * tearDown method
330
 *
331
 * @return void
332
 */
333
	public function tearDown() {
334
		parent::tearDown();
335
 
336
		TestAuthComponent::clearUser();
337
		$this->Auth->Session->delete('Auth');
338
		$this->Auth->Session->delete('Message.auth');
339
		unset($this->Controller, $this->Auth);
340
	}
341
 
342
/**
343
 * testNoAuth method
344
 *
345
 * @return void
346
 */
347
	public function testNoAuth() {
348
		$this->assertFalse($this->Auth->isAuthorized());
349
	}
350
 
351
/**
352
 * testIsErrorOrTests
353
 *
354
 * @return void
355
 */
356
	public function testIsErrorOrTests() {
357
		$this->Controller->Auth->initialize($this->Controller);
358
 
359
		$this->Controller->name = 'CakeError';
360
		$this->assertTrue($this->Controller->Auth->startup($this->Controller));
361
 
362
		$this->Controller->name = 'Post';
363
		$this->Controller->request['action'] = 'thisdoesnotexist';
364
		$this->assertTrue($this->Controller->Auth->startup($this->Controller));
365
 
366
		$this->Controller->scaffold = null;
367
		$this->Controller->request['action'] = 'index';
368
		$this->assertFalse($this->Controller->Auth->startup($this->Controller));
369
	}
370
 
371
/**
372
 * testLogin method
373
 *
374
 * @return void
375
 */
376
	public function testLogin() {
377
		$AuthLoginFormAuthenticate = $this->getMock('FormAuthenticate', array(), array(), '', false);
378
		$this->Auth->authenticate = array(
379
			'AuthLoginForm' => array(
380
				'userModel' => 'AuthUser'
381
			)
382
		);
383
		$this->Auth->Session = $this->getMock('SessionComponent', array('renew'), array(), '', false);
384
 
385
		$this->Auth->setAuthenticateObject(0, $AuthLoginFormAuthenticate);
386
 
387
		$this->Auth->request->data = array(
388
			'AuthUser' => array(
389
				'username' => 'mark',
390
				'password' => Security::hash('cake', null, true)
391
			)
392
		);
393
 
394
		$user = array(
395
			'id' => 1,
396
			'username' => 'mark'
397
		);
398
 
399
		$AuthLoginFormAuthenticate->expects($this->once())
400
			->method('authenticate')
401
			->with($this->Auth->request)
402
			->will($this->returnValue($user));
403
 
404
		$this->Auth->Session->expects($this->once())
405
			->method('renew');
406
 
407
		$result = $this->Auth->login();
408
		$this->assertTrue($result);
409
 
410
		$this->assertTrue($this->Auth->loggedIn());
411
		$this->assertEquals($user, $this->Auth->user());
412
	}
413
 
414
/**
415
 * testRedirectVarClearing method
416
 *
417
 * @return void
418
 */
419
	public function testRedirectVarClearing() {
420
		$this->Controller->request['controller'] = 'auth_test';
421
		$this->Controller->request['action'] = 'admin_add';
422
		$this->Controller->here = '/auth_test/admin_add';
423
		$this->assertNull($this->Auth->Session->read('Auth.redirect'));
424
 
425
		$this->Auth->authenticate = array('Form');
426
		$this->Auth->startup($this->Controller);
427
		$this->assertEquals('/auth_test/admin_add', $this->Auth->Session->read('Auth.redirect'));
428
 
429
		$this->Auth->Session->write('Auth.User', array('username' => 'admad'));
430
		$this->Auth->startup($this->Controller);
431
		$this->assertNull($this->Auth->Session->read('Auth.redirect'));
432
	}
433
 
434
/**
435
 * testAuthorizeFalse method
436
 *
437
 * @return void
438
 */
439
	public function testAuthorizeFalse() {
440
		$this->AuthUser = new AuthUser();
441
		$user = $this->AuthUser->find();
442
		$this->Auth->Session->write('Auth.User', $user['AuthUser']);
443
		$this->Controller->Auth->userModel = 'AuthUser';
444
		$this->Controller->Auth->authorize = false;
445
		$this->Controller->request->addParams(Router::parse('auth_test/add'));
446
		$this->Controller->Auth->initialize($this->Controller);
447
		$result = $this->Controller->Auth->startup($this->Controller);
448
		$this->assertTrue($result);
449
 
450
		$this->Auth->Session->delete('Auth');
451
		$result = $this->Controller->Auth->startup($this->Controller);
452
		$this->assertFalse($result);
453
		$this->assertTrue($this->Auth->Session->check('Message.auth'));
454
 
455
		$this->Controller->request->addParams(Router::parse('auth_test/camelCase'));
456
		$result = $this->Controller->Auth->startup($this->Controller);
457
		$this->assertFalse($result);
458
	}
459
 
460
/**
461
 * @expectedException CakeException
462
 * @return void
463
 */
464
	public function testIsAuthorizedMissingFile() {
465
		$this->Controller->Auth->authorize = 'Missing';
466
		$this->Controller->Auth->isAuthorized(array('User' => array('id' => 1)));
467
	}
468
 
469
/**
470
 * test that isAuthorized calls methods correctly
471
 *
472
 * @return void
473
 */
474
	public function testIsAuthorizedDelegation() {
475
		$AuthMockOneAuthorize = $this->getMock('BaseAuthorize', array('authorize'), array(), '', false);
476
		$AuthMockTwoAuthorize = $this->getMock('BaseAuthorize', array('authorize'), array(), '', false);
477
		$AuthMockThreeAuthorize = $this->getMock('BaseAuthorize', array('authorize'), array(), '', false);
478
 
479
		$this->Auth->setAuthorizeObject(0, $AuthMockOneAuthorize);
480
		$this->Auth->setAuthorizeObject(1, $AuthMockTwoAuthorize);
481
		$this->Auth->setAuthorizeObject(2, $AuthMockThreeAuthorize);
482
		$request = $this->Auth->request;
483
 
484
		$AuthMockOneAuthorize->expects($this->once())
485
			->method('authorize')
486
			->with(array('User'), $request)
487
			->will($this->returnValue(false));
488
 
489
		$AuthMockTwoAuthorize->expects($this->once())
490
			->method('authorize')
491
			->with(array('User'), $request)
492
			->will($this->returnValue(true));
493
 
494
		$AuthMockThreeAuthorize->expects($this->never())
495
			->method('authorize');
496
 
497
		$this->assertTrue($this->Auth->isAuthorized(array('User'), $request));
498
	}
499
 
500
/**
501
 * test that isAuthorized will use the session user if none is given.
502
 *
503
 * @return void
504
 */
505
	public function testIsAuthorizedUsingUserInSession() {
506
		$AuthMockFourAuthorize = $this->getMock('BaseAuthorize', array('authorize'), array(), '', false);
507
		$this->Auth->authorize = array('AuthMockFour');
508
		$this->Auth->setAuthorizeObject(0, $AuthMockFourAuthorize);
509
 
510
		$user = array('user' => 'mark');
511
		$this->Auth->Session->write('Auth.User', $user);
512
		$request = $this->Controller->request;
513
 
514
		$AuthMockFourAuthorize->expects($this->once())
515
			->method('authorize')
516
			->with($user, $request)
517
			->will($this->returnValue(true));
518
 
519
		$this->assertTrue($this->Auth->isAuthorized(null, $request));
520
	}
521
 
522
/**
523
 * test that loadAuthorize resets the loaded objects each time.
524
 *
525
 * @return void
526
 */
527
	public function testLoadAuthorizeResets() {
528
		$this->Controller->Auth->authorize = array(
529
			'Controller'
530
		);
531
		$result = $this->Controller->Auth->constructAuthorize();
532
		$this->assertEquals(1, count($result));
533
 
534
		$result = $this->Controller->Auth->constructAuthorize();
535
		$this->assertEquals(1, count($result));
536
	}
537
 
538
/**
539
 * @expectedException CakeException
540
 * @return void
541
 */
542
	public function testLoadAuthenticateNoFile() {
543
		$this->Controller->Auth->authenticate = 'Missing';
544
		$this->Controller->Auth->identify($this->Controller->request, $this->Controller->response);
545
	}
546
 
547
/**
548
 * test the * key with authenticate
549
 *
550
 * @return void
551
 */
552
	public function testAllConfigWithAuthorize() {
553
		$this->Controller->Auth->authorize = array(
554
			AuthComponent::ALL => array('actionPath' => 'controllers/'),
555
			'Actions'
556
		);
557
		$objects = $this->Controller->Auth->constructAuthorize();
558
		$result = $objects[0];
559
		$this->assertEquals('controllers/', $result->settings['actionPath']);
560
	}
561
 
562
/**
563
 * test that loadAuthorize resets the loaded objects each time.
564
 *
565
 * @return void
566
 */
567
	public function testLoadAuthenticateResets() {
568
		$this->Controller->Auth->authenticate = array(
569
			'Form'
570
		);
571
		$result = $this->Controller->Auth->constructAuthenticate();
572
		$this->assertEquals(1, count($result));
573
 
574
		$result = $this->Controller->Auth->constructAuthenticate();
575
		$this->assertEquals(1, count($result));
576
	}
577
 
578
/**
579
 * test the * key with authenticate
580
 *
581
 * @return void
582
 */
583
	public function testAllConfigWithAuthenticate() {
584
		$this->Controller->Auth->authenticate = array(
585
			AuthComponent::ALL => array('userModel' => 'AuthUser'),
586
			'Form'
587
		);
588
		$objects = $this->Controller->Auth->constructAuthenticate();
589
		$result = $objects[0];
590
		$this->assertEquals('AuthUser', $result->settings['userModel']);
591
	}
592
 
593
/**
594
 * test defining the same Authenticate object but with different password hashers
595
 *
596
 * @return void
597
 */
598
	public function testSameAuthenticateWithDifferentHashers() {
599
		$this->Controller->Auth->authenticate = array(
600
			'FormSimple' => array('className' => 'Form', 'passwordHasher' => 'Simple'),
601
			'FormBlowfish' => array('className' => 'Form', 'passwordHasher' => 'Blowfish'),
602
		);
603
 
604
		$objects = $this->Controller->Auth->constructAuthenticate();
605
		$this->assertEquals(2, count($objects));
606
 
607
		$this->assertInstanceOf('FormAuthenticate', $objects[0]);
608
		$this->assertInstanceOf('FormAuthenticate', $objects[1]);
609
 
610
		$this->assertInstanceOf('SimplePasswordHasher', $objects[0]->passwordHasher());
611
		$this->assertInstanceOf('BlowfishPasswordHasher', $objects[1]->passwordHasher());
612
	}
613
 
614
/**
615
 * Tests that deny always takes precedence over allow
616
 *
617
 * @return void
618
 */
619
	public function testAllowDenyAll() {
620
		$this->Controller->Auth->initialize($this->Controller);
621
 
622
		$this->Controller->Auth->allow();
623
		$this->Controller->Auth->deny('add', 'camelCase');
624
 
625
		$this->Controller->request['action'] = 'delete';
626
		$this->assertTrue($this->Controller->Auth->startup($this->Controller));
627
 
628
		$this->Controller->request['action'] = 'add';
629
		$this->assertFalse($this->Controller->Auth->startup($this->Controller));
630
 
631
		$this->Controller->request['action'] = 'camelCase';
632
		$this->assertFalse($this->Controller->Auth->startup($this->Controller));
633
 
634
		$this->Controller->Auth->allow();
635
		$this->Controller->Auth->deny(array('add', 'camelCase'));
636
 
637
		$this->Controller->request['action'] = 'delete';
638
		$this->assertTrue($this->Controller->Auth->startup($this->Controller));
639
 
640
		$this->Controller->request['action'] = 'camelCase';
641
		$this->assertFalse($this->Controller->Auth->startup($this->Controller));
642
 
643
		$this->Controller->Auth->allow('*');
644
		$this->Controller->Auth->deny();
645
 
646
		$this->Controller->request['action'] = 'camelCase';
647
		$this->assertFalse($this->Controller->Auth->startup($this->Controller));
648
 
649
		$this->Controller->request['action'] = 'add';
650
		$this->assertFalse($this->Controller->Auth->startup($this->Controller));
651
 
652
		$this->Controller->Auth->allow('camelCase');
653
		$this->Controller->Auth->deny();
654
 
655
		$this->Controller->request['action'] = 'camelCase';
656
		$this->assertFalse($this->Controller->Auth->startup($this->Controller));
657
 
658
		$this->Controller->request['action'] = 'login';
659
		$this->assertFalse($this->Controller->Auth->startup($this->Controller));
660
 
661
		$this->Controller->Auth->deny();
662
		$this->Controller->Auth->allow(null);
663
 
664
		$this->Controller->request['action'] = 'camelCase';
665
		$this->assertTrue($this->Controller->Auth->startup($this->Controller));
666
 
667
		$this->Controller->Auth->allow();
668
		$this->Controller->Auth->deny(null);
669
 
670
		$this->Controller->request['action'] = 'camelCase';
671
		$this->assertFalse($this->Controller->Auth->startup($this->Controller));
672
	}
673
 
674
/**
675
 * test that deny() converts camel case inputs to lowercase.
676
 *
677
 * @return void
678
 */
679
	public function testDenyWithCamelCaseMethods() {
680
		$this->Controller->Auth->initialize($this->Controller);
681
		$this->Controller->Auth->allow();
682
		$this->Controller->Auth->deny('add', 'camelCase');
683
 
684
		$url = '/auth_test/camelCase';
685
		$this->Controller->request->addParams(Router::parse($url));
686
		$this->Controller->request->query['url'] = Router::normalize($url);
687
 
688
		$this->assertFalse($this->Controller->Auth->startup($this->Controller));
689
 
690
		$url = '/auth_test/CamelCase';
691
		$this->Controller->request->addParams(Router::parse($url));
692
		$this->Controller->request->query['url'] = Router::normalize($url);
693
		$this->assertFalse($this->Controller->Auth->startup($this->Controller));
694
	}
695
 
696
/**
697
 * test that allow() and allowedActions work with camelCase method names.
698
 *
699
 * @return void
700
 */
701
	public function testAllowedActionsWithCamelCaseMethods() {
702
		$url = '/auth_test/camelCase';
703
		$this->Controller->request->addParams(Router::parse($url));
704
		$this->Controller->request->query['url'] = Router::normalize($url);
705
		$this->Controller->Auth->initialize($this->Controller);
706
		$this->Controller->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
707
		$this->Controller->Auth->userModel = 'AuthUser';
708
		$this->Controller->Auth->allow();
709
		$result = $this->Controller->Auth->startup($this->Controller);
710
		$this->assertTrue($result, 'startup() should return true, as action is allowed. %s');
711
 
712
		$url = '/auth_test/camelCase';
713
		$this->Controller->request->addParams(Router::parse($url));
714
		$this->Controller->request->query['url'] = Router::normalize($url);
715
		$this->Controller->Auth->initialize($this->Controller);
716
		$this->Controller->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
717
		$this->Controller->Auth->userModel = 'AuthUser';
718
		$this->Controller->Auth->allowedActions = array('delete', 'camelCase', 'add');
719
		$result = $this->Controller->Auth->startup($this->Controller);
720
		$this->assertTrue($result, 'startup() should return true, as action is allowed. %s');
721
 
722
		$this->Controller->Auth->allowedActions = array('delete', 'add');
723
		$result = $this->Controller->Auth->startup($this->Controller);
724
		$this->assertFalse($result, 'startup() should return false, as action is not allowed. %s');
725
 
726
		$url = '/auth_test/delete';
727
		$this->Controller->request->addParams(Router::parse($url));
728
		$this->Controller->request->query['url'] = Router::normalize($url);
729
		$this->Controller->Auth->initialize($this->Controller);
730
		$this->Controller->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
731
		$this->Controller->Auth->userModel = 'AuthUser';
732
 
733
		$this->Controller->Auth->allow(array('delete', 'add'));
734
		$result = $this->Controller->Auth->startup($this->Controller);
735
		$this->assertTrue($result, 'startup() should return true, as action is allowed. %s');
736
	}
737
 
738
	public function testAllowedActionsSetWithAllowMethod() {
739
		$url = '/auth_test/action_name';
740
		$this->Controller->request->addParams(Router::parse($url));
741
		$this->Controller->request->query['url'] = Router::normalize($url);
742
		$this->Controller->Auth->initialize($this->Controller);
743
		$this->Controller->Auth->allow('action_name', 'anotherAction');
744
		$this->assertEquals(array('action_name', 'anotherAction'), $this->Controller->Auth->allowedActions);
745
	}
746
 
747
/**
748
 * testLoginRedirect method
749
 *
750
 * @return void
751
 */
752
	public function testLoginRedirect() {
753
		$_SERVER['HTTP_REFERER'] = false;
754
		$_ENV['HTTP_REFERER'] = false;
755
		putenv('HTTP_REFERER=');
756
 
757
		$this->Auth->Session->write('Auth', array(
758
			'AuthUser' => array('id' => '1', 'username' => 'nate')
759
		));
760
 
761
		$this->Auth->request->addParams(Router::parse('users/login'));
762
		$this->Auth->request->url = 'users/login';
763
		$this->Auth->initialize($this->Controller);
764
 
765
		$this->Auth->loginRedirect = array(
766
			'controller' => 'pages', 'action' => 'display', 'welcome'
767
		);
768
		$this->Auth->startup($this->Controller);
769
		$expected = Router::normalize($this->Auth->loginRedirect);
770
		$this->assertEquals($expected, $this->Auth->redirectUrl());
771
 
772
		$this->Auth->Session->delete('Auth');
773
 
774
		//empty referer no session
775
		$_SERVER['HTTP_REFERER'] = false;
776
		$_ENV['HTTP_REFERER'] = false;
777
		putenv('HTTP_REFERER=');
778
		$url = '/posts/view/1';
779
 
780
		$this->Auth->Session->write('Auth', array(
781
			'AuthUser' => array('id' => '1', 'username' => 'nate'))
782
		);
783
		$this->Controller->testUrl = null;
784
		$this->Auth->request->addParams(Router::parse($url));
785
		array_push($this->Controller->methods, 'view', 'edit', 'index');
786
 
787
		$this->Auth->initialize($this->Controller);
788
		$this->Auth->authorize = 'controller';
789
 
790
		$this->Auth->loginAction = array(
791
			'controller' => 'AuthTest', 'action' => 'login'
792
		);
793
		$this->Auth->startup($this->Controller);
794
		$expected = Router::normalize('/AuthTest/login');
795
		$this->assertEquals($expected, $this->Controller->testUrl);
796
 
797
		$this->Auth->Session->delete('Auth');
798
		$_SERVER['HTTP_REFERER'] = $_ENV['HTTP_REFERER'] = Router::url('/admin', true);
799
		$this->Auth->Session->write('Auth', array(
800
			'AuthUser' => array('id' => '1', 'username' => 'nate')
801
		));
802
		$this->Auth->request->params['action'] = 'login';
803
		$this->Auth->request->url = 'auth_test/login';
804
		$this->Auth->initialize($this->Controller);
805
		$this->Auth->loginAction = 'auth_test/login';
806
		$this->Auth->loginRedirect = false;
807
		$this->Auth->startup($this->Controller);
808
		$expected = Router::normalize('/admin');
809
		$this->assertEquals($expected, $this->Auth->redirectUrl());
810
 
811
		// Ticket #4750
812
		// Named Parameters
813
		$this->Controller->request = $this->Auth->request;
814
		$this->Auth->Session->delete('Auth');
815
		$url = '/posts/index/year:2008/month:feb';
816
		$this->Auth->request->addParams(Router::parse($url));
817
		$this->Auth->request->url = $this->Auth->request->here = Router::normalize($url);
818
		$this->Auth->initialize($this->Controller);
819
		$this->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
820
		$this->Auth->startup($this->Controller);
821
		$expected = Router::normalize('posts/index/year:2008/month:feb');
822
		$this->assertEquals($expected, $this->Auth->Session->read('Auth.redirect'));
823
 
824
		// Passed Arguments
825
		$this->Auth->Session->delete('Auth');
826
		$url = '/posts/view/1';
827
		$this->Auth->request->addParams(Router::parse($url));
828
		$this->Auth->request->url = $this->Auth->request->here = Router::normalize($url);
829
		$this->Auth->initialize($this->Controller);
830
		$this->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
831
		$this->Auth->startup($this->Controller);
832
		$expected = Router::normalize('posts/view/1');
833
		$this->assertEquals($expected, $this->Auth->Session->read('Auth.redirect'));
834
 
835
		// QueryString parameters
836
		$_back = $_GET;
837
		$_GET = array(
838
			'print' => 'true',
839
			'refer' => 'menu'
840
		);
841
		$this->Auth->Session->delete('Auth');
842
		$url = '/posts/index/29';
843
		$this->Auth->request->addParams(Router::parse($url));
844
		$this->Auth->request->url = $this->Auth->request->here = Router::normalize($url);
845
		$this->Auth->request->query = $_GET;
846
 
847
		$this->Auth->initialize($this->Controller);
848
		$this->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
849
		$this->Auth->startup($this->Controller);
850
		$expected = Router::normalize('posts/index/29?print=true&refer=menu');
851
		$this->assertEquals($expected, $this->Auth->Session->read('Auth.redirect'));
852
 
853
		// Different base urls.
854
		$appConfig = Configure::read('App');
855
 
856
		$_GET = array();
857
 
858
		Configure::write('App', array(
859
			'dir' => APP_DIR,
860
			'webroot' => WEBROOT_DIR,
861
			'base' => false,
862
			'baseUrl' => '/cake/index.php'
863
		));
864
 
865
		$this->Auth->Session->delete('Auth');
866
 
867
		$url = '/posts/add';
868
		$this->Auth->request = $this->Controller->request = new CakeRequest($url);
869
		$this->Auth->request->addParams(Router::parse($url));
870
		$this->Auth->request->url = Router::normalize($url);
871
 
872
		$this->Auth->initialize($this->Controller);
873
		$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
874
		$this->Auth->startup($this->Controller);
875
		$expected = Router::normalize('/posts/add');
876
		$this->assertEquals($expected, $this->Auth->Session->read('Auth.redirect'));
877
 
878
		$this->Auth->Session->delete('Auth');
879
		Configure::write('App', $appConfig);
880
 
881
		$_GET = $_back;
882
 
883
		// External Authed Action
884
		$_SERVER['HTTP_REFERER'] = 'http://webmail.example.com/view/message';
885
		$this->Auth->Session->delete('Auth');
886
		$url = '/posts/edit/1';
887
		$request = new CakeRequest($url);
888
		$request->query = array();
889
		$this->Auth->request = $this->Controller->request = $request;
890
		$this->Auth->request->addParams(Router::parse($url));
891
		$this->Auth->request->url = $this->Auth->request->here = Router::normalize($url);
892
		$this->Auth->initialize($this->Controller);
893
		$this->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
894
		$this->Auth->startup($this->Controller);
895
		$expected = Router::normalize('/posts/edit/1');
896
		$this->assertEquals($expected, $this->Auth->Session->read('Auth.redirect'));
897
 
898
		// External Direct Login Link
899
		$_SERVER['HTTP_REFERER'] = 'http://webmail.example.com/view/message';
900
		$this->Auth->Session->delete('Auth');
901
		$url = '/AuthTest/login';
902
		$this->Auth->request = $this->Controller->request = new CakeRequest($url);
903
		$this->Auth->request->addParams(Router::parse($url));
904
		$this->Auth->request->url = Router::normalize($url);
905
		$this->Auth->initialize($this->Controller);
906
		$this->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
907
		$this->Auth->startup($this->Controller);
908
		$expected = Router::normalize('/');
909
		$this->assertEquals($expected, $this->Auth->Session->read('Auth.redirect'));
910
 
911
		$this->Auth->Session->delete('Auth');
912
	}
913
 
914
/**
915
 * testNoLoginRedirectForAuthenticatedUser method
916
 *
917
 * @return void
918
 */
919
	public function testNoLoginRedirectForAuthenticatedUser() {
920
		$this->Controller->request['controller'] = 'auth_test';
921
		$this->Controller->request['action'] = 'login';
922
		$this->Controller->here = '/auth_test/login';
923
		$this->Auth->request->url = 'auth_test/login';
924
 
925
		$this->Auth->Session->write('Auth.User.id', '1');
926
		$this->Auth->authenticate = array('Form');
927
		$this->getMock('BaseAuthorize', array('authorize'), array(), 'NoLoginRedirectMockAuthorize', false);
928
		$this->Auth->authorize = array('NoLoginRedirectMockAuthorize');
929
		$this->Auth->loginAction = array('controller' => 'auth_test', 'action' => 'login');
930
 
931
		$return = $this->Auth->startup($this->Controller);
932
		$this->assertTrue($return);
933
		$this->assertNull($this->Controller->testUrl);
934
	}
935
 
936
/**
937
 * Default to loginRedirect, if set, on authError.
938
 *
939
 * @return void
940
 */
941
	public function testDefaultToLoginRedirect() {
942
		$_SERVER['HTTP_REFERER'] = false;
943
		$_ENV['HTTP_REFERER'] = false;
944
		putenv('HTTP_REFERER=');
945
 
946
		$url = '/party/on';
947
		$this->Auth->request = $CakeRequest = new CakeRequest($url);
948
		$this->Auth->request->addParams(Router::parse($url));
949
		$this->Auth->authorize = array('Controller');
950
		$this->Auth->login(array('username' => 'mariano', 'password' => 'cake'));
951
		$this->Auth->loginRedirect = array(
952
			'controller' => 'something', 'action' => 'else',
953
		);
954
 
955
		$CakeResponse = new CakeResponse();
956
		$Controller = $this->getMock(
957
			'Controller',
958
			array('on', 'redirect'),
959
			array($CakeRequest, $CakeResponse)
960
		);
961
 
962
		$expected = Router::url($this->Auth->loginRedirect);
963
		$Controller->expects($this->once())
964
			->method('redirect')
965
			->with($this->equalTo($expected));
966
		$this->Auth->startup($Controller);
967
	}
968
 
969
/**
970
 * testRedirectToUnauthorizedRedirect
971
 *
972
 * @return void
973
 */
974
	public function testRedirectToUnauthorizedRedirect() {
975
		$url = '/party/on';
976
		$this->Auth->request = $CakeRequest = new CakeRequest($url);
977
		$this->Auth->request->addParams(Router::parse($url));
978
		$this->Auth->authorize = array('Controller');
979
		$this->Auth->login(array('username' => 'admad', 'password' => 'cake'));
980
		$this->Auth->unauthorizedRedirect = array(
981
			'controller' => 'no_can_do', 'action' => 'jack'
982
		);
983
 
984
		$CakeResponse = new CakeResponse();
985
		$Controller = $this->getMock(
986
			'Controller',
987
			array('on', 'redirect'),
988
			array($CakeRequest, $CakeResponse)
989
		);
990
		$this->Auth->Session = $this->getMock(
991
			'SessionComponent',
992
			array('setFlash'),
993
			array($Controller->Components)
994
		);
995
 
996
		$expected = array(
997
			'controller' => 'no_can_do', 'action' => 'jack'
998
		);
999
		$Controller->expects($this->once())
1000
			->method('redirect')
1001
			->with($this->equalTo($expected));
1002
		$this->Auth->Session->expects($this->once())
1003
			->method('setFlash');
1004
		$this->Auth->startup($Controller);
1005
	}
1006
 
1007
/**
1008
 * testRedirectToUnauthorizedRedirectSuppressedAuthError
1009
 *
1010
 * @return void
1011
 */
1012
	public function testRedirectToUnauthorizedRedirectSuppressedAuthError() {
1013
		$url = '/party/on';
1014
		$this->Auth->request = $CakeRequest = new CakeRequest($url);
1015
		$this->Auth->request->addParams(Router::parse($url));
1016
		$this->Auth->authorize = array('Controller');
1017
		$this->Auth->login(array('username' => 'admad', 'password' => 'cake'));
1018
		$this->Auth->unauthorizedRedirect = array(
1019
			'controller' => 'no_can_do', 'action' => 'jack'
1020
		);
1021
		$this->Auth->authError = false;
1022
 
1023
		$CakeResponse = new CakeResponse();
1024
		$Controller = $this->getMock(
1025
			'Controller',
1026
			array('on', 'redirect'),
1027
			array($CakeRequest, $CakeResponse)
1028
		);
1029
		$this->Auth->Session = $this->getMock(
1030
			'SessionComponent',
1031
			array('setFlash'),
1032
			array($Controller->Components)
1033
		);
1034
 
1035
		$expected = array(
1036
			'controller' => 'no_can_do', 'action' => 'jack'
1037
		);
1038
		$Controller->expects($this->once())
1039
			->method('redirect')
1040
			->with($this->equalTo($expected));
1041
		$this->Auth->Session->expects($this->never())
1042
			->method('setFlash');
1043
		$this->Auth->startup($Controller);
1044
	}
1045
 
1046
/**
1047
 * Throw ForbiddenException if AuthComponent::$unauthorizedRedirect set to false
1048
 * @expectedException ForbiddenException
1049
 * @return void
1050
 */
1051
	public function testForbiddenException() {
1052
		$url = '/party/on';
1053
		$this->Auth->request = $CakeRequest = new CakeRequest($url);
1054
		$this->Auth->request->addParams(Router::parse($url));
1055
		$this->Auth->authorize = array('Controller');
1056
		$this->Auth->authorize = array('Controller');
1057
		$this->Auth->unauthorizedRedirect = false;
1058
		$this->Auth->login(array('username' => 'baker', 'password' => 'cake'));
1059
 
1060
		$CakeResponse = new CakeResponse();
1061
		$Controller = $this->getMock(
1062
			'Controller',
1063
			array('on', 'redirect'),
1064
			array($CakeRequest, $CakeResponse)
1065
		);
1066
 
1067
		$this->Auth->startup($Controller);
1068
	}
1069
 
1070
/**
1071
 * Test that no redirects or authorization tests occur on the loginAction
1072
 *
1073
 * @return void
1074
 */
1075
	public function testNoRedirectOnLoginAction() {
1076
		$controller = $this->getMock('Controller');
1077
		$controller->methods = array('login');
1078
 
1079
		$url = '/AuthTest/login';
1080
		$this->Auth->request = $controller->request = new CakeRequest($url);
1081
		$this->Auth->request->addParams(Router::parse($url));
1082
		$this->Auth->loginAction = array('controller' => 'AuthTest', 'action' => 'login');
1083
		$this->Auth->authorize = array('Controller');
1084
 
1085
		$controller->expects($this->never())
1086
			->method('redirect');
1087
 
1088
		$this->Auth->startup($controller);
1089
	}
1090
 
1091
/**
1092
 * Ensure that no redirect is performed when a 404 is reached
1093
 * And the user doesn't have a session.
1094
 *
1095
 * @return void
1096
 */
1097
	public function testNoRedirectOn404() {
1098
		$this->Auth->Session->delete('Auth');
1099
		$this->Auth->initialize($this->Controller);
1100
		$this->Auth->request->addParams(Router::parse('auth_test/something_totally_wrong'));
1101
		$result = $this->Auth->startup($this->Controller);
1102
		$this->assertTrue($result, 'Auth redirected a missing action %s');
1103
	}
1104
 
1105
/**
1106
 * testAdminRoute method
1107
 *
1108
 * @return void
1109
 */
1110
	public function testAdminRoute() {
1111
		$pref = Configure::read('Routing.prefixes');
1112
		Configure::write('Routing.prefixes', array('admin'));
1113
		Router::reload();
1114
		require CAKE . 'Config' . DS . 'routes.php';
1115
 
1116
		$url = '/admin/auth_test/add';
1117
		$this->Auth->request->addParams(Router::parse($url));
1118
		$this->Auth->request->query['url'] = ltrim($url, '/');
1119
		$this->Auth->request->base = '';
1120
 
1121
		Router::setRequestInfo($this->Auth->request);
1122
		$this->Auth->initialize($this->Controller);
1123
 
1124
		$this->Auth->loginAction = array(
1125
			'admin' => true, 'controller' => 'auth_test', 'action' => 'login'
1126
		);
1127
 
1128
		$this->Auth->startup($this->Controller);
1129
		$this->assertEquals('/admin/auth_test/login', $this->Controller->testUrl);
1130
 
1131
		Configure::write('Routing.prefixes', $pref);
1132
	}
1133
 
1134
/**
1135
 * testAjaxLogin method
1136
 *
1137
 * @return void
1138
 */
1139
	public function testAjaxLogin() {
1140
		App::build(array(
1141
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
1142
		));
1143
		$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
1144
 
1145
		App::uses('Dispatcher', 'Routing');
1146
 
1147
		$Response = new CakeResponse();
1148
		ob_start();
1149
		$Dispatcher = new Dispatcher();
1150
		$Dispatcher->dispatch(new CakeRequest('/ajax_auth/add'), $Response, array('return' => 1));
1151
		$result = ob_get_clean();
1152
 
1153
		$this->assertEquals(403, $Response->statusCode());
1154
		$this->assertEquals("Ajax!\nthis is the test element", str_replace("\r\n", "\n", $result));
1155
		unset($_SERVER['HTTP_X_REQUESTED_WITH']);
1156
	}
1157
 
1158
/**
1159
 * testLoginActionRedirect method
1160
 *
1161
 * @return void
1162
 */
1163
	public function testLoginActionRedirect() {
1164
		$admin = Configure::read('Routing.prefixes');
1165
		Configure::write('Routing.prefixes', array('admin'));
1166
		Router::reload();
1167
		require CAKE . 'Config' . DS . 'routes.php';
1168
 
1169
		$url = '/admin/auth_test/login';
1170
		$this->Auth->request->addParams(Router::parse($url));
1171
		$this->Auth->request->url = ltrim($url, '/');
1172
		Router::setRequestInfo(array(
1173
			array(
1174
				'pass' => array(), 'action' => 'admin_login', 'plugin' => null, 'controller' => 'auth_test',
1175
				'admin' => true,
1176
			),
1177
			array(
1178
				'base' => null, 'here' => $url,
1179
				'webroot' => '/', 'passedArgs' => array(),
1180
			)
1181
		));
1182
 
1183
		$this->Auth->initialize($this->Controller);
1184
		$this->Auth->loginAction = array('admin' => true, 'controller' => 'auth_test', 'action' => 'login');
1185
		$this->Auth->startup($this->Controller);
1186
 
1187
		$this->assertNull($this->Controller->testUrl);
1188
 
1189
		Configure::write('Routing.prefixes', $admin);
1190
	}
1191
 
1192
/**
1193
 * Stateless auth methods like Basic should populate data that can be
1194
 * accessed by $this->user().
1195
 *
1196
 * @return void
1197
 */
1198
	public function testStatelessAuthWorksWithUser() {
1199
		$_SERVER['PHP_AUTH_USER'] = 'mariano';
1200
		$_SERVER['PHP_AUTH_PW'] = 'cake';
1201
		$url = '/auth_test/add';
1202
		$this->Auth->request->addParams(Router::parse($url));
1203
 
1204
		$this->Auth->authenticate = array(
1205
			'Basic' => array('userModel' => 'AuthUser')
1206
		);
1207
		$this->Auth->startup($this->Controller);
1208
 
1209
		$result = $this->Auth->user();
1210
		$this->assertEquals('mariano', $result['username']);
1211
 
1212
		$result = $this->Auth->user('username');
1213
		$this->assertEquals('mariano', $result);
1214
	}
1215
 
1216
/**
1217
 * test $settings in Controller::$components
1218
 *
1219
 * @return void
1220
 */
1221
	public function testComponentSettings() {
1222
		$request = new CakeRequest(null, false);
1223
		$this->Controller = new AuthTestController($request, $this->getMock('CakeResponse'));
1224
 
1225
		$this->Controller->components = array(
1226
			'Auth' => array(
1227
				'loginAction' => array('controller' => 'people', 'action' => 'login'),
1228
				'logoutRedirect' => array('controller' => 'people', 'action' => 'login'),
1229
			),
1230
			'Session'
1231
		);
1232
		$this->Controller->Components->init($this->Controller);
1233
		$this->Controller->Components->trigger('initialize', array(&$this->Controller));
1234
		Router::reload();
1235
 
1236
		$expected = array(
1237
			'loginAction' => array('controller' => 'people', 'action' => 'login'),
1238
			'logoutRedirect' => array('controller' => 'people', 'action' => 'login'),
1239
		);
1240
		$this->assertEquals($expected['loginAction'], $this->Controller->Auth->loginAction);
1241
		$this->assertEquals($expected['logoutRedirect'], $this->Controller->Auth->logoutRedirect);
1242
	}
1243
 
1244
/**
1245
 * test that logout deletes the session variables. and returns the correct URL
1246
 *
1247
 * @return void
1248
 */
1249
	public function testLogout() {
1250
		$this->Auth->Session->write('Auth.User.id', '1');
1251
		$this->Auth->Session->write('Auth.redirect', '/users/login');
1252
		$this->Auth->logoutRedirect = '/';
1253
		$result = $this->Auth->logout();
1254
 
1255
		$this->assertEquals('/', $result);
1256
		$this->assertNull($this->Auth->Session->read('Auth.AuthUser'));
1257
		$this->assertNull($this->Auth->Session->read('Auth.redirect'));
1258
	}
1259
 
1260
/**
1261
 * Logout should trigger a logout method on authentication objects.
1262
 *
1263
 * @return void
1264
 */
1265
	public function testLogoutTrigger() {
1266
		$LogoutTriggerMockAuthenticate = $this->getMock('BaseAuthenticate', array('authenticate', 'logout'), array(), '', false);
1267
 
1268
		$this->Auth->authenticate = array('LogoutTriggerMock');
1269
		$this->Auth->setAuthenticateObject(0, $LogoutTriggerMockAuthenticate);
1270
		$LogoutTriggerMockAuthenticate->expects($this->once())
1271
			->method('logout');
1272
 
1273
		$this->Auth->logout();
1274
	}
1275
 
1276
/**
1277
 * test mapActions loading and delegating to authorize objects.
1278
 *
1279
 * @return void
1280
 */
1281
	public function testMapActionsDelegation() {
1282
		$MapActionMockAuthorize = $this->getMock('BaseAuthorize', array('authorize', 'mapActions'), array(), '', false);
1283
 
1284
		$this->Auth->authorize = array('MapActionMock');
1285
		$this->Auth->setAuthorizeObject(0, $MapActionMockAuthorize);
1286
		$MapActionMockAuthorize->expects($this->once())
1287
			->method('mapActions')
1288
			->with(array('create' => array('my_action')));
1289
 
1290
		$this->Auth->mapActions(array('create' => array('my_action')));
1291
	}
1292
 
1293
/**
1294
 * test logging in with a request.
1295
 *
1296
 * @return void
1297
 */
1298
	public function testLoginWithRequestData() {
1299
		$RequestLoginMockAuthenticate = $this->getMock('FormAuthenticate', array(), array(), '', false);
1300
		$request = new CakeRequest('users/login', false);
1301
		$user = array('username' => 'mark', 'role' => 'admin');
1302
 
1303
		$this->Auth->request = $request;
1304
		$this->Auth->authenticate = array('RequestLoginMock');
1305
		$this->Auth->setAuthenticateObject(0, $RequestLoginMockAuthenticate);
1306
		$RequestLoginMockAuthenticate->expects($this->once())
1307
			->method('authenticate')
1308
			->with($request)
1309
			->will($this->returnValue($user));
1310
 
1311
		$this->assertTrue($this->Auth->login());
1312
		$this->assertEquals($user['username'], $this->Auth->user('username'));
1313
	}
1314
 
1315
/**
1316
 * test login() with user data
1317
 *
1318
 * @return void
1319
 */
1320
	public function testLoginWithUserData() {
1321
		$this->assertFalse($this->Auth->loggedIn());
1322
 
1323
		$user = array(
1324
			'username' => 'mariano',
1325
			'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
1326
			'created' => '2007-03-17 01:16:23',
1327
			'updated' => '2007-03-17 01:18:31'
1328
		);
1329
		$this->assertTrue($this->Auth->login($user));
1330
		$this->assertTrue($this->Auth->loggedIn());
1331
		$this->assertEquals($user['username'], $this->Auth->user('username'));
1332
	}
1333
 
1334
/**
1335
 * test flash settings.
1336
 *
1337
 * @return void
1338
 */
1339
	public function testFlashSettings() {
1340
		$this->Auth->Session = $this->getMock('SessionComponent', array(), array(), '', false);
1341
		$this->Auth->Session->expects($this->once())
1342
			->method('setFlash')
1343
			->with('Auth failure', 'custom', array(1), 'auth-key');
1344
 
1345
		$this->Auth->flash = array(
1346
			'element' => 'custom',
1347
			'params' => array(1),
1348
			'key' => 'auth-key'
1349
		);
1350
		$this->Auth->flash('Auth failure');
1351
	}
1352
 
1353
/**
1354
 * test the various states of Auth::redirect()
1355
 *
1356
 * @return void
1357
 */
1358
	public function testRedirectSet() {
1359
		$value = array('controller' => 'users', 'action' => 'home');
1360
		$result = $this->Auth->redirectUrl($value);
1361
		$this->assertEquals('/users/home', $result);
1362
		$this->assertEquals($value, $this->Auth->Session->read('Auth.redirect'));
1363
	}
1364
 
1365
/**
1366
 * test redirect using Auth.redirect from the session.
1367
 *
1368
 * @return void
1369
 */
1370
	public function testRedirectSessionRead() {
1371
		$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
1372
		$this->Auth->Session->write('Auth.redirect', '/users/home');
1373
 
1374
		$result = $this->Auth->redirectUrl();
1375
		$this->assertEquals('/users/home', $result);
1376
		$this->assertFalse($this->Auth->Session->check('Auth.redirect'));
1377
	}
1378
 
1379
/**
1380
 * test redirectUrl with duplicate base.
1381
 *
1382
 * @return void
1383
 */
1384
	public function testRedirectSessionReadDuplicateBase() {
1385
		$this->Auth->request->webroot = '/waves/';
1386
		$this->Auth->request->base = '/waves';
1387
 
1388
		Router::setRequestInfo($this->Auth->request);
1389
 
1390
		$this->Auth->Session->write('Auth.redirect', '/waves/add');
1391
 
1392
		$result = $this->Auth->redirectUrl();
1393
		$this->assertEquals('/waves/add', $result);
1394
	}
1395
 
1396
/**
1397
 * test that redirect does not return loginAction if that is what's stored in Auth.redirect.
1398
 * instead loginRedirect should be used.
1399
 *
1400
 * @return void
1401
 */
1402
	public function testRedirectSessionReadEqualToLoginAction() {
1403
		$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
1404
		$this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'home');
1405
		$this->Auth->Session->write('Auth.redirect', array('controller' => 'users', 'action' => 'login'));
1406
 
1407
		$result = $this->Auth->redirectUrl();
1408
		$this->assertEquals('/users/home', $result);
1409
		$this->assertFalse($this->Auth->Session->check('Auth.redirect'));
1410
	}
1411
 
1412
/**
1413
 * test that the returned URL doesn't contain the base URL.
1414
 *
1415
 * @see https://cakephp.lighthouseapp.com/projects/42648/tickets/3922-authcomponentredirecturl-prepends-appbaseurl
1416
 *
1417
 * @return void This test method doesn't return anything.
1418
 */
1419
	public function testRedirectUrlWithBaseSet() {
1420
		$App = Configure::read('App');
1421
 
1422
		Configure::write('App', array(
1423
			'dir' => APP_DIR,
1424
			'webroot' => WEBROOT_DIR,
1425
			'base' => false,
1426
			'baseUrl' => '/cake/index.php'
1427
		));
1428
 
1429
		$url = '/users/login';
1430
		$this->Auth->request = $this->Controller->request = new CakeRequest($url);
1431
		$this->Auth->request->addParams(Router::parse($url));
1432
		$this->Auth->request->url = Router::normalize($url);
1433
 
1434
		Router::setRequestInfo($this->Auth->request);
1435
 
1436
		$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
1437
		$this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'home');
1438
 
1439
		$result = $this->Auth->redirectUrl();
1440
		$this->assertEquals('/users/home', $result);
1441
		$this->assertFalse($this->Auth->Session->check('Auth.redirect'));
1442
 
1443
		Configure::write('App', $App);
1444
		Router::reload();
1445
	}
1446
 
1447
/**
1448
 * test password hashing
1449
 *
1450
 * @return void
1451
 */
1452
	public function testPassword() {
1453
		$result = $this->Auth->password('password');
1454
		$expected = Security::hash('password', null, true);
1455
		$this->assertEquals($expected, $result);
1456
	}
1457
 
1458
/**
1459
 * testUser method
1460
 *
1461
 * @return void
1462
 */
1463
	public function testUser() {
1464
		$data = array(
1465
			'User' => array(
1466
				'id' => '2',
1467
				'username' => 'mark',
1468
				'group_id' => 1,
1469
				'Group' => array(
1470
					'id' => '1',
1471
					'name' => 'Members'
1472
				),
1473
				'is_admin' => false,
1474
		));
1475
		$this->Auth->Session->write('Auth', $data);
1476
 
1477
		$result = $this->Auth->user();
1478
		$this->assertEquals($data['User'], $result);
1479
 
1480
		$result = $this->Auth->user('username');
1481
		$this->assertEquals($data['User']['username'], $result);
1482
 
1483
		$result = $this->Auth->user('Group.name');
1484
		$this->assertEquals($data['User']['Group']['name'], $result);
1485
 
1486
		$result = $this->Auth->user('invalid');
1487
		$this->assertEquals(null, $result);
1488
 
1489
		$result = $this->Auth->user('Company.invalid');
1490
		$this->assertEquals(null, $result);
1491
 
1492
		$result = $this->Auth->user('is_admin');
1493
		$this->assertFalse($result);
1494
	}
1495
 
1496
/**
1497
 * testStatelessAuthNoRedirect method
1498
 *
1499
 * @expectedException UnauthorizedException
1500
 * @expectedExceptionCode 401
1501
 * @return void
1502
 */
1503
	public function testStatelessAuthNoRedirect() {
1504
		if (CakeSession::id()) {
1505
			session_destroy();
1506
			CakeSession::$id = null;
1507
		}
1508
		$_SESSION = null;
1509
 
1510
		AuthComponent::$sessionKey = false;
1511
		$this->Auth->authenticate = array('Basic');
1512
		$this->Controller->request['action'] = 'admin_add';
1513
 
1514
		$this->Auth->startup($this->Controller);
1515
	}
1516
 
1517
/**
1518
 * testStatelessAuthNoSessionStart method
1519
 *
1520
 * @return void
1521
 */
1522
	public function testStatelessAuthNoSessionStart() {
1523
		if (CakeSession::id()) {
1524
			session_destroy();
1525
			CakeSession::$id = null;
1526
		}
1527
		$_SESSION = null;
1528
 
1529
		$_SERVER['PHP_AUTH_USER'] = 'mariano';
1530
		$_SERVER['PHP_AUTH_PW'] = 'cake';
1531
 
1532
		AuthComponent::$sessionKey = false;
1533
		$this->Auth->authenticate = array(
1534
			'Basic' => array('userModel' => 'AuthUser')
1535
		);
1536
		$this->Controller->request['action'] = 'admin_add';
1537
 
1538
		$result = $this->Auth->startup($this->Controller);
1539
		$this->assertTrue($result);
1540
 
1541
		$this->assertNull(CakeSession::id());
1542
	}
1543
 
1544
/**
1545
 * testStatelessAuthRedirect method
1546
 *
1547
 * @return void
1548
 */
1549
	public function testStatelessFollowedByStatefulAuth() {
1550
		$this->Auth->authenticate = array('Basic', 'Form');
1551
		$this->Controller->request['action'] = 'admin_add';
1552
 
1553
		$this->Auth->response->expects($this->never())->method('statusCode');
1554
		$this->Auth->response->expects($this->never())->method('send');
1555
 
1556
		$result = $this->Auth->startup($this->Controller);
1557
		$this->assertFalse($result);
1558
 
1559
		$this->assertEquals('/users/login', $this->Controller->testUrl);
1560
	}
1561
}