Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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