| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://cakephp.org CakePHP(tm) Project
- * @since 2.0.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Auth;
- use Cake\Auth\FormAuthenticate;
- use Cake\Cache\Cache;
- use Cake\Core\App;
- use Cake\Core\Configure;
- use Cake\Core\Plugin;
- use Cake\I18n\Time;
- use Cake\Network\Request;
- use Cake\ORM\Entity;
- use Cake\ORM\TableRegistry;
- use Cake\TestSuite\TestCase;
- use Cake\Utility\Security;
- /**
- * Test case for FormAuthentication
- *
- */
- class FormAuthenticateTest extends TestCase
- {
- /**
- * Fixtures
- *
- * @var array
- */
- public $fixtures = ['core.users', 'core.auth_users'];
- /**
- * setup
- *
- * @return void
- */
- public function setUp()
- {
- parent::setUp();
- $this->Collection = $this->getMock('Cake\Controller\ComponentRegistry');
- $this->auth = new FormAuthenticate($this->Collection, [
- 'userModel' => 'Users'
- ]);
- $password = password_hash('password', PASSWORD_DEFAULT);
- TableRegistry::clear();
- $Users = TableRegistry::get('Users');
- $Users->updateAll(['password' => $password], []);
- $this->response = $this->getMock('Cake\Network\Response');
- }
- /**
- * test applying settings in the constructor
- *
- * @return void
- */
- public function testConstructor()
- {
- $object = new FormAuthenticate($this->Collection, [
- 'userModel' => 'AuthUsers',
- 'fields' => ['username' => 'user', 'password' => 'password']
- ]);
- $this->assertEquals('AuthUsers', $object->config('userModel'));
- $this->assertEquals(['username' => 'user', 'password' => 'password'], $object->config('fields'));
- }
- /**
- * test the authenticate method
- *
- * @return void
- */
- public function testAuthenticateNoData()
- {
- $request = new Request('posts/index');
- $request->data = [];
- $this->assertFalse($this->auth->authenticate($request, $this->response));
- }
- /**
- * test the authenticate method
- *
- * @return void
- */
- public function testAuthenticateNoUsername()
- {
- $request = new Request('posts/index');
- $request->data = ['password' => 'foobar'];
- $this->assertFalse($this->auth->authenticate($request, $this->response));
- }
- /**
- * test the authenticate method
- *
- * @return void
- */
- public function testAuthenticateNoPassword()
- {
- $request = new Request('posts/index');
- $request->data = ['username' => 'mariano'];
- $this->assertFalse($this->auth->authenticate($request, $this->response));
- }
- /**
- * test authenticate password is false method
- *
- * @return void
- */
- public function testAuthenticatePasswordIsFalse()
- {
- $request = new Request('posts/index', false);
- $request->data = [
- 'username' => 'mariano',
- 'password' => null
- ];
- $this->assertFalse($this->auth->authenticate($request, $this->response));
- }
- /**
- * Test for password as empty string with _checkFields() call skipped
- * Refs https://github.com/cakephp/cakephp/pull/2441
- *
- * @return void
- */
- public function testAuthenticatePasswordIsEmptyString()
- {
- $request = new Request('posts/index', false);
- $request->data = [
- 'username' => 'mariano',
- 'password' => ''
- ];
- $this->auth = $this->getMock(
- 'Cake\Auth\FormAuthenticate',
- ['_checkFields'],
- [
- $this->Collection,
- [
- 'userModel' => 'Users'
- ]
- ]
- );
- // Simulate that check for ensuring password is not empty is missing.
- $this->auth->expects($this->once())
- ->method('_checkFields')
- ->will($this->returnValue(true));
- $this->assertFalse($this->auth->authenticate($request, $this->response));
- }
- /**
- * test authenticate field is not string
- *
- * @return void
- */
- public function testAuthenticateFieldsAreNotString()
- {
- $request = new Request('posts/index', false);
- $request->data = [
- 'username' => ['mariano', 'phpnut'],
- 'password' => 'my password'
- ];
- $this->assertFalse($this->auth->authenticate($request, $this->response));
- $request->data = [
- 'username' => 'mariano',
- 'password' => ['password1', 'password2']
- ];
- $this->assertFalse($this->auth->authenticate($request, $this->response));
- }
- /**
- * test the authenticate method
- *
- * @return void
- */
- public function testAuthenticateInjection()
- {
- $request = new Request('posts/index');
- $request->data = [
- 'username' => '> 1',
- 'password' => "' OR 1 = 1"
- ];
- $this->assertFalse($this->auth->authenticate($request, $this->response));
- }
- /**
- * test authenticate success
- *
- * @return void
- */
- public function testAuthenticateSuccess()
- {
- $request = new Request('posts/index');
- $request->data = [
- 'username' => 'mariano',
- 'password' => 'password'
- ];
- $result = $this->auth->authenticate($request, $this->response);
- $expected = [
- 'id' => 1,
- 'username' => 'mariano',
- 'created' => new Time('2007-03-17 01:16:23'),
- 'updated' => new Time('2007-03-17 01:18:31')
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * Test that authenticate() includes virtual fields.
- *
- * @return void
- */
- public function testAuthenticateIncludesVirtualFields()
- {
- $users = TableRegistry::get('Users');
- $users->entityClass('TestApp\Model\Entity\VirtualUser');
- $request = new Request('posts/index');
- $request->data = [
- 'username' => 'mariano',
- 'password' => 'password'
- ];
- $result = $this->auth->authenticate($request, $this->response);
- $expected = [
- 'id' => 1,
- 'username' => 'mariano',
- 'bonus' => 'bonus',
- 'created' => new Time('2007-03-17 01:16:23'),
- 'updated' => new Time('2007-03-17 01:18:31')
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * test scope failure.
- *
- * @return void
- */
- public function testAuthenticateScopeFail()
- {
- $this->auth->config('scope', ['Users.id' => 2]);
- $request = new Request('posts/index');
- $request->data = [
- 'username' => 'mariano',
- 'password' => 'password'
- ];
- $this->assertFalse($this->auth->authenticate($request, $this->response));
- }
- /**
- * test a model in a plugin.
- *
- * @return void
- */
- public function testPluginModel()
- {
- Cache::delete('object_map', '_cake_core_');
- Plugin::load('TestPlugin');
- $PluginModel = TableRegistry::get('TestPlugin.AuthUsers');
- $user['id'] = 1;
- $user['username'] = 'gwoo';
- $user['password'] = password_hash(Security::salt() . 'cake', PASSWORD_BCRYPT);
- $PluginModel->save(new Entity($user));
- $this->auth->config('userModel', 'TestPlugin.AuthUsers');
- $request = new Request('posts/index');
- $request->data = [
- 'username' => 'gwoo',
- 'password' => 'cake'
- ];
- $result = $this->auth->authenticate($request, $this->response);
- $expected = [
- 'id' => 1,
- 'username' => 'gwoo',
- 'created' => new Time('2007-03-17 01:16:23'),
- 'updated' => new Time('2007-03-17 01:18:31')
- ];
- $this->assertEquals($expected, $result);
- Plugin::unload();
- }
- /**
- * test password hasher settings
- *
- * @return void
- */
- public function testPasswordHasherSettings()
- {
- $this->auth->config('passwordHasher', [
- 'className' => 'Default',
- 'hashType' => PASSWORD_BCRYPT
- ]);
- $passwordHasher = $this->auth->passwordHasher();
- $result = $passwordHasher->config();
- $this->assertEquals(PASSWORD_BCRYPT, $result['hashType']);
- $hash = password_hash('mypass', PASSWORD_BCRYPT);
- $User = TableRegistry::get('Users');
- $User->updateAll(
- ['password' => $hash],
- ['username' => 'mariano']
- );
- $request = new Request('posts/index');
- $request->data = [
- 'username' => 'mariano',
- 'password' => 'mypass'
- ];
- $result = $this->auth->authenticate($request, $this->response);
- $expected = [
- 'id' => 1,
- 'username' => 'mariano',
- 'created' => new Time('2007-03-17 01:16:23'),
- 'updated' => new Time('2007-03-17 01:18:31')
- ];
- $this->assertEquals($expected, $result);
- $this->auth = new FormAuthenticate($this->Collection, [
- 'fields' => ['username' => 'username', 'password' => 'password'],
- 'userModel' => 'Users'
- ]);
- $this->auth->config('passwordHasher', [
- 'className' => 'Default'
- ]);
- $this->assertEquals($expected, $this->auth->authenticate($request, $this->response));
- $User->updateAll(
- ['password' => '$2y$10$/G9GBQDZhWUM4w/WLes3b.XBZSK1hGohs5dMi0vh/oen0l0a7DUyK'],
- ['username' => 'mariano']
- );
- $this->assertFalse($this->auth->authenticate($request, $this->response));
- }
- /**
- * Tests that using default means password don't need to be rehashed
- *
- * @return void
- */
- public function testAuthenticateNoRehash()
- {
- $request = new Request('posts/index');
- $request->data = [
- 'username' => 'mariano',
- 'password' => 'password'
- ];
- $result = $this->auth->authenticate($request, $this->response);
- $this->assertNotEmpty($result);
- $this->assertFalse($this->auth->needsPasswordRehash());
- }
- /**
- * Tests that not using the Default password hasher means that the password
- * needs to be rehashed
- *
- * @return void
- */
- public function testAuthenticateRehash()
- {
- $this->auth = new FormAuthenticate($this->Collection, [
- 'userModel' => 'Users',
- 'passwordHasher' => 'Weak'
- ]);
- $password = $this->auth->passwordHasher()->hash('password');
- TableRegistry::get('Users')->updateAll(['password' => $password], []);
- $request = new Request('posts/index');
- $request->data = [
- 'username' => 'mariano',
- 'password' => 'password'
- ];
- $result = $this->auth->authenticate($request, $this->response);
- $this->assertNotEmpty($result);
- $this->assertTrue($this->auth->needsPasswordRehash());
- }
- }
|